-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace react state mangement by zustand
- Loading branch information
Showing
28 changed files
with
445 additions
and
842 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,44 +1,8 @@ | ||
import dayjs from "dayjs"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useAppState } from "~/state/appState.js"; | ||
import { KeyboardDataState } from "~/state/keyboardData.js"; | ||
|
||
import { getCharacterCounts, Keyboard } from "../lib/api.js"; | ||
import { FilterQuery } from "keystats-common/dto/keyboard"; | ||
import { useFetchActions } from "~/state/fetch.js"; | ||
export default function useCharacters(): Pick<KeyboardDataState, "characters"> { | ||
const characters = useAppState((state) => state.characters); | ||
|
||
type Data = Awaited<ReturnType<typeof getCharacterCounts>>; | ||
|
||
export default function useCharacters( | ||
keyboard: Keyboard | null, | ||
date?: dayjs.Dayjs | null, | ||
): [Data | null, () => Promise<void>] { | ||
const [state, setState] = useState<Data | null>(null); | ||
const { setLoading, addError } = useFetchActions(); | ||
|
||
const fetchData = useCallback(async () => { | ||
if (!keyboard) { | ||
return; | ||
} | ||
setLoading(true); | ||
try { | ||
const filters: FilterQuery = {}; | ||
if (date) { | ||
filters.date = date; | ||
} | ||
const data = await getCharacterCounts(keyboard.id, filters); | ||
setState(data); | ||
} catch (error) { | ||
console.error(error); | ||
if (error instanceof Error) { | ||
addError(error); | ||
} | ||
} finally { | ||
setLoading(false); | ||
} | ||
}, [date, keyboard]); | ||
|
||
useEffect(() => { | ||
fetchData(); | ||
}, [fetchData]); | ||
|
||
return [state, fetchData]; | ||
return { characters }; | ||
} |
Oops, something went wrong.