|
| 1 | +import { Component, Show, createResource } from "solid-js" |
| 2 | +import { ButtonV2 } from "@opencode-ai/ui/v2/button-v2" |
| 3 | +import { usePlatform } from "@/context/platform" |
| 4 | +import { useServer } from "@/context/server" |
| 5 | +import { SettingsListV2 } from "./parts/list" |
| 6 | +import { SettingsRowV2 } from "./parts/row" |
| 7 | +import "./settings-v2.css" |
| 8 | + |
| 9 | +/** |
| 10 | + * Settings > Account (#187966 K1 + K2): who the app is acting as, their plan, and how much of the |
| 11 | + * week's allowance is left. |
| 12 | + * |
| 13 | + * The copy is English, like the rest of the IRIS-only surfaces (titlebar pills, Upgrade dialog): |
| 14 | + * the locale files carry the upstream app's strings, and parity requires every key in all 61. |
| 15 | + */ |
| 16 | + |
| 17 | +export interface AccountState { |
| 18 | + measured: boolean |
| 19 | + reason?: string |
| 20 | + signedIn: boolean |
| 21 | + tokenSource: string |
| 22 | + id: number | null |
| 23 | + name: string | null |
| 24 | + email: string | null |
| 25 | + plan: { measured: boolean; plan: string | null; paid: boolean | null; uncapped: boolean; upgradeUrl: string | null } |
| 26 | + allowance: { |
| 27 | + measured: boolean |
| 28 | + window: string |
| 29 | + capUsd: number | null |
| 30 | + uncapped: boolean |
| 31 | + spendUsd: number | null |
| 32 | + fraction: number | null |
| 33 | + resetsAt: string | null |
| 34 | + thresholds: number[] |
| 35 | + upgradeUrl: string | null |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export type MeterView = |
| 40 | + | { kind: "unknown"; text: string } |
| 41 | + | { kind: "uncapped"; text: string } |
| 42 | + | { kind: "meter"; percent: number; text: string; resets: string | null; marks: number[] } |
| 43 | + |
| 44 | +const usd = (n: number) => `$${n.toFixed(2)}` |
| 45 | + |
| 46 | +/** |
| 47 | + * One meter, read from the gate (ADR-01). "Could not ask" and "no limit" each get their own words. |
| 48 | + * Neither is ever drawn as an empty bar, which would tell someone they have used nothing. |
| 49 | + */ |
| 50 | +export function meterView(a: AccountState["allowance"] | undefined, now = new Date()): MeterView { |
| 51 | + if (!a?.measured) return { kind: "unknown", text: "Usage is unavailable right now. This is not zero." } |
| 52 | + if (a.uncapped || a.capUsd === null) return { kind: "uncapped", text: "No weekly limit on this account." } |
| 53 | + if (a.fraction === null || a.spendUsd === null) |
| 54 | + return { kind: "unknown", text: "Usage is unavailable right now. This is not zero." } |
| 55 | + const percent = Math.max(0, Math.min(100, Math.round(a.fraction * 100))) |
| 56 | + return { |
| 57 | + kind: "meter", |
| 58 | + percent, |
| 59 | + text: `${usd(a.spendUsd)} of ${usd(a.capUsd)} this ${a.window} (${percent}%)`, |
| 60 | + resets: resetsText(a.resetsAt, now), |
| 61 | + marks: a.thresholds.filter((t) => t > 0 && t < 1).map((t) => Math.round(t * 100)), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function resetsText(iso: string | null, now: Date): string | null { |
| 66 | + if (!iso) return null |
| 67 | + const at = new Date(iso) |
| 68 | + if (Number.isNaN(at.getTime())) return null |
| 69 | + const day = at.toLocaleDateString(undefined, { weekday: "long", month: "short", day: "numeric" }) |
| 70 | + return at.getTime() <= now.getTime() ? "Resets now" : `Resets ${day}` |
| 71 | +} |
| 72 | + |
| 73 | +/** The server reports staff as plan null (fl-iris-api 4eda580d). An unmeasured plan is "Unknown", never "Free". */ |
| 74 | +export function planLabel(p: AccountState["plan"] | undefined): string { |
| 75 | + if (!p?.measured) return "Unknown" |
| 76 | + if (p.plan === null) return "Staff" |
| 77 | + return p.plan.charAt(0).toUpperCase() + p.plan.slice(1) |
| 78 | +} |
| 79 | + |
| 80 | +/** A Hive node key can sign the app in, but it belongs to a machine, not to a person. */ |
| 81 | +export function sourceNote(source: string): string | null { |
| 82 | + if (source.includes("node_api_key")) return "Signed in with this machine's Hive node key, not a personal sign-in." |
| 83 | + if (source.startsWith("none")) return null |
| 84 | + return `Credential from ${source}.` |
| 85 | +} |
| 86 | + |
| 87 | +export const SettingsAccountV2: Component = () => { |
| 88 | + const platform = usePlatform() |
| 89 | + const server = useServer() |
| 90 | + |
| 91 | + const [me, { refetch }] = createResource( |
| 92 | + () => server.current?.http?.url?.replace(/\/$/, ""), |
| 93 | + async (base): Promise<AccountState | undefined> => { |
| 94 | + try { |
| 95 | + const res = await (platform.fetch ?? globalThis.fetch)(`${base}/iris/me`) |
| 96 | + return res.ok ? ((await res.json()) as AccountState) : undefined |
| 97 | + } catch { |
| 98 | + return undefined |
| 99 | + } |
| 100 | + }, |
| 101 | + ) |
| 102 | + |
| 103 | + const meter = () => meterView(me()?.allowance) |
| 104 | + const upgradeUrl = () => { |
| 105 | + const p = me()?.plan |
| 106 | + return p?.measured && p.paid === false ? (p.upgradeUrl ?? me()?.allowance.upgradeUrl ?? null) : null |
| 107 | + } |
| 108 | + |
| 109 | + return ( |
| 110 | + <> |
| 111 | + <div class="settings-v2-tab-header"> |
| 112 | + <h2 class="settings-v2-tab-title">Account</h2> |
| 113 | + </div> |
| 114 | + <div class="settings-v2-tab-body"> |
| 115 | + <div class="settings-v2-section"> |
| 116 | + <SettingsListV2> |
| 117 | + <Show |
| 118 | + when={me()?.signedIn} |
| 119 | + fallback={ |
| 120 | + <SettingsRowV2 |
| 121 | + title={me.loading ? "Checking…" : me()?.measured === false ? "Can't reach IRIS" : "Not signed in"} |
| 122 | + description={ |
| 123 | + me()?.measured === false |
| 124 | + ? `IRIS did not answer (${me()?.reason ?? "no response"}). You may still be signed in.` |
| 125 | + : "Sign in to use IRIS models, Atlas and Hive." |
| 126 | + } |
| 127 | + > |
| 128 | + <ButtonV2 size="normal" variant="neutral" onClick={() => void refetch()}> |
| 129 | + Retry |
| 130 | + </ButtonV2> |
| 131 | + </SettingsRowV2> |
| 132 | + } |
| 133 | + > |
| 134 | + <SettingsRowV2 |
| 135 | + title={me()?.name ?? me()?.email ?? `User ${me()?.id}`} |
| 136 | + description={ |
| 137 | + <> |
| 138 | + <span data-action="account-email">{me()?.email ?? "No email on this account"}</span> |
| 139 | + <Show when={sourceNote(me()?.tokenSource ?? "")}> |
| 140 | + {(note) => ( |
| 141 | + <> |
| 142 | + <br /> |
| 143 | + {note()} |
| 144 | + </> |
| 145 | + )} |
| 146 | + </Show> |
| 147 | + </> |
| 148 | + } |
| 149 | + > |
| 150 | + <span data-action="account-id" class="text-text-weak"> |
| 151 | + #{me()?.id} |
| 152 | + </span> |
| 153 | + </SettingsRowV2> |
| 154 | + <SettingsRowV2 title="Plan" description={upgradeUrl() ? "Upgrade for a higher weekly allowance." : ""}> |
| 155 | + <div class="flex items-center gap-2"> |
| 156 | + <span data-action="account-plan">{planLabel(me()?.plan)}</span> |
| 157 | + <Show when={upgradeUrl()}> |
| 158 | + {(url) => ( |
| 159 | + <ButtonV2 size="normal" variant="neutral" onClick={() => platform.openExternal(url())}> |
| 160 | + Upgrade |
| 161 | + </ButtonV2> |
| 162 | + )} |
| 163 | + </Show> |
| 164 | + </div> |
| 165 | + </SettingsRowV2> |
| 166 | + </Show> |
| 167 | + </SettingsListV2> |
| 168 | + </div> |
| 169 | + |
| 170 | + <Show when={me()?.signedIn}> |
| 171 | + <div class="settings-v2-section"> |
| 172 | + <h3 class="settings-v2-section-title">This week's allowance</h3> |
| 173 | + <SettingsListV2> |
| 174 | + <div data-component="settings-v2-row" data-action="account-allowance"> |
| 175 | + <div data-slot="settings-v2-row-copy" style={{ width: "100%" }}> |
| 176 | + <div data-slot="settings-v2-row-title">{meter().text}</div> |
| 177 | + <Show |
| 178 | + when={meter().kind === "meter" ? (meter() as Extract<MeterView, { kind: "meter" }>) : undefined} |
| 179 | + > |
| 180 | + {(m) => ( |
| 181 | + <> |
| 182 | + <div |
| 183 | + role="meter" |
| 184 | + aria-valuemin={0} |
| 185 | + aria-valuemax={100} |
| 186 | + aria-valuenow={m().percent} |
| 187 | + aria-label="Weekly allowance used" |
| 188 | + style={{ |
| 189 | + position: "relative", |
| 190 | + height: "8px", |
| 191 | + "border-radius": "4px", |
| 192 | + "margin-top": "8px", |
| 193 | + background: "var(--border-base, rgba(127, 127, 127, 0.35))", |
| 194 | + }} |
| 195 | + > |
| 196 | + <div |
| 197 | + style={{ |
| 198 | + width: `${m().percent}%`, |
| 199 | + height: "100%", |
| 200 | + "border-radius": "4px", |
| 201 | + background: |
| 202 | + m().percent >= 90 |
| 203 | + ? "var(--icon-critical-base, #d14343)" |
| 204 | + : "var(--icon-interactive-base, #3b82f6)", |
| 205 | + }} |
| 206 | + /> |
| 207 | + {m().marks.map((mark) => ( |
| 208 | + <div |
| 209 | + title={`You are told once at ${mark}%`} |
| 210 | + style={{ |
| 211 | + position: "absolute", |
| 212 | + left: `${mark}%`, |
| 213 | + top: "-3px", |
| 214 | + width: "2px", |
| 215 | + height: "14px", |
| 216 | + background: "var(--text-weak, #888)", |
| 217 | + }} |
| 218 | + /> |
| 219 | + ))} |
| 220 | + </div> |
| 221 | + <div data-slot="settings-v2-row-description" style={{ "margin-top": "6px" }}> |
| 222 | + {[m().resets, m().marks.length ? `You are told once at ${m().marks.join("%, ")}%.` : null] |
| 223 | + .filter(Boolean) |
| 224 | + .join(" · ")} |
| 225 | + </div> |
| 226 | + </> |
| 227 | + )} |
| 228 | + </Show> |
| 229 | + </div> |
| 230 | + </div> |
| 231 | + </SettingsListV2> |
| 232 | + </div> |
| 233 | + </Show> |
| 234 | + </div> |
| 235 | + </> |
| 236 | + ) |
| 237 | +} |
0 commit comments