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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
import { useEffect, useState } from "react";
import { FlatList, Pressable, Text, View } from "react-native";
import { Link } from "expo-router";
import { TailwindResolver } from "@/components/TailwindResolver";
import CustomSafeAreaView from "@/components/ui/CustomSafeAreaView";
import PageTitle from "@/components/ui/PageTitle";
import { api } from "@/lib/trpc";
import { ChevronRight, Triangle } from "lucide-react-native";
import { useBookmarkLists } from "@hoarder/shared-react/hooks/lists";
import { ZBookmarkListTreeNode } from "@hoarder/shared/utils/listUtils";
interface ListLink {
id: string;
logo: string;
name: string;
href: string;
level: number;
parent?: string;
numChildren: number;
collapsed: boolean;
}
function traverseTree(
node: ZBookmarkListTreeNode,
links: ListLink[],
showChildrenOf: Record<string, boolean>,
parent?: string,
level = 0,
) {
links.push({
id: node.item.id,
logo: node.item.icon,
name: node.item.name,
href: `/dashboard/lists/${node.item.id}`,
level,
parent,
numChildren: node.children?.length ?? 0,
collapsed: !showChildrenOf[node.item.id],
});
if (node.children && showChildrenOf[node.item.id]) {
node.children.forEach((child) =>
traverseTree(child, links, showChildrenOf, node.item.id, level + 1),
);
}
}
export default function Lists() {
const [refreshing, setRefreshing] = useState(false);
const { data: lists, isPending } = useBookmarkLists();
const [showChildrenOf, setShowChildrenOf] = useState<Record<string, boolean>>(
{},
);
const apiUtils = api.useUtils();
useEffect(() => {
setRefreshing(isPending);
}, [isPending]);
if (!lists) {
// Add spinner
return <View />;
}
const onRefresh = () => {
apiUtils.lists.list.invalidate();
};
const links: ListLink[] = [
{
id: "fav",
logo: "⭐️",
name: "Favourites",
href: "/dashboard/favourites",
level: 0,
numChildren: 0,
collapsed: false,
},
{
id: "arch",
logo: "🗄️",
name: "Archive",
href: "/dashboard/archive",
level: 0,
numChildren: 0,
collapsed: false,
},
];
Object.values(lists.root).forEach((list) =>
traverseTree(list, links, showChildrenOf),
);
return (
<CustomSafeAreaView>
<FlatList
className="h-full"
ListHeaderComponent={<PageTitle title="Lists" />}
contentContainerStyle={{
gap: 5,
}}
renderItem={(l) => (
<View
className="mx-2 flex flex-row items-center gap-x-2 rounded-xl border border-input bg-white px-4 py-2 dark:bg-accent"
style={{ marginLeft: l.item.level * 20 }}
>
{l.item.numChildren > 0 && (
<Pressable
onPress={() => {
setShowChildrenOf((prev) => ({
...prev,
[l.item.id]: !prev[l.item.id],
}));
}}
>
<TailwindResolver
className="text-foreground"
comp={(style) => (
<Triangle
size={10}
color={style?.color?.toString()}
fill={style?.color?.toString()}
style={{
transform: [
{ rotate: l.item.collapsed ? "90deg" : "180deg" },
],
}}
/>
)}
/>
</Pressable>
)}
<Link asChild key={l.item.id} href={l.item.href} className="flex-1">
<Pressable className="flex flex-row justify-between">
<Text className="text-lg text-accent-foreground">
{l.item.logo} {l.item.name}
</Text>
<ChevronRight color="rgb(0, 122, 255)" />
</Pressable>
</Link>
</View>
)}
data={links}
refreshing={refreshing}
onRefresh={onRefresh}
/>
</CustomSafeAreaView>
);
}
|