(refreshStatus: () => void)
| 13 | import { useCallback, useMemo, useState } from "react"; |
| 14 | |
| 15 | export default function useDoltCreateBranchModal(refreshStatus: () => void) { |
| 16 | const { databaseDriver } = useStudioContext(); |
| 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 inline-flex rounded px-2 font-mono"> |
| 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> |
no test coverage detected