summaryrefslogtreecommitdiff
path: root/gui/src/logic/trill/helpers.ts
blob: 8bd1b0c64a8b9d29752f8407187bf89f9b0fcf16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import type { FullNode, Poast } from "@/types/trill";

export function toFlat(n: FullNode): Poast {
  return {
    ...n,
    children: !n.children
      ? []
      : Object.keys(n.children).map((c) => n.children[c].id),
  };
}

type res = { threadChildren: FullNode[]; replies: FullNode[] };
const bunt: res = { threadChildren: [], replies: [] };
export function extractThread(node: FullNode): res {
  if (!node.children) return bunt;
  const r = Object.keys(node.children)
    .sort()
    .reduce((acc, index) => {
      const n = node.children[index];
      // if (typeof n.post === "string") return acc;
      const nn = n as FullNode;
      return n.author !== node.author
        ? { ...acc, replies: [...acc.replies, nn] }
        : {
            ...acc,
            threadChildren: [
              ...acc.threadChildren,
              nn,
              ...extractThread(nn).threadChildren,
            ],
          };
    }, bunt);
  return r;
}