1
Fork 0
mirror of https://github.com/Steffo99/todocolors.git synced 2024-11-22 16:24:19 +00:00

Add toggleStarred callback to StarredProvider

This commit is contained in:
Steffo 2023-08-09 04:32:25 +02:00
parent 506a27e989
commit 0d731c6d5a
Signed by: steffo
GPG key ID: 2A24051445686895
2 changed files with 19 additions and 1 deletions

View file

@ -6,5 +6,6 @@ export interface StarredContextData {
setStarred: Dispatch<SetStateAction<string[] | undefined>> setStarred: Dispatch<SetStateAction<string[] | undefined>>
addStarred: (key: string) => void, addStarred: (key: string) => void,
removeStarred: (key: string) => void, removeStarred: (key: string) => void,
toggleStarred: (key: string) => boolean,
isStarred: (key: string) => boolean, isStarred: (key: string) => boolean,
} }

View file

@ -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) => { const isStarred = useCallback((value: string) => {
return starred.indexOf(value) >= 0 return starred.indexOf(value) >= 0
}, [starred]) }, [starred])
return ( return (
<StarredContext.Provider value={{starred, setStarred, addStarred, removeStarred, isStarred}}> <StarredContext.Provider value={{starred, setStarred, addStarred, removeStarred, toggleStarred, isStarred}}>
{children} {children}
</StarredContext.Provider> </StarredContext.Provider>
) )