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 // Android has no timeline, so freshness is decided on every render; the
23 // expiry alarm and each tap trigger one while the app is closed.
24 const now = Date.now();
25 // The 4x3 default cell fits two quotas per provider with their reset text.
26 const limit = 2;
27 const colors = getMaterialColors({
28 scheme: environment.colorScheme === "dark" ? "dark" : "light",
29 });
30 const muted = colors.onSurfaceVariant;
31 const providers = props.providers ?? [
32 { name: "Codex", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 },
33 { name: "Claude", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 },
34 ];
35 return (
36 // The card is one Button so a tap reaches the app's interaction listener,
37 // which opens props.url. expo-widgets has no Android counterpart to widgetURL.
38 <Button
39 colors={{ containerColor: colors.surface }}
40 modifiers={[fillMaxSize()]}
41 onClick={() => {}}
42 >
43 <Column modifiers={[fillMaxSize(), paddingAll(16)]}>
44 {providers.map((provider, index) => {
45 const stale =
46 provider.windows.length > 0 && provider.expiresAt > 0 && now >= provider.expiresAt;
47 const shown = stale ? [] : provider.windows.slice(0, limit);
48 const hidden = stale ? 0 : (provider.totalWindows ?? provider.windows.length) - limit;
49 return (
50 <Column
51 key={provider.name}
52 modifiers={[fillMaxWidth(), padding(0, index === 0 ? 0 : 10, 0, 0)]}
53 >
54 <Text
55 color={colors.onSurface}
56 maxLines={1}
57 style={{ fontSize: 13, fontWeight: "bold" }}
58 >
59 {provider.name}
60 </Text>
61 {shown.length === 0 ? (
62 <Text color={muted} maxLines={1} style={{ fontSize: 11 }}>
63 {stale ? "Open T3 to refresh" : provider.detail}
64 </Text>
65 ) : null}
66 {shown.map((window) => {
67 const low = window.remaining <= 10;
68 return (
69 <Column key={window.label} modifiers={[fillMaxWidth(), padding(0, 4, 0, 0)]}>
70 <Text
71 color={low ? colors.error : colors.onSurface}
72 maxLines={1}
73 style={{ fontSize: 11 }}
74 >
75 {`${window.label} · ${window.remaining}% left`}
76 </Text>
77 <Column modifiers={[fillMaxWidth(), padding(0, 3, 0, 3)]}>
78 <LinearProgressIndicator
79 progress={window.remaining / 100}
80 color={low ? colors.error : colors.primary}
81 trackColor={colors.surfaceVariant}
82 modifiers={[fillMaxWidth(), height(6)]}
83 />
84 </Column>
85 <Text color={muted} maxLines={1} style={{ fontSize: 10 }}>
86 {window.reset}
87 </Text>
88 </Column>
89 );
90 })}
91 {hidden > 0 ? (
92 <Text color={muted} maxLines={1} style={{ fontSize: 10 }}>
93 {`${hidden} more in T3`}
94 </Text>
95 ) : null}
96 </Column>
97 );
98 })}
99 <Text
100 color={muted}
101 maxLines={1}
102 style={{ fontSize: 10 }}
103 modifiers={[padding(0, 10, 0, 0)]}
104 >
105 {props.checkedAt
106 ? `As of ${new Date(props.checkedAt).toLocaleString(undefined, { hour: "numeric", minute: "2-digit", month: "short", day: "numeric" })}`
107 : "Tap to connect in T3"}
108 </Text>
109 </Column>
110 </Button>
111 );
112}
113
114export default createWidget("SubscriptionUsage", SubscriptionUsage);