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: replace prop drilling with Zustand store for library data

Luis Ortizcf1862fd33b8
diff --git a/src/core/guides/service.ts b/src/core/guides/service.ts
index 3c52aea..d763f28 100644
--- a/src/core/guides/service.ts
+++ b/src/core/guides/service.ts
@@ -149,9 +149,10 @@ export async function deleteStep(guideId: string, stepId: string): Promise<void>
}
}
-export async function getFirstStepUrl(guideId: string): Promise<string | null> {
+export async function getGuideDomain(guideId: string): Promise<string> {
+ const { getMostCommonDomain } = await import('@/lib/utils');
const steps = await db.steps.where('guideId').equals(guideId).sortBy('index');
- return steps[0]?.url || null;
+ return getMostCommonDomain(steps);
}
export async function saveScreenshot(screenshot: Screenshot): Promise<void> {
diff --git a/src/stores/fullview.ts b/src/stores/fullview.ts
index ed53ba4..10b6cec 100644
--- a/src/stores/fullview.ts
+++ b/src/stores/fullview.ts
@@ -1,4 +1,5 @@
import { create } from 'zustand';
+import { useShallow } from 'zustand/react/shallow';
import type { Guide, Screenshot, Step } from '@/core/guides/types';
interface GuideExportData {
@@ -10,6 +11,13 @@ interface GuideExportData {
interface FullviewStore {
// Library
+ guides: Guide[];
+ setGuides: (guides: Guide[]) => void;
+ updateGuide: (id: string, patch: Partial<Guide>) => void;
+ thumbnails: Map<string, Screenshot>;
+ setThumbnails: (thumbnails: Map<string, Screenshot>) => void;
+ libraryLoading: boolean;
+ setLibraryLoading: (loading: boolean) => void;
counts: { all: number; starred: number; trash: number };
setCounts: (counts: { all: number; starred: number; trash: number }) => void;
@@ -32,6 +40,13 @@ interface FullviewStore {
}
export const useFullviewStore = create<FullviewStore>((set) => ({
+ guides: [],
+ setGuides: (guides) => set({ guides }),
+ updateGuide: (id, patch) => set((s) => ({ guides: s.guides.map((g) => (g.id === id ? { ...g, ...patch } : g)) })),
+ thumbnails: new Map(),
+ setThumbnails: (thumbnails) => set({ thumbnails }),
+ libraryLoading: true,
+ setLibraryLoading: (libraryLoading) => set({ libraryLoading }),
counts: { all: 0, starred: 0, trash: 0 },
setCounts: (counts) => set({ counts }),
@@ -53,3 +68,7 @@ export const useFullviewStore = create<FullviewStore>((set) => ({
activeStepId: null,
setActiveStepId: (activeStepId) => set({ activeStepId }),
}));
+
+export function useFullview<T>(selector: (s: FullviewStore) => T): T {
+ return useFullviewStore(useShallow(selector));
+}
diff --git a/src/ui/fullview/App.tsx b/src/ui/fullview/App.tsx
index d3181b0..787cfca 100644
--- a/src/ui/fullview/App.tsx
+++ b/src/ui/fullview/App.tsx
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
-import { useFullviewStore } from '@/stores/fullview';
+import { useFullview } from '@/stores/fullview';
import GuideContent from './GuideContent';
import LibraryContent from './LibraryContent';
import { useRoute } from './router';
@@ -8,7 +8,7 @@ import TopNav from './TopNav';
export default function FullViewApp() {
const route = useRoute();
- const toggleSearch = useFullviewStore((s) => s.toggleSearch);
+ const { toggleSearch } = useFullview((s) => ({ toggleSearch: s.toggleSearch }));
useEffect(() => {
const handler = (e: KeyboardEvent) => {
diff --git a/src/ui/fullview/LibraryContent.tsx b/src/ui/fullview/LibraryContent.tsx
index 79202a6..af042b5 100644
--- a/src/ui/fullview/LibraryContent.tsx
+++ b/src/ui/fullview/LibraryContent.tsx
@@ -12,8 +12,8 @@ import {
softDeleteGuide,
toggleStar,
} from '@/core/guides/service';
-import type { Guide, Screenshot } from '@/core/guides/types';
-import { useFullviewStore } from '@/stores/fullview';
+import type { Screenshot } from '@/core/guides/types';
+import { useFullview } from '@/stores/fullview';
import { Button } from '@/ui/components/ui/button';
import GuideGridView from './components/GuideGridView';
import GuideListView from './components/GuideListView';
@@ -30,11 +30,24 @@ const emptyMessages: Record<string, string> = {
};
export default function LibraryContent({ category }: LibraryContentProps) {
- const setCounts = useFullviewStore((s) => s.setCounts);
+ const {
+ guides,
+ setGuides,
+ updateGuide,
+ setThumbnails,
+ libraryLoading: loading,
+ setLibraryLoading: setLoading,
+ setCounts,
+ } = useFullview((s) => ({
+ guides: s.guides,
+ setGuides: s.setGuides,
+ updateGuide: s.updateGuide,
+ setThumbnails: s.setThumbnails,
+ libraryLoading: s.libraryLoading,
+ setLibraryLoading: s.setLibraryLoading,
+ setCounts: s.setCounts,
+ }));
- const [guides, setGuides] = useState<Guide[]>([]);
- const [thumbnails, setThumbnails] = useState<Map<string, Screenshot>>(new Map());
- const [loading, setLoading] = useState(true);
const [display, setDisplay] = useState<'list' | 'grid'>(
() => (localStorage.getItem('mimik-display') as 'list' | 'grid') || 'list',
);
@@ -59,7 +72,7 @@ export default function LibraryContent({ category }: LibraryContentProps) {
}
setThumbnails(thumbMap);
setLoading(false);
- }, [category, setCounts]);
+ }, [category, setCounts, setGuides, setThumbnails, setLoading]);
useEffect(() => {
loadGuides();
@@ -69,13 +82,13 @@ export default function LibraryContent({ category }: LibraryContentProps) {
() =>
onGuidesChanged((event: GuideChangeEvent) => {
if (event.type === 'starred') {
- setGuides((prev) => prev.map((g) => (g.id === event.id ? { ...g, starred: event.starred } : g)));
+ updateGuide(event.id, { starred: event.starred });
refreshCounts();
} else {
loadGuides();
}
}),
- [refreshCounts, loadGuides],
+ [refreshCounts, loadGuides, updateGuide],
);
const toggleDisplay = () => {
@@ -86,7 +99,8 @@ export default function LibraryContent({ category }: LibraryContentProps) {
const handleStar = async (e: React.MouseEvent, id: string) => {
e.stopPropagation();
- setGuides((prev) => prev.map((g) => (g.id === id ? { ...g, starred: !g.starred } : g)));
+ const guide = guides.find((g) => g.id === id);
+ if (guide) updateGuide(id, { starred: !guide.starred });
await toggleStar(id);
await refreshCounts();
};
@@ -130,7 +144,6 @@ export default function LibraryContent({ category }: LibraryContentProps) {
</div>
) : display === 'list' ? (
<GuideListView
- guides={guides}
category={category}
onStar={handleStar}
onTrash={handleTrash}
@@ -138,7 +151,7 @@ export default function LibraryContent({ category }: LibraryContentProps) {
onPermanentDelete={handlePermanentDelete}
/>
) : (
- <GuideGridView guides={guides} thumbnails={thumbnails} />
+ <GuideGridView />
)}
</div>
);
diff --git a/src/ui/fullview/SearchModal.tsx b/src/ui/fullview/SearchModal.tsx
index de5375c..70ed6ce 100644
--- a/src/ui/fullview/SearchModal.tsx
+++ b/src/ui/fullview/SearchModal.tsx
@@ -1,9 +1,9 @@
import { Search, X } from 'lucide-react';
import { useCallback, useEffect, useRef, useState } from 'react';
-import { getFirstStepUrl, getGuides } from '@/core/guides/service';
+import { getGuideDomain, getGuides } from '@/core/guides/service';
import type { Guide } from '@/core/guides/types';
import { getFaviconUrl } from '@/lib/utils';
-import { useFullviewStore } from '@/stores/fullview';
+import { useFullview } from '@/stores/fullview';
import { Dialog, DialogOverlay, DialogPortal } from '@/ui/components/ui/dialog';
import { Input } from '@/ui/components/ui/input';
import KeyboardHints from './components/KeyboardHints';
@@ -16,8 +16,10 @@ interface GuideResult {
}
export default function SearchModal() {
- const open = useFullviewStore((s) => s.searchOpen);
- const setSearchOpen = useFullviewStore((s) => s.setSearchOpen);
+ const { searchOpen: open, setSearchOpen } = useFullview((s) => ({
+ searchOpen: s.searchOpen,
+ setSearchOpen: s.setSearchOpen,
+ }));
const [query, setQuery] = useState('');
const [results, setResults] = useState<GuideResult[]>([]);
@@ -28,8 +30,8 @@ export default function SearchModal() {
const guides = await getGuides();
const withFavicons = await Promise.all(
guides.map(async (guide) => {
- const url = await getFirstStepUrl(guide.id);
- return { guide, favicon: url ? getFaviconUrl(url) : '' };
+ const domain = await getGuideDomain(guide.id);
+ return { guide, favicon: domain ? getFaviconUrl(domain) : '' };
}),
);
setResults(withFavicons);
diff --git a/src/ui/fullview/TopNav.tsx b/src/ui/fullview/TopNav.tsx
index e8f2151..a4c4d5b 100644
--- a/src/ui/fullview/TopNav.tsx
+++ b/src/ui/fullview/TopNav.tsx
@@ -1,5 +1,5 @@
import { ChevronRight, FileText, Search, Star, Trash2 } from 'lucide-react';
-import { useFullviewStore } from '@/stores/fullview';
+import { useFullview } from '@/stores/fullview';
import { Button } from '@/ui/components/ui/button';
import ExportMenu from '@/ui/sidepanel/ExportMenu';
import MascotIcon from './components/MascotIcon';
@@ -17,11 +17,19 @@ const navItems = [
];
export default function TopNav({ route }: TopNavProps) {
- const counts = useFullviewStore((s) => s.counts);
- const guideTitle = useFullviewStore((s) => s.guideTitle);
- const guideStepCount = useFullviewStore((s) => s.guideStepCount);
- const exportData = useFullviewStore((s) => s.guideExportData);
- const setSearchOpen = useFullviewStore((s) => s.setSearchOpen);
+ const {
+ counts,
+ guideTitle,
+ guideStepCount,
+ guideExportData: exportData,
+ setSearchOpen,
+ } = useFullview((s) => ({
+ counts: s.counts,
+ guideTitle: s.guideTitle,
+ guideStepCount: s.guideStepCount,
+ guideExportData: s.guideExportData,
+ setSearchOpen: s.setSearchOpen,
+ }));
return (
<header className="flex items-center gap-5 px-7 h-16 shrink-0 bg-gradient-to-br from-amber to-amber-light">
@@ -41,8 +49,14 @@ export default function TopNav({ route }: TopNavProps) {
<>
<ChevronRight size={14} className="text-foreground opacity-25" />
{guideTitle === 'Untitled Guide' && guideStepCount > 0 ? (
- <span className="text-[13px] font-medium truncate max-w-sm animate-gradient-text bg-[length:300%_100%] bg-clip-text text-transparent bg-gradient-to-r from-muted-foreground via-amber to-muted-foreground">
- Generating title...
+ <span className="flex items-center gap-1">
+ {[0, 1, 2].map((i) => (
+ <span
+ key={i}
+ className="w-[5px] h-[5px] rounded-full bg-foreground animate-bounce"
+ style={{ animationDelay: `${i * 150}ms`, animationDuration: '1.2s' }}
+ />
+ ))}
</span>
) : (
<span className="text-[13px] font-medium truncate max-w-sm text-foreground">{guideTitle}</span>
diff --git a/src/ui/fullview/components/GuideGridView.tsx b/src/ui/fullview/components/GuideGridView.tsx
index 148fd03..753454c 100644
--- a/src/ui/fullview/components/GuideGridView.tsx
+++ b/src/ui/fullview/components/GuideGridView.tsx
@@ -1,5 +1,5 @@
-import type { Guide, Screenshot } from '@/core/guides/types';
import { formatDate } from '@/lib/utils';
+import { useFullview } from '@/stores/fullview';
import ZoomScreenshot from '@/ui/sidepanel/ZoomScreenshot';
import { navigate } from '../router';
@@ -15,12 +15,12 @@ function MimikEyes() {
);
}
-interface GuideGridViewProps {
- guides: Guide[];
- thumbnails: Map<string, Screenshot>;
-}
+export default function GuideGridView() {
+ const { guides, thumbnails } = useFullview((s) => ({
+ guides: s.guides,
+ thumbnails: s.thumbnails,
+ }));
-export default function GuideGridView({ guides, thumbnails }: GuideGridViewProps) {
return (
<div className="grid grid-cols-2 lg:grid-cols-3 gap-4">
{guides.map((guide) => {
diff --git a/src/ui/fullview/components/GuideListView.tsx b/src/ui/fullview/components/GuideListView.tsx
index 8a8420f..f546429 100644
--- a/src/ui/fullview/components/GuideListView.tsx
+++ b/src/ui/fullview/components/GuideListView.tsx
@@ -1,10 +1,9 @@
import { RotateCcw, Star, Trash2 } from 'lucide-react';
-import type { Guide } from '@/core/guides/types';
import { formatDate } from '@/lib/utils';
+import { useFullview } from '@/stores/fullview';
import { navigate } from '../router';
interface GuideListViewProps {
- guides: Guide[];
category: 'all' | 'starred' | 'trash';
onStar: (e: React.MouseEvent, id: string) => void;
onTrash: (e: React.MouseEvent, id: string) => void;
@@ -12,14 +11,9 @@ interface GuideListViewProps {
onPermanentDelete: (e: React.MouseEvent, id: string) => void;
}
-export default function GuideListView({
- guides,
- category,
- onStar,
- onTrash,
- onRestore,
- onPermanentDelete,
-}: GuideListViewProps) {
+export default function GuideListView({ category, onStar, onTrash, onRestore, onPermanentDelete }: GuideListViewProps) {
+ const { guides } = useFullview((s) => ({ guides: s.guides }));
+
return (
<div className="rounded-xl overflow-hidden bg-card border border-border">
{guides.map((guide, idx) => (
diff --git a/src/ui/fullview/components/GuideStepList.tsx b/src/ui/fullview/components/GuideStepList.tsx
index fea7774..523efdc 100644
--- a/src/ui/fullview/components/GuideStepList.tsx
+++ b/src/ui/fullview/components/GuideStepList.tsx
@@ -1,7 +1,8 @@
import { useEffect, useRef, useState } from 'react';
import { reorderSteps } from '@/core/guides/service';
import type { Screenshot, Step } from '@/core/guides/types';
-import { useFullviewStore } from '@/stores/fullview';
+import { useFullview } from '@/stores/fullview';
+import EmptyGuideState from '@/ui/shared/EmptyGuideState';
import StepCard from '@/ui/sidepanel/StepCard';
interface GuideStepListProps {
@@ -23,8 +24,10 @@ export default function GuideStepList({
onBlur,
onReorder,
}: GuideStepListProps) {
- const scrollToStepId = useFullviewStore((s) => s.scrollToStepId);
- const setActiveStepId = useFullviewStore((s) => s.setActiveStepId);
+ const { scrollToStepId, setActiveStepId } = useFullview((s) => ({
+ scrollToStepId: s.scrollToStepId,
+ setActiveStepId: s.setActiveStepId,
+ }));
const [dragIndex, setDragIndex] = useState<number | null>(null);
const [dragOverIndex, setDragOverIndex] = useState<number | null>(null);
@@ -68,7 +71,7 @@ export default function GuideStepList({
};
if (steps.length === 0) {
- return <p className="text-sm text-center py-12 text-warm">No steps in this guide.</p>;
+ return <EmptyGuideState />;
}
return (