The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | Box, |
| 3 | Button, |
| 4 | Column, |
| 5 | getMaterialColors, |
| 6 | Image, |
| 7 | Row, |
| 8 | Spacer, |
| 9 | Text, |
| 10 | } from "@expo/ui/jetpack-compose"; |
| 11 | import { |
| 12 | background, |
| 13 | cornerRadius, |
| 14 | fillMaxSize, |
| 15 | fillMaxWidth, |
| 16 | height, |
| 17 | padding, |
| 18 | paddingAll, |
| 19 | size, |
| 20 | width, |
| 21 | } from "@expo/ui/jetpack-compose/modifiers"; |
| 22 | import { createWidget, type WidgetEnvironment } from "expo-widgets"; |
| 23 | |
| 24 | import type { |
| 25 | SubscriptionUsageSnapshot as SubscriptionUsageProps, |
| 26 | WidgetLimitAccount, |
| 27 | } from "./subscriptionUsageSnapshot"; |
| 28 | |
| 29 | export function SubscriptionUsage(props: SubscriptionUsageProps, environment: WidgetEnvironment) { |
| 30 | "widget"; |
| 31 | // The widget runtime evaluates this function without the app's module scope. |
| 32 | const now = Date.now(); |
| 33 | const mode = props.theme?.mode; |
| 34 | const appearance = |
| 35 | mode === "light" || mode === "dark" |
| 36 | ? mode |
| 37 | : environment.colorScheme === "dark" |
| 38 | ? "dark" |
| 39 | : "light"; |
| 40 | const palette = props.theme?.[appearance]; |
| 41 | const material = getMaterialColors({ scheme: appearance }); |
| 42 | const colors = palette |
| 43 | ? { |
| 44 | surface: palette.background, |
| 45 | onSurface: palette.foreground, |
| 46 | muted: palette.secondary, |
| 47 | track: palette.track, |
| 48 | error: palette.danger, |
| 49 | } |
| 50 | : { |
| 51 | surface: material.surface, |
| 52 | onSurface: material.onSurface, |
| 53 | muted: material.onSurfaceVariant, |
| 54 | track: material.surfaceVariant, |
| 55 | error: material.error, |
| 56 | }; |
| 57 | // English until the app publishes its first snapshot in the UI language. |
| 58 | const text = props.text ?? { |
| 59 | refresh: "Open T3 to refresh", |
| 60 | left: "left", |
| 61 | asOf: "As of {time}", |
| 62 | tapToConnect: "Tap to connect in T3", |
| 63 | moreInApp: {} as Record<string, string>, |
| 64 | }; |
| 65 | const providers = props.providers ?? [ |
| 66 | // oxlint-disable-next-line t3code/no-untranslated-copy -- widget fallback until the app publishes translated text |
| 67 | { name: "Codex", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 }, |
| 68 | // oxlint-disable-next-line t3code/no-untranslated-copy -- widget fallback until the app publishes translated text |
| 69 | { name: "Claude", detail: "Open T3 to connect", windows: [], expiresAt: 0, totalWindows: 0 }, |
| 70 | ]; |
| 71 | |
| 72 | const pad = 14; |
| 73 | const labelWidth = 92; |
| 74 | const footer = 16; |
| 75 | const widgetWidth = environment.width ?? 320; |
| 76 | const contentWidth = widgetWidth - pad * 2; |
| 77 | const contentHeight = (environment.height ?? 200) - pad * 2 - footer; |
| 78 | const kindOrder: Record<string, number> = { session: 0, weekly: 1 }; |
| 79 | |
| 80 | type Line = |
| 81 | | { |
| 82 | type: "window"; |
| 83 | provider: string; |
| 84 | color: string; |
| 85 | window: SubscriptionUsageProps["providers"][number]["windows"][number]; |
| 86 | } |
| 87 | | { type: "note"; provider: string; text: string }; |
| 88 | const lines: Line[] = providers.flatMap((provider): Line[] => { |
| 89 | const stale = |
| 90 | provider.windows.length > 0 && provider.expiresAt > 0 && now >= provider.expiresAt; |
| 91 | if (stale || provider.windows.length === 0) |
| 92 | return [ |
| 93 | { |
| 94 | type: "note", |
| 95 | provider: provider.name, |
| 96 | text: stale ? text.refresh : provider.detail, |
| 97 | }, |
| 98 | ]; |
| 99 | return [...provider.windows] |
| 100 | .sort((a, b) => (kindOrder[a.kind ?? ""] ?? 2) - (kindOrder[b.kind ?? ""] ?? 2)) |
| 101 | .map((window) => ({ |
| 102 | type: "window", |
| 103 | provider: provider.name, |
| 104 | color: provider.colors?.[appearance] ?? colors.onSurface, |
| 105 | window, |
| 106 | })); |
| 107 | }); |
| 108 | |
| 109 | // The snapshot keeps a few windows per provider; the rest still count toward "N more". |
| 110 | const dropped = providers.reduce( |
| 111 | (sum, provider) => |
| 112 | lines.some((line) => line.type === "window" && line.provider === provider.name) |
| 113 | ? sum + Math.max(0, (provider.totalWindows ?? 0) - provider.windows.length) |
| 114 | : sum, |
| 115 | 0, |
| 116 | ); |
| 117 | const slots = lines.length + (dropped > 0 ? 1 : 0); |
| 118 | // Roomy rows when every limit fits, thin bars when they would not. |
| 119 | const fullRow = 34; |
| 120 | const compactRow = 20; |
| 121 | const full = slots * fullRow <= contentHeight; |
| 122 | // Glance columns hold at most ten children, the overflow note included. |
| 123 | const capacity = Math.max( |
| 124 | 1, |
| 125 | Math.min(9, Math.floor(contentHeight / (full ? fullRow : compactRow))), |
| 126 | ); |
| 127 | const shown = slots > capacity ? lines.slice(0, capacity - 1) : lines; |
| 128 | const hidden = lines.length - shown.length + dropped; |
| 129 | // Rows share any spare height so short lists do not bunch at the top. |
| 130 | const rowHeight = Math.min( |
| 131 | full ? 52 : 28, |
| 132 | contentHeight / Math.max(1, shown.length + (hidden > 0 ? 1 : 0)), |
| 133 | ); |
| 134 | |
| 135 | const low = (remaining: number) => remaining <= 10; |
| 136 | const timeLeft = (account: WidgetLimitAccount) => |
| 137 | account.resetsAt && account.durationMs |
| 138 | ? Math.max(0, Math.min(1, (account.resetsAt - now) / account.durationMs)) |
| 139 | : null; |
| 140 | const paceArrow = (accounts: WidgetLimitAccount[]) => { |
| 141 | const timed = accounts.flatMap((account) => { |
| 142 | const left = timeLeft(account); |
| 143 | return left === null || account.remaining === undefined |
| 144 | ? [] |
| 145 | : [{ used: 100 - account.remaining, elapsed: 1 - left }]; |
| 146 | }); |
| 147 | if (timed.length === 0) return ""; |
| 148 | const gap = timed.reduce((sum, t) => sum + t.used - t.elapsed * 100, 0) / timed.length; |
| 149 | return gap > 5 ? " ↗" : gap < -5 ? " ↘" : " →"; |
| 150 | }; |
| 151 | |
| 152 | const segment = ( |
| 153 | account: WidgetLimitAccount, |
| 154 | color: string, |
| 155 | segmentWidth: number, |
| 156 | barHeight: number, |
| 157 | labels: { name: boolean; percent: boolean; reset: boolean }, |
| 158 | ) => { |
| 159 | if (account.remaining === undefined) |
| 160 | return <Spacer key={account.name} modifiers={[size(segmentWidth, barHeight)]} />; |
| 161 | const fill = (segmentWidth * account.remaining) / 100; |
| 162 | const refill = (segmentWidth * Math.min(100 - account.remaining, account.refill ?? 0)) / 100; |
| 163 | const left = timeLeft(account); |
| 164 | const tick = |
| 165 | left === null ? null : Math.max(0, Math.min(segmentWidth - 5, segmentWidth * left - 2.5)); |
| 166 | return ( |
| 167 | <Box |
| 168 | key={account.name} |
| 169 | modifiers={[size(segmentWidth, barHeight), background(colors.track), cornerRadius(6)]} |
| 170 | > |
| 171 | {fill > 0 ? ( |
| 172 | <Box modifiers={[size(fill, barHeight), background(`${color}59`), cornerRadius(6)]} /> |
| 173 | ) : null} |
| 174 | {refill > 0 ? ( |
| 175 | <Row modifiers={[size(segmentWidth, barHeight)]}> |
| 176 | <Spacer modifiers={[width(segmentWidth - refill)]} /> |
| 177 | <Image |
| 178 | source={{ uri: "t3_widget_hatch" }} |
| 179 | tint={color} |
| 180 | alpha={0.22} |
| 181 | contentScale="fillBounds" |
| 182 | modifiers={[size(refill, barHeight)]} |
| 183 | /> |
| 184 | </Row> |
| 185 | ) : null} |
| 186 | {tick === null ? null : ( |
| 187 | <Row modifiers={[size(segmentWidth, barHeight)]}> |
| 188 | <Spacer modifiers={[width(tick)]} /> |
| 189 | <Box |
| 190 | contentAlignment="center" |
| 191 | modifiers={[size(5, barHeight), background(colors.surface)]} |
| 192 | > |
| 193 | <Box |
| 194 | modifiers={[ |
| 195 | size(2, Math.max(2, barHeight - 4)), |
| 196 | background(colors.onSurface), |
| 197 | cornerRadius(1), |
| 198 | ]} |
| 199 | /> |
| 200 | </Box> |
| 201 | </Row> |
| 202 | )} |
| 203 | {labels.percent ? ( |
| 204 | <Row |
| 205 | verticalAlignment="center" |
| 206 | modifiers={[size(segmentWidth, barHeight), padding(6, 0, 6, 0)]} |
| 207 | > |
| 208 | {labels.name ? ( |
| 209 | <Text color={colors.onSurface} maxLines={1} style={{ fontSize: 10 }}> |
| 210 | {`${account.name} `} |
| 211 | </Text> |
| 212 | ) : null} |
| 213 | <Text |
| 214 | color={low(account.remaining) ? colors.error : colors.onSurface} |
| 215 | maxLines={1} |
| 216 | style={{ fontSize: 10, fontWeight: "bold" }} |
| 217 | > |
| 218 | {`${account.remaining}%`} |
| 219 | </Text> |
| 220 | </Row> |
| 221 | ) : null} |
| 222 | {labels.reset && account.reset ? ( |
| 223 | <Box |
| 224 | contentAlignment="centerEnd" |
| 225 | modifiers={[size(segmentWidth, barHeight), padding(0, 0, 3, 0)]} |
| 226 | > |
| 227 | <Box modifiers={[background(colors.surface), cornerRadius(4), padding(4, 1, 4, 1)]}> |
| 228 | <Text color={colors.onSurface} maxLines={1} style={{ fontSize: 9 }}> |
| 229 | {`↻ ${account.reset}`} |
| 230 | </Text> |
| 231 | </Box> |
| 232 | </Box> |
| 233 | ) : null} |
| 234 | </Box> |
| 235 | ); |
| 236 | }; |
| 237 | |
| 238 | const bar = (line: Extract<Line, { type: "window" }>, barWidth: number, barHeight: number) => { |
| 239 | const fallback: WidgetLimitAccount = { name: line.provider, remaining: line.window.remaining }; |
| 240 | const listed = line.window.accounts ?? []; |
| 241 | // Glance rows hold at most ten children: five segments and their gaps. |
| 242 | const accounts = listed.length > 0 && listed.length <= 5 ? listed : [fallback]; |
| 243 | const gap = 3; |
| 244 | const segmentWidth = (barWidth - gap * (accounts.length - 1)) / accounts.length; |
| 245 | const multi = accounts.length > 1; |
| 246 | return ( |
| 247 | <Row modifiers={[size(barWidth, barHeight)]}> |
| 248 | {accounts.flatMap((account, index) => [ |
| 249 | index > 0 ? <Spacer key={`gap-${account.name}`} modifiers={[width(gap)]} /> : null, |
| 250 | segment(account, account.color ?? line.color, segmentWidth, barHeight, { |
| 251 | percent: barHeight >= 16 && multi && segmentWidth >= 34, |
| 252 | name: multi && segmentWidth >= 76, |
| 253 | reset: barHeight >= 16 && !multi && segmentWidth >= 120, |
| 254 | }), |
| 255 | ])} |
| 256 | </Row> |
| 257 | ); |
| 258 | }; |
| 259 | |
| 260 | const percentText = (remaining: number, fontSize: number) => ( |
| 261 | <Text |
| 262 | color={low(remaining) ? colors.error : colors.onSurface} |
| 263 | maxLines={1} |
| 264 | style={{ fontSize, fontWeight: "bold" }} |
| 265 | > |
| 266 | {`${remaining}%`} |
| 267 | </Text> |
| 268 | ); |
| 269 | |
| 270 | const row = (line: Line) => { |
| 271 | const label = `${line.provider} ${line.type === "window" ? line.window.label : ""}`.trim(); |
| 272 | if (line.type === "note") |
| 273 | return ( |
| 274 | <Row |
| 275 | key={`${line.provider}-note`} |
| 276 | verticalAlignment="center" |
| 277 | modifiers={[fillMaxWidth(), height(rowHeight)]} |
| 278 | > |
| 279 | <Text color={colors.onSurface} maxLines={1} style={{ fontSize: 11 }}> |
| 280 | {`${line.provider} `} |
| 281 | </Text> |
| 282 | <Text color={colors.muted} maxLines={1} style={{ fontSize: 11 }}> |
| 283 | {line.text} |
| 284 | </Text> |
| 285 | </Row> |
| 286 | ); |
| 287 | if (!full) |
| 288 | return ( |
| 289 | <Row key={label} verticalAlignment="center" modifiers={[fillMaxWidth(), height(rowHeight)]}> |
| 290 | <Text |
| 291 | color={colors.muted} |
| 292 | maxLines={1} |
| 293 | style={{ fontSize: 10 }} |
| 294 | modifiers={[width(labelWidth)]} |
| 295 | > |
| 296 | {label} |
| 297 | </Text> |
| 298 | {bar(line, contentWidth - labelWidth - 40, 6)} |
| 299 | <Box contentAlignment="centerEnd" modifiers={[width(40)]}> |
| 300 | {percentText(line.window.remaining, 10)} |
| 301 | </Box> |
| 302 | </Row> |
| 303 | ); |
| 304 | return ( |
| 305 | <Row key={label} verticalAlignment="center" modifiers={[fillMaxWidth(), height(rowHeight)]}> |
| 306 | <Column modifiers={[width(labelWidth)]}> |
| 307 | <Text color={colors.muted} maxLines={1} style={{ fontSize: 10 }}> |
| 308 | {label} |
| 309 | </Text> |
| 310 | <Row verticalAlignment="bottom"> |
| 311 | {percentText(line.window.remaining, 18)} |
| 312 | <Text |
| 313 | color={colors.muted} |
| 314 | maxLines={1} |
| 315 | style={{ fontSize: 10 }} |
| 316 | modifiers={[padding(3, 0, 0, 3)]} |
| 317 | > |
| 318 | {`${text.left}${paceArrow(line.window.accounts ?? [])}`} |
| 319 | </Text> |
| 320 | </Row> |
| 321 | </Column> |
| 322 | <Spacer modifiers={[width(8)]} /> |
| 323 | {bar(line, contentWidth - labelWidth - 8, 20)} |
| 324 | </Row> |
| 325 | ); |
| 326 | }; |
| 327 | |
| 328 | return ( |
| 329 | // Android lacks widgetURL; one card Button routes taps to the app listener opening props.url. |
| 330 | <Button |
| 331 | colors={{ containerColor: colors.surface }} |
| 332 | modifiers={[fillMaxSize()]} |
| 333 | onClick={() => {}} |
| 334 | > |
| 335 | <Column modifiers={[fillMaxSize(), paddingAll(pad)]}> |
| 336 | <Column modifiers={[fillMaxWidth(), height(contentHeight)]}> |
| 337 | {shown.map(row)} |
| 338 | {hidden > 0 ? ( |
| 339 | <Text color={colors.muted} maxLines={1} style={{ fontSize: 10 }}> |
| 340 | {/* oxlint-disable-next-line t3code/no-untranslated-copy -- widget fallback until the app publishes translated text */} |
| 341 | {text.moreInApp[String(hidden)] ?? `${hidden} more in T3`} |
| 342 | </Text> |
| 343 | ) : null} |
| 344 | </Column> |
| 345 | <Text |
| 346 | color={colors.muted} |
| 347 | maxLines={1} |
| 348 | style={{ fontSize: 10 }} |
| 349 | modifiers={[height(footer)]} |
| 350 | > |
| 351 | {props.checkedAt |
| 352 | ? text.asOf.replace( |
| 353 | "{time}", |
| 354 | new Date(props.checkedAt).toLocaleString(undefined, { |
| 355 | hour: "numeric", |
| 356 | minute: "2-digit", |
| 357 | month: "short", |
| 358 | day: "numeric", |
| 359 | }), |
| 360 | ) |
| 361 | : text.tapToConnect} |
| 362 | </Text> |
| 363 | </Column> |
| 364 | </Button> |
| 365 | ); |
| 366 | } |
| 367 | |
| 368 | export default createWidget("SubscriptionUsage", SubscriptionUsage); |