Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SubscriptionUsage.tsx

263 lines8.9 KB

1import { HStack, ProgressView, Spacer, Text, VStack } from "@expo/ui/swift-ui";
2import {
3 accessibilityElement,
4 accessibilityLabel,
5 fixedSize,
6 font,
7 foregroundStyle,
8 frame,
9 layoutPriority,
10 lineLimit,
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() ?? 0, Date.now());
33 const accessory = family === "accessoryRectangular";
34 const compact =
35 family === "systemSmall" || accessory || environment.levelOfDetail === "simplified";
36 const dense = family === "systemSmall" || family === "systemMedium";
37 const limit = family === "systemExtraLarge" ? 6 : family === "systemLarge" ? 4 : 2;
38 const monochrome =
39 environment.widgetRenderingMode !== "fullColor" || environment.isLuminanceReduced;
40 const providers = props.providers ?? [
41 { name: "Codex", detail: "Open T3 to connect", windows: [], expiresAt: 0 },
42 { name: "Claude", detail: "Open T3 to connect", windows: [], expiresAt: 0 },
43 ];
44 const columns = providers.map((provider) => {
45 const stale = provider.windows.length > 0 && now >= provider.expiresAt;
46 const period =
47 environment.configuration?.[provider.name === "Claude" ? "claudePeriod" : "codexPeriod"] ??
48 "auto";
49 const windows = stale
50 ? []
51 : provider.windows.filter((window) => period === "auto" || window.kind === period);
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 frame({ height: 4 }),
81 ...(monochrome ? [] : [tint(provider.name === "Claude" ? "#d97757" : "#8e8e93")]),
82 ];
83 if (accessory) {
84 return (
85 <VStack
86 key={provider.name}
87 alignment="leading"
88 spacing={2}
89 modifiers={[
90 accessibilityElement("ignore"),
91 accessibilityLabel(
92 tightest
93 ? `${provider.name}, ${tightest.label}, ${tightest.remaining} percent remaining. ${tightest.reset}. ${provider.detail}.`
94 : `${provider.name}. ${detail}.`,
95 ),
96 ]}
97 >
98 <HStack spacing={4}>
99 <Text
100 modifiers={[
101 font({ textStyle: "caption", weight: "semibold" }),
102 lineLimit(1),
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={dense ? 1 : compact ? 2 : 4}
136 modifiers={[
137 frame({ maxWidth: Infinity, alignment: "leading" }),
138 fixedSize({ horizontal: false, vertical: true }),
139 ]}
140 >
141 <Text
142 modifiers={[
143 font({ textStyle: compact ? "caption" : "headline", weight: "bold" }),
144 lineLimit(1),
145 foregroundStyle("primary"),
146 ]}
147 >
148 {provider.name}
149 </Text>
150 {!compact || shown.length === 0 ? (
151 <Text
152 modifiers={[
153 font({ textStyle: "caption2" }),
154 foregroundStyle("secondary"),
155 lineLimit(1),
156 ]}
157 >
158 {detail === "Subscription remaining" ? " " : detail}
159 </Text>
160 ) : null}
161 {shown.map((window) => (
162 <VStack
163 key={window.label}
164 alignment="leading"
165 spacing={dense ? 1 : 2}
166 modifiers={[
167 accessibilityElement("ignore"),
168 accessibilityLabel(
169 `${provider.name}, ${window.label}, ${window.remaining} percent remaining. ${window.reset}. ${provider.detail}.`,
170 ),
171 ]}
172 >
173 <HStack spacing={4}>
174 <Text
175 modifiers={[
176 font({ textStyle: compact || dense ? "caption2" : "caption" }),
177 foregroundStyle("secondary"),
178 lineLimit(1),
179 ]}
180 >
181 {window.label}
182 </Text>
183 <Spacer />
184 <Text
185 modifiers={[
186 font({
187 textStyle: compact || dense ? "caption2" : "caption",
188 weight: "semibold",
189 }),
190 lineLimit(1),
191 layoutPriority(1),
192 foregroundStyle(
193 window.remaining <= 10 && !monochrome
194 ? environment.colorScheme === "light"
195 ? "#dc2626"
196 : "#fca5a5"
197 : "primary",
198 ),
199 ]}
200 >
201 {window.remaining}% left
202 </Text>
203 </HStack>
204 <ProgressView value={window.remaining / 100} modifiers={barModifiers} />
205 {!compact ? (
206 <Text modifiers={[font({ size: 10 }), foregroundStyle("secondary"), lineLimit(1)]}>
207 {window.reset}
208 </Text>
209 ) : null}
210 </VStack>
211 ))}
212 {!compact &&
213 !stale &&
214 (period === "auto" ? (provider.totalWindows ?? windows.length) : windows.length) > limit ? (
215 <Text
216 modifiers={[
217 font({ textStyle: "caption2" }),
218 foregroundStyle("secondary"),
219 lineLimit(1),
220 ]}
221 >
222 {(period === "auto" ? (provider.totalWindows ?? windows.length) : windows.length) -
223 limit}{" "}
224 more in T3
225 </Text>
226 ) : null}
227 </VStack>
228 );
229 });
230 return (
231 <VStack
232 alignment="leading"
233 spacing={accessory || dense ? 2 : 6}
234 modifiers={props.url ? [widgetURL(props.url)] : []}
235 >
236 {providers.length === 0 ? (
237 <Text modifiers={[font({ textStyle: "caption" }), foregroundStyle("secondary")]}>
238 No subscription limits available.
239 </Text>
240 ) : compact ? (
241 <VStack alignment="leading" spacing={accessory || dense ? 4 : 8}>
242 {columns}
243 </VStack>
244 ) : (
245 <HStack alignment="top" spacing={16}>
246 {columns}
247 </HStack>
248 )}
249 {!accessory ? <Spacer /> : null}
250 {!accessory ? (
251 <Text
252 modifiers={[font({ textStyle: "caption2" }), foregroundStyle("secondary"), lineLimit(1)]}
253 >
254 {props.checkedAt
255 ? `As of ${new Date(props.checkedAt).toLocaleString(undefined, { hour: "numeric", minute: "2-digit", month: "short", day: "numeric" })}`
256 : "Tap to connect in T3"}
257 </Text>
258 ) : null}
259 </VStack>
260 );
262​
263export default createWidget("SubscriptionUsage", SubscriptionUsage);