|
| 1 | +'use client'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Minimal offline screen shown on the Drive when there's no network. You can still pick files — |
| 5 | + * they're stored locally (IndexedDB) and uploaded automatically when the connection returns. |
| 6 | + */ |
| 7 | +import { useMemo, useRef, useState } from 'react'; |
| 8 | +import { CloudOff, UploadCloud, X, Loader2 } from 'lucide-react'; |
| 9 | +import type { PublicFolder } from '@opencoperlock/shared/client'; |
| 10 | +import { FAST_UPLOAD_FOLDER_NAME, formatBytes } from '@opencoperlock/shared/client'; |
| 11 | +import { useT } from '@/lib/i18n'; |
| 12 | +import { useOffline } from '@/lib/offline'; |
| 13 | +import { Select } from '@/components/ui/Select'; |
| 14 | + |
| 15 | +function cachedFolders(): PublicFolder[] { |
| 16 | + try { |
| 17 | + const raw = localStorage.getItem('ocl_folders'); |
| 18 | + return raw ? (JSON.parse(raw) as PublicFolder[]) : []; |
| 19 | + } catch { |
| 20 | + return []; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +export function OfflineView() { |
| 25 | + const { t } = useT(); |
| 26 | + const { items, enqueueFiles, removeItem } = useOffline(); |
| 27 | + const fileInput = useRef<HTMLInputElement>(null); |
| 28 | + const [dragging, setDragging] = useState(false); |
| 29 | + |
| 30 | + const folders = useMemo(() => cachedFolders().filter((f) => !f.isZeroKnowledge), []); |
| 31 | + const options = [ |
| 32 | + { value: '', label: FAST_UPLOAD_FOLDER_NAME }, |
| 33 | + ...folders.map((f) => ({ value: f.id, label: f.name })), |
| 34 | + ]; |
| 35 | + const defaultId = folders.find((f) => f.name === FAST_UPLOAD_FOLDER_NAME)?.id ?? ''; |
| 36 | + const [folderId, setFolderId] = useState(defaultId); |
| 37 | + |
| 38 | + async function add(list: FileList | File[] | null) { |
| 39 | + if (!list) return; |
| 40 | + const name = options.find((o) => o.value === folderId)?.label ?? FAST_UPLOAD_FOLDER_NAME; |
| 41 | + await enqueueFiles(list, folderId, name); |
| 42 | + } |
| 43 | + |
| 44 | + return ( |
| 45 | + <div className="mx-auto max-w-xl space-y-5 py-6"> |
| 46 | + <div className="flex items-center gap-3"> |
| 47 | + <span className="grid h-11 w-11 place-items-center rounded-xl bg-amber-500/10 text-amber-300"> |
| 48 | + <CloudOff size={22} /> |
| 49 | + </span> |
| 50 | + <div> |
| 51 | + <h1 className="text-xl font-semibold text-white">{t('offline.title')}</h1> |
| 52 | + <p className="text-sm text-zinc-500">{t('offline.subtitle')}</p> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + |
| 56 | + <div className="card space-y-3"> |
| 57 | + <label className="block text-sm text-zinc-400">{t('offline.destination')}</label> |
| 58 | + <Select className="w-full" value={folderId} onChange={setFolderId} options={options} /> |
| 59 | + |
| 60 | + <div |
| 61 | + onDragOver={(e) => { |
| 62 | + e.preventDefault(); |
| 63 | + setDragging(true); |
| 64 | + }} |
| 65 | + onDragLeave={() => setDragging(false)} |
| 66 | + onDrop={(e) => { |
| 67 | + e.preventDefault(); |
| 68 | + setDragging(false); |
| 69 | + void add(e.dataTransfer.files); |
| 70 | + }} |
| 71 | + onClick={() => fileInput.current?.click()} |
| 72 | + className={`grid cursor-pointer place-items-center gap-2 rounded-xl border-2 border-dashed px-6 py-10 text-center transition ${ |
| 73 | + dragging ? 'border-accent/70 bg-accent/[0.06]' : 'border-white/15 hover:border-white/25' |
| 74 | + }`} |
| 75 | + > |
| 76 | + <UploadCloud size={28} className="text-zinc-400" /> |
| 77 | + <p className="text-sm font-medium text-zinc-200">{t('offline.pick')}</p> |
| 78 | + <p className="text-xs text-zinc-500">{t('offline.pickHint')}</p> |
| 79 | + </div> |
| 80 | + <input ref={fileInput} type="file" multiple className="hidden" onChange={(e) => void add(e.target.files)} /> |
| 81 | + </div> |
| 82 | + |
| 83 | + {items.length > 0 && ( |
| 84 | + <div className="card space-y-2"> |
| 85 | + <p className="text-sm font-medium text-zinc-200">{t('offline.queued', { n: items.length })}</p> |
| 86 | + <div className="divide-y divide-white/[0.06]"> |
| 87 | + {items.map((it) => ( |
| 88 | + <div key={it.id} className="flex items-center justify-between gap-2 py-2 text-sm"> |
| 89 | + <div className="flex min-w-0 items-center gap-2"> |
| 90 | + <Loader2 size={14} className="shrink-0 animate-spin text-amber-300" /> |
| 91 | + <span className="truncate text-zinc-200">{it.name}</span> |
| 92 | + <span className="shrink-0 text-xs text-zinc-500">{formatBytes(it.blob.size)} · {it.folderName}</span> |
| 93 | + </div> |
| 94 | + <button |
| 95 | + className="rounded-lg p-1 text-zinc-500 hover:bg-white/5 hover:text-zinc-200" |
| 96 | + onClick={() => it.id != null && void removeItem(it.id)} |
| 97 | + title={t('offline.remove')} |
| 98 | + > |
| 99 | + <X size={15} /> |
| 100 | + </button> |
| 101 | + </div> |
| 102 | + ))} |
| 103 | + </div> |
| 104 | + <p className="text-xs text-zinc-500">{t('offline.willSync')}</p> |
| 105 | + </div> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + ); |
| 109 | +} |
0 commit comments