This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
124 lines (110 loc) · 3.34 KB
/
index.js
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
import { ScrollView } from "react-native";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import { Container, Title } from "../Container.styles";
import WatchingCard from "../../components/WatchingCard";
import {
createEpisodeTable,
openEpisodeDatabase,
selectAllEpisodes,
} from "../../database";
import { sortBy } from "lodash";
import { groupBy } from "../../utils/utils";
import { useFocusEffect } from "@react-navigation/native";
import {
useGetAnimeListQuery,
useGetViewerQuery,
} from "../../utils/graphql/generated";
import { useAccessToken } from "../../contexts";
import { MediaListStatusWithLabel } from "../../utils/constants";
import { utils } from "../../utils";
const ContinueWatching = ({ refreshing, setRefreshing }) => {
const [data, setData] = useState(null);
const [status, setStatus] = useState(MediaListStatusWithLabel[0].value);
const { accessToken, setAccessToken } = useAccessToken();
const { loading: loadingViewer, data: viewerData } = useGetViewerQuery({
skip: !accessToken,
});
const {
loading: loadingAnimeList,
data: animeListData,
refetch,
} = useGetAnimeListQuery({
skip: !accessToken || !viewerData?.Viewer?.id,
variables: {
userId: viewerData?.Viewer?.id,
status,
},
// TODO: figure out how to maintain the list position while also updating the cache
fetchPolicy: "no-cache",
notifyOnNetworkStatusChange: false,
});
const list = useMemo(
() =>
sortBy(
(
(animeListData?.MediaListCollection?.lists &&
animeListData?.MediaListCollection?.lists[0]?.entries) ??
[]
).filter(utils.notEmpty),
(m) => m.media?.title?.english
),
[animeListData]
);
// get Watching data
const newList = !list.length ? data : list.concat(data);
const uniqueList = !list.length
? newList
: Array.from(new Set(newList.map((a) => a?.animeId || a?.media?.id))).map(
(id) => {
return newList.find((a) => a?.animeId || a?.media?.id === id);
}
);
const getContinueWatching = async () => {
const db = await openEpisodeDatabase();
await createEpisodeTable(db);
const select = await selectAllEpisodes(db);
const grouped = groupBy(select, "animeId");
// find highest episode in each group
const highest = Object.keys(grouped).map((key) => {
return grouped[key].reduce((prev, current) =>
prev.number > current.number ? prev : current
);
});
setData(highest);
};
useFocusEffect(
useCallback(() => {
getContinueWatching();
}, [])
);
useEffect(() => {
if (refreshing) {
getContinueWatching();
refetch();
setRefreshing(loadingAnimeList);
}
}, [refreshing]);
if (
!uniqueList ||
uniqueList.length === 0 ||
(uniqueList.length === 1 && uniqueList.includes(null))
)
return null;
return (
<Container>
<Title>Continue Watching</Title>
<ScrollView horizontal={true} showsHorizontalScrollIndicator={false}>
{uniqueList?.map((item, i) => {
return (
<WatchingCard
key={`cw-${i}-${item.id}`}
{...item}
watchedAmount={item?.watchedAmount ? item.watchedAmount : 0}
/>
);
})}
</ScrollView>
</Container>
);
};
export default ContinueWatching;