Fix: bevar SpacetimeDB-meldinger ved refresh, stille feil ved 404
- loadFromPg() merger nå PG-data med eksisterende SpacetimeDB-meldinger i stedet for å overskrive (hindrer at chatten tømmes) - onReaction/onTogglePin kaller bare refresh() ved OK-respons - Fjernet debug-logging Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
e4b0eb77ea
commit
b24f323083
2 changed files with 26 additions and 16 deletions
|
|
@ -19,22 +19,26 @@
|
||||||
const chatCallbacks = {
|
const chatCallbacks = {
|
||||||
onMentionClick: (entityId: string) => goto(`/entities/${entityId}`),
|
onMentionClick: (entityId: string) => goto(`/entities/${entityId}`),
|
||||||
onReaction: async (messageId: string, reaction: string) => {
|
onReaction: async (messageId: string, reaction: string) => {
|
||||||
|
try {
|
||||||
const msg = chat?.messages.find(m => m.id === messageId);
|
const msg = chat?.messages.find(m => m.id === messageId);
|
||||||
const existing = msg?.reactions?.find(r => r.reaction === reaction);
|
const existing = msg?.reactions?.find(r => r.reaction === reaction);
|
||||||
await fetch(`/api/messages/${messageId}/reactions`, {
|
const res = await fetch(`/api/messages/${messageId}/reactions`, {
|
||||||
method: existing?.user_reacted ? 'DELETE' : 'POST',
|
method: existing?.user_reacted ? 'DELETE' : 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ reaction })
|
body: JSON.stringify({ reaction })
|
||||||
});
|
});
|
||||||
await chat?.refresh();
|
if (res.ok) await chat?.refresh();
|
||||||
|
} catch { /* stille feil */ }
|
||||||
},
|
},
|
||||||
onTogglePin: async (messageId: string, pinned: boolean) => {
|
onTogglePin: async (messageId: string, pinned: boolean) => {
|
||||||
await fetch(`/api/messages/${messageId}`, {
|
try {
|
||||||
|
const res = await fetch(`/api/messages/${messageId}`, {
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ pinned })
|
body: JSON.stringify({ pinned })
|
||||||
});
|
});
|
||||||
await chat?.refresh();
|
if (res.ok) await chat?.refresh();
|
||||||
|
} catch { /* stille feil */ }
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -42,13 +42,19 @@ export function createSpacetimeChat(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hent historikk fra PG
|
// Hent historikk fra PG (merger med SpacetimeDB-meldinger som ikke finnes i PG)
|
||||||
async function loadFromPg() {
|
async function loadFromPg() {
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`/api/channels/${channelId}/messages`);
|
const res = await fetch(`/api/channels/${channelId}/messages`);
|
||||||
if (!res.ok) throw new Error('Feil ved lasting');
|
if (!res.ok) throw new Error('Feil ved lasting');
|
||||||
const raw: Record<string, unknown>[] = await res.json();
|
const raw: Record<string, unknown>[] = await res.json();
|
||||||
messages = raw.map(toMessageData);
|
const pgMessages = raw.map(toMessageData);
|
||||||
|
const pgIds = new Set(pgMessages.map(m => m.id));
|
||||||
|
// Behold SpacetimeDB-meldinger som ikke finnes i PG ennå
|
||||||
|
const spacetimeOnly = messages.filter(m => !pgIds.has(m.id));
|
||||||
|
messages = [...pgMessages, ...spacetimeOnly].sort(
|
||||||
|
(a, b) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()
|
||||||
|
);
|
||||||
} catch {
|
} catch {
|
||||||
error = 'Kunne ikke laste meldinger';
|
error = 'Kunne ikke laste meldinger';
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue