-
-
Notifications
You must be signed in to change notification settings - Fork 489
Expand file tree
/
Copy pathfolder-create.tsx
More file actions
149 lines (135 loc) · 4.04 KB
/
Copy pathfolder-create.tsx
File metadata and controls
149 lines (135 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use client";
import { useState } from "react";
import { useConfig } from "@/contexts/config-context";
import { joinPathSegments, normalizePath } from "@/lib/utils/file";
import { toast } from "sonner";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
type FolderCreateResult = {
path: string;
[key: string]: unknown;
};
const FolderCreate = ({
children,
path,
type,
name,
onCreate,
}: {
children: React.ReactElement<{ onClick: () => void }>;
path: string;
type: "content" | "media";
name?: string;
onCreate?: (entry: FolderCreateResult) => void;
}) => {
const { config } = useConfig();
if (!config) throw new Error(`Configuration not found.`);
const [open, setOpen] = useState(false);
const [folderPath, setFolderPath] = useState("");
const [isSubmitting, setIsSubmitting] = useState(false);
const handleCreate = async () => {
const normalizedFolderInput = normalizePath(folderPath.trim());
if (!normalizedFolderInput) {
toast.error("Folder name is required.");
return;
}
const fullNewPath = joinPathSegments([
normalizePath(path),
normalizedFolderInput,
]);
setIsSubmitting(true);
try {
const createPromise: Promise<{
status: string;
message?: string;
data: FolderCreateResult;
}> = fetch(`/api/${config.owner}/${config.repo}/${encodeURIComponent(config.branch)}/files/${encodeURIComponent(fullNewPath + "/.gitkeep")}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
type,
name,
content: "",
onConflict: "error",
}),
}).then(async (response) => {
const payload = await response.json().catch(() => null);
if (!response.ok) {
if (response.status === 409) {
throw new Error(`Folder \"${fullNewPath}\" already exists.`);
}
throw new Error(payload?.message || "Failed to create folder");
}
if (!payload || payload.status !== "success") {
throw new Error(payload?.message || "Failed to create folder");
}
return payload;
});
await toast.promise(createPromise, {
loading: `Creating folder "${fullNewPath}"`,
success: `Folder "${fullNewPath}" created successfully.`,
error: (error: any) => error.message,
});
const result = await createPromise;
if (onCreate) onCreate(result.data);
setFolderPath("");
setOpen(false);
} catch (error) {
console.error(error);
} finally {
setIsSubmitting(false);
}
};
return (
<Dialog
open={open}
onOpenChange={(nextOpen) => {
setOpen(nextOpen);
if (!nextOpen) {
setFolderPath("");
setIsSubmitting(false);
}
}}
>
<DialogTrigger asChild>
{children}
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Create a folder</DialogTitle>
<DialogDescription>Choose a name for the folder to create{path ? ` under "${normalizePath(path)}"` : null}.</DialogDescription>
</DialogHeader>
<form
onSubmit={async (event) => {
event.preventDefault();
if (!isSubmitting) await handleCreate();
}}
className="space-y-4"
>
<Input
autoFocus
value={folderPath}
onChange={(e) => setFolderPath(e.target.value)}
/>
<DialogFooter>
<DialogClose asChild>
<Button type="button" variant="secondary" disabled={isSubmitting}>Cancel</Button>
</DialogClose>
<Button type="submit" disabled={isSubmitting || !folderPath.trim()}>Create</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
);
};
export { FolderCreate };