|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + DialogContent, |
| 5 | + DialogDescription, |
| 6 | + DialogFooter, |
| 7 | + DialogHeader, |
| 8 | + DialogTitle, |
| 9 | +} from "@/components/ui/dialog"; |
| 10 | +import { Input } from "@/components/ui/input"; |
| 11 | +import { useDatabaseDriver } from "@/context/driver-provider"; |
| 12 | +import { GitBranch, Loader } from "lucide-react"; |
| 13 | +import { useCallback, useMemo, useState } from "react"; |
| 14 | + |
| 15 | +export default function useDoltCreateBranchModal(refreshStatus: () => void) { |
| 16 | + const { databaseDriver } = useDatabaseDriver(); |
| 17 | + const [open, setOpen] = useState(false); |
| 18 | + const [loading, setLoading] = useState(false); |
| 19 | + const [errorMessage, setErrorMessage] = useState<string | undefined>(); |
| 20 | + const [branchName, setBranchName] = useState<string>(); |
| 21 | + const [commitHash, setCommitHash] = useState<string | undefined>(); |
| 22 | + |
| 23 | + const onSaveClicked = useCallback(() => { |
| 24 | + const command = commitHash |
| 25 | + ? `CALL DOLT_CHECKOUT('-b', ${databaseDriver.escapeValue(branchName)}, ${databaseDriver.escapeValue(commitHash)});` |
| 26 | + : `CALL DOLT_CHECKOUT('-b', ${databaseDriver.escapeValue(branchName)});`; |
| 27 | + |
| 28 | + setLoading(true); |
| 29 | + databaseDriver |
| 30 | + .query(command) |
| 31 | + .then(() => { |
| 32 | + refreshStatus(); |
| 33 | + setOpen(false); |
| 34 | + }) |
| 35 | + .catch((e) => { |
| 36 | + setLoading(false); |
| 37 | + if (e instanceof Error) { |
| 38 | + setErrorMessage(e.message); |
| 39 | + } else { |
| 40 | + setErrorMessage("An error occurred"); |
| 41 | + } |
| 42 | + }); |
| 43 | + }, [databaseDriver, refreshStatus, commitHash, branchName]); |
| 44 | + |
| 45 | + const modal = useMemo(() => { |
| 46 | + if (!open) return null; |
| 47 | + |
| 48 | + return ( |
| 49 | + <Dialog open={open} onOpenChange={setOpen}> |
| 50 | + <DialogContent> |
| 51 | + <DialogHeader> |
| 52 | + <DialogTitle>Create Branch</DialogTitle> |
| 53 | + </DialogHeader> |
| 54 | + |
| 55 | + <DialogDescription> |
| 56 | + {commitHash ? ( |
| 57 | + <> |
| 58 | + Creating new branch from{" "} |
| 59 | + <span className="bg-muted rounded inline-flex font-mono px-2"> |
| 60 | + {commitHash} |
| 61 | + </span> |
| 62 | + </> |
| 63 | + ) : ( |
| 64 | + "Creating new branch" |
| 65 | + )} |
| 66 | + </DialogDescription> |
| 67 | + |
| 68 | + {errorMessage && ( |
| 69 | + <div className="py-2 font-mono text-red-500">{errorMessage}</div> |
| 70 | + )} |
| 71 | + |
| 72 | + <div> |
| 73 | + <Input |
| 74 | + value={branchName} |
| 75 | + placeholder="Branch Name" |
| 76 | + onChange={(e) => setBranchName(e.currentTarget.value)} |
| 77 | + /> |
| 78 | + </div> |
| 79 | + |
| 80 | + <DialogFooter> |
| 81 | + <Button disabled={!branchName || loading} onClick={onSaveClicked}> |
| 82 | + {loading ? ( |
| 83 | + <Loader className="mr-2 w-4 h-4 animate-spin" /> |
| 84 | + ) : ( |
| 85 | + <GitBranch className="mr-2 w-4 h-4" /> |
| 86 | + )} |
| 87 | + Create |
| 88 | + </Button> |
| 89 | + </DialogFooter> |
| 90 | + </DialogContent> |
| 91 | + </Dialog> |
| 92 | + ); |
| 93 | + }, [open, branchName, commitHash, onSaveClicked, loading, errorMessage]); |
| 94 | + |
| 95 | + const openModal = (commitHash?: string) => { |
| 96 | + setCommitHash(commitHash); |
| 97 | + setBranchName(""); |
| 98 | + setLoading(false); |
| 99 | + setOpen(true); |
| 100 | + setErrorMessage(undefined); |
| 101 | + }; |
| 102 | + |
| 103 | + return { modal, openModal }; |
| 104 | +} |
0 commit comments