Open History to see the last commit
1import { HStack, ProgressView, Spacer, Text, VStack } from "@expo/ui/swift-ui";
2import {
3 accessibilityElement,
4 accessibilityLabel,
5 font,
6 foregroundStyle,
7 frame,
8 layoutPriority,
9 lineLimit,
10 minimumScaleFactor,
11 progressViewStyle,
12 tint,
13 widgetURL,
14} from "@expo/ui/swift-ui/modifiers";
15import { createWidget, type WidgetEnvironment } from "expo-widgets";
16
17import type { SubscriptionUsageSnapshot as SubscriptionUsageProps } from "./subscriptionUsageSnapshot";
18
19type UsageConfiguration = {
20 codexPeriod?: "auto" | "session" | "weekly";
21 claudePeriod?: "auto" | "session" | "weekly";
22};
23
24function SubscriptionUsage(
25 props: SubscriptionUsageProps,
26 environment: WidgetEnvironment<UsageConfiguration>,
27) {
28 "widget";
29 // The extension evaluates this function without the app's module scope.
30 const family = environment.widgetFamily;
31 // Gallery snapshots can render an old timeline entry after it has expired.
32 const now = Math.max(environment.date.getTime(), Date.now());
33 const accessory = family === "accessoryRectangular";
34 const compact =
35 family === "systemSmall" || accessory || environment.levelOfDetail === "simplified";
36 const limit = family === "systemExtraLarge" ? 6 : family === "systemLarge" ? 4 : 2;
37 const monochrome =
38 environment.widgetRenderingMode !== "fullColor" || environment.isLuminanceReduced;
39 const providers = props.providers ?? [
40 { name: "Codex", detail: "Open T3 to connect", windows: [], expiresAt: 0 },
41 { name: "Claude", detail: "Open T3 to connect", windows: [], expiresAt: 0 },
42 ];
43 const columns = providers.map((provider) => {
44 const stale = provider.windows.length > 0 && now >= provider.expiresAt;
45 const period =
46 environment.configuration?.[provider.name === "Claude" ? "claudePeriod" : "codexPeriod"] ??
47 "auto";
48 const windows = stale
49 ? []
50 : provider.windows.filter((window) => period === "auto" || window.kind === period);
51 // Lock Screen widgets surface the tightest selected limit.
52 const tightest = windows.reduce<(typeof windows)[number] | undefined>(
53 (result, window) => (!result || window.remaining < result.remaining ? window : result),
54 undefined,
55 );
56 const compactWindows = [
57 windows.find((window) => window.kind === "session"),
58 windows.find((window) => window.kind === "weekly"),
59 ].filter((window) => window !== undefined);
60 const shown =
61 accessory || environment.levelOfDetail === "simplified"
62 ? tightest
63 ? [tightest]
64 : []
65 : family === "systemSmall" && compactWindows.length > 0
66 ? compactWindows
67 : period === "auto" && compactWindows.length > 0
68 ? [
69 ...compactWindows,
70 ...windows.filter((window) => !compactWindows.includes(window)),
71 ].slice(0, limit)
72 : windows.slice(0, limit);
73 const detail = stale
74 ? "Open T3 to refresh"
75 : period !== "auto" && windows.length === 0 && provider.windows.length > 0
76 ? `No ${period} limit reported`
77 : provider.detail;
78 const barModifiers = [
79 progressViewStyle("linear"),
80 ...(monochrome ? [] : [tint(provider.name === "Claude" ? "#d97757" : "#8e8e93")]),
81 ];
82 if (accessory) {
83 return (
84 <VStack
85 key={provider.name}
86 alignment="leading"
87 spacing={2}
88 modifiers={[
89 accessibilityElement("ignore"),
90 accessibilityLabel(
91 tightest
92 ? `${provider.name}, ${tightest.label}, ${tightest.remaining} percent remaining. ${tightest.reset}. ${provider.detail}.`
93 : `${provider.name}. ${detail}.`,
94 ),
95 ]}
96 >
97 <HStack spacing={4}>
98 <Text
99 modifiers={[
100 font({ textStyle: "caption", weight: "semibold" }),
101 lineLimit(1),
102 minimumScaleFactor(0.75),
103 foregroundStyle("primary"),
104 ]}
105 >
106 {provider.name}
107 {tightest ? ` · ${tightest.label}` : ""}
108 </Text>
109 <Spacer />
110 <Text
111 modifiers={[
112 font({ textStyle: "caption", weight: "semibold" }),
113 lineLimit(1),
114 layoutPriority(1),
115 foregroundStyle("primary"),
116 ]}
117 >
118 {tightest
119 ? `${tightest.remaining}% left`
120 : period !== "auto" && !stale && provider.windows.length > 0
121 ? "N/A"
122 : "Open T3"}
123 </Text>
124 </HStack>
125 {tightest ? (
126 <ProgressView value={tightest.remaining / 100} modifiers={barModifiers} />
127 ) : null}
128 </VStack>
129 );
130 }
131 return (
132 <VStack
133 key={provider.name}
134 alignment="leading"
135 spacing={compact ? 2 : 4}
136 modifiers={[frame({ maxWidth: Infinity, alignment: "leading" })]}
137 >
138 <Text
139 modifiers={[
140 font({ textStyle: compact ? "caption" : "headline", weight: "bold" }),
141 lineLimit(1),
142 minimumScaleFactor(0.75),
143 foregroundStyle("primary"),
144 ]}
145 >
146 {provider.name}
147 </Text>
148 {(!compact || shown.length === 0) && detail !== "Subscription remaining" ? (
149 <Text
150 modifiers={[
151 font({ textStyle: "caption2" }),
152 foregroundStyle("secondary"),
153 lineLimit(compact ? 1 : 2),
154 ]}
155 >
156 {detail}
157 </Text>
158 ) : null}
159 {shown.map((window) => (
160 <VStack
161 key={window.label}
162 alignment="leading"
163 spacing={2}
164 modifiers={[
165 accessibilityElement("ignore"),
166 accessibilityLabel(
167 `${provider.name}, ${window.label}, ${window.remaining} percent remaining. ${window.reset}. ${provider.detail}.`,
168 ),
169 ]}
170 >
171 <HStack spacing={4}>
172 <Text
173 modifiers={[
174 font({ textStyle: compact ? "caption2" : "caption" }),
175 foregroundStyle("secondary"),
176 lineLimit(1),
177 minimumScaleFactor(0.75),
178 ]}
179 >
180 {window.label}
181 </Text>
182 <Spacer />
183 <Text
184 modifiers={[
185 font({ textStyle: compact ? "caption2" : "caption", weight: "semibold" }),
186 lineLimit(1),
187 minimumScaleFactor(0.75),
188 layoutPriority(1),
189 foregroundStyle(
190 window.remaining <= 10 && !monochrome
191 ? environment.colorScheme === "light"
192 ? "#dc2626"
193 : "#fca5a5"
194 : "primary",
195 ),
196 ]}
197 >
198 {window.remaining}% left
199 </Text>
200 </HStack>
201 <ProgressView value={window.remaining / 100} modifiers={barModifiers} />
202 {!compact ? (
203 <Text
204 modifiers={[
205 font({ textStyle: "caption2" }),
206 foregroundStyle("secondary"),
207 lineLimit(1),
208 minimumScaleFactor(0.75),
209 ]}
210 >
211 {window.reset}
212 </Text>
213 ) : null}
214 </VStack>
215 ))}
216 {!compact &&
217 !stale &&
218 (period === "auto" ? (provider.totalWindows ?? windows.length) : windows.length) > limit ? (
219 <Text modifiers={[font({ textStyle: "caption2" }), foregroundStyle("secondary")]}>
220 {(period === "auto" ? (provider.totalWindows ?? windows.length) : windows.length) -
221 limit}{" "}
222 more in T3
223 </Text>
224 ) : null}
225 </VStack>
226 );
227 });
228 return (
229 <VStack
230 alignment="leading"
231 spacing={accessory ? 2 : 6}
232 modifiers={props.url ? [widgetURL(props.url)] : []}
233 >
234 {compact ? (
235 <VStack alignment="leading" spacing={accessory ? 4 : 8}>
236 {columns}
237 </VStack>
238 ) : (
239 <HStack alignment="top" spacing={16}>
240 {columns}
241 </HStack>
242 )}
243 {!accessory ? <Spacer /> : null}
244 {!accessory ? (
245 <Text
246 modifiers={[
247 font({ textStyle: "caption2" }),
248 foregroundStyle("secondary"),
249 lineLimit(1),
250 minimumScaleFactor(0.75),
251 ]}
252 >
253 {props.checkedAt
254 ? `As of ${new Date(props.checkedAt).toLocaleString(undefined, { hour: "numeric", minute: "2-digit", month: "short", day: "numeric" })}`
255 : "Tap to connect in T3"}
256 </Text>
257 ) : null}
258 </VStack>
259 );
260}
261
262export default createWidget("SubscriptionUsage", SubscriptionUsage);