|
| 1 | +import { Button } from '@/components/ui/button'; |
| 2 | +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'; |
| 3 | +import { Input } from '@/components/ui/input'; |
| 4 | +import { Label } from '@/components/ui/label'; |
| 5 | +import { useInstanceClientIdParams } from '@/config/useInstanceClient'; |
| 6 | +import { useEditorView } from '@/features/instance/applications/hooks/useEditorView'; |
| 7 | +import { usePackageComponentMutation } from '@/features/instance/operations/mutations/packageComponent'; |
| 8 | +import { setWatchedValue, useSetWatchedValue, useWatchedValue } from '@/lib/events/watcher'; |
| 9 | +import { DownloadIcon } from 'lucide-react'; |
| 10 | +import { ChangeEvent, MouseEvent, useCallback, useState } from 'react'; |
| 11 | +import { toast } from 'sonner'; |
| 12 | + |
| 13 | +export function DownloadApplicationModal() { |
| 14 | + const isModalOpen = useWatchedValue('ShowDownloadApplicationModal', false); |
| 15 | + |
| 16 | + const instanceParams = useInstanceClientIdParams(); |
| 17 | + const { openedEntry } = useEditorView(); |
| 18 | + const { mutate: packageComponent, isPending, isSuccess } = usePackageComponentMutation(); |
| 19 | + const actionStatus = isSuccess ? `Downloaded` : isPending ? `Downloading` : 'Download'; |
| 20 | + |
| 21 | + const [skipNodeModules, setSkipNodeModules] = useState(true); |
| 22 | + const [skipSymlinks, setSkipSymlinks] = useState(true); |
| 23 | + |
| 24 | + const skipNodeModulesChanged = useCallback((e: ChangeEvent<HTMLInputElement>) => { |
| 25 | + setSkipNodeModules(e.target.checked); |
| 26 | + }, []); |
| 27 | + const skipSymlinksChanged = useCallback((e: ChangeEvent<HTMLInputElement>) => { |
| 28 | + setSkipSymlinks(e.target.checked); |
| 29 | + }, []); |
| 30 | + |
| 31 | + const closeModal = useSetWatchedValue('ShowDownloadApplicationModal', false); |
| 32 | + const onClickYes = useCallback((e: MouseEvent) => { |
| 33 | + e.preventDefault(); |
| 34 | + if (!openedEntry) { |
| 35 | + return; |
| 36 | + } |
| 37 | + setWatchedValue('ShowDownloadApplicationModal', false); |
| 38 | + const toastId = toast.loading('Packaging...'); |
| 39 | + packageComponent( |
| 40 | + { |
| 41 | + packageName: openedEntry.package, |
| 42 | + project: openedEntry.project, |
| 43 | + skipNodeModules, |
| 44 | + skipSymlinks, |
| 45 | + ...instanceParams, |
| 46 | + }, |
| 47 | + { |
| 48 | + onSuccess: (response) => { |
| 49 | + toast.success('Download ready!', { id: toastId }); |
| 50 | + |
| 51 | + const element = document.createElement('a'); |
| 52 | + element.setAttribute('class', 'invisible absolute top-0'); |
| 53 | + const bytes = Uint8Array.from(atob(response.payload), c => c.charCodeAt(0)); |
| 54 | + const file = new Blob([bytes], { type: 'application/gzip' }); |
| 55 | + element.href = URL.createObjectURL(file); |
| 56 | + element.download = `${openedEntry.project}.gz`; |
| 57 | + document.body.appendChild(element); |
| 58 | + element.click(); |
| 59 | + document.body.removeChild(element); |
| 60 | + }, |
| 61 | + }, |
| 62 | + ); |
| 63 | + }, [openedEntry, packageComponent, skipNodeModules, skipSymlinks, instanceParams]); |
| 64 | + |
| 65 | + |
| 66 | + return ( |
| 67 | + <Dialog onOpenChange={closeModal} open={isModalOpen}> |
| 68 | + <DialogContent aria-describedby={undefined} className="text-white"> |
| 69 | + <DialogHeader> |
| 70 | + <DialogTitle>Download Application</DialogTitle> |
| 71 | + <DialogDescription> |
| 72 | + This will package up the contents of <strong>{openedEntry?.project}</strong> into a gzipped file. |
| 73 | + </DialogDescription> |
| 74 | + </DialogHeader> |
| 75 | + |
| 76 | + <Label className="flex"> |
| 77 | + <Input |
| 78 | + type="checkbox" |
| 79 | + className="w-6" |
| 80 | + disabled={isPending} |
| 81 | + checked={skipNodeModules} |
| 82 | + onChange={skipNodeModulesChanged} |
| 83 | + /> |
| 84 | + <span className="pl-4 pr-8 flex-1 py-2.5">Skip Node Modules</span> |
| 85 | + </Label> |
| 86 | + |
| 87 | + <Label className="flex"> |
| 88 | + <Input |
| 89 | + type="checkbox" |
| 90 | + className="w-6" |
| 91 | + disabled={isPending} |
| 92 | + checked={skipSymlinks} |
| 93 | + onChange={skipSymlinksChanged} |
| 94 | + /> |
| 95 | + <span className="pl-4 pr-8 flex-1 py-2.5">Skip Symlinks</span> |
| 96 | + </Label> |
| 97 | + |
| 98 | + <div className="flex w-full gap-4"> |
| 99 | + <Button variant="ghostOutline" className="w-full rounded-full" onClick={closeModal}> |
| 100 | + Cancel |
| 101 | + </Button> |
| 102 | + <Button |
| 103 | + variant="positive" |
| 104 | + type="button" |
| 105 | + className="w-full rounded-full" |
| 106 | + disabled={isPending} |
| 107 | + autoFocus={true} |
| 108 | + onClick={onClickYes} |
| 109 | + > |
| 110 | + <DownloadIcon /> {actionStatus}{isPending ? '...' : ''} |
| 111 | + </Button> |
| 112 | + </div> |
| 113 | + </DialogContent> |
| 114 | + </Dialog> |
| 115 | + ); |
| 116 | +} |
0 commit comments