Open History to see the last commit
1import {
2 Button,
3 Column,
4 getMaterialColors,
5 LinearProgressIndicator,
6 Text,
7} from "@expo/ui/jetpack-compose";
8import {
9 fillMaxSize,
10 fillMaxWidth,
11 height,
12 padding,
13 paddingAll,
14} from "@expo/ui/jetpack-compose/modifiers";
15import { createWidget, type WidgetEnvironment } from "expo-widgets";
16
17import type { SubscriptionUsageSnapshot as SubscriptionUsageProps } from "./subscriptionUsageSnapshot";
18
19export function SubscriptionUsage(props: SubscriptionUsageProps, environment: WidgetEnvironment) {
20 "widget";
21 // The widget runtime evaluates this function without the app's module scope.
22 const now = Date.now();
23 const limit = 2;
24 const colors = getMaterialColors({
25 scheme: environment.colorScheme === "dark" ? "dark" : "light",
26 });
27 const muted = colors.onSurfaceVariant;
28 const providers = props.providers ?? [
29 { name: "Codex", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 },
30 { name: "Claude", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 },
31 ];
32 return (
33 // The card is one Button so a tap reaches the app's interaction listener,
34 // which opens props.url. expo-widgets has no Android counterpart to widgetURL.
35 <Button
36 colors={{ containerColor: colors.surface }}
37 modifiers={[fillMaxSize()]}
38 onClick={() => {}}
39 >
40 <Column modifiers={[fillMaxSize(), paddingAll(16)]}>
41 {providers.map((provider, index) => {
42 const stale =
43 provider.windows.length > 0 && provider.expiresAt > 0 && now >= provider.expiresAt;
44 const shown = stale ? [] : provider.windows.slice(0, limit);
45 const hidden = stale ? 0 : (provider.totalWindows ?? provider.windows.length) - limit;
46 return (
47 <Column
48 key={provider.name}
49 modifiers={[fillMaxWidth(), padding(0, index === 0 ? 0 : 10, 0, 0)]}
50 >
51 <Text
52 color={colors.onSurface}
53 maxLines={1}
54 style={{ fontSize: 13, fontWeight: "bold" }}
55 >
56 {provider.name}
57 </Text>
58 {shown.length === 0 ? (
59 <Text color={muted} maxLines={1} style={{ fontSize: 11 }}>
60 {stale ? "Open T3 to refresh" : provider.detail}
61 </Text>
62 ) : null}
63 {shown.map((window) => {
64 const low = window.remaining <= 10;
65 return (
66 <Column key={window.label} modifiers={[fillMaxWidth(), padding(0, 4, 0, 0)]}>
67 <Text
68 color={low ? colors.error : colors.onSurface}
69 maxLines={1}
70 style={{ fontSize: 11 }}
71 >
72 {`${window.label} · ${window.remaining}% left`}
73 </Text>
74 <Column modifiers={[fillMaxWidth(), padding(0, 3, 0, 3)]}>
75 <LinearProgressIndicator
76 progress={window.remaining / 100}
77 color={low ? colors.error : colors.primary}
78 trackColor={colors.surfaceVariant}
79 modifiers={[fillMaxWidth(), height(6)]}
80 />
81 </Column>
82 <Text color={muted} maxLines={1} style={{ fontSize: 10 }}>
83 {window.reset}
84 </Text>
85 </Column>
86 );
87 })}
88 {hidden > 0 ? (
89 <Text color={muted} maxLines={1} style={{ fontSize: 10 }}>
90 {`${hidden} more in T3`}
91 </Text>
92 ) : null}
93 </Column>
94 );
95 })}
96 <Text
97 color={muted}
98 maxLines={1}
99 style={{ fontSize: 10 }}
100 modifiers={[padding(0, 10, 0, 0)]}
101 >
102 {props.checkedAt
103 ? `As of ${new Date(props.checkedAt).toLocaleString(undefined, { hour: "numeric", minute: "2-digit", month: "short", day: "numeric" })}`
104 : "Tap to connect in T3"}
105 </Text>
106 </Column>
107 </Button>
108 );
109}
110
111export default createWidget("SubscriptionUsage", SubscriptionUsage);