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 πŸ“–

fix(fullview): report a failed duplicate on the guide it happened on

davbio67d56c5ec5fb
diff --git a/src/ui/fullview/TopNav.tsx b/src/ui/fullview/TopNav.tsx
index f663a2f..5f4b8e9 100644
--- a/src/ui/fullview/TopNav.tsx
+++ b/src/ui/fullview/TopNav.tsx
@@ -26,8 +26,6 @@ interface TopNavProps {
route: Route;
}
-// Long enough to swallow the tail of a fast multi-click, short enough that deliberately forking a
-// copy again is never blocked for a noticeable moment.
const DUPLICATE_SETTLE_MS = 700;
const navItems = [
@@ -70,29 +68,29 @@ export default function TopNav({ route }: TopNavProps) {
}));
const [exportOpen, setExportOpen] = useState(false);
const [duplicating, setDuplicating] = useState(false);
+ const [duplicateFailedOnRoute, setDuplicateFailedOnRoute] = useState<string | null>(null);
const settleTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const routeKey = route.page === 'guide' ? `guide/${route.guideId}` : `library/${route.category}`;
+ const duplicateFailed = duplicateFailedOnRoute === routeKey;
+
useEffect(() => () => clearTimeout(settleTimer.current ?? undefined), []);
- // Straight into the copy: the reason to duplicate a guide is to trim it down into a shorter one,
- // and that work happens in the copy, not the original.
const handleDuplicate = async (guideId: string) => {
if (duplicating) return;
setDuplicating(true);
+ setDuplicateFailedOnRoute(null);
try {
const copyId = await duplicateGuide(guideId);
if (copyId) {
navigate({ page: 'guide', guideId: copyId });
- // Hold the button disabled across the navigation. Copying takes a few milliseconds, so an
- // in-flight guard alone is no guard at all: the button keeps its place while the page under
- // it becomes the copy, and a second click of a fast double-click lands on the copy and forks
- // *it*, chaining "Copy of Copy of…" for as long as the user keeps clicking.
settleTimer.current = setTimeout(() => setDuplicating(false), DUPLICATE_SETTLE_MS);
return;
}
} catch (err) {
logger.error(' Duplicate guide failed', err);
}
+ setDuplicateFailedOnRoute(routeKey);
setDuplicating(false);
};
@@ -110,123 +108,138 @@ export default function TopNav({ route }: TopNavProps) {
};
return (
- <header className="flex items-center gap-5 px-7 h-16 shrink-0 bg-card border-b border-border">
- {/* Brand */}
- <button
- onClick={() => navigate({ page: 'library', category: 'all' })}
- className="flex items-center gap-2 mr-4 cursor-pointer h-full"
- >
- <div className="mb-1">
- <MascotIcon size={22} />
- </div>
- <span className="text-[15px] font-bold tracking-tight text-foreground">{i18n.t('app_name')}</span>
- </button>
+ <>
+ <header className="flex items-center gap-5 px-7 h-16 shrink-0 bg-card border-b border-border">
+ {/* Brand */}
+ <button
+ onClick={() => navigate({ page: 'library', category: 'all' })}
+ className="flex items-center gap-2 mr-4 cursor-pointer h-full"
+ >
+ <div className="mb-1">
+ <MascotIcon size={22} />
+ </div>
+ <span className="text-[15px] font-bold tracking-tight text-foreground">{i18n.t('app_name')}</span>
+ </button>
- {route.page === 'guide'
- ? guideTitle && (
- <>
- <ChevronRight size={14} className="text-foreground opacity-25" />
- {guideTitle === i18n.t('fullview_untitledGuide') && guideStepCount > 0 ? (
- <span className="flex items-center gap-1">
- {[0, 1, 2].map((i) => (
- <span
- key={i}
- className="w-[5px] h-[5px] rounded-full bg-foreground animate-bounce"
- style={{ animationDelay: `${i * 150}ms`, animationDuration: '1.2s' }}
- />
- ))}
- </span>
- ) : (
- <span className="text-[13px] font-medium truncate max-w-sm text-foreground">{guideTitle}</span>
- )}
- </>
- )
- : navItems.map((item) => {
- const active = route.page === 'library' && route.category === item.key;
- const count = counts[item.key];
- return (
- <button
- key={item.key}
- onClick={() => navigate({ page: 'library', category: item.key })}
- className={`flex items-center gap-1.5 text-[13px] h-8 px-3 rounded-md transition-all
- ${active ? 'bg-primary text-primary-foreground font-semibold' : 'text-foreground font-medium hover:bg-secondary'}`}
- >
- <item.icon size={13.5} />
- {i18n.t(item.labelKey)}
- {count > 0 && (
- <span
- className={`text-[11px] ml-0.5 ${active ? 'text-primary-foreground/70' : 'text-muted-foreground'}`}
- >
- {count}
+ {route.page === 'guide'
+ ? guideTitle && (
+ <>
+ <ChevronRight size={14} className="text-foreground opacity-25" />
+ {guideTitle === i18n.t('fullview_untitledGuide') && guideStepCount > 0 ? (
+ <span className="flex items-center gap-1">
+ {[0, 1, 2].map((i) => (
+ <span
+ key={i}
+ className="w-[5px] h-[5px] rounded-full bg-foreground animate-bounce"
+ style={{ animationDelay: `${i * 150}ms`, animationDuration: '1.2s' }}
+ />
+ ))}
</span>
+ ) : (
+ <span className="text-[13px] font-medium truncate max-w-sm text-foreground">{guideTitle}</span>
)}
- </button>
- );
- })}
+ </>
+ )
+ : navItems.map((item) => {
+ const active = route.page === 'library' && route.category === item.key;
+ const count = counts[item.key];
+ return (
+ <button
+ key={item.key}
+ onClick={() => navigate({ page: 'library', category: item.key })}
+ className={`flex items-center gap-1.5 text-[13px] h-8 px-3 rounded-md transition-all
+ ${active ? 'bg-primary text-primary-foreground font-semibold' : 'text-foreground font-medium hover:bg-secondary'}`}
+ >
+ <item.icon size={13.5} />
+ {i18n.t(item.labelKey)}
+ {count > 0 && (
+ <span
+ className={`text-[11px] ml-0.5 ${active ? 'text-primary-foreground/70' : 'text-muted-foreground'}`}
+ >
+ {count}
+ </span>
+ )}
+ </button>
+ );
+ })}
- {/* Right side */}
- <div className="ml-auto flex items-center gap-3">
- <Button
- size="sm"
- variant="ghost"
- onClick={() => setSearchOpen(true)}
- className={`w-52 justify-start ${NAV_CONTROL}`}
- >
- <Search size={14} className="shrink-0 text-muted-foreground" />
- <span className="flex-1 text-left text-muted-foreground">{i18n.t('fullview_searchPlaceholder')}</span>
- <span className="text-[10px] font-semibold px-1.5 py-0.5 rounded-md bg-secondary text-muted-foreground">
- ⌘K
- </span>
- </Button>
- {route.page === 'guide' && exportData && (
- <>
- <Button size="sm" variant="ghost" onClick={() => toggleEditing(exportData.guideId)} className={NAV_CONTROL}>
- {editing ? <Check size={14} /> : <Pencil size={14} />}
- {editing ? i18n.t('editor.done') : i18n.t('editor.edit')}
- </Button>
- <Button size="sm" variant="ghost" onClick={() => setHistoryOpen(!historyOpen)} className={NAV_CONTROL}>
- <History size={14} />
- {i18n.t('editor.versionHistory')}
- </Button>
- {hasTranscript && (
+ {/* Right side */}
+ <div className="ml-auto flex items-center gap-3">
+ <Button
+ size="sm"
+ variant="ghost"
+ onClick={() => setSearchOpen(true)}
+ className={`w-52 justify-start ${NAV_CONTROL}`}
+ >
+ <Search size={14} className="shrink-0 text-muted-foreground" />
+ <span className="flex-1 text-left text-muted-foreground">{i18n.t('fullview_searchPlaceholder')}</span>
+ <span className="text-[10px] font-semibold px-1.5 py-0.5 rounded-md bg-secondary text-muted-foreground">
+ ⌘K
+ </span>
+ </Button>
+ {route.page === 'guide' && exportData && (
+ <>
<Button
size="sm"
variant="ghost"
- onClick={() => setTranscriptOpen(!transcriptOpen)}
+ onClick={() => toggleEditing(exportData.guideId)}
className={NAV_CONTROL}
>
- <MessageSquareQuote size={14} />
- {i18n.t('transcript.title')}
+ {editing ? <Check size={14} /> : <Pencil size={14} />}
+ {editing ? i18n.t('editor.done') : i18n.t('editor.edit')}
</Button>
- )}
- {!editing && (
- <>
+ <Button size="sm" variant="ghost" onClick={() => setHistoryOpen(!historyOpen)} className={NAV_CONTROL}>
+ <History size={14} />
+ {i18n.t('editor.versionHistory')}
+ </Button>
+ {hasTranscript && (
<Button
size="sm"
variant="ghost"
- disabled={duplicating}
- onClick={() => handleDuplicate(exportData.guideId)}
+ onClick={() => setTranscriptOpen(!transcriptOpen)}
className={NAV_CONTROL}
>
- <Copy size={14} />
- {i18n.t('library.duplicate')}
- </Button>
- <Button size="sm" onClick={() => setExportOpen(true)} className="h-8 rounded-lg">
- <Download size={14} />
- {i18n.t('common.export')}
+ <MessageSquareQuote size={14} />
+ {i18n.t('transcript.title')}
</Button>
- </>
- )}
- <ExportPreviewModal
- open={exportOpen}
- onOpenChange={setExportOpen}
- guide={exportData.guide}
- steps={exportData.steps}
- screenshots={exportData.screenshots}
- />
- </>
- )}
- </div>
- </header>
+ )}
+ {!editing && (
+ <>
+ <Button
+ size="sm"
+ variant="ghost"
+ disabled={duplicating}
+ onClick={() => handleDuplicate(exportData.guideId)}
+ className={NAV_CONTROL}
+ >
+ <Copy size={14} />
+ {i18n.t('library.duplicate')}
+ </Button>
+ <Button size="sm" onClick={() => setExportOpen(true)} className="h-8 rounded-lg">
+ <Download size={14} />
+ {i18n.t('common.export')}
+ </Button>
+ </>
+ )}
+ <ExportPreviewModal
+ open={exportOpen}
+ onOpenChange={setExportOpen}
+ guide={exportData.guide}
+ steps={exportData.steps}
+ screenshots={exportData.screenshots}
+ />
+ </>
+ )}
+ </div>
+ </header>
+ {duplicateFailed && (
+ <p
+ role="alert"
+ className="text-xs py-2 px-7 text-center shrink-0 bg-card border-b border-border text-destructive"
+ >
+ {i18n.t('library.duplicateFailed')}
+ </p>
+ )}
+ </>
);
}
diff --git a/src/ui/fullview/__tests__/topnav-duplicate.test.tsx b/src/ui/fullview/__tests__/topnav-duplicate.test.tsx
index a642e02..856fdb0 100644
--- a/src/ui/fullview/__tests__/topnav-duplicate.test.tsx
+++ b/src/ui/fullview/__tests__/topnav-duplicate.test.tsx
@@ -47,7 +47,6 @@ function duplicateButton(): HTMLButtonElement {
return btn as HTMLButtonElement;
}
-/** Lets the awaited duplicateGuide() resolve and React flush its state update. */
async function settleMicrotasks() {
await act(async () => {
await Promise.resolve();
@@ -78,9 +77,6 @@ describe('TopNav duplicate', () => {
it('makes one copy from a fast multi-click, not a chain of copies of copies', async () => {
render(<TopNav route={route} />);
- // The first click navigates to the copy in a couple of milliseconds. Without a guard that
- // outlives the navigation, the later clicks land on the copy's own Duplicate button and fork
- // it again, so a triple-click leaves "Copy of Copy of Copy of ..." behind.
fireEvent.click(duplicateButton());
await settleMicrotasks();
fireEvent.click(duplicateButton());
@@ -107,7 +103,7 @@ describe('TopNav duplicate', () => {
expect(duplicateGuide).toHaveBeenCalledTimes(2);
});
- it('re-enables immediately when the copy could not be made', async () => {
+ it('re-enables immediately when the copy could not be made, and says so', async () => {
duplicateGuide.mockRejectedValue(new Error('boom'));
render(<TopNav route={route} />);
@@ -116,5 +112,45 @@ describe('TopNav duplicate', () => {
expect(navigate).not.toHaveBeenCalled();
expect(duplicateButton().disabled).toBe(false);
+ expect(screen.getByRole('alert')).toBeTruthy();
+ });
+
+ it('reports a guide that was not there, which resolves null rather than throwing', async () => {
+ duplicateGuide.mockResolvedValue(null);
+ render(<TopNav route={route} />);
+
+ fireEvent.click(duplicateButton());
+ await settleMicrotasks();
+
+ expect(navigate).not.toHaveBeenCalled();
+ expect(screen.getByRole('alert')).toBeTruthy();
+ });
+
+ it('does not carry the message onto another guide, since the nav never unmounts', async () => {
+ duplicateGuide.mockRejectedValue(new Error('boom'));
+ const { rerender } = render(<TopNav route={route} />);
+
+ fireEvent.click(duplicateButton());
+ await settleMicrotasks();
+ expect(screen.getByRole('alert')).toBeTruthy();
+
+ rerender(<TopNav route={{ page: 'guide', guideId: 'g2' } as const} />);
+
+ expect(screen.queryByRole('alert')).toBeNull();
+ });
+
+ it('clears the message when a later attempt succeeds', async () => {
+ duplicateGuide.mockRejectedValueOnce(new Error('boom'));
+ render(<TopNav route={route} />);
+
+ fireEvent.click(duplicateButton());
+ await settleMicrotasks();
+ expect(screen.getByRole('alert')).toBeTruthy();
+
+ duplicateGuide.mockResolvedValue('copy-1');
+ fireEvent.click(duplicateButton());
+ await settleMicrotasks();
+
+ expect(screen.queryByRole('alert')).toBeNull();
});
});