-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f6b705
commit f701535
Showing
4 changed files
with
114 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { DraftGoalComment, useLocalStorage } from './useLocalStorage'; | ||
|
||
type LSParams = Parameters<typeof useLocalStorage>; | ||
|
||
type DraftComment = DraftGoalComment[keyof DraftGoalComment]; | ||
type DraftGoalCommentKey = 'draftGoalComment'; | ||
type DraftGoalCommentKeyReturn = { | ||
saveDraft: (id: string, draft: DraftComment) => void; | ||
resolveDraft: (id: string) => DraftComment; | ||
removeDraft: (id: string) => void; | ||
}; | ||
|
||
function useLSDraft( | ||
storageKey: DraftGoalCommentKey, | ||
initialValue: Record<string, DraftComment>, | ||
): DraftGoalCommentKeyReturn; | ||
|
||
/** | ||
* before using, you need to set up an overload | ||
*/ | ||
function useLSDraft<KDG extends DraftGoalCommentKey>( | ||
storageKey: KDG, | ||
initialValue: Record<string, LSParams[1]>, | ||
): DraftGoalCommentKeyReturn { | ||
const [draft, setDraft] = useLocalStorage(storageKey, initialValue); | ||
|
||
const saveDraft = useCallback( | ||
(id: string, draft: DraftComment) => { | ||
setDraft((prev) => { | ||
// @ts-expect-error | ||
prev[id] = draft; | ||
return prev; | ||
}); | ||
}, | ||
[setDraft], | ||
); | ||
|
||
const resolveDraft = useCallback( | ||
(id: string) => { | ||
return draft[id]; | ||
}, | ||
[draft], | ||
); | ||
|
||
const removeDraft = useCallback( | ||
(id: string) => { | ||
setDraft((prev) => { | ||
delete prev[id]; | ||
return prev; | ||
}); | ||
}, | ||
[setDraft], | ||
); | ||
|
||
return { | ||
saveDraft, | ||
resolveDraft, | ||
removeDraft, | ||
}; | ||
} | ||
|
||
export { useLSDraft }; |
This file was deleted.
Oops, something went wrong.