-
Notifications
You must be signed in to change notification settings - Fork 0
/
useListScreen.ts
29 lines (27 loc) · 979 Bytes
/
useListScreen.ts
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
import { StackActions, useNavigation } from "@react-navigation/native";
import { useCallback } from "react";
import { useStore } from "@/components/StoreContext";
import type { CheckListId } from "@/types/check_list";
import type { Item } from "@/types/item";
import { findCheckedItemIdsByCheckListId, findItem } from "@/types/store";
export function useListScreen(checkListId: CheckListId): {
handleListItemOnPress: (item: Item) => () => void;
items: Item[];
} {
const navigation = useNavigation();
const { store } = useStore();
const itemIds = findCheckedItemIdsByCheckListId(store, checkListId);
const items = itemIds
.map((itemId): Item | null => findItem(store, itemId))
.filter((item): item is Item => item !== null);
const handleListItemOnPress = useCallback(
(item: Item) => () => {
navigation.dispatch(StackActions.push("Item", { itemId: item.id }));
},
[navigation]
);
return {
handleListItemOnPress,
items,
};
}