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 { T, useGT, useMessages } from "gt-react"; |
| 2 | import { BookmarkIcon, CheckIcon, ChevronDownIcon, PlusIcon } from "lucide-react"; |
| 3 | import { useId, useState } from "react"; |
| 4 | import type * as Schema from "effect/Schema"; |
| 5 | import { |
| 6 | SAVED_VIEW_NAME_MAX_LENGTH, |
| 7 | savedViewNameError, |
| 8 | } from "@t3tools/client-runtime/saved-views"; |
| 9 | |
| 10 | import { getLocalStorageItem, useLocalStorage } from "../hooks/useLocalStorage"; |
| 11 | import { randomUUID } from "../lib/utils"; |
| 12 | import { Button } from "./ui/button"; |
| 13 | import { |
| 14 | Dialog, |
| 15 | DialogDescription, |
| 16 | DialogFooter, |
| 17 | DialogHeader, |
| 18 | DialogPanel, |
| 19 | DialogPopup, |
| 20 | DialogTitle, |
| 21 | } from "./ui/dialog"; |
| 22 | import { Input } from "./ui/input"; |
| 23 | import { Label } from "./ui/label"; |
| 24 | import { Menu, MenuItem, MenuItemLabel, MenuPopup, MenuSeparator, MenuTrigger } from "./ui/menu"; |
| 25 | import { toastManager } from "./ui/toast"; |
| 26 | import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; |
| 27 | |
| 28 | export interface SavedView<Preferences> { |
| 29 | readonly id: string; |
| 30 | readonly name: string; |
| 31 | readonly preferences: Preferences; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * What one list (pull requests, issues) stores and how it compares its list controls. `Stored` |
| 36 | * is the schema's decoded shape, which may spell absent fields as `undefined`. |
| 37 | */ |
| 38 | export interface SavedViewsKind<Preferences extends Stored, Stored, Encoded> { |
| 39 | readonly storageKey: string; |
| 40 | readonly schema: Schema.Codec<ReadonlyArray<SavedView<Stored>>, Encoded>; |
| 41 | readonly empty: ReadonlyArray<SavedView<Stored>>; |
| 42 | /** Picks whole translated sentences for this list; never inject it into copy. */ |
| 43 | readonly list: "pullRequest" | "issue"; |
| 44 | /** `msg()` text, resolved where it renders. */ |
| 45 | readonly placeholder: string; |
| 46 | /** `msg()` text or "". */ |
| 47 | readonly suggestedName: (preferences: Preferences) => string; |
| 48 | /** Drops the parts of the list state that a view does not keep. */ |
| 49 | readonly normalize: (preferences: Stored) => Preferences; |
| 50 | readonly matches: (view: SavedView<Stored>, preferences: Preferences) => boolean; |
| 51 | } |
| 52 | |
| 53 | export function SavedViewsMenu<Preferences extends Stored, Stored, Encoded>({ |
| 54 | kind, |
| 55 | preferences, |
| 56 | onApply, |
| 57 | }: { |
| 58 | kind: SavedViewsKind<Preferences, Stored, Encoded>; |
| 59 | preferences: Preferences; |
| 60 | onApply: (preferences: Preferences) => void; |
| 61 | }) { |
| 62 | const gt = useGT(); |
| 63 | const messages = useMessages(); |
| 64 | const [views, setViews] = useLocalStorage(kind.storageKey, kind.empty, kind.schema); |
| 65 | const [selectedId, setSelectedId] = useState<string | null>(null); |
| 66 | const [editor, setEditor] = useState<{ id?: string; name: string } | null>(null); |
| 67 | const inputId = useId(); |
| 68 | const errorId = useId(); |
| 69 | const selectedView = |
| 70 | views.find((view) => view.id === selectedId) ?? |
| 71 | views.find((view) => kind.matches(view, preferences)); |
| 72 | if (selectedView && selectedId !== selectedView.id) setSelectedId(selectedView.id); |
| 73 | const modified = selectedView !== undefined && !kind.matches(selectedView, preferences); |
| 74 | const nameError = editor ? savedViewNameError(editor.name, views, editor.id, messages) : null; |
| 75 | const savedViewsLabel = gt( |
| 76 | "{list, select, pullRequest {Saved pull request views} other {Saved issue views}}", |
| 77 | { list: kind.list }, |
| 78 | ); |
| 79 | |
| 80 | function persist( |
| 81 | update: (current: ReadonlyArray<SavedView<Stored>>) => ReadonlyArray<SavedView<Stored>>, |
| 82 | ) { |
| 83 | let written: ReadonlyArray<SavedView<Stored>> | undefined; |
| 84 | setViews((current) => { |
| 85 | written = update(current); |
| 86 | return written; |
| 87 | }); |
| 88 | // The shared hook logs storage failures. Keep the editor open and explain when saving failed. |
| 89 | try { |
| 90 | if ( |
| 91 | written && |
| 92 | JSON.stringify(getLocalStorageItem(kind.storageKey, kind.schema)) === |
| 93 | JSON.stringify(written) |
| 94 | ) { |
| 95 | return true; |
| 96 | } |
| 97 | } catch { |
| 98 | // A browser policy can deny reads as well as writes. |
| 99 | } |
| 100 | toastManager.add({ |
| 101 | type: "error", |
| 102 | title: gt( |
| 103 | "{list, select, pullRequest {Could not save PR views} other {Could not save issue views}}", |
| 104 | { list: kind.list }, |
| 105 | ), |
| 106 | description: gt( |
| 107 | "Local storage is unavailable or full. Try again after freeing space or allowing storage.", |
| 108 | ), |
| 109 | }); |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | function saveName() { |
| 114 | if (!editor || nameError) return; |
| 115 | const id = editor.id ?? randomUUID(); |
| 116 | const name = editor.name.trim(); |
| 117 | const editingId = editor.id; |
| 118 | let missing = false; |
| 119 | const saved = persist((current) => { |
| 120 | if (editingId && !current.some((view) => view.id === editingId)) { |
| 121 | missing = true; |
| 122 | return current; |
| 123 | } |
| 124 | return editingId |
| 125 | ? current.map((view) => (view.id === editingId ? { ...view, name } : view)) |
| 126 | : [...current, { id, name, preferences: kind.normalize(preferences) }]; |
| 127 | }); |
| 128 | if (missing) { |
| 129 | setEditor((current) => (current ? { name: current.name } : null)); |
| 130 | toastManager.add({ |
| 131 | type: "error", |
| 132 | title: gt("This saved view was removed"), |
| 133 | description: gt("Your name has been kept. Save it as a new view or cancel."), |
| 134 | }); |
| 135 | return; |
| 136 | } |
| 137 | if (!saved) return; |
| 138 | setSelectedId(id); |
| 139 | setEditor(null); |
| 140 | } |
| 141 | |
| 142 | return ( |
| 143 | <> |
| 144 | <Menu> |
| 145 | <Tooltip> |
| 146 | <TooltipTrigger |
| 147 | render={ |
| 148 | <MenuTrigger render={<Button variant="outline" />} aria-label={savedViewsLabel} /> |
| 149 | } |
| 150 | > |
| 151 | <BookmarkIcon aria-hidden className="size-4" /> |
| 152 | <span className="max-w-40 truncate"> |
| 153 | {selectedView?.name ?? gt("Views", { $context: "saved filter views menu" })} |
| 154 | </span> |
| 155 | {modified ? <span aria-label={gt("Modified view")}>*</span> : null} |
| 156 | <ChevronDownIcon aria-hidden className="size-3 shrink-0" /> |
| 157 | </TooltipTrigger> |
| 158 | <TooltipPopup>{selectedView?.name ?? savedViewsLabel}</TooltipPopup> |
| 159 | </Tooltip> |
| 160 | <MenuPopup align="start"> |
| 161 | {views.map((view) => ( |
| 162 | <Tooltip key={view.id}> |
| 163 | <TooltipTrigger |
| 164 | render={ |
| 165 | <MenuItem |
| 166 | onClick={() => { |
| 167 | setSelectedId(view.id); |
| 168 | onApply(kind.normalize(view.preferences)); |
| 169 | }} |
| 170 | /> |
| 171 | } |
| 172 | > |
| 173 | {selectedView?.id === view.id && !modified ? ( |
| 174 | <CheckIcon aria-hidden /> |
| 175 | ) : ( |
| 176 | <BookmarkIcon aria-hidden /> |
| 177 | )} |
| 178 | <MenuItemLabel>{view.name}</MenuItemLabel> |
| 179 | </TooltipTrigger> |
| 180 | <TooltipPopup side="right">{view.name}</TooltipPopup> |
| 181 | </Tooltip> |
| 182 | ))} |
| 183 | {views.length > 0 ? <MenuSeparator /> : null} |
| 184 | <MenuItem onClick={() => setEditor({ name: messages(kind.suggestedName(preferences)) })}> |
| 185 | <PlusIcon aria-hidden /> |
| 186 | <T>Save current view…</T> |
| 187 | </MenuItem> |
| 188 | {selectedView ? ( |
| 189 | <> |
| 190 | {modified ? ( |
| 191 | <MenuItem |
| 192 | onClick={() => |
| 193 | persist((current) => |
| 194 | current.map((view) => |
| 195 | view.id === selectedView.id |
| 196 | ? { ...view, preferences: kind.normalize(preferences) } |
| 197 | : view, |
| 198 | ), |
| 199 | ) |
| 200 | } |
| 201 | > |
| 202 | <T>Update saved view</T> |
| 203 | </MenuItem> |
| 204 | ) : null} |
| 205 | <MenuItem onClick={() => setEditor({ id: selectedView.id, name: selectedView.name })}> |
| 206 | <T>Rename view…</T> |
| 207 | </MenuItem> |
| 208 | <MenuItem |
| 209 | variant="destructive" |
| 210 | onClick={() => { |
| 211 | if (persist((current) => current.filter((view) => view.id !== selectedView.id))) |
| 212 | setSelectedId(null); |
| 213 | }} |
| 214 | > |
| 215 | <T>Delete view</T> |
| 216 | </MenuItem> |
| 217 | </> |
| 218 | ) : null} |
| 219 | </MenuPopup> |
| 220 | </Menu> |
| 221 | <Dialog |
| 222 | open={editor !== null} |
| 223 | onOpenChange={(open) => { |
| 224 | if (!open) setEditor(null); |
| 225 | }} |
| 226 | > |
| 227 | <DialogPopup> |
| 228 | <form |
| 229 | className="flex min-h-0 flex-col" |
| 230 | onSubmit={(event) => { |
| 231 | event.preventDefault(); |
| 232 | saveName(); |
| 233 | }} |
| 234 | > |
| 235 | <DialogHeader> |
| 236 | <DialogTitle> |
| 237 | {editor?.id |
| 238 | ? gt("{list, select, pullRequest {Rename PR view} other {Rename issue view}}", { |
| 239 | list: kind.list, |
| 240 | }) |
| 241 | : gt("{list, select, pullRequest {Save PR view} other {Save issue view}}", { |
| 242 | list: kind.list, |
| 243 | })} |
| 244 | </DialogTitle> |
| 245 | <DialogDescription> |
| 246 | <T>Save your search, filters, and sort order on this device.</T> |
| 247 | </DialogDescription> |
| 248 | </DialogHeader> |
| 249 | <DialogPanel> |
| 250 | <div className="flex flex-col gap-2"> |
| 251 | <Label htmlFor={inputId}> |
| 252 | <T>View name</T> |
| 253 | </Label> |
| 254 | <Input |
| 255 | id={inputId} |
| 256 | autoFocus |
| 257 | value={editor?.name ?? ""} |
| 258 | maxLength={SAVED_VIEW_NAME_MAX_LENGTH} |
| 259 | placeholder={messages(kind.placeholder)} |
| 260 | aria-invalid={Boolean(editor?.name && nameError)} |
| 261 | aria-describedby={nameError && editor?.name ? errorId : undefined} |
| 262 | onChange={(event) => |
| 263 | setEditor((current) => |
| 264 | current ? { ...current, name: event.target.value } : null, |
| 265 | ) |
| 266 | } |
| 267 | /> |
| 268 | {nameError && editor?.name ? ( |
| 269 | <p id={errorId} className="text-sm text-destructive-foreground"> |
| 270 | {nameError} |
| 271 | </p> |
| 272 | ) : null} |
| 273 | </div> |
| 274 | </DialogPanel> |
| 275 | <DialogFooter> |
| 276 | <Button variant="outline" onClick={() => setEditor(null)}> |
| 277 | <T>Cancel</T> |
| 278 | </Button> |
| 279 | <Button type="submit" disabled={nameError !== null}> |
| 280 | <T>Save</T> |
| 281 | </Button> |
| 282 | </DialogFooter> |
| 283 | </form> |
| 284 | </DialogPopup> |
| 285 | </Dialog> |
| 286 | </> |
| 287 | ); |
| 288 | } |