Repository navigation
feat(web,mobile): pick This month or an earlier month on Usage #32824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ed8c5dd
1de6436
24806f8
b31ea44
fda01e1
68e4824
bcc866d
656c687
5a1f02d
525d48a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,12 @@ | ||
| import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; | ||
| import { ChatGptUsageSummary } from "./ChatGptUsageSummary"; | ||
| import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; | ||
| import { EnvironmentId, USAGE_CONTRACT_VERSION, type UsageProviderKind } from "@t3tools/contracts"; | ||
| import { | ||
| EnvironmentId, | ||
| USAGE_CONTRACT_VERSION, | ||
| type UsageProviderKind, | ||
| type UsageSummaryInput, | ||
| } from "@t3tools/contracts"; | ||
| import { type RouteProp, useIsFocused, useRoute } from "@react-navigation/native"; | ||
| import { | ||
| updatingProvidersLabel, | ||
|
|
@@ -18,11 +23,18 @@ import { | |
| enumerateDays, | ||
| enumerateHourStarts, | ||
| formatCount, | ||
| formatDayRange, | ||
| formatPercent, | ||
| formatTokens, | ||
| formatUsageMonth, | ||
| formatUsd, | ||
| makeWindow, | ||
| makePeriodWindow, | ||
| parseUsagePeriod, | ||
| selectableUsageMonths, | ||
| type UsageMonthKey, | ||
| type UsagePeriod, | ||
| } from "@t3tools/shared/usageFormat"; | ||
| import type { MenuAction } from "@react-native-menu/menu"; | ||
| import { formatUsageContractMismatch, usageSourceMessages } from "@t3tools/shared/usageFormatCopy"; | ||
| import { msg, T, useGT, useLocale, useMessages, Var } from "gt-react-native"; | ||
| import { use, useRef, useState, type ReactNode } from "react"; | ||
|
|
@@ -32,6 +44,8 @@ import Animated, { FadeIn, ReduceMotion } from "react-native-reanimated"; | |
| import { useSafeAreaInsets } from "react-native-safe-area-context"; | ||
|
|
||
| import { SegmentedControl } from "../../components/ui/SegmentedControl"; | ||
| import { ControlPill } from "../../components/ui/ControlPill"; | ||
| import { Menu } from "../../components/ui/Menu"; | ||
| import { useReduceMotionEnabled } from "../../lib/appState"; | ||
| import { AppText as Text } from "../../components/ui/AppText"; | ||
| import { useWorkspaceSettledContentWidth } from "../layout/workspace-content-width"; | ||
|
|
@@ -77,6 +91,11 @@ const WINDOW_OPTIONS = [ | |
| label: msg("90d", { $context: "short label for the past 90 days" }), | ||
| accessibilityLabel: msg("Past 90 days"), | ||
| }, | ||
| { | ||
| value: "month", | ||
| label: msg("Month", { $context: "short label for the current calendar month" }), | ||
| accessibilityLabel: msg("This month"), | ||
| }, | ||
| ] as const; | ||
|
|
||
| const CHART_HEIGHT = 180; | ||
|
|
@@ -121,12 +140,13 @@ function UsageScreen(props: { | |
| // A workspace pane knows its width in the render that resizes it; measuring lags a frame. | ||
| const width = useWorkspaceSettledContentWidth() ?? measuredWidth; | ||
| const wide = width >= WIDE_WIDTH; | ||
| const [windowSelection, setWindowSelection] = useState(() => ({ | ||
| days: 30, | ||
| window: makeWindow(30), | ||
| })); | ||
| const { days: windowDays, window } = windowSelection; | ||
| const isPast24Hours = windowDays === 1; | ||
| const [windowSelection, setWindowSelection] = useState<{ | ||
| readonly period: UsagePeriod; | ||
| readonly window: UsageSummaryInput; | ||
| }>(() => ({ period: 30, window: makePeriodWindow(30) })); | ||
| const { period, window } = windowSelection; | ||
|
Comment on lines
+146
to
+147
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the “This month” window when the calendar month changes. If the screen stays mounted from November 30 to December 1, switching from Cost to Tokens still uses the stored November window. The picker builds its choices for December, but “This month” remains selected above November totals. Rebuild the current-month window when the viewer's calendar date changes, including on resume. Keep explicit past months fixed while they remain selectable.
|
||
| const isPast24Hours = period === 1; | ||
| const isMonthPeriod = typeof period === "string"; | ||
| const [selectedEnvironmentIds, setSelectedEnvironmentIds] = | ||
| useState<ReadonlySet<EnvironmentId> | null>(null); | ||
| const { merged, environments, selectedEnvironments, isPending, refresh } = useUsage( | ||
|
|
@@ -173,22 +193,19 @@ function UsageScreen(props: { | |
| }, | ||
| m, | ||
| ); | ||
| const selectWindow = (days: number) => { | ||
| setWindowSelection({ | ||
| days, | ||
| window: makeWindow(days, undefined, days === 1 ? "hour" : "day"), | ||
| }); | ||
| const selectPeriod = (period: UsagePeriod) => { | ||
| setWindowSelection({ period, window: makePeriodWindow(period) }); | ||
| }; | ||
| const refreshWindow = () => { | ||
| if (refreshingRef.current) return; | ||
| const nextWindow = makeWindow(windowDays, undefined, isPast24Hours ? "hour" : "day"); | ||
| const nextWindow = makePeriodWindow(period); | ||
| if ( | ||
| nextWindow.sinceDay !== window.sinceDay || | ||
| nextWindow.untilDay !== window.untilDay || | ||
| nextWindow.sinceTime !== window.sinceTime || | ||
| nextWindow.untilTime !== window.untilTime | ||
| ) { | ||
| setWindowSelection({ days: windowDays, window: nextWindow }); | ||
| setWindowSelection({ period, window: nextWindow }); | ||
| } | ||
| refreshingRef.current = true; | ||
| setRefreshingUsage(true); | ||
|
|
@@ -209,6 +226,15 @@ function UsageScreen(props: { | |
| timeZone: window.timeZone, | ||
| }; | ||
| const providers = <ProviderSection merged={merged} metric={metric} compact={wide} />; | ||
| const monthPicker = | ||
| isMonthPeriod && !showingLimits ? ( | ||
| <UsageMonthPicker | ||
| period={period} | ||
| sinceDay={window.sinceDay} | ||
| untilDay={window.untilDay} | ||
| onSelect={selectPeriod} | ||
| /> | ||
| ) : null; | ||
|
|
||
| const showEnvironmentFilter = environments.length > 0 || selectedEnvironmentIds !== null; | ||
| const selectEnvironment = (value: string) => { | ||
|
|
@@ -274,8 +300,8 @@ function UsageScreen(props: { | |
| ) : ( | ||
| <SegmentedControl | ||
| options={windowOptions} | ||
| selected={windowDays} | ||
| onSelect={selectWindow} | ||
| selected={isMonthPeriod ? "month" : period} | ||
| onSelect={selectPeriod} | ||
| size="compact" | ||
| className="flex-1" | ||
| /> | ||
|
|
@@ -297,6 +323,7 @@ function UsageScreen(props: { | |
| role="tab" | ||
| /> | ||
| )} | ||
| {wide ? monthPicker : null} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Render the month picker only once in the wide layout. On a wide screen, Cost and Tokens render
|
||
| <Animated.View | ||
| key={showingLimits ? "limits" : "usage"} | ||
| entering={FadeIn.duration(160).reduceMotion(ReduceMotion.System)} | ||
|
|
@@ -314,11 +341,12 @@ function UsageScreen(props: { | |
| {wide ? null : ( | ||
| <SegmentedControl | ||
| options={windowOptions} | ||
| selected={windowDays} | ||
| onSelect={selectWindow} | ||
| selected={isMonthPeriod ? "month" : period} | ||
| onSelect={selectPeriod} | ||
| size="compact" | ||
| /> | ||
| )} | ||
| {monthPicker} | ||
| <ChatGptUsageSummary selectedEnvironmentIds={selectedEnvironmentIds} /> | ||
| {merged.duplicateSources.length > 0 ? ( | ||
| <Text className="text-sm text-foreground-muted"> | ||
|
|
@@ -409,6 +437,59 @@ function UsageScreen(props: { | |
| ); | ||
| } | ||
|
|
||
| function UsageMonthPicker(props: { | ||
| readonly period: "month" | UsageMonthKey; | ||
| readonly sinceDay: string; | ||
| readonly untilDay: string; | ||
| readonly onSelect: (period: UsagePeriod) => void; | ||
| }) { | ||
| const gt = useGT(); | ||
| const locale = useLocale(); | ||
| const monthKeys = selectableUsageMonths(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reconcile the selected month when it leaves the picker. If a user selects September on November 30 and returns to the still-mounted screen on December 3, the picker no longer lists September. The pill and results still show September, but no menu action is checked. Pull-to-refresh also keeps the expired selection. When a selected past month leaves the available list, update the selected period and query window together to “This month.” Keep past months selected while they are still in the list.
|
||
| const thisMonth = gt("This month"); | ||
| const chooseMonth = gt("Choose month"); | ||
| const actions: MenuAction[] = [ | ||
| { id: "month", title: thisMonth, state: props.period === "month" ? "on" : "off" }, | ||
| ...monthKeys.slice(1).map((key): MenuAction => ({ | ||
| id: key, | ||
| title: formatUsageMonth(key, true, locale), | ||
| state: props.period === key ? "on" : "off", | ||
| })), | ||
| ]; | ||
| return ( | ||
| <View className="flex-row items-center gap-3"> | ||
| <Menu | ||
| accessible | ||
| accessibilityLabel={chooseMonth} | ||
| accessibilityRole="button" | ||
| actions={actions} | ||
| onPressAction={({ nativeEvent }) => { | ||
| const next = parseUsagePeriod(nativeEvent.event); | ||
| if (next !== undefined) props.onSelect(next); | ||
| }} | ||
| > | ||
| <ControlPill | ||
| variant="pill" | ||
| icon="calendar.badge.clock" | ||
| label={ | ||
| props.period === "month" | ||
| ? thisMonth | ||
| : formatUsageMonth( | ||
| props.period, | ||
| props.period.slice(0, 4) !== monthKeys[0]?.slice(0, 4), | ||
| locale, | ||
| ) | ||
| } | ||
| accessibilityLabel={chooseMonth} | ||
| /> | ||
| </Menu> | ||
| <Text className="text-sm text-foreground-muted"> | ||
| {formatDayRange(props.sinceDay, props.untilDay, locale)} | ||
| </Text> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| function UsageUpdating({ | ||
| dimmed, | ||
| label, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not announce a selected past month as “This month.”
If an Android user selects October, the period control still marks
"month"as selected. The Android control pairs that selected state with the accessibility label “This month,” even though the totals cover October. Use a generic “Month” label for the segment, or announce the actual selected month. (raw.githubusercontent.com)