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(guides): group consecutive identical snapshots for display

Luis Ortiz0ab382280918
diff --git a/src/core/guides/__tests__/snapshot-groups.test.ts b/src/core/guides/__tests__/snapshot-groups.test.ts
new file mode 100644
index 0000000..169c654
--- /dev/null
+++ b/src/core/guides/__tests__/snapshot-groups.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from 'vitest';
+import { groupSnapshots } from '../snapshot-groups';
+import type { Snapshot } from '../types';
+
+function snap(id: string, contentHash: string): Snapshot {
+ return { id, guideId: 'g1', createdAt: 0, contentHash, title: '', stepIds: [], steps: [], screenshots: [] };
+}
+
+describe('groupSnapshots', () => {
+ it('returns a plain entry for a lone hash', () => {
+ expect(groupSnapshots([snap('a', 'h1')])).toEqual([{ kind: 'entry', snapshot: snap('a', 'h1') }]);
+ });
+
+ it('collapses a run of matching hashes into one group', () => {
+ const rows = groupSnapshots([snap('a', 'h1'), snap('b', 'h1'), snap('c', 'h1')]);
+
+ expect(rows).toHaveLength(1);
+ expect(rows[0].kind).toBe('group');
+ expect(rows[0].kind === 'group' && rows[0].snapshots).toHaveLength(3);
+ });
+
+ it('keeps runs separate and preserves order', () => {
+ const rows = groupSnapshots([snap('a', 'h1'), snap('b', 'h2'), snap('c', 'h2'), snap('d', 'h3')]);
+
+ expect(rows.map((r) => r.kind)).toEqual(['entry', 'group', 'entry']);
+ });
+
+ it('preserves snapshot identity and order across rows', () => {
+ const rows = groupSnapshots([snap('a', 'h1'), snap('b', 'h2'), snap('c', 'h2'), snap('d', 'h3')]);
+
+ expect(rows[0].kind === 'entry' && rows[0].snapshot.id).toBe('a');
+ expect(rows[1].kind === 'group' && rows[1].snapshots.map((s) => s.id)).toEqual(['b', 'c']);
+ expect(rows[2].kind === 'entry' && rows[2].snapshot.id).toBe('d');
+ });
+
+ it('does not merge non-adjacent matching hashes', () => {
+ const rows = groupSnapshots([snap('a', 'h1'), snap('b', 'h2'), snap('c', 'h1')]);
+
+ expect(rows.map((r) => r.kind)).toEqual(['entry', 'entry', 'entry']);
+ });
+
+ it('returns nothing for an empty list', () => {
+ expect(groupSnapshots([])).toEqual([]);
+ });
+});
diff --git a/src/core/guides/snapshot-groups.ts b/src/core/guides/snapshot-groups.ts
new file mode 100644
index 0000000..3ca0cb7
--- /dev/null
+++ b/src/core/guides/snapshot-groups.ts
@@ -0,0 +1,22 @@
+import type { Snapshot } from './types';
+
+export type SnapshotRow = { kind: 'entry'; snapshot: Snapshot } | { kind: 'group'; snapshots: Snapshot[] };
+
+export function groupSnapshots(snapshots: Snapshot[]): SnapshotRow[] {
+ const rows: SnapshotRow[] = [];
+ let run: Snapshot[] = [];
+
+ const flush = () => {
+ if (run.length === 0) return;
+ rows.push(run.length === 1 ? { kind: 'entry', snapshot: run[0] } : { kind: 'group', snapshots: run });
+ run = [];
+ };
+
+ for (const snapshot of snapshots) {
+ if (run.length > 0 && run[0].contentHash !== snapshot.contentHash) flush();
+ run.push(snapshot);
+ }
+ flush();
+
+ return rows;
+}