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 } from "@t3tools/contracts"; |
| 2 | import type { SourceControlDefinition } from "@t3tools/client-runtime/source-control-clients"; |
| 3 | import * as Equal from "effect/Equal"; |
| 4 | import { T } from "gt-react"; |
| 5 | import { useState } from "react"; |
| 6 | |
| 7 | import { useEnvironmentSettings } from "../../hooks/useSettings"; |
| 8 | import { serverEnvironment } from "../../state/server"; |
| 9 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 10 | import { Button } from "../ui/button"; |
| 11 | import { ProviderSettingsForm } from "./ProviderSettingsForm"; |
| 12 | |
| 13 | export function SourceControlHostSettings({ |
| 14 | environmentId, |
| 15 | definition, |
| 16 | onSaved, |
| 17 | }: { |
| 18 | readonly environmentId: EnvironmentId; |
| 19 | readonly definition: SourceControlDefinition & { |
| 20 | readonly settings: NonNullable<SourceControlDefinition["settings"]>; |
| 21 | }; |
| 22 | readonly onSaved: () => void; |
| 23 | }) { |
| 24 | const saved = useEnvironmentSettings( |
| 25 | environmentId, |
| 26 | (settings) => settings.sourceControlHosts[definition.kind], |
| 27 | ); |
| 28 | const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { |
| 29 | label: `save ${definition.label} settings`, |
| 30 | }); |
| 31 | const [saving, setSaving] = useState(false); |
| 32 | const formDefinition = { settingsSchema: definition.settings }; |
| 33 | const hasSaved = Object.values(saved ?? {}).some((value) => value !== ""); |
| 34 | |
| 35 | const save = async (fields: Record<string, unknown>) => { |
| 36 | setSaving(true); |
| 37 | const result = await updateSettings({ |
| 38 | environmentId, |
| 39 | input: { patch: { sourceControlHosts: { [definition.kind]: fields } } }, |
| 40 | }); |
| 41 | setSaving(false); |
| 42 | if (result._tag === "Success") onSaved(); |
| 43 | }; |
| 44 | |
| 45 | return ( |
| 46 | // Locked while saving, so an edit made mid-request is not overwritten by the saved value. |
| 47 | <fieldset disabled={saving} className="contents"> |
| 48 | <ProviderSettingsForm |
| 49 | definition={formDefinition} |
| 50 | value={saved ?? {}} |
| 51 | idPrefix={`source-control-${definition.kind}-${environmentId}`} |
| 52 | variant="settings" |
| 53 | environmentId={environmentId} |
| 54 | onChange={(next) => { |
| 55 | const fields = next ?? {}; |
| 56 | const changed = Object.fromEntries( |
| 57 | Object.entries(fields).filter(([key, value]) => !Equal.equals(saved?.[key], value)), |
| 58 | ); |
| 59 | const cleared = Object.keys(saved ?? {}).filter((key) => !(key in fields)); |
| 60 | void save({ ...changed, ...Object.fromEntries(cleared.map((key) => [key, ""])) }); |
| 61 | }} |
| 62 | /> |
| 63 | {hasSaved ? ( |
| 64 | <div className="flex justify-end pt-2"> |
| 65 | <Button |
| 66 | size="xs" |
| 67 | variant="outline" |
| 68 | onClick={() => |
| 69 | void save(Object.fromEntries(Object.keys(saved ?? {}).map((key) => [key, ""]))) |
| 70 | } |
| 71 | > |
| 72 | <T>Remove</T> |
| 73 | </Button> |
| 74 | </div> |
| 75 | ) : null} |
| 76 | </fieldset> |
| 77 | ); |
| 78 | } |