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:
parent
e3e3bbc24f
commit
568c385cd0
2 changed files with 47 additions and 4 deletions
|
|
@ -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<Note | null>(null);
|
||||
let _message = $state<MessageData | null>(null);
|
||||
let _error = $state('');
|
||||
let _loading = $state(true);
|
||||
let _saving = $state(false);
|
||||
let _saveTimeout: ReturnType<typeof setTimeout> | null = null;
|
||||
let _interval: ReturnType<typeof setInterval> | 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) {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue