|
| 1 | +import { standardSchemaResolver as zodResolver } from "@hookform/resolvers/standard-schema"; |
| 2 | +import { Upload } from "lucide-react"; |
| 3 | +import { useState } from "react"; |
| 4 | +import { useForm } from "react-hook-form"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Dialog, |
| 9 | + DialogContent, |
| 10 | + DialogDescription, |
| 11 | + DialogFooter, |
| 12 | + DialogHeader, |
| 13 | + DialogTitle, |
| 14 | + DialogTrigger, |
| 15 | +} from "@/components/ui/dialog"; |
| 16 | +import { DropdownMenuItem } from "@/components/ui/dropdown-menu"; |
| 17 | +import { Dropzone } from "@/components/ui/dropzone"; |
| 18 | +import { |
| 19 | + Form, |
| 20 | + FormControl, |
| 21 | + FormField, |
| 22 | + FormItem, |
| 23 | + FormLabel, |
| 24 | + FormMessage, |
| 25 | +} from "@/components/ui/form"; |
| 26 | +import { Input } from "@/components/ui/input"; |
| 27 | +import { api } from "@/utils/api"; |
| 28 | +import { |
| 29 | + uploadFileToContainerSchema, |
| 30 | + type UploadFileToContainer, |
| 31 | +} from "@/utils/schema"; |
| 32 | + |
| 33 | +interface Props { |
| 34 | + containerId: string; |
| 35 | + serverId?: string; |
| 36 | + children?: React.ReactNode; |
| 37 | +} |
| 38 | + |
| 39 | +export const UploadFileModal = ({ children, containerId, serverId }: Props) => { |
| 40 | + const [open, setOpen] = useState(false); |
| 41 | + |
| 42 | + const { mutateAsync: uploadFile, isPending: isLoading } = |
| 43 | + api.docker.uploadFileToContainer.useMutation({ |
| 44 | + onSuccess: () => { |
| 45 | + toast.success("File uploaded successfully"); |
| 46 | + setOpen(false); |
| 47 | + form.reset(); |
| 48 | + }, |
| 49 | + onError: (error) => { |
| 50 | + toast.error(error.message || "Failed to upload file to container"); |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const form = useForm({ |
| 55 | + resolver: zodResolver(uploadFileToContainerSchema), |
| 56 | + defaultValues: { |
| 57 | + containerId, |
| 58 | + destinationPath: "/", |
| 59 | + serverId: serverId || undefined, |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + const file = form.watch("file"); |
| 64 | + |
| 65 | + const onSubmit = async (values: UploadFileToContainer) => { |
| 66 | + if (!values.file) { |
| 67 | + toast.error("Please select a file to upload"); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const formData = new FormData(); |
| 72 | + formData.append("containerId", values.containerId); |
| 73 | + formData.append("file", values.file); |
| 74 | + formData.append("destinationPath", values.destinationPath); |
| 75 | + if (values.serverId) { |
| 76 | + formData.append("serverId", values.serverId); |
| 77 | + } |
| 78 | + |
| 79 | + await uploadFile(formData); |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <Dialog open={open} onOpenChange={setOpen}> |
| 84 | + <DialogTrigger asChild> |
| 85 | + <DropdownMenuItem |
| 86 | + className="w-full cursor-pointer space-x-3" |
| 87 | + onSelect={(e) => e.preventDefault()} |
| 88 | + > |
| 89 | + {children} |
| 90 | + </DropdownMenuItem> |
| 91 | + </DialogTrigger> |
| 92 | + <DialogContent className="sm:max-w-2xl"> |
| 93 | + <DialogHeader> |
| 94 | + <DialogTitle className="flex items-center gap-2"> |
| 95 | + <Upload className="h-5 w-5" /> |
| 96 | + Upload File to Container |
| 97 | + </DialogTitle> |
| 98 | + <DialogDescription> |
| 99 | + Upload a file directly into the container's filesystem |
| 100 | + </DialogDescription> |
| 101 | + </DialogHeader> |
| 102 | + |
| 103 | + <Form {...form}> |
| 104 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 105 | + <FormField |
| 106 | + control={form.control} |
| 107 | + name="destinationPath" |
| 108 | + render={({ field }) => ( |
| 109 | + <FormItem> |
| 110 | + <FormLabel>Destination Path</FormLabel> |
| 111 | + <FormControl> |
| 112 | + <Input |
| 113 | + {...field} |
| 114 | + placeholder="/path/to/file" |
| 115 | + className="font-mono" |
| 116 | + /> |
| 117 | + </FormControl> |
| 118 | + <FormMessage /> |
| 119 | + <p className="text-xs text-muted-foreground"> |
| 120 | + Enter the full path where the file should be uploaded in the |
| 121 | + container (e.g., /app/config.json) |
| 122 | + </p> |
| 123 | + </FormItem> |
| 124 | + )} |
| 125 | + /> |
| 126 | + |
| 127 | + <FormField |
| 128 | + control={form.control} |
| 129 | + name="file" |
| 130 | + render={({ field }) => ( |
| 131 | + <FormItem> |
| 132 | + <FormLabel>File</FormLabel> |
| 133 | + <FormControl> |
| 134 | + <Dropzone |
| 135 | + {...field} |
| 136 | + dropMessage="Drop file here or click to browse" |
| 137 | + onChange={(files) => { |
| 138 | + if (files && files.length > 0) { |
| 139 | + field.onChange(files[0]); |
| 140 | + } else { |
| 141 | + field.onChange(null); |
| 142 | + } |
| 143 | + }} |
| 144 | + /> |
| 145 | + </FormControl> |
| 146 | + <FormMessage /> |
| 147 | + {file instanceof File && ( |
| 148 | + <div className="flex items-center gap-2 p-2 bg-muted rounded-md"> |
| 149 | + <span className="text-sm text-muted-foreground flex-1"> |
| 150 | + {file.name} ({(file.size / 1024).toFixed(2)} KB) |
| 151 | + </span> |
| 152 | + <Button |
| 153 | + type="button" |
| 154 | + variant="ghost" |
| 155 | + size="sm" |
| 156 | + onClick={() => field.onChange(null)} |
| 157 | + > |
| 158 | + Remove |
| 159 | + </Button> |
| 160 | + </div> |
| 161 | + )} |
| 162 | + </FormItem> |
| 163 | + )} |
| 164 | + /> |
| 165 | + |
| 166 | + <DialogFooter> |
| 167 | + <Button |
| 168 | + type="button" |
| 169 | + variant="outline" |
| 170 | + onClick={() => setOpen(false)} |
| 171 | + > |
| 172 | + Cancel |
| 173 | + </Button> |
| 174 | + <Button |
| 175 | + type="submit" |
| 176 | + isLoading={isLoading} |
| 177 | + disabled={!file || isLoading} |
| 178 | + > |
| 179 | + Upload File |
| 180 | + </Button> |
| 181 | + </DialogFooter> |
| 182 | + </form> |
| 183 | + </Form> |
| 184 | + </DialogContent> |
| 185 | + </Dialog> |
| 186 | + ); |
| 187 | +}; |
0 commit comments