From 255ff549a4971d55ac9eb6ad63467465eb5d797c Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Thu, 14 May 2026 17:52:52 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20getThoughtThread=20parses=20flat=20array?= =?UTF-8?q?=20and=20builds=20nested=20tree=20=E2=80=94=20backend=20returns?= =?UTF-8?q?=20Vec=20not=20nested=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thoughts-frontend/lib/api.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/thoughts-frontend/lib/api.ts b/thoughts-frontend/lib/api.ts index 69b0e61..ef743ce 100644 --- a/thoughts-frontend/lib/api.ts +++ b/thoughts-frontend/lib/api.ts @@ -243,8 +243,22 @@ export const deleteThought = (thoughtId: string, token: string) => export const getThoughtById = (thoughtId: string, token: string | null) => apiFetch(`/thoughts/${thoughtId}`, {}, ThoughtSchema, token); -export const getThoughtThread = (thoughtId: string, token: string | null) => - apiFetch(`/thoughts/${thoughtId}/thread`, {}, ThoughtThreadSchema, token); +export const getThoughtThread = async (thoughtId: string, token: string | null): Promise => { + const thoughts = await apiFetch(`/thoughts/${thoughtId}/thread`, {}, z.array(ThoughtSchema), token); + type T = z.infer; + const repliesMap: Record = {}; + for (const t of thoughts) { + if (t.replyToId) { + (repliesMap[t.replyToId] ??= []).push(t); + } + } + function build(t: T): ThoughtThread { + return { ...t, replies: (repliesMap[t.id] ?? []).map(build) }; + } + const root = thoughts.find((t) => t.id === thoughtId) ?? thoughts[0]; + if (!root) throw new Error("Thread not found"); + return build(root); +}; // ── Tags ──────────────────────────────────────────────────────────────────