Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

feat(library): import a guide from a file or a drop

davbio3d1fba48a70f
diff --git a/src/ui/fullview/LibraryContent.tsx b/src/ui/fullview/LibraryContent.tsx
index 2106e98..f178faa 100644
--- a/src/ui/fullview/LibraryContent.tsx
+++ b/src/ui/fullview/LibraryContent.tsx
@@ -1,4 +1,12 @@
-import { ArrowDownWideNarrow, ChevronDown, ChevronLeft, ChevronRight, LayoutGrid, LayoutList } from 'lucide-react';
+import {
+ ArrowDownWideNarrow,
+ ChevronDown,
+ ChevronLeft,
+ ChevronRight,
+ LayoutGrid,
+ LayoutList,
+ Upload,
+} from 'lucide-react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { i18n } from '#imports';
import {
@@ -14,11 +22,14 @@ import {
toggleStar,
} from '@/core/guides/service';
import type { Guide, Screenshot } from '@/core/guides/types';
+import { BUNDLE_EXTENSION } from '@/core/transfer/schema';
import { useFullview } from '@/stores/fullview';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/ui/components/ui/tooltip';
import ConfirmDeleteModal from './components/ConfirmDeleteModal';
import GuideGridView from './components/GuideGridView';
import GuideListView from './components/GuideListView';
+import ImportGuideModal from './components/ImportGuideModal';
+import { navigate } from './router';
interface LibraryContentProps {
category: 'all' | 'starred' | 'trash';
@@ -165,7 +176,11 @@ export default function LibraryContent({ category }: LibraryContentProps) {
const [sortOpen, setSortOpen] = useState(false);
const [page, setPage] = useState(0);
const [deleteTarget, setDeleteTarget] = useState<string | null>(null);
+ const [importFile, setImportFile] = useState<File | null>(null);
+ const [dragging, setDragging] = useState(false);
const sortRef = useRef<HTMLDivElement>(null);
+ const fileRef = useRef<HTMLInputElement>(null);
+ const dragDepth = useRef(0);
const allGuidesRef = useRef<Guide[]>([]);
@@ -283,11 +298,71 @@ export default function LibraryContent({ category }: LibraryContentProps) {
await loadGuides();
};
+ // Prefer a .mimik among several dropped files rather than only inspecting the
+ // first, and hand back whatever was dropped otherwise so the modal can explain
+ // itself. Silently ignoring a drop reads as the app being broken.
+ const bundleFrom = (list: FileList | null): File | null => {
+ const files = Array.from(list ?? []);
+ if (files.length === 0) return null;
+ return files.find((f) => f.name.toLowerCase().endsWith(`.${BUNDLE_EXTENSION}`)) ?? files[0];
+ };
+
+ // Counting enter/leave keeps the overlay steady as the pointer crosses children.
+ const handleDragEnter = (e: React.DragEvent) => {
+ if (!e.dataTransfer.types.includes('Files')) return;
+ dragDepth.current += 1;
+ setDragging(true);
+ };
+ const handleDragLeave = () => {
+ dragDepth.current = Math.max(0, dragDepth.current - 1);
+ if (dragDepth.current === 0) setDragging(false);
+ };
+ const handleDrop = (e: React.DragEvent) => {
+ e.preventDefault();
+ dragDepth.current = 0;
+ setDragging(false);
+ setImportFile(bundleFrom(e.dataTransfer.files));
+ };
+
const showPagination = !loading && allGuidesRef.current.length > PAGE_SIZE;
return (
- <div>
+ <div
+ className="relative"
+ onDragEnter={handleDragEnter}
+ onDragLeave={handleDragLeave}
+ onDragOver={(e) => {
+ if (e.dataTransfer.types.includes('Files')) e.preventDefault();
+ }}
+ onDrop={handleDrop}
+ >
+ {dragging && (
+ <div className="absolute inset-0 z-20 flex items-center justify-center rounded-xl border-2 border-dashed border-accent bg-secondary/90 pointer-events-none">
+ <div className="flex items-center gap-2 text-[13px] font-semibold text-accent">
+ <Upload size={16} />
+ {i18n.t('import.dropHere')}
+ </div>
+ </div>
+ )}
+
<div className="flex items-center justify-end gap-2 mb-4">
+ <input
+ ref={fileRef}
+ type="file"
+ accept={`.${BUNDLE_EXTENSION}`}
+ className="hidden"
+ onChange={(e) => {
+ setImportFile(bundleFrom(e.target.files));
+ e.target.value = '';
+ }}
+ />
+ <button
+ onClick={() => fileRef.current?.click()}
+ className="flex items-center gap-1.5 text-xs font-medium text-muted-foreground px-3 py-1.5 rounded-lg border border-border bg-card hover:border-violet hover:text-purple transition-colors"
+ >
+ <Upload size={13} />
+ {i18n.t('import.button')}
+ </button>
<div ref={sortRef} className="relative">
<button
onClick={() => setSortOpen(!sortOpen)}
@@ -382,6 +457,14 @@ export default function LibraryContent({ category }: LibraryContentProps) {
onClose={() => setDeleteTarget(null)}
onConfirm={confirmPermanentDelete}
/>
+ <ImportGuideModal
+ file={importFile}
+ onClose={() => setImportFile(null)}
+ onImported={(guideId) => {
+ setImportFile(null);
+ navigate({ page: 'guide', guideId });
+ }}
+ />
</div>
);
}
diff --git a/src/ui/fullview/components/ImportGuideModal.tsx b/src/ui/fullview/components/ImportGuideModal.tsx
new file mode 100644
index 0000000..9981b4e
--- /dev/null
+++ b/src/ui/fullview/components/ImportGuideModal.tsx
@@ -0,0 +1,165 @@
+import { AlertTriangle, FileWarning, Loader2, Package } from 'lucide-react';
+import { useEffect, useState } from 'react';
+import { i18n } from '#imports';
+import { importGuide } from '@/core/guides/service';
+import type { ParsedBundle } from '@/core/transfer/parse';
+import { BundleError } from '@/core/transfer/schema';
+import { logger } from '@/lib/logger';
+import { Button } from '@/ui/components/ui/button';
+import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog';
+
+interface ImportGuideModalProps {
+ file: File | null;
+ onClose: () => void;
+ onImported: (guideId: string) => void;
+}
+
+const errorKeys: Record<string, string> = {
+ unreadable: 'import.errorUnreadable',
+ 'not-a-bundle': 'import.errorNotABundle',
+ 'unsupported-version': 'import.errorVersion',
+ empty: 'import.errorEmpty',
+};
+
+export default function ImportGuideModal({ file, onClose, onImported }: ImportGuideModalProps) {
+ const [bundle, setBundle] = useState<ParsedBundle | null>(null);
+ const [error, setError] = useState<string | null>(null);
+ const [cover, setCover] = useState<string | null>(null);
+ const [importing, setImporting] = useState(false);
+
+ useEffect(() => {
+ if (!file) return;
+ let active = true;
+ let url: string | null = null;
+
+ setBundle(null);
+ setError(null);
+ setCover(null);
+
+ (async () => {
+ try {
+ const { readBundle } = await import('@/core/transfer/parse');
+ const parsed = await readBundle(file);
+ if (!active) return;
+ setBundle(parsed);
+
+ const first = parsed.manifest.screenshots[0];
+ const image = first ? parsed.images.get(first.file) : undefined;
+ if (image) {
+ url = URL.createObjectURL(image);
+ setCover(url);
+ }
+ } catch (err) {
+ if (!active) return;
+ const kind = err instanceof BundleError ? err.kind : 'unreadable';
+ setError(i18n.t(errorKeys[kind] as never));
+ logger.error(' Bundle could not be read', err);
+ }
+ })();
+
+ return () => {
+ active = false;
+ if (url) URL.revokeObjectURL(url);
+ };
+ }, [file]);
+
+ const handleImport = async () => {
+ if (!bundle) return;
+ setImporting(true);
+ try {
+ onImported(await importGuide(bundle));
+ } catch (err) {
+ setError(i18n.t('import.errorFailed'));
+ logger.error(' Import failed', err);
+ } finally {
+ setImporting(false);
+ }
+ };
+
+ const manifest = bundle?.manifest;
+ const stepCount = manifest?.steps.length ?? 0;
+
+ const notes = manifest
+ ? [
+ manifest.redacted.screenshots ? i18n.t('import.noteRedacted') : null,
+ manifest.redacted.inputValues ? i18n.t('import.noteInputs') : null,
+ manifest.redacted.urls === 'path' ? i18n.t('import.noteUrls') : null,
+ manifest.redacted.urls === 'origin' ? i18n.t('import.noteUrlsOrigin') : null,
+ ].filter((note): note is string => note !== null)
+ : [];
+
+ return (
+ <Dialog open={file !== null} onOpenChange={(open) => !open && onClose()}>
+ <DialogContent className="sm:max-w-[460px]">
+ <DialogHeader>
+ <DialogTitle className="text-[15px] font-bold flex items-center gap-2">
+ <Package size={16} className="text-accent" />
+ {i18n.t('import.title')}
+ </DialogTitle>
+ <DialogDescription className="sr-only">{i18n.t('import.dialogDescription')}</DialogDescription>
+ </DialogHeader>
+
+ {error ? (
+ <div className="flex items-start gap-2.5 rounded-lg border border-border bg-secondary/50 px-3.5 py-3">
+ <FileWarning size={16} className="text-muted-foreground shrink-0 mt-0.5" />
+ <p className="text-[13px] text-foreground leading-snug">{error}</p>
+ </div>
+ ) : !manifest ? (
+ <div className="flex items-center gap-2 py-6 justify-center text-[13px] text-muted-foreground">
+ <Loader2 size={14} className="animate-spin" />
+ {i18n.t('import.reading')}
+ </div>
+ ) : (
+ <div className="space-y-3">
+ {cover && (
+ <img
+ src={cover}
+ alt=""
+ className="w-full rounded-lg border border-border object-cover max-h-40 bg-secondary"
+ />
+ )}
+
+ <div>
+ <div className="text-[15px] font-semibold text-foreground leading-snug">{manifest.guide.title}</div>
+ <div className="text-[12px] text-muted-foreground mt-0.5">
+ {stepCount === 1
+ ? i18n.t('fullview.stepCount', [String(stepCount)])
+ : i18n.t('fullview.stepCountPlural', [String(stepCount)])}
+ {manifest.sourceDomain ? ` Β· ${manifest.sourceDomain}` : ''}
+ </div>
+ </div>
+
+ {manifest.sourceDomain && (
+ <div className="flex items-start gap-2.5 rounded-lg border border-border bg-secondary/50 px-3.5 py-2.5">
+ <AlertTriangle size={14} className="text-accent shrink-0 mt-0.5" />
+ <p className="text-[12px] text-muted-foreground leading-snug">
+ {i18n.t('import.guideMeHint', [manifest.sourceDomain])}
+ </p>
+ </div>
+ )}
+
+ {notes.length > 0 && (
+ <ul className="space-y-1 pl-1">
+ {notes.map((note) => (
+ <li key={note} className="text-[11.5px] text-muted-foreground leading-snug">
+ Β· {note}
+ </li>
+ ))}
+ </ul>
+ )}
+ </div>
+ )}
+
+ <div className="flex justify-end gap-2 pt-1">
+ <Button size="sm" variant="ghost" onClick={onClose} className="border border-border">
+ {i18n.t('import.cancel')}
+ </Button>
+ <Button size="sm" disabled={!manifest || importing} onClick={handleImport}>
+ {importing && <Loader2 size={14} className="animate-spin" />}
+ {i18n.t('import.confirm')}
+ </Button>
+ </div>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/ui/sidepanel/LibraryView.tsx b/src/ui/sidepanel/LibraryView.tsx
index 6166a25..18500da 100644
--- a/src/ui/sidepanel/LibraryView.tsx
+++ b/src/ui/sidepanel/LibraryView.tsx
@@ -1,4 +1,4 @@
-import { Star, Trash2 } from 'lucide-react';
+import { LayoutDashboard, Star, Trash2 } from 'lucide-react';
import { useCallback, useEffect, useState } from 'react';
import { i18n } from '#imports';
import {
@@ -10,10 +10,27 @@ import {
toggleStar,
} from '@/core/guides/service';
import type { Guide } from '@/core/guides/types';
+import { createTab, focusWindow, getExtensionURL, queryTabs, updateTab } from '@/lib/browser-api';
import { formatRelativeTime } from '@/lib/utils';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/ui/components/ui/tooltip';
import FaviconImg from '@/ui/shared/FaviconImg';
+/**
+ * The dashboard is where the library, import and export live. Reuses an open
+ * dashboard tab rather than stacking up duplicates.
+ */
+async function openDashboard() {
+ const url = getExtensionURL('/fullview.html');
+ const tabs = await queryTabs({ url });
+ const existing = tabs[0];
+ if (existing?.id) {
+ await updateTab(existing.id, { active: true });
+ if (existing.windowId) await focusWindow(existing.windowId);
+ } else {
+ await createTab({ url });
+ }
+}
+
interface LibraryViewProps {
onOpen: (guideId: string) => void;
onStartRecording?: () => void;
@@ -128,6 +145,15 @@ export default function LibraryView({ onOpen, searchQuery = '' }: LibraryViewPro
</svg>
<p className="text-sm font-medium text-foreground mt-3">{i18n.t('library.noGuidesTitle')}</p>
<p className="text-xs mt-1 text-purple">{i18n.t('library.noGuidesSub')}</p>
+ <button
+ type="button"
+ onClick={() => void openDashboard()}
+ className="mt-4 inline-flex items-center gap-1.5 text-xs font-medium text-foreground px-3 py-1.5 rounded-lg border border-border bg-card hover:border-violet hover:text-purple transition-colors"
+ >
+ <LayoutDashboard size={13} />
+ {i18n.t('library.openDashboard')}
+ </button>
+ <p className="text-[11px] mt-2 text-muted-foreground">{i18n.t('library.openDashboardHint')}</p>
</div>
);
}