|
1 |
| -import { type PostgrestError } from "@supabase/supabase-js"; |
2 |
| -import { useQueryClient } from "@tanstack/react-query"; |
3 |
| -import { isEmpty } from "lodash"; |
4 | 1 | import find from "lodash/find";
|
5 | 2 |
|
| 3 | +import useGetSortBy from "../../hooks/useGetSortBy"; |
6 | 4 | import AlphabeticalIcon from "../../icons/sortByIcons/alphabeticalIcon";
|
7 | 5 | import ClockRewindIcon from "../../icons/sortByIcons/clockRewindIcon";
|
8 | 6 | import DateIcon from "../../icons/sortByIcons/dateIcon";
|
9 | 7 | import TickIcon from "../../icons/tickIcon";
|
10 |
| -import { |
11 |
| - useLoadersStore, |
12 |
| - useSupabaseSession, |
13 |
| -} from "../../store/componentStore"; |
14 |
| -import { |
15 |
| - type CategoriesData, |
16 |
| - type FetchSharedCategoriesData, |
17 |
| - type ProfilesTableTypes, |
18 |
| -} from "../../types/apiTypes"; |
| 8 | +import { useLoadersStore } from "../../store/componentStore"; |
19 | 9 | import {
|
20 | 10 | type BookmarksSortByTypes,
|
21 | 11 | type BookmarkViewCategories,
|
22 | 12 | } from "../../types/componentStoreTypes";
|
23 |
| -import { type CategoryIdUrlTypes } from "../../types/componentTypes"; |
24 | 13 | import { dropdownMenuItemClassName } from "../../utils/commonClassNames";
|
25 |
| -import { |
26 |
| - CATEGORIES_KEY, |
27 |
| - SHARED_CATEGORIES_TABLE_NAME, |
28 |
| - USER_PROFILE, |
29 |
| -} from "../../utils/constants"; |
30 | 14 | import { AriaDropdownMenu } from "../ariaDropdown";
|
31 | 15 | import AriaSelect from "../ariaSelect";
|
32 | 16 | import Spinner from "../spinner";
|
33 | 17 |
|
34 | 18 | type BookmarksSortDropdownTypes = {
|
35 |
| - categoryId: CategoryIdUrlTypes; |
36 | 19 | isDropdown?: boolean;
|
37 | 20 | renderOnlyButton?: boolean;
|
38 | 21 | setBookmarksView: (
|
39 | 22 | value: BookmarksSortByTypes,
|
40 | 23 | type: BookmarkViewCategories,
|
41 | 24 | ) => void;
|
42 |
| - userId: string; |
43 | 25 | };
|
44 | 26 |
|
45 | 27 | const BookmarksSortDropdown = (props: BookmarksSortDropdownTypes) => {
|
46 | 28 | const {
|
47 | 29 | setBookmarksView,
|
48 |
| - categoryId, |
49 |
| - userId, |
50 | 30 | isDropdown = true,
|
51 | 31 | renderOnlyButton = false,
|
52 | 32 | } = props;
|
53 | 33 |
|
54 |
| - const queryClient = useQueryClient(); |
55 |
| - const session = useSupabaseSession((state) => state.session); |
56 |
| - |
57 |
| - const categoryData = queryClient.getQueryData([CATEGORIES_KEY, userId]) as { |
58 |
| - data: CategoriesData[]; |
59 |
| - error: PostgrestError; |
60 |
| - }; |
61 |
| - |
62 |
| - const userProfilesData = queryClient.getQueryData([USER_PROFILE, userId]) as { |
63 |
| - data: ProfilesTableTypes[]; |
64 |
| - error: PostgrestError; |
65 |
| - }; |
66 |
| - |
67 |
| - const sharedCategoriesData = queryClient.getQueryData([ |
68 |
| - SHARED_CATEGORIES_TABLE_NAME, |
69 |
| - ]) as { |
70 |
| - data: FetchSharedCategoriesData[]; |
71 |
| - error: PostgrestError; |
72 |
| - }; |
73 |
| - |
74 | 34 | const isSortByLoading = useLoadersStore((state) => state.isSortByLoading);
|
75 | 35 |
|
76 |
| - const currentCategory = find( |
77 |
| - categoryData?.data, |
78 |
| - (item) => item?.id === categoryId, |
79 |
| - ); |
80 |
| - |
81 |
| - const isInNonCategoryPage = typeof categoryId !== "number"; |
82 |
| - |
83 |
| - const getSortValue = () => { |
84 |
| - if (!isInNonCategoryPage) { |
85 |
| - // user is in a category page |
86 |
| - |
87 |
| - // tells if the user is the category owner |
88 |
| - const isUserTheCategoryOwner = currentCategory?.user_id?.id === userId; |
89 |
| - |
90 |
| - if (isUserTheCategoryOwner) { |
91 |
| - // if user is the category owner then get value from category table |
92 |
| - return currentCategory?.category_views?.sortBy; |
93 |
| - } else { |
94 |
| - // if user is not the category owner then get value from the shared category table |
95 |
| - const sharedCategoryUserData = find( |
96 |
| - sharedCategoriesData?.data, |
97 |
| - (item) => |
98 |
| - item?.category_id === categoryId && |
99 |
| - item?.email === session?.user?.email, |
100 |
| - ); |
101 |
| - |
102 |
| - return sharedCategoryUserData?.category_views?.sortBy; |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 |
| - if (!isEmpty(userProfilesData?.data)) { |
107 |
| - return userProfilesData?.data[0]?.bookmarks_view?.sortBy as string; |
108 |
| - } |
109 |
| - |
110 |
| - return ""; |
111 |
| - }; |
112 |
| - |
113 |
| - const bookmarksSortValue = getSortValue(); |
| 36 | + const { sortBy: bookmarksSortValue } = useGetSortBy(); |
114 | 37 |
|
115 | 38 | const sortOptions = [
|
116 | 39 | // {
|
|
0 commit comments