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 type { EnvironmentId, UnifiedSettings } from "@t3tools/contracts"; |
| 2 | import { T, useGT, Var } from "gt-react"; |
| 3 | import { PlusIcon } from "lucide-react"; |
| 4 | import { useState } from "react"; |
| 5 | |
| 6 | import { useUpdateEnvironmentSettings } from "../../hooks/useSettings"; |
| 7 | import { |
| 8 | AlertDialog, |
| 9 | AlertDialogClose, |
| 10 | AlertDialogDescription, |
| 11 | AlertDialogFooter, |
| 12 | AlertDialogHeader, |
| 13 | AlertDialogPopup, |
| 14 | AlertDialogTitle, |
| 15 | } from "../ui/alert-dialog"; |
| 16 | import { Button } from "../ui/button"; |
| 17 | import { AddUsageLimitSourceDialog } from "./AddUsageLimitSourceDialog"; |
| 18 | import { useSearchableSetting } from "./settingsSearch"; |
| 19 | import { SettingsRow, SettingsSection } from "./settingsLayout"; |
| 20 | |
| 21 | const CLI_PROXY_NAME = "CLI Proxy"; |
| 22 | |
| 23 | export function UsageProviderSettings({ |
| 24 | environmentId, |
| 25 | environmentLabel, |
| 26 | sources, |
| 27 | readOnly, |
| 28 | }: { |
| 29 | readonly environmentId: EnvironmentId; |
| 30 | readonly environmentLabel: string; |
| 31 | readonly sources: UnifiedSettings["usageLimitSources"]; |
| 32 | readonly readOnly: boolean; |
| 33 | }) { |
| 34 | const gt = useGT(); |
| 35 | const searchableSetting = useSearchableSetting(); |
| 36 | const updateSettings = useUpdateEnvironmentSettings(environmentId); |
| 37 | const [adding, setAdding] = useState(false); |
| 38 | const entries = Object.entries(sources); |
| 39 | |
| 40 | return ( |
| 41 | <> |
| 42 | <SettingsSection |
| 43 | {...searchableSetting("usage-providers")} |
| 44 | headerAction={ |
| 45 | !readOnly ? ( |
| 46 | <Button size="xs" variant="outline" onClick={() => setAdding(true)}> |
| 47 | <PlusIcon className="size-3" aria-hidden /> |
| 48 | <T>Add hub</T> |
| 49 | </Button> |
| 50 | ) : null |
| 51 | } |
| 52 | > |
| 53 | {entries.length === 0 ? ( |
| 54 | <SettingsRow title={gt("No hubs configured.")} /> |
| 55 | ) : ( |
| 56 | entries.map(([id, source]) => { |
| 57 | const label = source.label?.trim() || source.url; |
| 58 | return ( |
| 59 | <SettingsRow |
| 60 | key={id} |
| 61 | title={label} |
| 62 | description={ |
| 63 | <span className="break-all"> |
| 64 | {source.enabled |
| 65 | ? CLI_PROXY_NAME |
| 66 | : gt("{name} · Disabled", { name: CLI_PROXY_NAME })} |
| 67 | {label !== source.url ? ` · ${source.url}` : ""} |
| 68 | </span> |
| 69 | } |
| 70 | control={ |
| 71 | !readOnly ? ( |
| 72 | <RemoveUsageProviderButton |
| 73 | label={label} |
| 74 | onConfirm={() => updateSettings({ usageLimitSources: { [id]: null } })} |
| 75 | /> |
| 76 | ) : null |
| 77 | } |
| 78 | /> |
| 79 | ); |
| 80 | }) |
| 81 | )} |
| 82 | </SettingsSection> |
| 83 | {adding && !readOnly ? ( |
| 84 | <AddUsageLimitSourceDialog |
| 85 | open |
| 86 | onOpenChange={setAdding} |
| 87 | environmentId={environmentId} |
| 88 | environmentLabel={environmentLabel} |
| 89 | sources={sources} |
| 90 | /> |
| 91 | ) : null} |
| 92 | </> |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | function RemoveUsageProviderButton({ |
| 97 | label, |
| 98 | onConfirm, |
| 99 | }: { |
| 100 | readonly label: string; |
| 101 | readonly onConfirm: () => void; |
| 102 | }) { |
| 103 | const [open, setOpen] = useState(false); |
| 104 | return ( |
| 105 | <> |
| 106 | <Button size="xs" variant="ghost" onClick={() => setOpen(true)}> |
| 107 | <T>Remove</T> |
| 108 | </Button> |
| 109 | <AlertDialog open={open} onOpenChange={setOpen}> |
| 110 | <AlertDialogPopup> |
| 111 | <AlertDialogHeader> |
| 112 | <AlertDialogTitle> |
| 113 | <T> |
| 114 | Remove <Var>{label}</Var>? |
| 115 | </T> |
| 116 | </AlertDialogTitle> |
| 117 | <AlertDialogDescription> |
| 118 | <T> |
| 119 | The hub's management key is deleted from this server. Its accounts leave the Limits |
| 120 | view; the hub itself is untouched. Add it again with the URL and key to bring them |
| 121 | back. |
| 122 | </T> |
| 123 | </AlertDialogDescription> |
| 124 | </AlertDialogHeader> |
| 125 | <AlertDialogFooter> |
| 126 | <AlertDialogClose render={<Button variant="outline" />}> |
| 127 | <T>Cancel</T> |
| 128 | </AlertDialogClose> |
| 129 | <Button |
| 130 | variant="destructive" |
| 131 | onClick={() => { |
| 132 | setOpen(false); |
| 133 | onConfirm(); |
| 134 | }} |
| 135 | > |
| 136 | <T>Remove hub</T> |
| 137 | </Button> |
| 138 | </AlertDialogFooter> |
| 139 | </AlertDialogPopup> |
| 140 | </AlertDialog> |
| 141 | </> |
| 142 | ); |
| 143 | } |