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): rewrite selected description text with AI

Luis Ortiz6b1314cc6479
diff --git a/src/ui/fullview/GuideContent.tsx b/src/ui/fullview/GuideContent.tsx
index 16f7fa0..29f37c8 100644
--- a/src/ui/fullview/GuideContent.tsx
+++ b/src/ui/fullview/GuideContent.tsx
@@ -20,6 +20,7 @@ import { sendMessage } from '@/lib/messaging';
import { formatDate, getMostCommonDomain } from '@/lib/utils';
import { useFullview } from '@/stores/fullview';
import AnnotationEditor from '@/ui/shared/AnnotationEditor';
+import { useAskAi } from '@/ui/shared/AskAi';
import FaviconImg from '@/ui/shared/FaviconImg';
import { guideDescriptionErrorMessage } from '@/ui/shared/guide-description-error';
import Toast from '@/ui/shared/Toast';
@@ -147,6 +148,17 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
editingDescriptionRef.current = false;
}, [data, guideId, description]);
+ const commitGuideDescription = useCallback(
+ (next: string) => {
+ setDescription(next);
+ void updateGuideDescription(guideId, next);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description: next } } : prev));
+ },
+ [guideId],
+ );
+
+ const askAi = useAskAi(description, commitGuideDescription, hasApiKey);
+
const handleGenerateDescription = useCallback(async () => {
setGenerating(true);
setDescriptionError(null);
@@ -363,28 +375,32 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
</div>
) : editing ? (
<div className="flex items-start gap-2 max-w-[720px]">
- <textarea
- ref={(el) => {
- if (el) {
+ <div className="flex-1 min-w-0">
+ <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`;
- }
- }}
- 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="flex-1 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"
- />
+ }}
+ onFocus={() => {
+ editingDescriptionRef.current = true;
+ }}
+ onSelect={askAi.onSelect}
+ onBlur={handleGuideDescriptionBlur}
+ placeholder={i18n.t('editor.descriptionPlaceholder')}
+ className="w-full 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"
+ />
+ {askAi.panel}
+ </div>
{hasApiKey && (
<button
type="button"
@@ -451,6 +467,7 @@ export default function GuideContent({ guideId, initialStepId, initialTool }: Gu
onOpenEditor={handleOpenEditor}
onReorder={(newSteps) => setData((prev) => (prev ? { ...prev, steps: newSteps } : prev))}
readOnly={!editing || preview !== null}
+ hasApiKey={hasApiKey}
onChanged={loadGuide}
/>
</div>
diff --git a/src/ui/fullview/components/GuideStepList.tsx b/src/ui/fullview/components/GuideStepList.tsx
index eb40f8c..bdbe7b3 100644
--- a/src/ui/fullview/components/GuideStepList.tsx
+++ b/src/ui/fullview/components/GuideStepList.tsx
@@ -16,6 +16,7 @@ interface GuideStepListProps {
onReorder: (newSteps: Step[]) => void;
readOnly?: boolean;
onChanged?: () => void;
+ hasApiKey?: boolean;
}
export default function GuideStepList({
@@ -28,6 +29,7 @@ export default function GuideStepList({
onReorder,
readOnly,
onChanged,
+ hasApiKey,
}: GuideStepListProps) {
const { scrollToStepId, setActiveStepId } = useFullview((s) => ({
scrollToStepId: s.scrollToStepId,
@@ -103,6 +105,7 @@ export default function GuideStepList({
onDelete={onDelete}
onOpenEditor={onOpenEditor}
readOnly={readOnly}
+ hasApiKey={hasApiKey}
onChanged={onChanged}
dragHandleProps={
readOnly
diff --git a/src/ui/shared/AskAi.tsx b/src/ui/shared/AskAi.tsx
new file mode 100644
index 0000000..8112a91
--- /dev/null
+++ b/src/ui/shared/AskAi.tsx
@@ -0,0 +1,177 @@
+import { Loader2, Sparkles } from 'lucide-react';
+import { useCallback, useEffect, useRef, useState } from 'react';
+import { i18n } from '#imports';
+import { REWRITE_PRESETS, type RewritePreset } from '@/core/capture/ai/prompts';
+import { sendMessage } from '@/lib/messaging';
+import { rewriteErrorMessage } from '@/ui/shared/rewrite-error';
+import Toast from '@/ui/shared/Toast';
+
+const PRESET_LABEL_KEYS = {
+ shorter: 'editor.rewriteShorter',
+ detail: 'editor.rewriteDetail',
+ grammar: 'editor.rewriteGrammar',
+ formal: 'editor.rewriteFormal',
+ casual: 'editor.rewriteCasual',
+} as const satisfies Record<RewritePreset, string>;
+
+const PRESET_ORDER = Object.keys(PRESET_LABEL_KEYS) as RewritePreset[];
+
+interface Span {
+ start: number;
+ end: number;
+ lead: string;
+ core: string;
+ trail: string;
+}
+
+function readSpan(value: string, start: number, end: number): Span | null {
+ const raw = value.slice(start, end);
+ const core = raw.trim();
+ if (!core) return null;
+ const lead = raw.slice(0, raw.indexOf(core[0]));
+ return { start, end, lead, core, trail: raw.slice(lead.length + core.length) };
+}
+
+export function useAskAi(value: string, onReplace: (next: string) => void, enabled = true) {
+ const [span, setSpan] = useState<Span | null>(null);
+ const [active, setActive] = useState<Span | null>(null);
+ const [instruction, setInstruction] = useState('');
+ const [busy, setBusy] = useState(false);
+ const [result, setResult] = useState<string | null>(null);
+ const [error, setError] = useState<string | null>(null);
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ useEffect(() => {
+ if (active && result === null) inputRef.current?.focus();
+ }, [active, result]);
+
+ const reset = useCallback(() => {
+ setActive(null);
+ setSpan(null);
+ setInstruction('');
+ setResult(null);
+ }, []);
+
+ const onSelect = useCallback((e: React.SyntheticEvent<HTMLTextAreaElement>) => {
+ const el = e.currentTarget;
+ setSpan(readSpan(el.value, el.selectionStart, el.selectionEnd));
+ }, []);
+
+ const run = useCallback(
+ async (target: Span, prompt: string) => {
+ if (!prompt.trim() || busy) return;
+ setBusy(true);
+ setError(null);
+ try {
+ const response = await sendMessage('rewriteSelection', { text: target.core, instruction: prompt });
+ if (response.error) {
+ setError(rewriteErrorMessage(response.error));
+ return;
+ }
+ if (response.text) setResult(response.text);
+ } catch {
+ setError(rewriteErrorMessage('generation-failed'));
+ } finally {
+ setBusy(false);
+ }
+ },
+ [busy],
+ );
+
+ const replace = useCallback(() => {
+ if (!active || !result) return;
+ if (value.slice(active.start, active.end) !== active.lead + active.core + active.trail) {
+ setError(i18n.t('editor.rewriteErrorStale'));
+ reset();
+ return;
+ }
+ onReplace(value.slice(0, active.start) + active.lead + result + active.trail + value.slice(active.end));
+ reset();
+ }, [active, result, value, onReplace, reset]);
+
+ const panel = !enabled ? null : (
+ <>
+ {span && !active && (
+ <button
+ type="button"
+ onClick={() => setActive(span)}
+ className="mt-1 flex items-center gap-1 text-[11px] font-medium text-accent hover:text-deep"
+ >
+ <Sparkles size={11} />
+ {i18n.t('editor.rewriteAskAi')}
+ </button>
+ )}
+
+ {active && (
+ <div className="mt-1.5 rounded-lg border border-border bg-card overflow-hidden">
+ {result === null ? (
+ <>
+ <div className="flex items-center gap-1.5 px-2 py-1.5 border-b border-border">
+ <Sparkles size={12} className="shrink-0 text-accent" />
+ <input
+ ref={inputRef}
+ value={instruction}
+ disabled={busy}
+ onChange={(e) => setInstruction(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault();
+ void run(active, instruction);
+ }
+ if (e.key === 'Escape') reset();
+ }}
+ placeholder={i18n.t('editor.rewritePlaceholder')}
+ className="flex-1 min-w-0 bg-transparent p-0 text-[12px] text-foreground placeholder:text-muted-foreground/60 focus:outline-none disabled:opacity-50"
+ />
+ {busy ? (
+ <Loader2 size={12} className="shrink-0 animate-spin text-accent" />
+ ) : (
+ <span className="shrink-0 text-[10px] text-muted-foreground">{i18n.t('editor.rewriteEnter')}</span>
+ )}
+ </div>
+ <div className="py-1">
+ {PRESET_ORDER.map((preset) => (
+ <button
+ key={preset}
+ type="button"
+ disabled={busy}
+ onClick={() => void run(active, REWRITE_PRESETS[preset])}
+ className="w-full px-2.5 py-1 text-left text-[12px] text-foreground hover:bg-secondary disabled:opacity-50"
+ >
+ {i18n.t(PRESET_LABEL_KEYS[preset])}
+ </button>
+ ))}
+ </div>
+ </>
+ ) : (
+ <>
+ <p className="px-2.5 py-2 text-[12px] leading-snug text-foreground whitespace-pre-wrap border-b border-border">
+ {result}
+ </p>
+ <div className="flex items-center gap-1 px-2 py-1.5">
+ <button
+ type="button"
+ onClick={replace}
+ className="rounded-md bg-accent px-2 py-1 text-[11px] font-medium text-primary-foreground hover:bg-deep"
+ >
+ {i18n.t('editor.rewriteReplace')}
+ </button>
+ <button
+ type="button"
+ onClick={reset}
+ className="rounded-md px-2 py-1 text-[11px] font-medium text-muted-foreground hover:bg-secondary"
+ >
+ {i18n.t('editor.rewriteDiscard')}
+ </button>
+ </div>
+ </>
+ )}
+ </div>
+ )}
+
+ <Toast message={error} onDismiss={() => setError(null)} />
+ </>
+ );
+
+ return { onSelect, panel };
+}
diff --git a/src/ui/shared/rewrite-error.ts b/src/ui/shared/rewrite-error.ts
new file mode 100644
index 0000000..188e6f3
--- /dev/null
+++ b/src/ui/shared/rewrite-error.ts
@@ -0,0 +1,11 @@
+import { i18n } from '#imports';
+import type { RewriteError } from '@/lib/messaging';
+
+const REWRITE_ERROR_KEYS = {
+ 'no-api-key': 'editor.rewriteErrorNoApiKey',
+ 'generation-failed': 'editor.rewriteErrorFailed',
+} as const satisfies Record<RewriteError, string>;
+
+export function rewriteErrorMessage(error: RewriteError): string {
+ return i18n.t(REWRITE_ERROR_KEYS[error]);
+}
diff --git a/src/ui/sidepanel/GuideEditor.tsx b/src/ui/sidepanel/GuideEditor.tsx
index 74dbd50..87835b3 100644
--- a/src/ui/sidepanel/GuideEditor.tsx
+++ b/src/ui/sidepanel/GuideEditor.tsx
@@ -17,6 +17,7 @@ import { logger } from '@/lib/logger';
import { sendMessage } from '@/lib/messaging';
import { getMostCommonDomain } from '@/lib/utils';
import { Input } from '@/ui/components/ui/input';
+import { useAskAi } from '@/ui/shared/AskAi';
import EmptyGuideState from '@/ui/shared/EmptyGuideState';
import FaviconImg from '@/ui/shared/FaviconImg';
import { guideDescriptionErrorMessage } from '@/ui/shared/guide-description-error';
@@ -106,6 +107,17 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
editingDescriptionRef.current = false;
}, [data, guideId, description]);
+ const commitGuideDescription = useCallback(
+ (next: string) => {
+ setDescription(next);
+ void updateGuideDescription(guideId, next);
+ setData((prev) => (prev ? { ...prev, guide: { ...prev.guide, description: next } } : prev));
+ },
+ [guideId],
+ );
+
+ const askAi = useAskAi(description, commitGuideDescription, hasApiKey);
+
const handleGenerateDescription = useCallback(async () => {
setGenerating(true);
setDescriptionError(null);
@@ -264,6 +276,7 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
onFocus={() => {
editingDescriptionRef.current = true;
}}
+ onSelect={askAi.onSelect}
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"
@@ -284,6 +297,7 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
: i18n.t('editor.generateDescription')}
</button>
)}
+ {askAi.panel}
<Toast message={descriptionError} onDismiss={() => setDescriptionError(null)} />
</div>
<div className="text-[11px] flex items-center gap-2 text-muted-foreground" style={{ marginLeft: '34px' }}>
@@ -324,6 +338,7 @@ export default function GuideEditor({ guideId, onBack, onGuideMe }: GuideEditorP
onDelete={handleDeleteStep}
onOpenEditor={(stepId, tool) => openInFullView(guideId, { stepId, tool })}
readOnly={!editing}
+ hasApiKey={hasApiKey}
onChanged={loadGuide}
dragHandleProps={
editing
diff --git a/src/ui/sidepanel/StepCard.tsx b/src/ui/sidepanel/StepCard.tsx
index 58d4493..4bd2e72 100644
--- a/src/ui/sidepanel/StepCard.tsx
+++ b/src/ui/sidepanel/StepCard.tsx
@@ -5,6 +5,7 @@ import { replaceScreenshot } from '@/core/guides/service';
import type { Screenshot, Step } from '@/core/guides/types';
import { imageDimensions, renderScreenshot } from '@/core/screenshot/render';
import { logger } from '@/lib/logger';
+import { useAskAi } from '@/ui/shared/AskAi';
import ConfirmDialog from '@/ui/shared/ConfirmDialog';
import ImagePlaceholder from '@/ui/shared/ImagePlaceholder';
import ScreenshotView from '@/ui/shared/ScreenshotView';
@@ -27,6 +28,7 @@ interface StepCardProps {
frameRatio?: number;
readOnly?: boolean;
onChanged?: () => void;
+ hasApiKey?: boolean;
}
export default function StepCard({
@@ -40,6 +42,7 @@ export default function StepCard({
frameRatio,
readOnly,
onChanged,
+ hasApiKey,
}: StepCardProps) {
const [description, setDescription] = useState(step.description);
const [dragOver, setDragOver] = useState(false);
@@ -55,6 +58,15 @@ export default function StepCard({
if (description !== step.description) onDescriptionChange(step.id, description);
};
+ const askAi = useAskAi(
+ description,
+ (next) => {
+ setDescription(next);
+ onDescriptionChange(step.id, next);
+ },
+ !readOnly && !step.aiPending && Boolean(hasApiKey),
+ );
+
const handleDelete = () => {
setConfirmDelete(false);
onDelete(step.id);
@@ -146,10 +158,12 @@ export default function StepCard({
value={description}
rows={1}
onChange={(e) => setDescription(e.target.value)}
+ onSelect={askAi.onSelect}
onBlur={handleDescriptionBlur}
/>
)}
</div>
+ {askAi.panel}
<div className="flex items-center justify-end mt-1">
<div className="flex items-center gap-0.5">
{screenshot && (