diff --git a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts index b095f7c..ac4a136 100644 --- a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts +++ b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredContextData.ts @@ -6,5 +6,6 @@ export interface StarredContextData { setStarred: Dispatch> addStarred: (key: string) => void, removeStarred: (key: string) => void, + toggleStarred: (key: string) => boolean, isStarred: (key: string) => boolean, } diff --git a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx index 297576a..90c9035 100644 --- a/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx +++ b/todoblue/src/app/[lang]/(layout)/(contextStarred)/StarredProvider.tsx @@ -32,12 +32,29 @@ export function StarredProvider({children}: {children: ReactNode}) { }) }, []) + const toggleStarred = useCallback((value: string) => { + setStarred(prev => { + if(!prev) { + return [value] + } + const result = [...prev] + const index = result.indexOf(value) + if(!index) { + result.push(value) + } + else { + result.splice(index, 1) + } + return result + }) + }, []) + const isStarred = useCallback((value: string) => { return starred.indexOf(value) >= 0 }, [starred]) return ( - + {children} )