aboutsummaryrefslogtreecommitdiffstats
path: root/apps/mobile/components/bookmarks/ListPickerModal.tsx
blob: 6079e53d1f718b400762ab294bdcccb2f5590aaf (plain) (blame)
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
import React from "react";
import { Pressable, Text, View } from "react-native";
import Checkbox from "expo-checkbox";
import {
  BottomSheetFlatList,
  BottomSheetModal,
  BottomSheetModalProps,
} from "@gorhom/bottom-sheet";

import {
  useAddBookmarkToList,
  useBookmarkLists,
  useRemoveBookmarkFromList,
} from "@hoarder/shared-react/hooks/lists";
import { api } from "@hoarder/shared-react/trpc";

import PageTitle from "../ui/PageTitle";
import { useToast } from "../ui/Toast";

const ListPickerModal = React.forwardRef<
  BottomSheetModal,
  Omit<BottomSheetModalProps, "children"> & {
    bookmarkId: string;
  }
>(({ bookmarkId, ...props }, ref) => {
  const { toast } = useToast();
  const onError = () => {
    toast({
      message: "Something went wrong",
      variant: "destructive",
      showProgress: false,
    });
  };
  const { data: existingLists } = api.lists.getListsOfBookmark.useQuery(
    {
      bookmarkId,
    },
    {
      select: (data) => new Set(data.lists.map((l) => l.id)),
    },
  );
  const { data } = useBookmarkLists();

  const { mutate: addToList } = useAddBookmarkToList({
    onSuccess: () => {
      toast({
        message: `The bookmark has been added to the list!`,
        showProgress: false,
      });
    },
    onError,
  });

  const { mutate: removeToList } = useRemoveBookmarkFromList({
    onSuccess: () => {
      toast({
        message: `The bookmark has been removed from the list!`,
        showProgress: false,
      });
    },
    onError,
  });

  const toggleList = (listId: string) => {
    if (!existingLists) {
      return;
    }
    if (existingLists.has(listId)) {
      removeToList({ bookmarkId, listId });
    } else {
      addToList({ bookmarkId, listId });
    }
  };

  const { allPaths } = data ?? {};
  return (
    <View>
      <BottomSheetModal ref={ref} {...props}>
        <BottomSheetFlatList
          ListHeaderComponent={<PageTitle title="Manage Lists" />}
          className="h-full"
          contentContainerStyle={{
            gap: 5,
          }}
          renderItem={(l) => (
            <View className="mx-2 flex flex-row items-center rounded-xl border border-input bg-white px-4 py-2 dark:bg-accent">
              <Pressable
                key={l.item[l.item.length - 1].id}
                onPress={() => toggleList(l.item[l.item.length - 1].id)}
                className="flex w-full flex-row justify-between"
              >
                <Text className="text-lg text-accent-foreground">
                  {l.item
                    .map((item) => `${item.icon} ${item.name}`)
                    .join(" / ")}
                </Text>
                <Checkbox
                  value={
                    existingLists &&
                    existingLists.has(l.item[l.item.length - 1].id)
                  }
                  onValueChange={() => {
                    toggleList(l.item[l.item.length - 1].id);
                  }}
                />
              </Pressable>
            </View>
          )}
          data={allPaths}
        />
      </BottomSheetModal>
    </View>
  );
});
ListPickerModal.displayName = "ListPickerModal";

export default ListPickerModal;