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 { useAtomValue } from "@effect/atom-react"; |
| 2 | import { AuthSettingsWriteScope, type EnvironmentId, sessionGrantsScope } from "@t3tools/contracts"; |
| 3 | import { ChevronDownIcon, PlusIcon, RotateCcwIcon, XIcon } from "lucide-react"; |
| 4 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 5 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 6 | import { useRef, useState } from "react"; |
| 7 | |
| 8 | import { cn } from "../../lib/utils"; |
| 9 | import { environmentPresentations } from "../../state/presentation"; |
| 10 | import { serverEnvironment } from "../../state/server"; |
| 11 | import { environmentSession, readEnvironmentScope } from "../../state/session"; |
| 12 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 13 | import type { EnvironmentUsageStatus } from "../../state/usage"; |
| 14 | import { Button } from "../ui/button"; |
| 15 | import { |
| 16 | Dialog, |
| 17 | DialogDescription, |
| 18 | DialogFooter, |
| 19 | DialogHeader, |
| 20 | DialogPanel, |
| 21 | DialogPopup, |
| 22 | DialogTitle, |
| 23 | } from "../ui/dialog"; |
| 24 | import { Input } from "../ui/input"; |
| 25 | import { Label } from "../ui/label"; |
| 26 | import { Menu, MenuCheckboxItem, MenuPopup, MenuSeparator, MenuTrigger } from "../ui/menu"; |
| 27 | import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "../ui/table"; |
| 28 | import { Tooltip, TooltipTrigger, TooltipPopup } from "../ui/tooltip"; |
| 29 | import { isUsagePriceEdit, USAGE_PRICE_FIELDS } from "./usagePriceForm"; |
| 30 | import { |
| 31 | isEmptyUsagePriceDraft, |
| 32 | usageAliasCell, |
| 33 | usagePriceCell, |
| 34 | usagePriceTableChanges, |
| 35 | usagePriceTableErrors, |
| 36 | type UsagePriceDraft, |
| 37 | type UsagePriceField, |
| 38 | } from "./usagePriceTable"; |
| 39 | import { |
| 40 | writeUsagePrices, |
| 41 | type UsagePriceTarget, |
| 42 | type UsagePriceWriteResult, |
| 43 | } from "./usagePriceTargets"; |
| 44 | |
| 45 | const priceTargetsAtom = Atom.make((get): readonly UsagePriceTarget[] => |
| 46 | [...get(environmentPresentations.presentationsAtom)].map(([environmentId, environment]) => { |
| 47 | const settings = get(serverEnvironment.settingsValueAtom(environmentId)); |
| 48 | const session = get(environmentSession.sessionStateAtom(environmentId)); |
| 49 | const sessionData = AsyncResult.getOrElse(session, () => null); |
| 50 | const access = |
| 51 | session._tag === "Failure" |
| 52 | ? "denied" |
| 53 | : sessionData === null |
| 54 | ? session.waiting |
| 55 | ? "pending" |
| 56 | : "denied" |
| 57 | : sessionGrantsScope(sessionData, AuthSettingsWriteScope) |
| 58 | ? "granted" |
| 59 | : "denied"; |
| 60 | return { |
| 61 | environmentId, |
| 62 | label: environment.entry.target.label, |
| 63 | prices: settings?.usagePriceOverrides ?? null, |
| 64 | aliases: |
| 65 | environment.serverConfig?.environment.capabilities.usageModelAliases === true |
| 66 | ? (settings?.usageModelAliases ?? null) |
| 67 | : null, |
| 68 | unavailable: |
| 69 | environment.connection.phase !== "connected" |
| 70 | ? msg("Offline") |
| 71 | : settings === null |
| 72 | ? msg("Prices not loaded") |
| 73 | : access === "pending" |
| 74 | ? msg("Checking permissions…") |
| 75 | : access === "denied" |
| 76 | ? msg("Read-only access") |
| 77 | : null, |
| 78 | }; |
| 79 | }), |
| 80 | ); |
| 81 | |
| 82 | type SaveAttempt = { |
| 83 | readonly drafts: readonly UsagePriceDraft[]; |
| 84 | readonly destinations: readonly { |
| 85 | readonly environmentId: EnvironmentId; |
| 86 | readonly label: string; |
| 87 | }[]; |
| 88 | readonly results: ReadonlyMap< |
| 89 | EnvironmentId, |
| 90 | UsagePriceWriteResult | { readonly status: "saving" } |
| 91 | >; |
| 92 | }; |
| 93 | |
| 94 | export function UsagePriceOverrides({ |
| 95 | usage, |
| 96 | initialSelectedEnvironmentIds, |
| 97 | initialModel, |
| 98 | onOpenChange, |
| 99 | }: { |
| 100 | readonly usage: readonly EnvironmentUsageStatus[]; |
| 101 | readonly initialSelectedEnvironmentIds: ReadonlySet<EnvironmentId> | null; |
| 102 | readonly initialModel?: string | undefined; |
| 103 | readonly onOpenChange: (open: boolean) => void; |
| 104 | }) { |
| 105 | const gt = useGT(); |
| 106 | const messages = useMessages(); |
| 107 | const environments = useAtomValue(priceTargetsAtom).map((target) => |
| 108 | target.unavailable === null ? target : { ...target, unavailable: messages(target.unavailable) }, |
| 109 | ); |
| 110 | const [selectedIds, setSelectedIds] = useState(initialSelectedEnvironmentIds); |
| 111 | const selected = environments.filter( |
| 112 | (environment) => selectedIds === null || selectedIds.has(environment.environmentId), |
| 113 | ); |
| 114 | const [drafts, setDrafts] = useState<readonly UsagePriceDraft[]>(() => |
| 115 | initialModel === undefined || |
| 116 | selected.some( |
| 117 | (environment) => |
| 118 | Object.hasOwn(environment.prices ?? {}, initialModel) || |
| 119 | Object.hasOwn(environment.aliases ?? {}, initialModel), |
| 120 | ) |
| 121 | ? [] |
| 122 | : [{ id: "new:initial", model: initialModel, isNew: true, values: {} }], |
| 123 | ); |
| 124 | const [pending, setPending] = useState(false); |
| 125 | const [attempt, setAttempt] = useState<SaveAttempt | null>(null); |
| 126 | const focusRowRef = useRef<string | null>(null); |
| 127 | const nextRowId = useRef(0); |
| 128 | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { reportFailure: false }); |
| 129 | const customModels = [ |
| 130 | ...new Set( |
| 131 | selected.flatMap((environment) => [ |
| 132 | ...Object.keys(environment.prices ?? {}), |
| 133 | ...Object.keys(environment.aliases ?? {}), |
| 134 | ]), |
| 135 | ), |
| 136 | ].sort(); |
| 137 | const models = [ |
| 138 | ...new Set([ |
| 139 | ...customModels, |
| 140 | ...usage |
| 141 | .filter((environment) => |
| 142 | selected.some((target) => target.environmentId === environment.environmentId), |
| 143 | ) |
| 144 | .flatMap((environment) => environment.summary?.buckets.map((bucket) => bucket.model) ?? []), |
| 145 | ]), |
| 146 | ].sort(); |
| 147 | const newModels = new Set( |
| 148 | drafts.filter((draft) => draft.isNew).map((draft) => draft.model.trim()), |
| 149 | ); |
| 150 | const rows: readonly UsagePriceDraft[] = [ |
| 151 | ...customModels |
| 152 | .filter((model) => attempt === null || !newModels.has(model)) |
| 153 | .map( |
| 154 | (model) => |
| 155 | drafts.find((draft) => draft.id === `model:${model}`) ?? { |
| 156 | id: `model:${model}`, |
| 157 | model, |
| 158 | isNew: false, |
| 159 | values: {}, |
| 160 | }, |
| 161 | ), |
| 162 | ...drafts.filter((draft) => draft.isNew), |
| 163 | ]; |
| 164 | const stagedDrafts = drafts.filter((draft) => !isEmptyUsagePriceDraft(draft)); |
| 165 | const errors = usagePriceTableErrors(selected, stagedDrafts, messages); |
| 166 | // New rows still block Save, but show a price error only once a price or mapping is typed. |
| 167 | const awaitingPrice = new Set<string>(); |
| 168 | for (const draft of stagedDrafts) { |
| 169 | if (!draft.isNew) continue; |
| 170 | if (draft.model.trim() === "") errors.set(draft.id, gt("Enter a model ID.")); |
| 171 | else if ( |
| 172 | attempt === null && |
| 173 | (customModels.includes(draft.model.trim()) || |
| 174 | drafts.some((other) => other.id !== draft.id && other.model.trim() === draft.model.trim())) |
| 175 | ) |
| 176 | errors.set(draft.id, gt("This model already has a row. Edit its prices there.")); |
| 177 | else if ( |
| 178 | (draft.alias ?? "").trim() === "" && |
| 179 | Object.values(draft.values).every((value) => value.trim() === "") |
| 180 | ) |
| 181 | awaitingPrice.add(draft.id); |
| 182 | } |
| 183 | const failedDestinations = |
| 184 | attempt?.destinations.filter( |
| 185 | (destination) => attempt.results.get(destination.environmentId)?.status === "failed", |
| 186 | ) ?? []; |
| 187 | const locked = pending || failedDestinations.length > 0; |
| 188 | const hasChanges = stagedDrafts.length > 0; |
| 189 | const destinationLabel = |
| 190 | selected.length === 1 |
| 191 | ? selected[0]!.label |
| 192 | : gt("{count, plural, one {{count} environments} other {{count} environments}}", { |
| 193 | count: selected.length, |
| 194 | }); |
| 195 | const selectionLabel = selectedIds === null ? gt("All environments") : destinationLabel; |
| 196 | const discard = () => { |
| 197 | setDrafts([]); |
| 198 | setAttempt(null); |
| 199 | }; |
| 200 | const selectEnvironments = (ids: ReadonlySet<EnvironmentId> | null) => { |
| 201 | setSelectedIds(ids); |
| 202 | setAttempt(null); |
| 203 | }; |
| 204 | const updateDraft = (draft: UsagePriceDraft) => { |
| 205 | setDrafts((previous) => |
| 206 | previous.some((entry) => entry.id === draft.id) |
| 207 | ? previous.map((entry) => (entry.id === draft.id ? draft : entry)) |
| 208 | : [...previous, draft], |
| 209 | ); |
| 210 | setAttempt(null); |
| 211 | }; |
| 212 | const matchesSaved = ( |
| 213 | row: UsagePriceDraft, |
| 214 | cell: { value: string; placeholder: string }, |
| 215 | value: string, |
| 216 | ) => |
| 217 | !row.isNew && |
| 218 | cell.placeholder !== gt("Mixed") && |
| 219 | cell.placeholder !== gt("Unavailable") && |
| 220 | value === cell.value; |
| 221 | const updateOrDropDraft = (draft: UsagePriceDraft) => { |
| 222 | if (!draft.isNew && Object.keys(draft.values).length === 0 && draft.alias === undefined) |
| 223 | setDrafts((previous) => previous.filter((entry) => entry.id !== draft.id)); |
| 224 | else updateDraft(draft); |
| 225 | }; |
| 226 | const editCell = (row: UsagePriceDraft, field: UsagePriceField, value: string) => { |
| 227 | const values = { ...row.values, [field]: value }; |
| 228 | const original = usagePriceCell(selected, row.model, field, messages); |
| 229 | if (matchesSaved(row, original, value)) delete values[field]; |
| 230 | updateOrDropDraft({ ...row, values }); |
| 231 | }; |
| 232 | const editAlias = (row: UsagePriceDraft, value: string) => { |
| 233 | const { alias: _alias, ...rest } = row; |
| 234 | const original = usageAliasCell(selected, row.model, messages); |
| 235 | updateOrDropDraft(matchesSaved(row, original, value) ? rest : { ...rest, alias: value }); |
| 236 | }; |
| 237 | const save = async (retry = false) => { |
| 238 | if (pending) return; |
| 239 | const destinations = retry ? failedDestinations : selected; |
| 240 | const edits = retry && attempt ? attempt.drafts : stagedDrafts; |
| 241 | if (destinations.length === 0 || edits.length === 0) return; |
| 242 | const targets = destinations.map( |
| 243 | (destination) => |
| 244 | environments.find( |
| 245 | (environment) => environment.environmentId === destination.environmentId, |
| 246 | ) ?? { |
| 247 | ...destination, |
| 248 | prices: null, |
| 249 | aliases: null, |
| 250 | unavailable: gt("Environment removed"), |
| 251 | }, |
| 252 | ); |
| 253 | const changes = new Map( |
| 254 | targets.map((target) => [ |
| 255 | target.environmentId, |
| 256 | usagePriceTableChanges(target, edits, messages), |
| 257 | ]), |
| 258 | ); |
| 259 | setPending(true); |
| 260 | setAttempt((previous) => ({ |
| 261 | drafts: edits, |
| 262 | destinations: retry && previous ? previous.destinations : targets, |
| 263 | results: new Map([ |
| 264 | ...(retry && previous ? previous.results : []), |
| 265 | ...targets.map((target) => [target.environmentId, { status: "saving" } as const] as const), |
| 266 | ]), |
| 267 | })); |
| 268 | let failed = false; |
| 269 | await writeUsagePrices({ |
| 270 | targets: targets.map((target) => ({ |
| 271 | ...target, |
| 272 | unavailable: |
| 273 | target.unavailable ?? [...changes.get(target.environmentId)!.errors.values()][0] ?? null, |
| 274 | })), |
| 275 | changes: new Map([...changes].map(([id, plan]) => [id, plan.changes])), |
| 276 | write: async (input) => |
| 277 | readEnvironmentScope(input.environmentId, AuthSettingsWriteScope) |
| 278 | ? updateSettings(input) |
| 279 | : { _tag: "Failure" }, |
| 280 | onResult: (environmentId, result) => { |
| 281 | if (result.status === "failed") failed = true; |
| 282 | setAttempt((previous) => |
| 283 | previous === null |
| 284 | ? null |
| 285 | : { ...previous, results: new Map(previous.results).set(environmentId, result) }, |
| 286 | ); |
| 287 | }, |
| 288 | }); |
| 289 | setPending(false); |
| 290 | if (!failed) setDrafts([]); |
| 291 | }; |
| 292 | |
| 293 | return ( |
| 294 | <Dialog |
| 295 | open |
| 296 | onOpenChange={(open) => { |
| 297 | if (!pending) onOpenChange(open); |
| 298 | }} |
| 299 | > |
| 300 | <DialogPopup className="max-w-4xl"> |
| 301 | <DialogHeader> |
| 302 | <DialogTitle> |
| 303 | <T>Custom model prices</T> |
| 304 | </DialogTitle> |
| 305 | <DialogDescription> |
| 306 | <T> |
| 307 | Prices and mappings apply to all past and future usage on the environments you select. |
| 308 | </T> |
| 309 | </DialogDescription> |
| 310 | </DialogHeader> |
| 311 | <DialogPanel> |
| 312 | <div className="flex flex-wrap items-center justify-between gap-2"> |
| 313 | <div className="flex min-w-0 items-center gap-2"> |
| 314 | <Label id="usage-prices-apply-label" className="shrink-0"> |
| 315 | <T>Apply to</T> |
| 316 | </Label> |
| 317 | <Menu> |
| 318 | <MenuTrigger |
| 319 | render={<Button variant="outline" size="sm" className="min-w-0" />} |
| 320 | aria-labelledby="usage-prices-apply-label usage-prices-selection" |
| 321 | disabled={pending || hasChanges} |
| 322 | > |
| 323 | <span id="usage-prices-selection" className="truncate"> |
| 324 | {selectionLabel} |
| 325 | </span> |
| 326 | <ChevronDownIcon className="size-3.5 shrink-0" aria-hidden /> |
| 327 | </MenuTrigger> |
| 328 | <MenuPopup align="start"> |
| 329 | <MenuCheckboxItem |
| 330 | checked={selectedIds === null} |
| 331 | closeOnClick={false} |
| 332 | onCheckedChange={(checked) => selectEnvironments(checked ? null : new Set())} |
| 333 | > |
| 334 | <T>All environments</T> |
| 335 | </MenuCheckboxItem> |
| 336 | <MenuSeparator /> |
| 337 | {environments.map((environment) => ( |
| 338 | <MenuCheckboxItem |
| 339 | key={environment.environmentId} |
| 340 | checked={selectedIds === null || selectedIds.has(environment.environmentId)} |
| 341 | closeOnClick={false} |
| 342 | onCheckedChange={(checked) => { |
| 343 | const next = new Set(selected.map((target) => target.environmentId)); |
| 344 | if (checked) next.add(environment.environmentId); |
| 345 | else next.delete(environment.environmentId); |
| 346 | selectEnvironments(next.size === environments.length ? null : next); |
| 347 | }} |
| 348 | > |
| 349 | <span className="flex min-w-0 items-center gap-3"> |
| 350 | <span className="min-w-0 flex-1 truncate">{environment.label}</span> |
| 351 | {environment.unavailable ? ( |
| 352 | <span className="text-xs text-muted-foreground"> |
| 353 | {environment.unavailable} |
| 354 | </span> |
| 355 | ) : null} |
| 356 | </span> |
| 357 | </MenuCheckboxItem> |
| 358 | ))} |
| 359 | </MenuPopup> |
| 360 | </Menu> |
| 361 | </div> |
| 362 | <span className="text-xs text-muted-foreground"> |
| 363 | <T>USD / million tokens</T> |
| 364 | </span> |
| 365 | </div> |
| 366 | {selected.length === 0 ? ( |
| 367 | <p className="text-sm text-muted-foreground"> |
| 368 | {environments.length === 0 |
| 369 | ? gt("Connect an environment to set model prices.") |
| 370 | : gt("Select an environment to see and change its model prices.")} |
| 371 | </p> |
| 372 | ) : ( |
| 373 | <> |
| 374 | <div className="min-w-0 overflow-hidden rounded-lg border border-border"> |
| 375 | <Table className="min-w-180 table-fixed" aria-label={gt("Custom model prices")}> |
| 376 | <colgroup> |
| 377 | <col className="w-[22%]" /> |
| 378 | <col className="w-[18%]" /> |
| 379 | {USAGE_PRICE_FIELDS.map((field) => ( |
| 380 | <col key={field.key} /> |
| 381 | ))} |
| 382 | <col className="w-10" /> |
| 383 | </colgroup> |
| 384 | <TableHeader> |
| 385 | <TableRow> |
| 386 | <TableHead> |
| 387 | <T>Model ID</T> |
| 388 | </TableHead> |
| 389 | <TableHead> |
| 390 | <T>Map to</T> |
| 391 | </TableHead> |
| 392 | {USAGE_PRICE_FIELDS.map((field) => ( |
| 393 | <TableHead key={field.key}>{messages(field.label)}</TableHead> |
| 394 | ))} |
| 395 | <TableHead> |
| 396 | <Button |
| 397 | size="icon-xs" |
| 398 | variant="ghost" |
| 399 | aria-label={gt("Add model price")} |
| 400 | disabled={locked} |
| 401 | onClick={() => { |
| 402 | const id = `new:${nextRowId.current++}`; |
| 403 | focusRowRef.current = id; |
| 404 | updateDraft({ id, model: "", isNew: true, values: {} }); |
| 405 | }} |
| 406 | > |
| 407 | <PlusIcon aria-hidden /> |
| 408 | </Button> |
| 409 | </TableHead> |
| 410 | </TableRow> |
| 411 | </TableHeader> |
| 412 | <TableBody> |
| 413 | {rows.length === 0 ? ( |
| 414 | <TableRow> |
| 415 | <TableCell colSpan={7} className="text-center whitespace-normal"> |
| 416 | <p className="py-6 text-muted-foreground"> |
| 417 | {selected.some((environment) => environment.prices === null) |
| 418 | ? gt("Some environment prices are unavailable.") |
| 419 | : gt("No custom prices or mappings. Add a row to set one.")} |
| 420 | </p> |
| 421 | </TableCell> |
| 422 | </TableRow> |
| 423 | ) : ( |
| 424 | rows.map((row) => { |
| 425 | const aliasCell = usageAliasCell(selected, row.model, messages); |
| 426 | const alias = (row.alias ?? aliasCell.value).trim(); |
| 427 | const error = awaitingPrice.has(row.id) ? undefined : errors.get(row.id); |
| 428 | return ( |
| 429 | <TableRow key={row.id} data-row-id={row.id}> |
| 430 | <TableCell className="whitespace-normal"> |
| 431 | {row.isNew ? ( |
| 432 | <Input |
| 433 | size="compact" |
| 434 | value={row.model} |
| 435 | ref={(node) => { |
| 436 | if (node && focusRowRef.current === row.id) { |
| 437 | node.focus(); |
| 438 | focusRowRef.current = null; |
| 439 | } |
| 440 | }} |
| 441 | aria-label={gt("New model ID")} |
| 442 | aria-invalid={ |
| 443 | (row.model.trim() !== "" && error !== undefined) || undefined |
| 444 | } |
| 445 | list="usage-price-models" |
| 446 | placeholder={gt("Model ID")} |
| 447 | autoComplete="off" |
| 448 | spellCheck={false} |
| 449 | disabled={locked} |
| 450 | onChange={(event) => |
| 451 | updateDraft({ ...row, model: event.target.value }) |
| 452 | } |
| 453 | /> |
| 454 | ) : ( |
| 455 | <span |
| 456 | className={cn( |
| 457 | "block break-all", |
| 458 | row.removed && "text-muted-foreground line-through", |
| 459 | )} |
| 460 | > |
| 461 | {row.model} |
| 462 | </span> |
| 463 | )} |
| 464 | {error !== undefined && |
| 465 | (row.model.trim() !== "" || |
| 466 | Object.values(row.values).some((value) => value !== "")) ? ( |
| 467 | <p role="alert" className="mt-1 text-xs text-destructive"> |
| 468 | {error} |
| 469 | </p> |
| 470 | ) : null} |
| 471 | </TableCell> |
| 472 | {row.removed ? ( |
| 473 | <TableCell colSpan={5}> |
| 474 | <span className="text-muted-foreground"> |
| 475 | <T>Automatic pricing after saving</T> |
| 476 | </span> |
| 477 | </TableCell> |
| 478 | ) : ( |
| 479 | <TableCell> |
| 480 | <Input |
| 481 | size="compact" |
| 482 | value={row.alias ?? aliasCell.value} |
| 483 | aria-label={gt("Map {model} to model", { |
| 484 | model: row.model || gt("new model"), |
| 485 | })} |
| 486 | list="usage-alias-models" |
| 487 | placeholder={row.isNew ? gt("Optional") : aliasCell.placeholder} |
| 488 | autoComplete="off" |
| 489 | spellCheck={false} |
| 490 | disabled={locked} |
| 491 | onChange={(event) => editAlias(row, event.target.value)} |
| 492 | /> |
| 493 | </TableCell> |
| 494 | )} |
| 495 | {row.removed ? null : alias !== "" ? ( |
| 496 | <TableCell colSpan={4} className="whitespace-normal"> |
| 497 | <span className="break-all text-muted-foreground"> |
| 498 | <T> |
| 499 | Counted as <Var>{alias}</Var> |
| 500 | </T> |
| 501 | </span> |
| 502 | </TableCell> |
| 503 | ) : ( |
| 504 | USAGE_PRICE_FIELDS.map((field) => { |
| 505 | const cell = usagePriceCell( |
| 506 | selected, |
| 507 | row.model, |
| 508 | field.key, |
| 509 | messages, |
| 510 | ); |
| 511 | const value = |
| 512 | row.values[field.key] ?? (row.isNew ? "" : cell.value); |
| 513 | return ( |
| 514 | <TableCell key={field.key}> |
| 515 | <Input |
| 516 | size="compact" |
| 517 | inputMode="decimal" |
| 518 | aria-label={gt("{field} price for {model}", { |
| 519 | field: messages(field.label), |
| 520 | model: row.model || gt("new model"), |
| 521 | })} |
| 522 | value={value} |
| 523 | placeholder={ |
| 524 | row.isNew |
| 525 | ? field.optional |
| 526 | ? gt("Input rate") |
| 527 | : "0.00" |
| 528 | : cell.placeholder |
| 529 | } |
| 530 | autoComplete="off" |
| 531 | disabled={locked} |
| 532 | font="mono" |
| 533 | onChange={(event) => { |
| 534 | if (isUsagePriceEdit(value, event.target.value)) |
| 535 | editCell(row, field.key, event.target.value); |
| 536 | }} |
| 537 | /> |
| 538 | </TableCell> |
| 539 | ); |
| 540 | }) |
| 541 | )} |
| 542 | <TableCell> |
| 543 | <Tooltip> |
| 544 | <TooltipTrigger |
| 545 | render={<Button size="icon-xs" variant="ghost" />} |
| 546 | disabled={locked} |
| 547 | aria-label={ |
| 548 | row.removed |
| 549 | ? gt("Undo reset for {model}", { model: row.model }) |
| 550 | : row.isNew |
| 551 | ? gt("Remove new model") |
| 552 | : gt("Reset price for {model} to automatic", { |
| 553 | model: row.model, |
| 554 | }) |
| 555 | } |
| 556 | onClick={() => { |
| 557 | if (row.isNew) |
| 558 | setDrafts((previous) => |
| 559 | previous.filter((entry) => entry.id !== row.id), |
| 560 | ); |
| 561 | else if (row.removed) |
| 562 | updateOrDropDraft({ ...row, removed: false }); |
| 563 | else updateDraft({ ...row, removed: true }); |
| 564 | }} |
| 565 | > |
| 566 | {row.isNew ? ( |
| 567 | <XIcon aria-hidden /> |
| 568 | ) : ( |
| 569 | <RotateCcwIcon aria-hidden /> |
| 570 | )} |
| 571 | </TooltipTrigger> |
| 572 | <TooltipPopup> |
| 573 | {row.removed |
| 574 | ? gt("Undo reset") |
| 575 | : row.isNew |
| 576 | ? gt("Remove row") |
| 577 | : gt("Reset to automatic")} |
| 578 | </TooltipPopup> |
| 579 | </Tooltip> |
| 580 | </TableCell> |
| 581 | </TableRow> |
| 582 | ); |
| 583 | }) |
| 584 | )} |
| 585 | </TableBody> |
| 586 | </Table> |
| 587 | </div> |
| 588 | <datalist id="usage-price-models"> |
| 589 | {models |
| 590 | .filter((model) => !customModels.includes(model)) |
| 591 | .map((model) => ( |
| 592 | <option key={model} value={model} /> |
| 593 | ))} |
| 594 | </datalist> |
| 595 | <datalist id="usage-alias-models"> |
| 596 | {models |
| 597 | .filter((model) => |
| 598 | selected.every( |
| 599 | (environment) => !Object.hasOwn(environment.aliases ?? {}, model), |
| 600 | ), |
| 601 | ) |
| 602 | .map((model) => ( |
| 603 | <option key={model} value={model} /> |
| 604 | ))} |
| 605 | </datalist> |
| 606 | <p className="text-xs text-muted-foreground"> |
| 607 | {gt( |
| 608 | "Blank cache rates use the input price. Enter 0 for free tokens. A mapped model’s usage counts as the model it maps to.", |
| 609 | )} |
| 610 | {selected.length > 1 |
| 611 | ? ` ${gt("Mixed cells keep each environment’s rate until you edit them.")}` |
| 612 | : ""} |
| 613 | </p> |
| 614 | </> |
| 615 | )} |
| 616 | {attempt ? ( |
| 617 | <div role="status" className="grid gap-1 text-xs"> |
| 618 | {attempt.destinations.map((destination) => { |
| 619 | const result = attempt.results.get(destination.environmentId); |
| 620 | return ( |
| 621 | <div key={destination.environmentId} className="flex justify-between gap-3"> |
| 622 | <span className="truncate text-muted-foreground">{destination.label}</span> |
| 623 | <span |
| 624 | className={ |
| 625 | result?.status === "failed" |
| 626 | ? "text-right text-destructive" |
| 627 | : "text-muted-foreground" |
| 628 | } |
| 629 | > |
| 630 | {result?.status === "failed" |
| 631 | ? gt("Not saved · {error}", { error: result.error }) |
| 632 | : result?.status === "saved" |
| 633 | ? gt("Saved") |
| 634 | : gt("Saving…")} |
| 635 | </span> |
| 636 | </div> |
| 637 | ); |
| 638 | })} |
| 639 | </div> |
| 640 | ) : null} |
| 641 | </DialogPanel> |
| 642 | <DialogFooter variant="bare" className="items-center sm:justify-between"> |
| 643 | <span className="text-xs text-muted-foreground"> |
| 644 | {hasChanges |
| 645 | ? gt("Changes apply to {destination}", { destination: destinationLabel }) |
| 646 | : ""} |
| 647 | </span> |
| 648 | <div className="flex w-full flex-wrap justify-end gap-2 sm:w-auto"> |
| 649 | {hasChanges ? ( |
| 650 | <Button variant="ghost" disabled={pending} onClick={discard}> |
| 651 | {failedDestinations.length > 0 |
| 652 | ? gt("Discard pending changes") |
| 653 | : gt("Discard changes")} |
| 654 | </Button> |
| 655 | ) : null} |
| 656 | <Button |
| 657 | disabled={ |
| 658 | pending || |
| 659 | (!hasChanges && failedDestinations.length === 0) || |
| 660 | (failedDestinations.length === 0 && (errors.size > 0 || selected.length === 0)) |
| 661 | } |
| 662 | onClick={() => void save(failedDestinations.length > 0)} |
| 663 | > |
| 664 | {pending |
| 665 | ? gt("Saving…") |
| 666 | : failedDestinations.length > 0 |
| 667 | ? gt("Retry failed saves") |
| 668 | : gt("Save changes")} |
| 669 | </Button> |
| 670 | </div> |
| 671 | </DialogFooter> |
| 672 | </DialogPopup> |
| 673 | </Dialog> |
| 674 | ); |
| 675 | } |