diff --git a/web/src/lib/notes/pg.svelte.ts b/web/src/lib/notes/pg.svelte.ts index 2448850..000a4de 100644 --- a/web/src/lib/notes/pg.svelte.ts +++ b/web/src/lib/notes/pg.svelte.ts @@ -1,13 +1,21 @@ +import type { MessageData } from '$lib/types/message'; import type { Note, NoteConnection } from './types'; +import { noteToMessage } from './types'; export function createPgNote(noteId: string): NoteConnection { let _note = $state(null); + let _message = $state(null); let _error = $state(''); let _loading = $state(true); let _saving = $state(false); let _saveTimeout: ReturnType | null = null; let _interval: ReturnType | null = null; + function updateMessage(note: Note) { + _note = note; + _message = noteToMessage(note); + } + async function fetchNote() { try { const res = await fetch(`/api/notes/${noteId}`); @@ -18,7 +26,7 @@ export function createPgNote(noteId: string): NoteConnection { const data = await res.json(); // Ikke overskriv lokale endringer mens bruker skriver if (!_saving) { - _note = data; + updateMessage(data); } _error = ''; } catch (e) { @@ -29,10 +37,11 @@ export function createPgNote(noteId: string): NoteConnection { } fetchNote(); - _interval = setInterval(fetchNote, 10000); // Sjeldnere polling for notater + _interval = setInterval(fetchNote, 10000); return { get note() { return _note; }, + get message() { return _message; }, get error() { return _error; }, get loading() { return _loading; }, get saving() { return _saving; }, @@ -40,7 +49,6 @@ export function createPgNote(noteId: string): NoteConnection { async save(updates) { _saving = true; - // Debounce: vent 500ms etter siste endring if (_saveTimeout) clearTimeout(_saveTimeout); _saveTimeout = setTimeout(async () => { @@ -54,7 +62,7 @@ export function createPgNote(noteId: string): NoteConnection { _error = `Lagring feilet: ${res.status}`; } else { const data = await res.json(); - _note = data; + updateMessage(data); _error = ''; } } catch (e) { diff --git a/web/src/lib/notes/types.ts b/web/src/lib/notes/types.ts index 9b82998..65509ce 100644 --- a/web/src/lib/notes/types.ts +++ b/web/src/lib/notes/types.ts @@ -1,3 +1,7 @@ +import type { MessageData } from '$lib/types/message'; + +export type { MessageData }; + export interface Note { id: string; title: string; @@ -5,8 +9,39 @@ export interface Note { updated_at: string; } +/** Konverterer MessageData til Note-grensesnittet som NotesBlock bruker */ +export function messageToNote(msg: MessageData): Note { + return { + id: msg.id, + title: msg.title ?? '', + content: msg.body, + updated_at: msg.updated_at + }; +} + +/** Konverterer Note til MessageData for felles datamodell */ +export function noteToMessage(note: Note): MessageData { + return { + id: note.id, + channel_id: null, + reply_to: null, + author_id: null, + author_name: null, + message_type: 'note', + title: note.title || null, + body: note.content, + pinned: false, + visibility: 'workspace', + created_at: note.updated_at, + updated_at: note.updated_at, + kanban_view: null, + calendar_view: null + }; +} + export interface NoteConnection { readonly note: Note | null; + readonly message: MessageData | null; readonly error: string; readonly loading: boolean; readonly saving: boolean;