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

refactor: centralize all DB operations in service layer

Luis Ortiz7854793bcfba
diff --git a/src/capture/screenshot.ts b/src/capture/screenshot.ts
index 5af311a..6874963 100644
--- a/src/capture/screenshot.ts
+++ b/src/capture/screenshot.ts
@@ -1,5 +1,5 @@
import { captureVisibleTab } from '@/lib/browser-api';
-import { db } from '@/guides/db';
+import { saveScreenshot } from '@/guides/service';
import type { Screenshot, ElementMeta } from '@/guides/types';
export async function captureAndStore(tabId: number, stepId: string): Promise<Screenshot> {
@@ -8,7 +8,7 @@ export async function captureAndStore(tabId: number, stepId: string): Promise<Sc
const screenshot: Screenshot = {
id: crypto.randomUUID(), stepId, blob, mimeType: 'image/jpeg', width: 0, height: 0,
};
- await db.screenshots.add(screenshot);
+ await saveScreenshot(screenshot);
return screenshot;
}
@@ -43,6 +43,6 @@ export async function captureAnnotated(
};
img.close();
- await db.screenshots.add(screenshot);
+ await saveScreenshot(screenshot);
return screenshot;
}
diff --git a/src/entrypoints/background/step-pipeline.ts b/src/entrypoints/background/step-pipeline.ts
index 7f8b640..b47e38f 100644
--- a/src/entrypoints/background/step-pipeline.ts
+++ b/src/entrypoints/background/step-pipeline.ts
@@ -1,34 +1,30 @@
+import { logger } from '@/lib/logger';
import { localStorage } from '@/lib/browser-api';
import { captureAnnotated } from '@/capture/screenshot';
import { buildFallbackDescription } from '@/capture/step-description';
import { getAIDescription } from '@/capture/ai-description';
-import { db } from '@/guides/db';
+import { createStep, addStepToGuide, updateStepDescription } from '@/guides/service';
import { getActor } from './actor';
import type { Browser } from '#imports';
-import type { ElementMeta } from '@/guides/types';
+import { CaptureState } from '@/capture/machine';
+import type { UserActionData, UserActionResponse } from '@/lib/messaging';
let captureQueue: Promise<void> = Promise.resolve();
function enqueue(fn: () => Promise<void>): void {
captureQueue = captureQueue
.then(fn)
- .catch((err) => console.error('[Mimik] Capture error', err));
-}
-
-interface UserActionData {
- guideId: string;
- action: string;
- elementMeta: ElementMeta;
+ .catch((err) => logger.error(' Capture error', err));
}
export function handleUserAction(
data: UserActionData,
sender: Browser.runtime.MessageSender,
-): { stepId: string } | { ignored: true } | { error: string } {
+): UserActionResponse {
const actor = getActor();
const snap = actor.getSnapshot();
- if (snap.value !== 'recording') {
+ if (snap.value !== CaptureState.RECORDING) {
return { ignored: true };
}
@@ -53,10 +49,10 @@ export function handleUserAction(
screenshotId = screenshot.id;
screenshotBlob = screenshot.blob;
} catch (err) {
- console.warn('[Mimik] Screenshot capture failed, storing step without screenshot', err);
+ logger.warn(' Screenshot capture failed, storing step without screenshot', err);
}
- await db.steps.add({
+ await createStep({
id: stepId,
guideId,
index: stepIndex,
@@ -67,13 +63,7 @@ export function handleUserAction(
screenshotId,
});
- const guide = await db.guides.get(guideId);
- if (guide) {
- await db.guides.update(guideId, {
- stepIds: [...guide.stepIds, stepId],
- updatedAt: Date.now(),
- });
- }
+ await addStepToGuide(guideId, stepId);
if (screenshotBlob) {
(async () => {
@@ -89,11 +79,11 @@ export function handleUserAction(
settings.aiApiKey as string,
);
if (aiDescription) {
- await db.steps.update(stepId, { description: aiDescription });
+ await updateStepDescription(stepId, aiDescription);
}
}
} catch (err) {
- console.error('[Mimik] AI description update failed', err);
+ logger.error(' AI description update failed', err);
}
})();
}
diff --git a/src/guides/service.ts b/src/guides/service.ts
index 20c61db..df72924 100644
--- a/src/guides/service.ts
+++ b/src/guides/service.ts
@@ -1,5 +1,29 @@
import { db } from './db';
-import type { Guide, Step, Screenshot } from './types';
+import type { Guide, Step, Screenshot, RrwebEventChunk } from './types';
+
+export async function createGuide(guideId: string): Promise<Guide> {
+ const guide: Guide = {
+ id: guideId,
+ title: 'Untitled Guide',
+ createdAt: Date.now(),
+ updatedAt: Date.now(),
+ stepIds: [],
+ starred: false,
+ deletedAt: null,
+ };
+ await db.guides.add(guide);
+ return guide;
+}
+
+export async function getGuide(id: string): Promise<{ guide: Guide; steps: Step[]; screenshots: Map<string, Screenshot> } | null> {
+ const guide = await db.guides.get(id);
+ if (!guide) return null;
+ const steps = await db.steps.where('guideId').equals(id).sortBy('index');
+ const screenshotIds = steps.map(s => s.screenshotId).filter(Boolean) as string[];
+ const screenshotRows = await db.screenshots.where('id').anyOf(screenshotIds).toArray();
+ const screenshots = new Map(screenshotRows.map(s => [s.stepId, s]));
+ return { guide, steps, screenshots };
+}
export async function getGuides(): Promise<Guide[]> {
return db.guides.orderBy('updatedAt').reverse()
@@ -16,14 +40,26 @@ export async function getTrashedGuides(): Promise<Guide[]> {
.filter(g => g.deletedAt != null).toArray();
}
-export async function getGuide(id: string): Promise<{ guide: Guide; steps: Step[]; screenshots: Map<string, Screenshot> } | null> {
+export async function updateGuideTitle(id: string, title: string): Promise<void> {
+ await db.guides.update(id, { title, updatedAt: Date.now() });
+}
+
+export async function addStepToGuide(guideId: string, stepId: string): Promise<void> {
+ const guide = await db.guides.get(guideId);
+ if (guide) {
+ await db.guides.update(guideId, {
+ stepIds: [...guide.stepIds, stepId],
+ updatedAt: Date.now(),
+ });
+ }
+}
+
+export async function toggleStar(id: string): Promise<boolean> {
const guide = await db.guides.get(id);
- if (!guide) return null;
- const steps = await db.steps.where('guideId').equals(id).sortBy('index');
- const screenshotIds = steps.map(s => s.screenshotId).filter(Boolean) as string[];
- const screenshotRows = await db.screenshots.where('id').anyOf(screenshotIds).toArray();
- const screenshots = new Map(screenshotRows.map(s => [s.stepId, s]));
- return { guide, steps, screenshots };
+ if (!guide) return false;
+ const starred = !guide.starred;
+ await db.guides.update(id, { starred, updatedAt: Date.now() });
+ return starred;
}
export async function softDeleteGuide(id: string): Promise<void> {
@@ -43,22 +79,6 @@ export async function permanentlyDeleteGuide(id: string): Promise<void> {
await db.guides.delete(id);
}
-export async function toggleStar(id: string): Promise<boolean> {
- const guide = await db.guides.get(id);
- if (!guide) return false;
- const starred = !guide.starred;
- await db.guides.update(id, { starred, updatedAt: Date.now() });
- return starred;
-}
-
-export async function updateGuideTitle(id: string, title: string): Promise<void> {
- await db.guides.update(id, { title, updatedAt: Date.now() });
-}
-
-export async function updateStepDescription(stepId: string, description: string): Promise<void> {
- await db.steps.update(stepId, { description });
-}
-
export async function reorderSteps(guideId: string, orderedStepIds: string[]): Promise<void> {
await db.transaction('rw', db.steps, db.guides, async () => {
for (let i = 0; i < orderedStepIds.length; i++) {
@@ -68,8 +88,16 @@ export async function reorderSteps(guideId: string, orderedStepIds: string[]): P
});
}
-export async function updateScreenshotBlob(screenshotId: string, blob: Blob): Promise<void> {
- await db.screenshots.update(screenshotId, { blob });
+export async function createStep(step: Step): Promise<void> {
+ await db.steps.add(step);
+}
+
+export async function updateStepDescription(stepId: string, description: string): Promise<void> {
+ await db.steps.update(stepId, { description });
+}
+
+export async function getStepsForGuide(guideId: string): Promise<Step[]> {
+ return db.steps.where('guideId').equals(guideId).sortBy('index');
}
export async function deleteStep(guideId: string, stepId: string): Promise<void> {
@@ -96,6 +124,19 @@ export async function getFirstStepUrl(guideId: string): Promise<string | null> {
return steps[0]?.url || null;
}
+export async function saveScreenshot(screenshot: Screenshot): Promise<void> {
+ await db.screenshots.add(screenshot);
+}
+
+export async function updateScreenshotBlob(screenshotId: string, blob: Blob): Promise<void> {
+ await db.screenshots.update(screenshotId, { blob });
+}
+
+export async function getScreenshotsForSteps(stepIds: string[]): Promise<Map<string, Screenshot>> {
+ const rows = await db.screenshots.where('id').anyOf(stepIds).toArray();
+ return new Map(rows.map(s => [s.stepId, s]));
+}
+
export async function getFirstScreenshot(guideId: string): Promise<Screenshot | null> {
const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');
for (const step of steps) {
@@ -106,3 +147,7 @@ export async function getFirstScreenshot(guideId: string): Promise<Screenshot |
}
return null;
}
+
+export async function saveRrwebChunk(chunk: RrwebEventChunk): Promise<void> {
+ await db.rrwebEvents.add(chunk);
+}
diff --git a/src/sidepanel/RecordingView.tsx b/src/sidepanel/RecordingView.tsx
index 2c6c2f5..3da93ab 100644
--- a/src/sidepanel/RecordingView.tsx
+++ b/src/sidepanel/RecordingView.tsx
@@ -1,7 +1,7 @@
import { useState, useEffect, useCallback, useRef } from 'react';
import { Check, Trash2, Square } from 'lucide-react';
import { getActiveTab } from '@/lib/browser-api';
-import { db } from '@/guides/db';
+import { getStepsForGuide, getScreenshotsForSteps, deleteStep, getGuide } from '@/guides/service';
import type { Step, Screenshot } from '@/guides/types';
import ZoomScreenshot from './ZoomScreenshot';
@@ -40,10 +40,9 @@ export default function RecordingView({ guideId, onStop }: RecordingViewProps) {
const bottomRef = useRef<HTMLDivElement>(null);
const loadSteps = useCallback(async () => {
- const allSteps = await db.steps.where('guideId').equals(guideId).sortBy('index');
+ const allSteps = await getStepsForGuide(guideId);
const screenshotIds = allSteps.map(s => s.screenshotId).filter(Boolean) as string[];
- const screenshots = await db.screenshots.where('id').anyOf(screenshotIds).toArray();
- const screenshotMap = new Map(screenshots.map(s => [s.stepId, s]));
+ const screenshotMap = await getScreenshotsForSteps(screenshotIds);
setSteps(prev => {
const oldUrls = new Map(prev.map(p => [p.step.id, p.objectUrl]));
@@ -93,22 +92,7 @@ export default function RecordingView({ guideId, onStop }: RecordingViewProps) {
}, []);
const handleDeleteStep = useCallback(async (stepId: string) => {
- const step = await db.steps.get(stepId);
- if (step?.screenshotId) {
- await db.screenshots.delete(step.screenshotId);
- }
- await db.steps.delete(stepId);
- const guide = await db.guides.get(guideId);
- if (guide) {
- const newStepIds = guide.stepIds.filter(id => id !== stepId);
- await db.guides.update(guideId, { stepIds: newStepIds, updatedAt: Date.now() });
- const remaining = await db.steps.where('guideId').equals(guideId).sortBy('index');
- for (let i = 0; i < remaining.length; i++) {
- if (remaining[i].index !== i) {
- await db.steps.update(remaining[i].id, { index: i });
- }
- }
- }
+ await deleteStep(guideId, stepId);
await loadSteps();
}, [guideId, loadSteps]);