Notes: typealignering til MessageData

NoteConnection eksponerer nå `message: MessageData | null` i tillegg til
`note: Note | null`, slik at notater deltar i den felles datamodellen.
Hjelpefunksjoner noteToMessage/messageToNote for konvertering.
NotesBlock uendret — bruker Editor direkte.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
vegard 2026-03-15 22:24:35 +01:00
parent e3e3bbc24f
commit 568c385cd0
2 changed files with 47 additions and 4 deletions

View file

@ -1,13 +1,21 @@
import type { MessageData } from '$lib/types/message';
import type { Note, NoteConnection } from './types'; import type { Note, NoteConnection } from './types';
import { noteToMessage } from './types';
export function createPgNote(noteId: string): NoteConnection { export function createPgNote(noteId: string): NoteConnection {
let _note = $state<Note | null>(null); let _note = $state<Note | null>(null);
let _message = $state<MessageData | null>(null);
let _error = $state(''); let _error = $state('');
let _loading = $state(true); let _loading = $state(true);
let _saving = $state(false); let _saving = $state(false);
let _saveTimeout: ReturnType<typeof setTimeout> | null = null; let _saveTimeout: ReturnType<typeof setTimeout> | null = null;
let _interval: ReturnType<typeof setInterval> | null = null; let _interval: ReturnType<typeof setInterval> | null = null;
function updateMessage(note: Note) {
_note = note;
_message = noteToMessage(note);
}
async function fetchNote() { async function fetchNote() {
try { try {
const res = await fetch(`/api/notes/${noteId}`); const res = await fetch(`/api/notes/${noteId}`);
@ -18,7 +26,7 @@ export function createPgNote(noteId: string): NoteConnection {
const data = await res.json(); const data = await res.json();
// Ikke overskriv lokale endringer mens bruker skriver // Ikke overskriv lokale endringer mens bruker skriver
if (!_saving) { if (!_saving) {
_note = data; updateMessage(data);
} }
_error = ''; _error = '';
} catch (e) { } catch (e) {
@ -29,10 +37,11 @@ export function createPgNote(noteId: string): NoteConnection {
} }
fetchNote(); fetchNote();
_interval = setInterval(fetchNote, 10000); // Sjeldnere polling for notater _interval = setInterval(fetchNote, 10000);
return { return {
get note() { return _note; }, get note() { return _note; },
get message() { return _message; },
get error() { return _error; }, get error() { return _error; },
get loading() { return _loading; }, get loading() { return _loading; },
get saving() { return _saving; }, get saving() { return _saving; },
@ -40,7 +49,6 @@ export function createPgNote(noteId: string): NoteConnection {
async save(updates) { async save(updates) {
_saving = true; _saving = true;
// Debounce: vent 500ms etter siste endring
if (_saveTimeout) clearTimeout(_saveTimeout); if (_saveTimeout) clearTimeout(_saveTimeout);
_saveTimeout = setTimeout(async () => { _saveTimeout = setTimeout(async () => {
@ -54,7 +62,7 @@ export function createPgNote(noteId: string): NoteConnection {
_error = `Lagring feilet: ${res.status}`; _error = `Lagring feilet: ${res.status}`;
} else { } else {
const data = await res.json(); const data = await res.json();
_note = data; updateMessage(data);
_error = ''; _error = '';
} }
} catch (e) { } catch (e) {

View file

@ -1,3 +1,7 @@
import type { MessageData } from '$lib/types/message';
export type { MessageData };
export interface Note { export interface Note {
id: string; id: string;
title: string; title: string;
@ -5,8 +9,39 @@ export interface Note {
updated_at: string; 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 { export interface NoteConnection {
readonly note: Note | null; readonly note: Note | null;
readonly message: MessageData | null;
readonly error: string; readonly error: string;
readonly loading: boolean; readonly loading: boolean;
readonly saving: boolean; readonly saving: boolean;