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 { HStack, Image, Spacer, Text, VStack } from "@expo/ui/swift-ui"; |
| 2 | import { |
| 3 | containerBackground, |
| 4 | font, |
| 5 | foregroundStyle, |
| 6 | frame, |
| 7 | layoutPriority, |
| 8 | lineLimit, |
| 9 | resizable, |
| 10 | widgetURL, |
| 11 | } from "@expo/ui/swift-ui/modifiers"; |
| 12 | import { createWidget, type WidgetEnvironment } from "expo-widgets"; |
| 13 | |
| 14 | import type { |
| 15 | AgentActivityPhase, |
| 16 | AgentActivityProps, |
| 17 | AgentActivityRowProps, |
| 18 | } from "./AgentActivity"; |
| 19 | |
| 20 | // Home-screen and Lock Screen counterpart of the AgentActivity Live Activity. |
| 21 | // It shares the Live Activity's name, but expo-widgets stores widget and Live |
| 22 | // Activity layouts under different keys, so each needs its own registration. |
| 23 | export function AgentActivityWidget( |
| 24 | publishedProps: Partial<AgentActivityProps> | undefined, |
| 25 | environment: WidgetEnvironment, |
| 26 | ) { |
| 27 | "widget"; |
| 28 | // The extension evaluates this function without the app's module scope. |
| 29 | // Before the app's first publish, the embedded layout renders with no props. |
| 30 | const props = publishedProps ?? {}; |
| 31 | const text = props.text; |
| 32 | const family = environment.widgetFamily; |
| 33 | const accessory = family === "accessoryRectangular"; |
| 34 | const small = family === "systemSmall"; |
| 35 | const monochrome = |
| 36 | environment.widgetRenderingMode === "accented" || |
| 37 | environment.widgetRenderingMode === "vibrant" || |
| 38 | environment.isLuminanceReduced === true; |
| 39 | const isLightScheme = environment.colorScheme === "light"; |
| 40 | |
| 41 | type Foreground = Parameters<typeof foregroundStyle>[0]; |
| 42 | const primaryForeground = { type: "hierarchical", style: "primary" } as const; |
| 43 | const secondaryForeground = { type: "hierarchical", style: "secondary" } as const; |
| 44 | const phaseTint = (phase: AgentActivityPhase): Foreground => { |
| 45 | if (monochrome) return primaryForeground; |
| 46 | switch (phase) { |
| 47 | case "waiting_for_approval": |
| 48 | return isLightScheme ? "#d97706" : "#fcd34d"; |
| 49 | case "waiting_for_input": |
| 50 | return isLightScheme ? "#4f46e5" : "#a5b4fc"; |
| 51 | case "failed": |
| 52 | return isLightScheme ? "#dc2626" : "#fca5a5"; |
| 53 | case "completed": |
| 54 | return isLightScheme ? "#059669" : "#6ee7b7"; |
| 55 | case "stale": |
| 56 | return secondaryForeground; |
| 57 | case "starting": |
| 58 | case "running": |
| 59 | default: |
| 60 | return isLightScheme ? "#0284c7" : "#7dd3fc"; |
| 61 | } |
| 62 | }; |
| 63 | const phasePriority = (phase: AgentActivityPhase): number => { |
| 64 | if (phase === "waiting_for_approval" || phase === "waiting_for_input") return 0; |
| 65 | if (phase === "failed") return 1; |
| 66 | if (phase === "running" || phase === "starting") return 2; |
| 67 | return 3; |
| 68 | }; |
| 69 | |
| 70 | const loaded = props.activities !== undefined; |
| 71 | const rows = [...(props.activities ?? [])].sort( |
| 72 | (a, b) => phasePriority(a.phase) - phasePriority(b.phase), |
| 73 | ); |
| 74 | const shown = rows.slice(0, accessory || small ? 2 : 4); |
| 75 | const attentionCount = rows.filter( |
| 76 | (row) => row.phase === "waiting_for_approval" || row.phase === "waiting_for_input", |
| 77 | ).length; |
| 78 | const workingCount = rows.filter( |
| 79 | (row) => row.phase === "running" || row.phase === "starting", |
| 80 | ).length; |
| 81 | const activeCount = props.activeCount ?? workingCount; |
| 82 | const summary = |
| 83 | attentionCount > 0 |
| 84 | ? ((small ? text?.needYou : text?.needAttention)?.[String(attentionCount)] ?? |
| 85 | `${attentionCount} need${attentionCount === 1 ? "s" : ""} ${small ? "you" : "attention"}`) |
| 86 | : workingCount > 0 |
| 87 | ? (text?.active[String(activeCount)] ?? `${activeCount} active`) |
| 88 | : rows.some((row) => row.phase === "stale") |
| 89 | ? (text?.outOfDate ?? "Out of date") |
| 90 | : rows.some((row) => row.phase === "failed") |
| 91 | ? (text?.failed ?? "Failed") |
| 92 | : rows.length > 0 |
| 93 | ? (text?.done ?? "Done") |
| 94 | : ""; |
| 95 | const summaryTint = rows[0] ? phaseTint(rows[0].phase) : secondaryForeground; |
| 96 | const emptyMessage = loaded |
| 97 | ? (props.subtitle ?? text?.noActiveAgents ?? "No active agents") |
| 98 | : "Open T3 Code to load agent activity"; |
| 99 | |
| 100 | // Widget taps open the containing app, and every build accepts the prod |
| 101 | // scheme as a link prefix, so dev and preview builds route this too. |
| 102 | const linkRow = rows[0]; |
| 103 | const deepLink = |
| 104 | linkRow && linkRow.deepLink.startsWith("/") && !linkRow.deepLink.startsWith("//") |
| 105 | ? `t3code://${linkRow.deepLink.slice(1)}` |
| 106 | : null; |
| 107 | |
| 108 | const background = accessory ? "clear" : isLightScheme ? "#ffffff" : "#1c1c1e"; |
| 109 | const rootModifiers = [ |
| 110 | containerBackground(background, "widget"), |
| 111 | ...(deepLink ? [widgetURL(deepLink)] : []), |
| 112 | ]; |
| 113 | |
| 114 | // The template image ignores frame/foregroundStyle applied to itself; size |
| 115 | // and tint it through a container the resizable image fills. |
| 116 | const renderLogo = (height: number) => ( |
| 117 | <HStack |
| 118 | modifiers={[frame({ width: height * 1.5, height }), foregroundStyle(primaryForeground)]} |
| 119 | > |
| 120 | <Image assetName="T3Mark" modifiers={[resizable()]} /> |
| 121 | </HStack> |
| 122 | ); |
| 123 | |
| 124 | const renderRow = (row: AgentActivityRowProps, key: string) => |
| 125 | small || accessory ? ( |
| 126 | <VStack key={key} alignment="leading" spacing={1}> |
| 127 | <Text |
| 128 | modifiers={[ |
| 129 | font({ weight: "semibold", size: accessory ? 13 : 12 }), |
| 130 | foregroundStyle(primaryForeground), |
| 131 | lineLimit(1), |
| 132 | ]} |
| 133 | > |
| 134 | {row.threadTitle} |
| 135 | </Text> |
| 136 | <Text |
| 137 | modifiers={[ |
| 138 | font({ weight: "medium", size: 11 }), |
| 139 | foregroundStyle(phaseTint(row.phase)), |
| 140 | lineLimit(1), |
| 141 | ]} |
| 142 | > |
| 143 | {row.status} |
| 144 | </Text> |
| 145 | </VStack> |
| 146 | ) : ( |
| 147 | <HStack key={key} spacing={7} alignment="center"> |
| 148 | <Text |
| 149 | modifiers={[ |
| 150 | font({ weight: "semibold", size: 13 }), |
| 151 | foregroundStyle(primaryForeground), |
| 152 | lineLimit(1), |
| 153 | ]} |
| 154 | > |
| 155 | {row.threadTitle} |
| 156 | </Text> |
| 157 | <Text modifiers={[font({ size: 11 }), foregroundStyle(secondaryForeground), lineLimit(1)]}> |
| 158 | {row.projectTitle} |
| 159 | </Text> |
| 160 | <Spacer minLength={8} /> |
| 161 | <Text |
| 162 | modifiers={[ |
| 163 | font({ weight: "semibold", size: 11 }), |
| 164 | foregroundStyle(phaseTint(row.phase)), |
| 165 | layoutPriority(1), |
| 166 | ]} |
| 167 | > |
| 168 | {row.status} |
| 169 | </Text> |
| 170 | </HStack> |
| 171 | ); |
| 172 | |
| 173 | return ( |
| 174 | <VStack alignment="leading" spacing={accessory ? 2 : 8} modifiers={rootModifiers}> |
| 175 | <HStack spacing={6} alignment="center"> |
| 176 | {accessory ? null : renderLogo(13)} |
| 177 | <Text |
| 178 | modifiers={[ |
| 179 | font({ weight: "semibold", size: 12 }), |
| 180 | foregroundStyle(accessory ? primaryForeground : summaryTint), |
| 181 | lineLimit(1), |
| 182 | ]} |
| 183 | > |
| 184 | {summary || (accessory ? "T3 Code" : "")} |
| 185 | </Text> |
| 186 | <Spacer minLength={0} /> |
| 187 | </HStack> |
| 188 | {shown.length > 0 ? ( |
| 189 | <VStack alignment="leading" spacing={accessory ? 1 : small ? 6 : 5}> |
| 190 | {shown.map((row, index) => renderRow(row, `${row.threadId}:${index}`))} |
| 191 | </VStack> |
| 192 | ) : ( |
| 193 | <Text modifiers={[font({ size: 12 }), foregroundStyle(secondaryForeground), lineLimit(2)]}> |
| 194 | {emptyMessage} |
| 195 | </Text> |
| 196 | )} |
| 197 | {accessory ? null : <Spacer minLength={0} />} |
| 198 | </VStack> |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | export default createWidget<Partial<AgentActivityProps>>("AgentActivity", AgentActivityWidget); |