|
| 1 | +// The owner at the bottom of the sidebar, like the account corner of a |
| 2 | +// chat app: their name and Claude plan, and a menu with usage, settings, |
| 3 | +// language, what's new and help (spec 15.1). |
| 4 | + |
| 5 | +import { |
| 6 | + ChartColumn, |
| 7 | + Check, |
| 8 | + ChevronRight, |
| 9 | + ChevronsUpDown, |
| 10 | + CircleHelp, |
| 11 | + Languages, |
| 12 | + Settings, |
| 13 | + Sparkles, |
| 14 | +} from "lucide-react"; |
| 15 | +import { type ReactNode, useCallback, useRef, useState } from "react"; |
| 16 | +import { LOCALES, setLocaleChoice, systemLocale, useLocale, useT } from "../../i18n"; |
| 17 | +import type { ClaudeAccount } from "../../lib/protocol.gen"; |
| 18 | +import { useApp, useHost } from "../../store/context"; |
| 19 | +import { POPOVER, POPOVER_ITEM } from "../../ui/surface"; |
| 20 | +import { attempt } from "../../ui/toast"; |
| 21 | +import { useDismiss } from "../../ui/useDismiss"; |
| 22 | +import { SettingsDialog } from "./SettingsDialog"; |
| 23 | +import { UsageDialog } from "./UsageDialog"; |
| 24 | + |
| 25 | +const RELEASES = "https://github.com/httpsphl/botloft/releases"; |
| 26 | +const HELP = "https://github.com/httpsphl/botloft#readme"; |
| 27 | + |
| 28 | +/** "max" reads "Max"; an unknown plan keeps its words. */ |
| 29 | +function planName(plan: string): string { |
| 30 | + const words = plan.replaceAll("_", " ").trim(); |
| 31 | + return words.charAt(0).toUpperCase() + words.slice(1); |
| 32 | +} |
| 33 | + |
| 34 | +export function AccountArea() { |
| 35 | + const a = useT().account; |
| 36 | + const account = useApp((state) => state.system?.account ?? null); |
| 37 | + const signedOut = useApp((state) => state.system?.claudeSignedIn === false); |
| 38 | + const [open, setOpen] = useState(false); |
| 39 | + const [dialog, setDialog] = useState<"usage" | "settings" | null>(null); |
| 40 | + const root = useRef<HTMLDivElement>(null); |
| 41 | + |
| 42 | + useDismiss( |
| 43 | + open, |
| 44 | + root, |
| 45 | + useCallback(() => setOpen(false), []), |
| 46 | + ); |
| 47 | + |
| 48 | + const name = account?.name.trim() || "Botloft"; |
| 49 | + const detail = describe(account?.claude ?? null, signedOut, a); |
| 50 | + const show = (which: "usage" | "settings") => { |
| 51 | + setOpen(false); |
| 52 | + setDialog(which); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div ref={root} className="relative shrink-0 border-line border-t p-2"> |
| 57 | + {open && ( |
| 58 | + <AccountMenu |
| 59 | + name={name} |
| 60 | + email={account?.claude?.email ?? null} |
| 61 | + onShow={show} |
| 62 | + onClose={() => setOpen(false)} |
| 63 | + /> |
| 64 | + )} |
| 65 | + <button |
| 66 | + type="button" |
| 67 | + aria-haspopup="menu" |
| 68 | + aria-expanded={open} |
| 69 | + aria-label={a.open(name)} |
| 70 | + onClick={() => setOpen(!open)} |
| 71 | + className={`flex w-full items-center gap-2.5 rounded-lg px-2 py-1.5 text-left hover:bg-sunken ${open ? "bg-sunken" : ""}`} |
| 72 | + > |
| 73 | + <span |
| 74 | + aria-hidden |
| 75 | + className="grid h-8 w-8 shrink-0 place-items-center rounded-full bg-ink font-semibold text-canvas text-sm" |
| 76 | + > |
| 77 | + {Array.from(name)[0]?.toUpperCase()} |
| 78 | + </span> |
| 79 | + <span className="min-w-0 flex-1"> |
| 80 | + <span className="block truncate font-medium text-sm">{name}</span> |
| 81 | + {detail && <span className="block truncate text-muted text-xs">{detail}</span>} |
| 82 | + </span> |
| 83 | + <ChevronsUpDown aria-hidden size={14} className="shrink-0 text-muted" /> |
| 84 | + </button> |
| 85 | + {dialog === "usage" && <UsageDialog onClose={() => setDialog(null)} />} |
| 86 | + {dialog === "settings" && <SettingsDialog onClose={() => setDialog(null)} />} |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function describe( |
| 92 | + claude: ClaudeAccount | null, |
| 93 | + signedOut: boolean, |
| 94 | + a: ReturnType<typeof useT>["account"], |
| 95 | +): string { |
| 96 | + if (claude?.plan) { |
| 97 | + return a.plan(planName(claude.plan)); |
| 98 | + } |
| 99 | + return claude?.organization ?? claude?.email ?? (signedOut ? a.notSignedIn : ""); |
| 100 | +} |
| 101 | + |
| 102 | +function AccountMenu({ |
| 103 | + name, |
| 104 | + email, |
| 105 | + onShow, |
| 106 | + onClose, |
| 107 | +}: { |
| 108 | + name: string; |
| 109 | + email: string | null; |
| 110 | + onShow(which: "usage" | "settings"): void; |
| 111 | + onClose(): void; |
| 112 | +}) { |
| 113 | + const t = useT(); |
| 114 | + const m = t.account.menu; |
| 115 | + const host = useHost(); |
| 116 | + const { choice } = useLocale(); |
| 117 | + const [languages, setLanguages] = useState(false); |
| 118 | + const systemName = LOCALES.find((entry) => entry.id === systemLocale())?.name ?? "English"; |
| 119 | + const open = (url: string) => { |
| 120 | + onClose(); |
| 121 | + return attempt(m.openFailed, () => host.openUrl(url)); |
| 122 | + }; |
| 123 | + |
| 124 | + return ( |
| 125 | + <div |
| 126 | + role="menu" |
| 127 | + aria-label={name} |
| 128 | + className={`absolute right-2 bottom-full left-2 mb-1 ${POPOVER}`} |
| 129 | + > |
| 130 | + {email && ( |
| 131 | + <p className="truncate px-2.5 pt-1.5 pb-2 text-muted text-xs" data-selectable> |
| 132 | + {email} |
| 133 | + </p> |
| 134 | + )} |
| 135 | + <Item icon={ChartColumn} onClick={() => onShow("usage")}> |
| 136 | + {m.usage} |
| 137 | + </Item> |
| 138 | + <Item icon={Settings} onClick={() => onShow("settings")}> |
| 139 | + {m.settings} |
| 140 | + </Item> |
| 141 | + <div className="relative"> |
| 142 | + <Item |
| 143 | + icon={Languages} |
| 144 | + onClick={() => setLanguages(!languages)} |
| 145 | + expanded={languages} |
| 146 | + trailing={<ChevronRight aria-hidden size={14} className="text-muted" />} |
| 147 | + > |
| 148 | + {m.language} |
| 149 | + </Item> |
| 150 | + {languages && ( |
| 151 | + <div |
| 152 | + role="menu" |
| 153 | + aria-label={m.language} |
| 154 | + className={`absolute bottom-0 left-full ml-2 min-w-56 ${POPOVER}`} |
| 155 | + > |
| 156 | + {[{ id: "system" as const, name: t.shell.language.system(systemName) }, ...LOCALES].map( |
| 157 | + (entry) => ( |
| 158 | + <button |
| 159 | + key={entry.id} |
| 160 | + type="button" |
| 161 | + role="menuitemradio" |
| 162 | + aria-checked={choice === entry.id} |
| 163 | + onClick={() => setLocaleChoice(entry.id)} |
| 164 | + className={`h-8 whitespace-nowrap ${POPOVER_ITEM}`} |
| 165 | + > |
| 166 | + <Check aria-hidden size={14} className={choice === entry.id ? "" : "invisible"} /> |
| 167 | + {entry.name} |
| 168 | + </button> |
| 169 | + ), |
| 170 | + )} |
| 171 | + </div> |
| 172 | + )} |
| 173 | + </div> |
| 174 | + <div className="mx-1 my-1 border-line border-t" /> |
| 175 | + <Item icon={Sparkles} onClick={() => open(RELEASES)}> |
| 176 | + {m.whatsNew} |
| 177 | + </Item> |
| 178 | + <Item icon={CircleHelp} onClick={() => open(HELP)}> |
| 179 | + {m.help} |
| 180 | + </Item> |
| 181 | + </div> |
| 182 | + ); |
| 183 | +} |
| 184 | + |
| 185 | +function Item({ |
| 186 | + icon: Icon, |
| 187 | + onClick, |
| 188 | + expanded, |
| 189 | + trailing, |
| 190 | + children, |
| 191 | +}: { |
| 192 | + icon: typeof Settings; |
| 193 | + onClick(): void; |
| 194 | + expanded?: boolean; |
| 195 | + trailing?: ReactNode; |
| 196 | + children: ReactNode; |
| 197 | +}) { |
| 198 | + return ( |
| 199 | + <button |
| 200 | + type="button" |
| 201 | + role="menuitem" |
| 202 | + aria-haspopup={expanded === undefined ? undefined : "menu"} |
| 203 | + aria-expanded={expanded} |
| 204 | + onClick={onClick} |
| 205 | + className={`h-8 text-ink ${POPOVER_ITEM}`} |
| 206 | + > |
| 207 | + <Icon aria-hidden size={14} className="text-ink-soft" /> |
| 208 | + <span className="flex-1">{children}</span> |
| 209 | + {trailing} |
| 210 | + </button> |
| 211 | + ); |
| 212 | +} |
0 commit comments