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
|
import AsyncStorage from '@react-native-async-storage/async-storage/src/AsyncStorage';
import {useStableCallback} from './useStableCallback';
import {useAtom} from 'jotai';
import {createJSONStorage, atomWithStorage} from 'jotai/utils';
const jsonStorage = createJSONStorage<string[]>(() => AsyncStorage);
const favoriteAppNamesAtom = atomWithStorage<string[]>(
'favorites',
[],
jsonStorage,
);
const isSameArray = (a: string[], b: string[]): boolean => {
if (a.length !== b.length) return false;
return a.every((item, index) => item === b[index]);
};
export const useFavorites = () => {
const [favoriteAppNames, setFavoriteAppNames] = useAtom(favoriteAppNamesAtom);
const setFavorites = useStableCallback(
async (
updater: (packageNames: string[]) => Promise<string[]> | string[],
) => {
const newNames = [...new Set(await updater(favoriteAppNames))];
if (isSameArray(newNames, favoriteAppNames)) return newNames;
setFavoriteAppNames(newNames);
return newNames;
},
);
const addToFavorites = useStableCallback(async (packageName: string) => {
setFavorites((names) => [...names, packageName]);
});
const removeFromFavorites = useStableCallback(async (packageName: string) => {
setFavorites((names) => names.filter((name) => name !== packageName));
});
const isFavorite = useStableCallback((packageName: string) =>
favoriteAppNames.includes(packageName),
);
return {
addToFavorites,
favoriteAppNames,
isFavorite,
removeFromFavorites,
setFavorites,
};
};
|