The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { useNavigation, type StaticScreenProps } from "@react-navigation/native"; |
| 3 | import { EnvironmentId } from "@t3tools/contracts"; |
| 4 | import { providerClients } from "@t3tools/client-runtime/provider-clients"; |
| 5 | import { normalizeProviderAccentColor } from "@t3tools/client-runtime/state/provider-instance-display"; |
| 6 | import { |
| 7 | collectLimitAccounts, |
| 8 | collectLimitPools, |
| 9 | displayLimitWindows, |
| 10 | paceRemainingPercent, |
| 11 | remainingPercent, |
| 12 | type LimitAccount, |
| 13 | type LimitPoolWindow, |
| 14 | } from "@t3tools/shared/usageLimits"; |
| 15 | import { |
| 16 | collectExternalUsageLinks, |
| 17 | collectLimitNotices, |
| 18 | cursorUsageWindowDetails, |
| 19 | } from "@t3tools/shared/usageLimitsCopy"; |
| 20 | import { msg, T, useGT, useMessages, Var } from "gt-react-native"; |
| 21 | import { Fragment, type ReactNode, useId, useState } from "react"; |
| 22 | import { Pressable, View } from "react-native"; |
| 23 | import { tryOpenExternalUrl } from "../../lib/openExternalUrl"; |
| 24 | import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; |
| 25 | import { Defs, Path, Pattern, Rect, Svg } from "react-native-svg"; |
| 26 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 27 | |
| 28 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 29 | import { AppText as Text } from "../../components/ui/AppText"; |
| 30 | import { ProviderIcon } from "../../components/ProviderIcon"; |
| 31 | import type { MessageResolver } from "../../i18n/messages"; |
| 32 | import { SettingsScreen } from "../settings/components/SettingsScreen"; |
| 33 | import { environmentPresentations } from "../../state/presentation"; |
| 34 | import { refillLabel, ResetCredits, resetLabels } from "./UsageLimitsSection"; |
| 35 | import { usageProviderKind, useProviderColors } from "./usageProviders"; |
| 36 | |
| 37 | const PACE_LABEL = { |
| 38 | ahead: msg("Ahead of pace"), |
| 39 | on: msg("On pace"), |
| 40 | under: msg("Under pace"), |
| 41 | } as const; |
| 42 | |
| 43 | function accountName(account: LimitAccount, resolve: MessageResolver) { |
| 44 | if (account.displayName) return account.displayName; |
| 45 | if (!account.email) return providerClients.get(account.driver)?.label ?? account.driver; |
| 46 | return ( |
| 47 | emailInitials(account.email) || resolve(msg("Account", { $context: "fallback account name" })) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | function emailInitials(email: string) { |
| 52 | const [local = "", domain = ""] = email.split("@"); |
| 53 | return `${local[0] ?? ""}${domain[0] ?? ""}`.toUpperCase(); |
| 54 | } |
| 55 | |
| 56 | function accountHue(email: string) { |
| 57 | let hash = 0; |
| 58 | for (let index = 0; index < email.length; index += 1) { |
| 59 | hash = (hash * 31 + email.charCodeAt(index)) | 0; |
| 60 | } |
| 61 | return Math.abs(hash) % 360; |
| 62 | } |
| 63 | |
| 64 | // Same hashed hue as the web account chip, so an account keeps its color across clients. |
| 65 | function AccountChip({ email }: { readonly email: string }) { |
| 66 | const hue = accountHue(email); |
| 67 | return ( |
| 68 | <View |
| 69 | className="size-5 items-center justify-center rounded-full" |
| 70 | style={{ backgroundColor: `hsl(${hue}, 45%, 82%)` }} |
| 71 | > |
| 72 | <Text className="text-[9px] font-t3-bold" style={{ color: `hsl(${hue}, 45%, 28%)` }}> |
| 73 | {emailInitials(email)} |
| 74 | </Text> |
| 75 | </View> |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | // Same gap-and-tick as the Limits rows: two clipped copies of the segment leave a see-through gap. |
| 80 | function PaceGap({ |
| 81 | timeLeft, |
| 82 | children, |
| 83 | }: { |
| 84 | readonly timeLeft: number | null; |
| 85 | readonly children: ReactNode; |
| 86 | }) { |
| 87 | const [width, setWidth] = useState(0); |
| 88 | const tickX = |
| 89 | timeLeft === null || width < 6 |
| 90 | ? null |
| 91 | : Math.min(Math.max((width * timeLeft) / 100, 3), width - 3); |
| 92 | return ( |
| 93 | <View |
| 94 | pointerEvents="none" |
| 95 | className="absolute inset-0" |
| 96 | onLayout={(event) => setWidth(event.nativeEvent.layout.width)} |
| 97 | > |
| 98 | {tickX === null |
| 99 | ? children |
| 100 | : ( |
| 101 | [ |
| 102 | [0, tickX - 3], |
| 103 | [tickX + 3, width], |
| 104 | ] as const |
| 105 | ).map(([start, end]) => ( |
| 106 | <View |
| 107 | key={start} |
| 108 | className="absolute top-0 bottom-0 overflow-hidden" |
| 109 | style={{ left: start, width: end - start }} |
| 110 | > |
| 111 | <View className="absolute top-0 bottom-0" style={{ left: -start, width }}> |
| 112 | {children} |
| 113 | </View> |
| 114 | </View> |
| 115 | ))} |
| 116 | {tickX !== null ? ( |
| 117 | <View |
| 118 | className="absolute top-0.5 bottom-0.5 w-0.5 rounded-full bg-foreground" |
| 119 | style={{ left: tickX - 1 }} |
| 120 | /> |
| 121 | ) : null} |
| 122 | </View> |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | function AccountSegment({ |
| 127 | remaining, |
| 128 | color, |
| 129 | refillPercent, |
| 130 | }: { |
| 131 | readonly remaining: number; |
| 132 | readonly color: string; |
| 133 | readonly refillPercent: number; |
| 134 | }) { |
| 135 | const patternId = useId().replace(/:/g, ""); |
| 136 | const [size, setSize] = useState({ width: 0, height: 0 }); |
| 137 | return ( |
| 138 | <View |
| 139 | pointerEvents="none" |
| 140 | className="absolute inset-0" |
| 141 | onLayout={(event) => { |
| 142 | const { width, height } = event.nativeEvent.layout; |
| 143 | setSize((previous) => |
| 144 | previous.width === width && previous.height === height ? previous : { width, height }, |
| 145 | ); |
| 146 | }} |
| 147 | > |
| 148 | {size.width > 0 && size.height > 0 ? ( |
| 149 | // Numeric canvas dimensions clear Android SVG geometry cached before a segment resized. |
| 150 | <Svg width={size.width} height={size.height} accessible={false}> |
| 151 | <Defs> |
| 152 | <Pattern id={patternId} width={6} height={6} patternUnits="userSpaceOnUse"> |
| 153 | <Path d="M-1 1L1 -1M0 6L6 0M5 7L7 5" stroke={color} strokeWidth={1} opacity={0.22} /> |
| 154 | </Pattern> |
| 155 | </Defs> |
| 156 | {refillPercent > 0 ? ( |
| 157 | <Rect |
| 158 | x={`${100 - Math.min(100 - remaining, refillPercent)}%`} |
| 159 | width={`${Math.min(100 - remaining, refillPercent)}%`} |
| 160 | height="100%" |
| 161 | fill={`url(#${patternId})`} |
| 162 | /> |
| 163 | ) : null} |
| 164 | <Rect width={`${remaining}%`} height="100%" fill={color} opacity={0.35} /> |
| 165 | </Svg> |
| 166 | ) : null} |
| 167 | </View> |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | function PoolWindowCard({ |
| 172 | pool, |
| 173 | color, |
| 174 | now, |
| 175 | environmentIds, |
| 176 | label, |
| 177 | description, |
| 178 | wide, |
| 179 | }: { |
| 180 | readonly pool: LimitPoolWindow; |
| 181 | readonly color: string; |
| 182 | readonly now: number; |
| 183 | readonly environmentIds: readonly string[] | null; |
| 184 | readonly label?: string | null; |
| 185 | readonly description?: string; |
| 186 | readonly wide: boolean; |
| 187 | }) { |
| 188 | const navigation = useNavigation(); |
| 189 | const gt = useGT(); |
| 190 | const m = useMessages(); |
| 191 | const [barWidth, setBarWidth] = useState(0); |
| 192 | // Each segment needs room for its account, percent and reset to label itself. |
| 193 | const segmentWidth = barWidth / Math.max(pool.columns.length, 1); |
| 194 | const inlineLabels = wide && segmentWidth >= 96; |
| 195 | const inlineReset = wide && segmentWidth >= 150; |
| 196 | const inlineName = wide && segmentWidth >= 220; |
| 197 | const nextRefill = pool.resets.find((reset) => reset.restoresPercent > 0); |
| 198 | const openAccount = (account: LimitAccount) => |
| 199 | navigation.navigate("SettingsUsageAccount", { |
| 200 | accountKey: account.key, |
| 201 | windowId: pool.id, |
| 202 | windowKind: pool.kind, |
| 203 | environmentIds, |
| 204 | now, |
| 205 | }); |
| 206 | const summary = ( |
| 207 | <> |
| 208 | <View className={wide ? "gap-1" : "flex-row items-start justify-between gap-3"}> |
| 209 | <View className="gap-1"> |
| 210 | {label !== null ? ( |
| 211 | <Text className="text-sm font-t3-medium text-foreground">{label ?? pool.label}</Text> |
| 212 | ) : null} |
| 213 | <View className="flex-row items-baseline gap-1.5"> |
| 214 | <Text className="text-sm text-foreground-muted"> |
| 215 | <T> |
| 216 | <Text className="text-3xl font-t3-bold tabular-nums text-foreground"> |
| 217 | <Var>{pool.remainingPercent}</Var>% |
| 218 | </Text>{" "} |
| 219 | left |
| 220 | </T> |
| 221 | </Text> |
| 222 | </View> |
| 223 | </View> |
| 224 | {pool.pace ? ( |
| 225 | <Text className="text-xs text-foreground-tertiary">{m(PACE_LABEL[pool.pace])}</Text> |
| 226 | ) : null} |
| 227 | </View> |
| 228 | {description ? <Text className="text-xs text-foreground-muted">{description}</Text> : null} |
| 229 | {nextRefill ? ( |
| 230 | <Text className="text-xs tabular-nums text-foreground-muted"> |
| 231 | {refillLabel(nextRefill.member.window, now, nextRefill.restoresPercent, m)} |
| 232 | </Text> |
| 233 | ) : null} |
| 234 | </> |
| 235 | ); |
| 236 | const bar = ( |
| 237 | <View |
| 238 | className="flex-row gap-1" |
| 239 | onLayout={(event) => setBarWidth(event.nativeEvent.layout.width)} |
| 240 | > |
| 241 | {pool.columns.map((member, index) => { |
| 242 | if (!member.window) |
| 243 | return ( |
| 244 | <View |
| 245 | key={member.account.key} |
| 246 | className={wide ? "h-9 min-w-0 flex-1" : "h-7 min-w-0 flex-1"} |
| 247 | /> |
| 248 | ); |
| 249 | const { account, window } = member; |
| 250 | const timeLeft = paceRemainingPercent(window, now); |
| 251 | const credits = account.limits.resetCredits?.availableCount ?? 0; |
| 252 | const resetsIn = resetLabels(window, now, m)?.compact; |
| 253 | return ( |
| 254 | <Pressable |
| 255 | key={account.key} |
| 256 | accessibilityRole="button" |
| 257 | accessibilityLabel={ |
| 258 | timeLeft === null |
| 259 | ? gt("Segment {index}, {account}, {remaining}% left", { |
| 260 | index: index + 1, |
| 261 | account: accountName(account, m), |
| 262 | remaining: remainingPercent(window), |
| 263 | }) |
| 264 | : gt( |
| 265 | "Segment {index}, {account}, {remaining}% left, {timeLeft}% of the window left", |
| 266 | { |
| 267 | index: index + 1, |
| 268 | account: accountName(account, m), |
| 269 | remaining: remainingPercent(window), |
| 270 | timeLeft, |
| 271 | }, |
| 272 | ) |
| 273 | } |
| 274 | accessibilityHint={gt("Show account details")} |
| 275 | onPress={() => openAccount(account)} |
| 276 | className={wide ? "h-9 min-w-0 flex-1 rounded-md" : "h-7 min-w-0 flex-1 rounded-md"} |
| 277 | > |
| 278 | <PaceGap timeLeft={timeLeft}> |
| 279 | <View className="absolute inset-0 rounded-md bg-subtle" /> |
| 280 | <View className="absolute inset-0 overflow-hidden rounded-md"> |
| 281 | <AccountSegment |
| 282 | remaining={remainingPercent(window)} |
| 283 | color={normalizeProviderAccentColor(account.accentColor) ?? color} |
| 284 | refillPercent={window.resetsAt ? member.refillPercent : 0} |
| 285 | /> |
| 286 | </View> |
| 287 | </PaceGap> |
| 288 | {inlineLabels ? ( |
| 289 | <View |
| 290 | pointerEvents="none" |
| 291 | className="absolute inset-0 flex-row items-center justify-between gap-2 overflow-hidden rounded-md px-2" |
| 292 | > |
| 293 | <View className="min-w-0 shrink flex-row items-center gap-1.5"> |
| 294 | {account.email ? <AccountChip email={account.email} /> : null} |
| 295 | {account.email && !(account.displayName && inlineName) ? null : ( |
| 296 | <Text |
| 297 | numberOfLines={1} |
| 298 | className="min-w-0 shrink text-xs font-t3-medium text-foreground" |
| 299 | > |
| 300 | {accountName(account, m)} |
| 301 | </Text> |
| 302 | )} |
| 303 | <Text className="text-sm font-t3-bold tabular-nums text-foreground"> |
| 304 | {remainingPercent(window)}% |
| 305 | </Text> |
| 306 | </View> |
| 307 | {inlineReset && (resetsIn || credits) ? ( |
| 308 | <View className="shrink-0 flex-row items-center gap-1 rounded bg-grouped-card/85 px-1.5 py-0.5"> |
| 309 | {resetsIn ? ( |
| 310 | <> |
| 311 | <SymbolView |
| 312 | name="arrow.clockwise" |
| 313 | size={11} |
| 314 | tintColorClassName="accent-foreground-muted" |
| 315 | /> |
| 316 | <Text className="text-xs tabular-nums text-foreground-muted"> |
| 317 | {resetsIn} |
| 318 | </Text> |
| 319 | </> |
| 320 | ) : null} |
| 321 | {credits ? ( |
| 322 | <> |
| 323 | <SymbolView name="ticket" size={12} tintColorClassName="accent-icon" /> |
| 324 | <Text className="text-xs font-t3-medium tabular-nums text-foreground"> |
| 325 | {credits} |
| 326 | </Text> |
| 327 | </> |
| 328 | ) : null} |
| 329 | </View> |
| 330 | ) : null} |
| 331 | </View> |
| 332 | ) : ( |
| 333 | <View |
| 334 | pointerEvents="none" |
| 335 | className="absolute inset-0 items-center justify-center overflow-hidden rounded-md" |
| 336 | > |
| 337 | <Text |
| 338 | numberOfLines={1} |
| 339 | className="max-w-full shrink text-xs font-t3-medium tabular-nums text-foreground" |
| 340 | > |
| 341 | {index + 1} |
| 342 | </Text> |
| 343 | </View> |
| 344 | )} |
| 345 | </Pressable> |
| 346 | ); |
| 347 | })} |
| 348 | </View> |
| 349 | ); |
| 350 | const legend = ( |
| 351 | <View> |
| 352 | {pool.columns.map(({ account, window }, index) => { |
| 353 | if (!window) return null; |
| 354 | const segmentColor = normalizeProviderAccentColor(account.accentColor) ?? color; |
| 355 | const credits = account.limits.resetCredits?.availableCount ?? 0; |
| 356 | const reset = resetLabels(window, now, m); |
| 357 | const resetsIn = reset?.full; |
| 358 | return ( |
| 359 | <Pressable |
| 360 | key={account.key} |
| 361 | accessibilityRole="button" |
| 362 | accessibilityLabel={ |
| 363 | resetsIn && credits |
| 364 | ? gt( |
| 365 | "Segment {index}, {account}, {remaining}% left, {resetsIn}, {credits, plural, other {{credits} reset credits banked}}", |
| 366 | { |
| 367 | index: index + 1, |
| 368 | account: accountName(account, m), |
| 369 | remaining: remainingPercent(window), |
| 370 | resetsIn, |
| 371 | credits, |
| 372 | }, |
| 373 | ) |
| 374 | : resetsIn |
| 375 | ? gt("Segment {index}, {account}, {remaining}% left, {resetsIn}", { |
| 376 | index: index + 1, |
| 377 | account: accountName(account, m), |
| 378 | remaining: remainingPercent(window), |
| 379 | resetsIn, |
| 380 | }) |
| 381 | : credits |
| 382 | ? gt( |
| 383 | "Segment {index}, {account}, {remaining}% left, {credits, plural, other {{credits} reset credits banked}}", |
| 384 | { |
| 385 | index: index + 1, |
| 386 | account: accountName(account, m), |
| 387 | remaining: remainingPercent(window), |
| 388 | credits, |
| 389 | }, |
| 390 | ) |
| 391 | : gt("Segment {index}, {account}, {remaining}% left", { |
| 392 | index: index + 1, |
| 393 | account: accountName(account, m), |
| 394 | remaining: remainingPercent(window), |
| 395 | }) |
| 396 | } |
| 397 | accessibilityHint={gt("Show account details")} |
| 398 | onPress={() => openAccount(account)} |
| 399 | className="min-h-[44px] flex-row items-center gap-2 active:opacity-60" |
| 400 | > |
| 401 | <View className="relative size-5 items-center justify-center overflow-hidden rounded-md bg-subtle-strong"> |
| 402 | <View |
| 403 | pointerEvents="none" |
| 404 | className="absolute inset-0" |
| 405 | style={{ backgroundColor: segmentColor, opacity: 0.35 }} |
| 406 | /> |
| 407 | <Text className="text-xs font-t3-medium tabular-nums text-foreground"> |
| 408 | {index + 1} |
| 409 | </Text> |
| 410 | </View> |
| 411 | <Text |
| 412 | numberOfLines={1} |
| 413 | className="min-w-0 flex-1 text-sm font-t3-medium text-foreground" |
| 414 | > |
| 415 | {accountName(account, m)} |
| 416 | </Text> |
| 417 | <Text className="text-sm font-t3-medium tabular-nums text-foreground"> |
| 418 | {remainingPercent(window)}% |
| 419 | </Text> |
| 420 | <View className="flex-row items-center gap-1"> |
| 421 | {resetsIn ? ( |
| 422 | <> |
| 423 | <SymbolView |
| 424 | name="arrow.clockwise" |
| 425 | size={12} |
| 426 | tintColorClassName="accent-foreground-muted" |
| 427 | /> |
| 428 | <Text className="text-xs tabular-nums text-foreground-muted"> |
| 429 | {reset?.compact} |
| 430 | </Text> |
| 431 | </> |
| 432 | ) : null} |
| 433 | {credits ? ( |
| 434 | <> |
| 435 | {resetsIn ? <Text className="text-xs text-foreground-tertiary">·</Text> : null} |
| 436 | <SymbolView name="ticket" size={13} tintColorClassName="accent-icon" /> |
| 437 | <Text className="text-xs font-t3-medium tabular-nums text-foreground"> |
| 438 | {credits} |
| 439 | </Text> |
| 440 | </> |
| 441 | ) : null} |
| 442 | </View> |
| 443 | </Pressable> |
| 444 | ); |
| 445 | })} |
| 446 | </View> |
| 447 | ); |
| 448 | if (wide) { |
| 449 | return ( |
| 450 | <View className="flex-row items-center gap-6 rounded-[24px] border-continuous bg-grouped-card p-4"> |
| 451 | <View className="w-44 gap-1">{summary}</View> |
| 452 | <View className="min-w-0 flex-1 gap-1"> |
| 453 | {bar} |
| 454 | {inlineLabels ? null : legend} |
| 455 | </View> |
| 456 | </View> |
| 457 | ); |
| 458 | } |
| 459 | return ( |
| 460 | <View className="gap-3 rounded-[24px] border-continuous bg-grouped-card p-4"> |
| 461 | {summary} |
| 462 | {bar} |
| 463 | {legend} |
| 464 | </View> |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | export function UsageLimitsSection({ |
| 469 | now, |
| 470 | failedLabels, |
| 471 | selectedEnvironmentIds, |
| 472 | wide = false, |
| 473 | }: { |
| 474 | readonly now: number; |
| 475 | readonly failedLabels: readonly string[]; |
| 476 | readonly selectedEnvironmentIds: ReadonlySet<EnvironmentId> | null; |
| 477 | readonly wide?: boolean; |
| 478 | }) { |
| 479 | const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom); |
| 480 | const selected = |
| 481 | selectedEnvironmentIds === null |
| 482 | ? presentations |
| 483 | : new Map([...presentations].filter(([id]) => selectedEnvironmentIds.has(id))); |
| 484 | const pools = collectLimitPools(collectLimitAccounts(selected), now); |
| 485 | const m = useMessages(); |
| 486 | const notices = collectLimitNotices(selected, m); |
| 487 | const externalLinks = collectExternalUsageLinks(selected, m); |
| 488 | const colors = useProviderColors(); |
| 489 | const gt = useGT(); |
| 490 | return ( |
| 491 | <View className="gap-6"> |
| 492 | {pools.length === 0 && |
| 493 | notices.length === 0 && |
| 494 | failedLabels.length === 0 && |
| 495 | externalLinks.length === 0 ? ( |
| 496 | <Text className="py-12 text-center text-base text-foreground-muted"> |
| 497 | {selected.size === 0 |
| 498 | ? gt("Select an environment to see limits.") |
| 499 | : gt("No provider on the selected environments reports subscription limits.")} |
| 500 | </Text> |
| 501 | ) : null} |
| 502 | {pools.map((pool) => { |
| 503 | const windows = displayLimitWindows(pool); |
| 504 | return ( |
| 505 | <Fragment key={pool.driver}> |
| 506 | <View className="gap-3"> |
| 507 | <View className="flex-row items-center gap-2 px-1"> |
| 508 | <ProviderIcon provider={pool.driver} size={18} /> |
| 509 | <Text className="text-base font-t3-medium text-foreground"> |
| 510 | {providerClients.get(pool.driver)?.label ?? pool.driver} |
| 511 | </Text> |
| 512 | </View> |
| 513 | {windows.map((window) => { |
| 514 | const details = |
| 515 | pool.driver === "cursor" |
| 516 | ? cursorUsageWindowDetails(window.id, pool, m) |
| 517 | : undefined; |
| 518 | return ( |
| 519 | <PoolWindowCard |
| 520 | key={`${window.kind}:${window.id}`} |
| 521 | pool={window} |
| 522 | color={colors[usageProviderKind(pool.driver) ?? "codex"]} |
| 523 | now={now} |
| 524 | environmentIds={ |
| 525 | selectedEnvironmentIds === null ? null : [...selectedEnvironmentIds] |
| 526 | } |
| 527 | label={details?.label} |
| 528 | description={details?.description} |
| 529 | wide={wide} |
| 530 | /> |
| 531 | ); |
| 532 | })} |
| 533 | </View> |
| 534 | </Fragment> |
| 535 | ); |
| 536 | })} |
| 537 | {externalLinks.map((link) => ( |
| 538 | <View key={link.url} className="gap-3 rounded-xl border border-border-subtle p-4"> |
| 539 | <Text className="text-base font-t3-medium text-foreground">{link.label}</Text> |
| 540 | <Text className="text-xs text-foreground-muted">{link.accounts.join(", ")}</Text> |
| 541 | {link.message ? ( |
| 542 | <Text className="text-sm text-foreground-muted">{link.message}</Text> |
| 543 | ) : null} |
| 544 | <Pressable |
| 545 | accessibilityRole="link" |
| 546 | className="min-h-11 justify-center" |
| 547 | onPress={() => void tryOpenExternalUrl(link.url)} |
| 548 | > |
| 549 | <Text className="text-sm font-t3-medium text-primary"> |
| 550 | <T>Manage usage</T> |
| 551 | </Text> |
| 552 | </Pressable> |
| 553 | </View> |
| 554 | ))} |
| 555 | {notices.length > 0 || failedLabels.length > 0 ? ( |
| 556 | <View |
| 557 | accessible |
| 558 | accessibilityRole="alert" |
| 559 | accessibilityLiveRegion="polite" |
| 560 | className="flex-row items-start gap-2 rounded-xl border border-warning-border bg-warning px-3.5 py-3" |
| 561 | > |
| 562 | <SymbolView |
| 563 | name="exclamationmark.triangle" |
| 564 | size={16} |
| 565 | tintColorClassName="accent-warning-foreground" |
| 566 | /> |
| 567 | <View className="min-w-0 flex-1 gap-0.5"> |
| 568 | {notices.map((notice) => ( |
| 569 | <Text key={notice} className="text-sm font-t3-medium text-warning-foreground"> |
| 570 | {notice} |
| 571 | </Text> |
| 572 | ))} |
| 573 | {failedLabels.length > 0 ? ( |
| 574 | <Text className="text-sm font-t3-medium text-warning-foreground"> |
| 575 | <T> |
| 576 | <Var>{failedLabels.join(", ")}</Var> could not refresh limits. Showing the last |
| 577 | known values. |
| 578 | </T> |
| 579 | </Text> |
| 580 | ) : null} |
| 581 | </View> |
| 582 | </View> |
| 583 | ) : null} |
| 584 | </View> |
| 585 | ); |
| 586 | } |
| 587 | |
| 588 | type AccountScreenProps = StaticScreenProps<{ |
| 589 | accountKey: string; |
| 590 | windowId: string; |
| 591 | windowKind: LimitPoolWindow["kind"]; |
| 592 | environmentIds: readonly string[] | null; |
| 593 | now: number; |
| 594 | }>; |
| 595 | |
| 596 | export function UsageLimitAccountScreen({ route }: AccountScreenProps) { |
| 597 | const insets = useSafeAreaInsets(); |
| 598 | const presentations = useAtomValue(environmentPresentations.presentationsWithProvidersAtom); |
| 599 | const { accountKey, windowId, windowKind, environmentIds, now } = route.params; |
| 600 | const selectedIds = |
| 601 | environmentIds === null ? null : new Set(environmentIds.map((id) => EnvironmentId.make(id))); |
| 602 | const selected = |
| 603 | selectedIds === null |
| 604 | ? presentations |
| 605 | : new Map([...presentations].filter(([id]) => selectedIds.has(id))); |
| 606 | const accounts = collectLimitAccounts(selected); |
| 607 | const account = accounts.find((candidate) => candidate.key === accountKey); |
| 608 | const pool = collectLimitPools(accounts, now) |
| 609 | .find((candidate) => candidate.driver === account?.driver) |
| 610 | ?.windows.find((candidate) => candidate.id === windowId && candidate.kind === windowKind); |
| 611 | const window = pool?.members.find((member) => member.account.key === accountKey)?.window; |
| 612 | const evenPace = window ? paceRemainingPercent(window, now) : null; |
| 613 | const reset = pool?.resets.find((candidate) => candidate.member.account.key === accountKey); |
| 614 | const [revealed, setRevealed] = useState(false); |
| 615 | const gt = useGT(); |
| 616 | return ( |
| 617 | <SettingsScreen title={gt("Account", { $context: "screen title" })}> |
| 618 | <ScrollView |
| 619 | contentInsetAdjustmentBehavior="automatic" |
| 620 | contentContainerClassName="gap-5 p-5" |
| 621 | contentContainerStyle={{ paddingBottom: insets.bottom + 24 }} |
| 622 | > |
| 623 | {!account || !window ? ( |
| 624 | <Text className="text-base text-foreground-muted"> |
| 625 | <T>This account is no longer reporting limits on the selected environments.</T> |
| 626 | </Text> |
| 627 | ) : ( |
| 628 | <> |
| 629 | <View className="gap-2"> |
| 630 | <View className="flex-row items-center gap-2"> |
| 631 | <ProviderIcon provider={account.driver} size={24} /> |
| 632 | <Text className="flex-1 text-xl font-t3-bold text-foreground"> |
| 633 | {account.displayName ?? |
| 634 | providerClients.get(account.driver)?.label ?? |
| 635 | account.driver} |
| 636 | </Text> |
| 637 | </View> |
| 638 | {account.email ? ( |
| 639 | <Pressable |
| 640 | accessibilityRole="button" |
| 641 | accessibilityLabel={ |
| 642 | revealed ? gt("Hide account email") : gt("Reveal account email") |
| 643 | } |
| 644 | onPress={() => setRevealed((value) => !value)} |
| 645 | className="min-h-[44px] justify-center" |
| 646 | > |
| 647 | <Text className="text-sm text-foreground-muted"> |
| 648 | {revealed ? account.email : "••••••@••••••"} |
| 649 | </Text> |
| 650 | </Pressable> |
| 651 | ) : null} |
| 652 | {account.plan ? ( |
| 653 | <Text selectable className="text-sm text-foreground-muted"> |
| 654 | {account.plan} |
| 655 | </Text> |
| 656 | ) : null} |
| 657 | </View> |
| 658 | <View className="gap-3 rounded-[24px] border-continuous bg-grouped-card p-4"> |
| 659 | <Text className="text-sm font-t3-medium text-foreground">{window.label}</Text> |
| 660 | <Text className="text-3xl font-t3-bold tabular-nums text-foreground"> |
| 661 | <T> |
| 662 | <Var>{remainingPercent(window)}</Var>% left |
| 663 | </T> |
| 664 | </Text> |
| 665 | {window.resetsAt ? ( |
| 666 | <Text selectable className="text-sm text-foreground-muted"> |
| 667 | <T> |
| 668 | Resets{" "} |
| 669 | <Var> |
| 670 | {new Date(window.resetsAt).toLocaleString(undefined, { |
| 671 | dateStyle: "medium", |
| 672 | timeStyle: "short", |
| 673 | })} |
| 674 | </Var> |
| 675 | </T> |
| 676 | </Text> |
| 677 | ) : null} |
| 678 | {evenPace !== null ? ( |
| 679 | <Text className="text-sm text-foreground-muted"> |
| 680 | <T> |
| 681 | Even pace: <Var>{evenPace}</Var>% left by now |
| 682 | </T> |
| 683 | </Text> |
| 684 | ) : null} |
| 685 | {reset && reset.restoresPercent > 0 ? ( |
| 686 | <Text className="text-sm text-foreground-muted"> |
| 687 | <T> |
| 688 | Restores <Var>{reset.restoresPercent}</Var>% of the pool |
| 689 | </T> |
| 690 | </Text> |
| 691 | ) : null} |
| 692 | </View> |
| 693 | <View className="gap-2 rounded-[24px] border-continuous bg-grouped-card p-4"> |
| 694 | <Text className="text-sm font-t3-medium text-foreground"> |
| 695 | {account.environments.length |
| 696 | ? gt("Signed in", { |
| 697 | $context: "heading for environments signed in to the account", |
| 698 | }) |
| 699 | : gt("Source", { $context: "heading for where account limits come from" })} |
| 700 | </Text> |
| 701 | {account.environments.length ? ( |
| 702 | account.environments.map((environment) => ( |
| 703 | <Text key={environment.environmentId} className="text-sm text-foreground-muted"> |
| 704 | {environment.label} |
| 705 | </Text> |
| 706 | )) |
| 707 | ) : ( |
| 708 | <Text className="text-sm text-foreground-muted">{account.sourceLabel}</Text> |
| 709 | )} |
| 710 | </View> |
| 711 | {account.redeem && account.limits.resetCredits ? ( |
| 712 | <View className="gap-3 rounded-[24px] border-continuous bg-grouped-card p-4"> |
| 713 | <Text className="text-sm font-t3-medium text-foreground"> |
| 714 | <T>Reset credits</T> |
| 715 | </Text> |
| 716 | <ResetCredits |
| 717 | key={account.key} |
| 718 | environmentId={account.redeem.environmentId} |
| 719 | input={account.redeem.input} |
| 720 | credits={account.limits.resetCredits} |
| 721 | now={now} |
| 722 | /> |
| 723 | </View> |
| 724 | ) : null} |
| 725 | </> |
| 726 | )} |
| 727 | </ScrollView> |
| 728 | </SettingsScreen> |
| 729 | ); |
| 730 | } |