The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, useGT } from "gt-react"; |
| 2 | import { useState } from "react"; |
| 3 | import { |
| 4 | AuthSourceControlWriteScope, |
| 5 | type EnvironmentId, |
| 6 | type ScopedThreadRef, |
| 7 | } from "@t3tools/contracts"; |
| 8 | |
| 9 | import { cn } from "~/lib/utils"; |
| 10 | import { readEnvironmentScope, useEnvironmentScope } from "~/state/session"; |
| 11 | |
| 12 | import { Button } from "../ui/button"; |
| 13 | import { Toggle, ToggleGroup } from "../ui/toggle-group"; |
| 14 | import { PullRequestMarkdown } from "./PullRequestMarkdown"; |
| 15 | import { |
| 16 | PullRequestMarkdownTextarea, |
| 17 | type PullRequestCompletionScope, |
| 18 | } from "./PullRequestMarkdownTextarea"; |
| 19 | |
| 20 | export function PullRequestMarkdownEditor({ |
| 21 | value, |
| 22 | cwd, |
| 23 | environmentId, |
| 24 | completionScope, |
| 25 | threadRef = null, |
| 26 | placeholder, |
| 27 | label, |
| 28 | saving, |
| 29 | allowEmpty = false, |
| 30 | className, |
| 31 | onSave, |
| 32 | onCancel, |
| 33 | }: { |
| 34 | readonly value: string; |
| 35 | readonly cwd: string; |
| 36 | readonly environmentId: EnvironmentId; |
| 37 | readonly completionScope: PullRequestCompletionScope | null; |
| 38 | readonly threadRef?: ScopedThreadRef | null; |
| 39 | readonly placeholder?: string | undefined; |
| 40 | readonly label: string; |
| 41 | readonly saving: boolean; |
| 42 | readonly allowEmpty?: boolean; |
| 43 | readonly className?: string | undefined; |
| 44 | readonly onSave: (next: string) => void; |
| 45 | readonly onCancel: () => void; |
| 46 | }) { |
| 47 | const gt = useGT(); |
| 48 | const canWriteSourceControl = useEnvironmentScope(environmentId, AuthSourceControlWriteScope); |
| 49 | const [draft, setDraft] = useState(value); |
| 50 | const [preview, setPreview] = useState(false); |
| 51 | const [seed, setSeed] = useState(value); |
| 52 | if (seed !== value) { |
| 53 | setSeed(value); |
| 54 | setDraft(value); |
| 55 | } |
| 56 | const empty = draft.trim().length === 0; |
| 57 | const saveDisabled = !canWriteSourceControl || saving || (empty && !allowEmpty); |
| 58 | |
| 59 | return ( |
| 60 | <div |
| 61 | className={cn("space-y-2", className)} |
| 62 | onKeyDown={(event) => { |
| 63 | if (event.nativeEvent.isComposing || event.keyCode === 229) return; |
| 64 | if ( |
| 65 | event.key === "Enter" && |
| 66 | (event.metaKey || event.ctrlKey) && |
| 67 | !event.shiftKey && |
| 68 | !event.altKey |
| 69 | ) { |
| 70 | event.preventDefault(); |
| 71 | event.stopPropagation(); |
| 72 | if ( |
| 73 | !saveDisabled && |
| 74 | !event.repeat && |
| 75 | readEnvironmentScope(environmentId, AuthSourceControlWriteScope) |
| 76 | ) |
| 77 | onSave(draft); |
| 78 | return; |
| 79 | } |
| 80 | if (event.key !== "Escape" || saving) return; |
| 81 | event.preventDefault(); |
| 82 | onCancel(); |
| 83 | }} |
| 84 | > |
| 85 | <ToggleGroup |
| 86 | aria-label={gt("Markdown editor mode")} |
| 87 | variant="segmented" |
| 88 | value={[preview ? "preview" : "write"]} |
| 89 | disabled={saving} |
| 90 | onValueChange={(next) => { |
| 91 | const mode = next[0]; |
| 92 | if (mode === "write" || mode === "preview") setPreview(mode === "preview"); |
| 93 | }} |
| 94 | > |
| 95 | <Toggle value="write"> |
| 96 | <T context="verb, markdown editor mode">Write</T> |
| 97 | </Toggle> |
| 98 | <Toggle value="preview"> |
| 99 | <T context="markdown editor mode">Preview</T> |
| 100 | </Toggle> |
| 101 | </ToggleGroup> |
| 102 | {preview ? ( |
| 103 | <div className="rounded-lg border border-border/60 px-3 py-2"> |
| 104 | {empty ? ( |
| 105 | <p className="text-xs text-muted-foreground"> |
| 106 | <T>Nothing to preview.</T> |
| 107 | </p> |
| 108 | ) : ( |
| 109 | <PullRequestMarkdown |
| 110 | text={draft} |
| 111 | cwd={cwd} |
| 112 | environmentId={environmentId} |
| 113 | threadRef={threadRef} |
| 114 | /> |
| 115 | )} |
| 116 | </div> |
| 117 | ) : ( |
| 118 | <PullRequestMarkdownTextarea |
| 119 | autoFocus |
| 120 | disabled={saving} |
| 121 | completionScope={completionScope} |
| 122 | value={draft} |
| 123 | rows={6} |
| 124 | placeholder={placeholder} |
| 125 | aria-label={label} |
| 126 | onValueChange={setDraft} |
| 127 | /> |
| 128 | )} |
| 129 | <div className="flex justify-end gap-2"> |
| 130 | <Button size="xs" variant="ghost" disabled={saving} onClick={onCancel}> |
| 131 | <T>Cancel</T> |
| 132 | </Button> |
| 133 | <Button |
| 134 | size="xs" |
| 135 | variant="outline" |
| 136 | disabled={!canWriteSourceControl || saving || (empty && !allowEmpty)} |
| 137 | onClick={() => { |
| 138 | if ( |
| 139 | !readEnvironmentScope(environmentId, AuthSourceControlWriteScope) || |
| 140 | saving || |
| 141 | (empty && !allowEmpty) |
| 142 | ) |
| 143 | return; |
| 144 | onSave(draft); |
| 145 | }} |
| 146 | > |
| 147 | {saving ? gt("Saving...") : gt("Save")} |
| 148 | </Button> |
| 149 | </div> |
| 150 | </div> |
| 151 | ); |
| 152 | } |