fix: getThoughtThread parses flat array and builds nested tree — backend returns Vec not nested object
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m51s
test / unit (pull_request) Successful in 16m52s
test / integration (pull_request) Failing after 17m6s
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m51s
test / unit (pull_request) Successful in 16m52s
test / integration (pull_request) Failing after 17m6s
This commit is contained in:
@@ -243,8 +243,22 @@ export const deleteThought = (thoughtId: string, token: string) =>
|
|||||||
export const getThoughtById = (thoughtId: string, token: string | null) =>
|
export const getThoughtById = (thoughtId: string, token: string | null) =>
|
||||||
apiFetch(`/thoughts/${thoughtId}`, {}, ThoughtSchema, token);
|
apiFetch(`/thoughts/${thoughtId}`, {}, ThoughtSchema, token);
|
||||||
|
|
||||||
export const getThoughtThread = (thoughtId: string, token: string | null) =>
|
export const getThoughtThread = async (thoughtId: string, token: string | null): Promise<ThoughtThread> => {
|
||||||
apiFetch(`/thoughts/${thoughtId}/thread`, {}, ThoughtThreadSchema, token);
|
const thoughts = await apiFetch(`/thoughts/${thoughtId}/thread`, {}, z.array(ThoughtSchema), token);
|
||||||
|
type T = z.infer<typeof ThoughtSchema>;
|
||||||
|
const repliesMap: Record<string, T[]> = {};
|
||||||
|
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 ──────────────────────────────────────────────────────────────────
|
// ── Tags ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user