refactor note service interaction

This commit is contained in:
2026-03-04 14:26:30 -05:00
parent 81a710b763
commit 70f935867d
2 changed files with 26 additions and 18 deletions
-10
View File
@@ -1,10 +0,0 @@
<script lang="ts">
import { setNoteService } from '$lib/context/notes.svelte';
// Initialize note service for all notes routes
setNoteService('default-user');
let { children } = $props();
</script>
{@render children()}
+26 -8
View File
@@ -1,22 +1,25 @@
<script lang="ts"> <script lang="ts">
import { goto } from '$app/navigation'; import { pushState } from '$app/navigation';
import { getNoteService } from '$lib/context/notes.svelte'; import { page } from '$app/state';
import { setNoteService } from '$lib/context/notes.svelte';
import { Button } from '$lib/components/ui/button/index.js'; import { Button } from '$lib/components/ui/button/index.js';
import * as Tabs from '$lib/components/ui/tabs/index.js'; import * as Tabs from '$lib/components/ui/tabs/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
import { Separator } from '$lib/components/ui/separator/index.js'; import { Separator } from '$lib/components/ui/separator/index.js';
import { Plus, LayoutGrid, List } from '@lucide/svelte'; import { Plus, LayoutGrid, List } from '@lucide/svelte';
import NotesGrid from './NotesGrid.svelte'; import NotesGrid from './NotesGrid.svelte';
import NoteSearchBar from './NoteSearchBar.svelte'; import NoteSearchBar from './NoteSearchBar.svelte';
import NoteEditor from './NoteEditor.svelte';
// Get note service from context // Get note service from layout context
const noteService = getNoteService(); const noteService = setNoteService('default-user');
let searchQuery = $state(''); let searchQuery = $state('');
let currentFilter = $state<'all' | 'pinned' | 'archived' | 'trash'>('all'); let currentFilter = $state<'all' | 'pinned' | 'archived' | 'trash'>('all');
let view = $state<'grid' | 'list'>('grid'); let view = $state<'grid' | 'list'>('grid');
// Get filtered notes based on current tab // Get filtered notes based on current tab
const filteredNotes = $derived(() => { const filteredNotes = $derived.by(() => {
let notes = noteService.activeNotes; let notes = noteService.activeNotes;
switch (currentFilter) { switch (currentFilter) {
@@ -49,12 +52,18 @@
// Wait a bit for the subscription to update // Wait a bit for the subscription to update
await new Promise((resolve) => setTimeout(resolve, 100)); await new Promise((resolve) => setTimeout(resolve, 100));
goto(`/notes/${note.id}`); pushState(`/notes/${note.id}`, { selectedNoteId: note.id });
} }
function openNote(noteId: string) { function openNote(noteId: string) {
goto(`/notes/${noteId}`); pushState(`/notes/${noteId}`, { selectedNoteId: noteId });
} }
function closeNote() {
history.back();
}
const selectedNoteId = $derived(page.state.selectedNoteId as string | undefined);
</script> </script>
<div class="w-full container mx-auto p-6 space-y-6"> <div class="w-full container mx-auto p-6 space-y-6">
@@ -140,7 +149,7 @@
<!-- Notes Grid --> <!-- Notes Grid -->
<NotesGrid <NotesGrid
notes={filteredNotes()} notes={filteredNotes}
{view} {view}
loading={noteService.loading} loading={noteService.loading}
emptyMessage={searchQuery emptyMessage={searchQuery
@@ -155,3 +164,12 @@
onNoteClick={(note) => openNote(note.id)} onNoteClick={(note) => openNote(note.id)}
/> />
</div> </div>
<!-- Note Editor Modal -->
<Dialog.Root open={!!selectedNoteId} onOpenChange={(open) => !open && closeNote()}>
<Dialog.Content class="w-[90vw]! max-w-6xl! h-[90vh] flex flex-col overflow-hidden">
{#if selectedNoteId}
<NoteEditor noteId={selectedNoteId} {noteService} />
{/if}
</Dialog.Content>
</Dialog.Root>