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): show and generate guide description

Luis Ortize7aa631e1f7e
diff --git a/src/ui/fullview/GuideContent.tsx b/src/ui/fullview/GuideContent.tsx
index a60a78a..84a387e 100644
--- a/src/ui/fullview/GuideContent.tsx
+++ b/src/ui/fullview/GuideContent.tsx
@@ -1,4 +1,4 @@
-import { History, Play } from 'lucide-react';
+import { History, Play, Sparkles } from 'lucide-react';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { TypeAnimation } from 'react-type-animation';
import { i18n } from '#imports';
@@ -7,19 +7,21 @@ import {
getGuide,
getScreenshotsForSteps,
onGuidesChanged,
+ updateGuideDescription,
updateGuideTitle,
updateStepDescription,
} from '@/core/guides/service';
import type { SnapshotLike } from '@/core/guides/snapshot-diff';
import type { Guide, Screenshot, Snapshot, Step } from '@/core/guides/types';
import type { ScreenshotEdits } from '@/core/screenshot/types';
-import { openSidebar } from '@/lib/browser-api';
+import { localStorage, openSidebar } from '@/lib/browser-api';
import { logger } from '@/lib/logger';
import { sendMessage } from '@/lib/messaging';
import { formatDate, getMostCommonDomain } from '@/lib/utils';
import { useFullview } from '@/stores/fullview';
import AnnotationEditor from '@/ui/shared/AnnotationEditor';
import FaviconImg from '@/ui/shared/FaviconImg';
+import { guideDescriptionErrorMessage } from '@/ui/shared/guide-description-error';
import GuideStepList from './components/GuideStepList';
import VersionHistoryPanel from './components/VersionHistoryPanel';
@@ -87,13 +89,19 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
const [editingTool, setEditingTool] = useState<'annotate' | 'redact' | 'crop' | 'target'>('annotate');
const [preview, setPreview] = useState<Snapshot | null>(null);
const [previewData, setPreviewData] = useState<PreviewData | null>(null);
+ const [description, setDescription] = useState('');
+ const [generating, setGenerating] = useState(false);
+ const [hasApiKey, setHasApiKey] = useState(false);
+ const [descriptionError, setDescriptionError] = useState<string | null>(null);
const titleRef = useRef('');
const appliedInitialRef = useRef(false);
+ const editingDescriptionRef = useRef(false);
const loadGuide = useCallback(async () => {
const result = await getGuide(guideId);
if (result) {
setData(result);
+ if (!editingDescriptionRef.current) setDescription(result.guide.description ?? '');
const newTitle = result.guide.title;
const prev = titleRef.current;
if (
@@ -119,6 +127,10 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
return onGuidesChanged(() => loadGuide());
}, [loadGuide]);
+ useEffect(() => {
+ localStorage.get(['aiApiKey']).then((s) => setHasApiKey(Boolean(s.aiApiKey)));
+ }, []);
+
const handleTitleBlur = useCallback(async () => {
if (!data || title === data.guide.title) return;
await updateGuideTitle(guideId, title);
@@ -126,6 +138,34 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
document.title = `${title} β€” ${i18n.t('app_name')}`;
}, [data, guideId, title]);
+ const handleGuideDescriptionBlur = useCallback(async () => {
+ if (data && description !== (data.guide.description ?? '')) {
+ await updateGuideDescription(guideId, description);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description } } : prev));
+ }
+ editingDescriptionRef.current = false;
+ }, [data, guideId, description]);
+
+ const handleGenerateDescription = useCallback(async () => {
+ setGenerating(true);
+ setDescriptionError(null);
+ try {
+ const result = await sendMessage('generateGuideDescription', { guideId });
+ if (result.error) {
+ setDescriptionError(guideDescriptionErrorMessage(result.error));
+ return;
+ }
+ const generated = result.description;
+ if (!generated) return;
+ setDescription(generated);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description: generated } } : prev));
+ } catch {
+ setDescriptionError(guideDescriptionErrorMessage('generation-failed'));
+ } finally {
+ setGenerating(false);
+ }
+ }, [guideId]);
+
const handleDescriptionChange = useCallback(async (stepId: string, description: string) => {
await updateStepDescription(stepId, description);
setData((prev) => {
@@ -313,6 +353,58 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
)}
</div>
+ {!preview && (
+ <div className="mt-3">
+ {editing ? (
+ <>
+ <textarea
+ ref={(el) => {
+ if (el) {
+ el.style.height = '0';
+ el.style.height = `${el.scrollHeight}px`;
+ }
+ }}
+ value={description}
+ rows={2}
+ onChange={(e) => {
+ setDescription(e.target.value);
+ const el = e.target;
+ el.style.height = '0';
+ el.style.height = `${el.scrollHeight}px`;
+ }}
+ onFocus={() => {
+ editingDescriptionRef.current = true;
+ }}
+ onBlur={handleGuideDescriptionBlur}
+ placeholder={i18n.t('editor.descriptionPlaceholder')}
+ className="w-full max-w-[720px] resize-none overflow-hidden bg-transparent p-0 text-[15px] leading-relaxed text-muted-foreground placeholder:text-muted-foreground/60 border-b-2 border-transparent hover:border-border focus:outline-none focus:border-accent"
+ />
+ {hasApiKey && (
+ <button
+ type="button"
+ onClick={handleGenerateDescription}
+ disabled={generating}
+ title={description ? i18n.t('editor.regenerateDescription') : i18n.t('editor.generateDescription')}
+ className="mt-1.5 inline-flex items-center gap-1.5 text-xs font-semibold text-accent hover:text-deep disabled:opacity-50 disabled:cursor-not-allowed"
+ >
+ <Sparkles size={13} className={generating ? 'animate-pulse' : ''} />
+ {generating
+ ? i18n.t('editor.generatingDescription')
+ : description
+ ? i18n.t('editor.regenerateDescription')
+ : i18n.t('editor.generateDescription')}
+ </button>
+ )}
+ {descriptionError && <p className="mt-1.5 text-xs text-destructive">{descriptionError}</p>}
+ </>
+ ) : (
+ description && (
+ <p className="max-w-[720px] text-[15px] leading-relaxed text-muted-foreground">{description}</p>
+ )
+ )}
+ </div>
+ )}
+
<div className="flex items-center gap-1.5 mt-2 mb-4 flex-wrap">
<span className="inline-flex items-center text-[11px] font-medium text-muted-foreground bg-card border border-border px-2.5 py-0.5 rounded-full">
{formatDate(data.guide.createdAt)}
diff --git a/src/ui/shared/__tests__/guide-description-error.test.ts b/src/ui/shared/__tests__/guide-description-error.test.ts
new file mode 100644
index 0000000..aaa0e7b
--- /dev/null
+++ b/src/ui/shared/__tests__/guide-description-error.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from 'vitest';
+import type { GuideDescriptionError } from '@/lib/messaging';
+import { guideDescriptionErrorMessage } from '../guide-description-error';
+
+const CASES: [GuideDescriptionError, string][] = [
+ ['no-api-key', 'editor.descriptionErrorNoApiKey'],
+ ['no-steps', 'editor.descriptionErrorNoSteps'],
+ ['generation-failed', 'editor.descriptionErrorFailed'],
+ ['save-failed', 'editor.descriptionErrorSaveFailed'],
+];
+
+describe('guideDescriptionErrorMessage', () => {
+ it.each(CASES)('maps %s to its locale key', (error, key) => {
+ expect(guideDescriptionErrorMessage(error)).toBe(key);
+ });
+
+ it('gives every error code a distinct message', () => {
+ const messages = CASES.map(([error]) => guideDescriptionErrorMessage(error));
+ expect(new Set(messages).size).toBe(CASES.length);
+ });
+});
diff --git a/src/ui/shared/guide-description-error.ts b/src/ui/shared/guide-description-error.ts
new file mode 100644
index 0000000..56afafa
--- /dev/null
+++ b/src/ui/shared/guide-description-error.ts
@@ -0,0 +1,13 @@
+import { i18n } from '#imports';
+import type { GuideDescriptionError } from '@/lib/messaging';
+
+const DESCRIPTION_ERROR_KEYS = {
+ 'no-api-key': 'editor.descriptionErrorNoApiKey',
+ 'no-steps': 'editor.descriptionErrorNoSteps',
+ 'generation-failed': 'editor.descriptionErrorFailed',
+ 'save-failed': 'editor.descriptionErrorSaveFailed',
+} as const satisfies Record<GuideDescriptionError, string>;
+
+export function guideDescriptionErrorMessage(error: GuideDescriptionError): string {
+ return i18n.t(DESCRIPTION_ERROR_KEYS[error]);
+}
diff --git a/src/ui/sidepanel/GuideEditor.tsx b/src/ui/sidepanel/GuideEditor.tsx
index 1cdcb87..afc94aa 100644
--- a/src/ui/sidepanel/GuideEditor.tsx
+++ b/src/ui/sidepanel/GuideEditor.tsx
@@ -1,5 +1,5 @@
-import { ArrowLeft, Check, Layers, Maximize2, Pencil, Play } from 'lucide-react';
-import { useCallback, useEffect, useState } from 'react';
+import { ArrowLeft, Check, Layers, Maximize2, Pencil, Play, Sparkles } from 'lucide-react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import { i18n } from '#imports';
import {
createSnapshot,
@@ -7,17 +7,19 @@ import {
getGuide,
onGuidesChanged,
reorderSteps,
+ updateGuideDescription,
updateGuideTitle,
updateStepDescription,
} from '@/core/guides/service';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
-import { createTab, focusWindow, getExtensionURL, queryTabs, updateTab } from '@/lib/browser-api';
+import { createTab, focusWindow, getExtensionURL, localStorage, queryTabs, updateTab } from '@/lib/browser-api';
import { logger } from '@/lib/logger';
import { sendMessage } from '@/lib/messaging';
import { getMostCommonDomain } from '@/lib/utils';
import { Input } from '@/ui/components/ui/input';
import EmptyGuideState from '@/ui/shared/EmptyGuideState';
import FaviconImg from '@/ui/shared/FaviconImg';
+import { guideDescriptionErrorMessage } from '@/ui/shared/guide-description-error';
import { dominantRatio } from '@/ui/shared/ImagePlaceholder';
import ExportMenu from './ExportMenu';
import StepCard from './StepCard';
@@ -50,8 +52,19 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
const [notFound, setNotFound] = useState(false);
const [title, setTitle] = useState('');
const [editing, setEditing] = useState(false);
+ const [description, setDescription] = useState('');
+ const [generating, setGenerating] = useState(false);
+ const [hasApiKey, setHasApiKey] = useState(false);
+ const [descriptionError, setDescriptionError] = useState<string | null>(null);
const [dragIndex, setDragIndex] = useState<number | null>(null);
const [dragOverIndex, setDragOverIndex] = useState<number | null>(null);
+ const editingDescriptionRef = useRef(false);
+
+ const applyGuide = useCallback((result: GuideData) => {
+ setData(result);
+ setTitle(result.guide.title);
+ if (!editingDescriptionRef.current) setDescription(result.guide.description ?? '');
+ }, []);
const loadGuide = useCallback(async () => {
const result = await getGuide(guideId);
@@ -60,10 +73,9 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
setLoading(false);
return;
}
- setData(result);
- setTitle(result.guide.title);
+ applyGuide(result);
setLoading(false);
- }, [guideId]);
+ }, [guideId, applyGuide]);
useEffect(() => {
loadGuide();
@@ -75,12 +87,44 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
});
}, [editing, loadGuide]);
+ useEffect(() => {
+ localStorage.get(['aiApiKey']).then((s) => setHasApiKey(Boolean(s.aiApiKey)));
+ }, []);
+
const handleTitleBlur = useCallback(async () => {
if (!data || title === data.guide.title) return;
await updateGuideTitle(guideId, title);
setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, title } } : prev));
}, [data, guideId, title]);
+ const handleGuideDescriptionBlur = useCallback(async () => {
+ if (data && description !== (data.guide.description ?? '')) {
+ await updateGuideDescription(guideId, description);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description } } : prev));
+ }
+ editingDescriptionRef.current = false;
+ }, [data, guideId, description]);
+
+ const handleGenerateDescription = useCallback(async () => {
+ setGenerating(true);
+ setDescriptionError(null);
+ try {
+ const result = await sendMessage('generateGuideDescription', { guideId });
+ if (result.error) {
+ setDescriptionError(guideDescriptionErrorMessage(result.error));
+ return;
+ }
+ const generated = result.description;
+ if (!generated) return;
+ setDescription(generated);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description: generated } } : prev));
+ } catch {
+ setDescriptionError(guideDescriptionErrorMessage('generation-failed'));
+ } finally {
+ setGenerating(false);
+ }
+ }, [guideId]);
+
const handleDescriptionChange = useCallback(async (stepId: string, description: string) => {
await updateStepDescription(stepId, description);
setData((prev) => {
@@ -94,15 +138,14 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
await deleteStep(guideId, stepId);
const result = await getGuide(guideId);
if (result) {
- setData(result);
- setTitle(result.guide.title);
+ applyGuide(result);
} else {
setData(null);
setLoading(true);
await loadGuide();
}
},
- [guideId, loadGuide],
+ [guideId, loadGuide, applyGuide],
);
const openInFullView = useCallback((targetGuideId: string, options?: OpenInFullViewOptions) => {
@@ -199,6 +242,47 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
<ExportMenu guideId={guideId} guide={data.guide} steps={data.steps} screenshots={data.screenshots} />
</div>
</div>
+ <div className="mt-1 mb-1.5" style={{ marginLeft: '34px' }}>
+ <textarea
+ ref={(el) => {
+ if (el) {
+ el.style.height = '0';
+ el.style.height = `${el.scrollHeight}px`;
+ }
+ }}
+ value={description}
+ rows={2}
+ onChange={(e) => {
+ setDescription(e.target.value);
+ const el = e.target;
+ el.style.height = '0';
+ el.style.height = `${el.scrollHeight}px`;
+ }}
+ onFocus={() => {
+ editingDescriptionRef.current = true;
+ }}
+ onBlur={handleGuideDescriptionBlur}
+ placeholder={i18n.t('editor.descriptionPlaceholder')}
+ className="w-full resize-none overflow-hidden bg-transparent p-0 text-xs leading-snug text-muted-foreground placeholder:text-muted-foreground/60 border-b border-transparent hover:border-border focus:outline-none focus:border-accent"
+ />
+ {hasApiKey && (
+ <button
+ type="button"
+ onClick={handleGenerateDescription}
+ disabled={generating}
+ title={description ? i18n.t('editor.regenerateDescription') : i18n.t('editor.generateDescription')}
+ className="mt-1 flex items-center gap-1 text-[11px] font-medium text-accent hover:text-deep disabled:opacity-50 disabled:cursor-not-allowed"
+ >
+ <Sparkles size={11} className={generating ? 'animate-pulse' : ''} />
+ {generating
+ ? i18n.t('editor.generatingDescription')
+ : description
+ ? i18n.t('editor.regenerateDescription')
+ : i18n.t('editor.generateDescription')}
+ </button>
+ )}
+ {descriptionError && <p className="mt-1 text-[11px] text-destructive">{descriptionError}</p>}
+ </div>
<div className="text-[11px] flex items-center gap-2 text-muted-foreground" style={{ marginLeft: '34px' }}>
<span className="flex items-center gap-1">
<Layers size={11} />