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(editor): put back what a step's narration said

davbio87287870893f
diff --git a/src/ui/sidepanel/StepCard.tsx b/src/ui/sidepanel/StepCard.tsx
index 18d3dd9..474de95 100644
--- a/src/ui/sidepanel/StepCard.tsx
+++ b/src/ui/sidepanel/StepCard.tsx
@@ -1,7 +1,7 @@
-import { Check, Copy, Loader2, Trash2 } from 'lucide-react';
+import { Check, Copy, Loader2, Trash2, Undo2 } from 'lucide-react';
import { useEffect, useState } from 'react';
import { i18n } from '#imports';
-import { replaceScreenshot } from '@/core/guides/service';
+import { replaceScreenshot, restoreNarratedDescription } from '@/core/guides/service';
import type { Screenshot, Step } from '@/core/guides/types';
import { imageDimensions, renderScreenshot } from '@/core/screenshot/render';
import { logger } from '@/lib/logger';
@@ -66,6 +66,16 @@ export default function StepCard({
!readOnly && !step.aiPending && Boolean(hasApiKey),
);
+ const spoken = step.narratedDescription?.trim();
+ const canRestoreSpoken = !readOnly && !step.aiPending && Boolean(spoken) && spoken !== description.trim();
+
+ const handleRestoreSpoken = async () => {
+ const restored = await restoreNarratedDescription(step.id);
+ if (!restored) return;
+ setDescription(restored);
+ onChanged?.();
+ };
+
const handleDelete = () => {
setConfirmDelete(false);
onDelete?.(step.id);
@@ -154,6 +164,20 @@ export default function StepCard({
<div className="flex items-center justify-between gap-2 mt-1">
{step.aiPending ? <span /> : <StepSourceBadge source={step.descriptionSource} />}
<div className="flex items-center gap-0.5">
+ {canRestoreSpoken && (
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <button
+ onClick={() => void handleRestoreSpoken()}
+ aria-label={i18n.t('editor.restoreSpoken')}
+ className="p-1 rounded-md transition-colors text-border hover:text-success"
+ >
+ <Undo2 size={13} />
+ </button>
+ </TooltipTrigger>
+ <TooltipContent>{i18n.t('editor.restoreSpoken')}</TooltipContent>
+ </Tooltip>
+ )}
{askAi.trigger}
{screenshot && (
<Tooltip>
diff --git a/src/ui/sidepanel/__tests__/step-card-restore.test.tsx b/src/ui/sidepanel/__tests__/step-card-restore.test.tsx
new file mode 100644
index 0000000..d061f30
--- /dev/null
+++ b/src/ui/sidepanel/__tests__/step-card-restore.test.tsx
@@ -0,0 +1,93 @@
+// @vitest-environment jsdom
+import { fireEvent, render, screen, waitFor } from '@testing-library/react';
+import { describe, expect, it, vi } from 'vitest';
+import type { Step } from '@/core/guides/types';
+import { TooltipProvider } from '@/ui/components/ui/tooltip';
+
+const restoreNarratedDescription = vi.fn();
+
+vi.mock('@/core/guides/service', () => ({
+ replaceScreenshot: vi.fn(),
+ restoreNarratedDescription: (...args: unknown[]) => restoreNarratedDescription(...args),
+}));
+vi.mock('@/core/screenshot/render', () => ({ imageDimensions: vi.fn(), renderScreenshot: vi.fn() }));
+
+import StepCard from '../StepCard';
+
+const SPOKEN = 'Open the billing tab on the left';
+
+function step(overrides: Partial<Step>): Step {
+ return {
+ id: 's1',
+ guideId: 'g1',
+ index: 0,
+ description: SPOKEN,
+ action: 'click',
+ url: 'https://a.test',
+ timestamp: 1,
+ ...overrides,
+ };
+}
+
+function renderCard(current: Step, readOnly = false, onChanged = vi.fn()) {
+ return render(
+ <TooltipProvider>
+ <StepCard
+ step={current}
+ number={1}
+ screenshot={undefined}
+ readOnly={readOnly}
+ onDescriptionChange={vi.fn()}
+ onDelete={vi.fn()}
+ onChanged={onChanged}
+ />
+ </TooltipProvider>,
+ );
+}
+
+const restoreButton = () => screen.queryByRole('button', { name: 'editor.restoreSpoken' });
+
+describe('restoring what the step said', () => {
+ it('is offered once an edit has moved the text away from the narration', () => {
+ renderCard(step({ description: 'Click Billing', narratedDescription: SPOKEN, descriptionSource: 'manual' }));
+ expect(restoreButton()).toBeTruthy();
+ });
+
+ it('is not offered while the text still matches the narration', () => {
+ renderCard(step({ description: SPOKEN, narratedDescription: SPOKEN, descriptionSource: 'narration' }));
+ expect(restoreButton()).toBeNull();
+ });
+
+ it('is not offered on a step narration never touched', () => {
+ renderCard(step({ description: 'Click Billing', descriptionSource: 'ai' }));
+ expect(restoreButton()).toBeNull();
+ });
+
+ it('is not offered while the card is read-only', () => {
+ renderCard(step({ description: 'Click Billing', narratedDescription: SPOKEN }), true);
+ expect(restoreButton()).toBeNull();
+ });
+
+ it('is not offered while a description is still being written', () => {
+ renderCard(step({ description: 'Click Billing', narratedDescription: SPOKEN, aiPending: true }));
+ expect(restoreButton()).toBeNull();
+ });
+
+ it('puts the spoken text back and tells the page to reload', async () => {
+ restoreNarratedDescription.mockResolvedValue(SPOKEN);
+ const onChanged = vi.fn();
+ renderCard(
+ step({ description: 'Click Billing', narratedDescription: SPOKEN, descriptionSource: 'manual' }),
+ false,
+ onChanged,
+ );
+
+ const button = restoreButton();
+ expect(button).toBeTruthy();
+ fireEvent.click(button as HTMLElement);
+
+ await waitFor(() => expect(onChanged).toHaveBeenCalled());
+ expect(restoreNarratedDescription).toHaveBeenCalledWith('s1');
+ expect(screen.getByDisplayValue(SPOKEN)).toBeTruthy();
+ });
+});