Skip to content

Commit

Permalink
feat(SyncGithub): Disable fetch if data is incomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
PrettyCoffee committed Apr 2, 2024
1 parent 3f687ca commit de108c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/data/gamesExternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { WEEK } from "~/utils/date"
import { parseMarkdownTable } from "~/utils/parseMarkdownTable"

import { gamesAtom } from "./games"
import { githubAtom } from "./github"
import { useGithub } from "./github"
import { playersAtom } from "./players"
import { fetchRepoFile } from "./service/fetchRepoFile"

Expand Down Expand Up @@ -50,14 +50,14 @@ const splitUserStats = (userStats: string) => {
}
export const useGames = () => {
const [games, setGames] = useAtom(externalGamesAtom)
const [{ filePath }] = useAtom(githubAtom)
const { filePath, incomplete } = useGithub()

const refreshGames = useCallback(() => {
setGames(null)
}, [setGames])

useEffect(() => {
if (games != null) return
if (incomplete || games != null) return

void fetchRepoFile(filePath)
.then(text => {
Expand All @@ -79,7 +79,7 @@ export const useGames = () => {
}))
})
.then(setGames)
}, [filePath, games, setGames])
}, [incomplete, filePath, games, setGames])

return { games, refreshGames }
}
Expand Down
10 changes: 9 additions & 1 deletion src/data/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ export const githubAtom = atom<GithubData>({
],
})

const isIncomplete = (github: GithubData) =>
!github.branch ||
!github.filePath ||
!github.repoName ||
!github.repoOwner ||
!github.token

export const useGithub = () => {
const [github, setGithub] = useAtom(githubAtom)
const incomplete = isIncomplete(github)

const setGithubAttribute = <Attribute extends keyof GithubData>(
key: Attribute,
Expand All @@ -34,6 +42,7 @@ export const useGithub = () => {

return {
...github,
incomplete,
setGithubAttribute,
}
}
Expand All @@ -60,7 +69,6 @@ export const onGithubMouseDown = () => {
window.addEventListener("mousemove", onMouseMove)

timeout = setTimeout(() => {
console.log("timeout", holding)
if (!holding.current) return
showGithubOptions.set(prev => !prev)
}, 3000)
Expand Down
4 changes: 3 additions & 1 deletion src/pages/settings/SyncGithub.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Icon } from "~/components/Icon"
import { LoadingData } from "~/components/LoadingData"
import { Button } from "~/components/ui/button"
import { useGames } from "~/data/gamesExternal"
import { useGithub } from "~/data/github"

const useDelayValueChange = <T,>(value: T, delay = 500) => {

Check failure on line 11 in src/pages/settings/SyncGithub.tsx

View workflow job for this annotation

GitHub Actions / build

'useDelayValueChange' is declared but its value is never read.
const [blockedValue, setBlockedValue] = useState(value)
Expand Down Expand Up @@ -45,6 +46,7 @@ const useDelayValueChange = <T,>(value: T, delay = 500) => {
export const SyncGithub = () => {
const [loading, setLoading] = useState(false)
const { games, refreshGames } = useGames()
const { incomplete } = useGithub()

const onRefresh = () => {
refreshGames()
Expand All @@ -61,7 +63,7 @@ export const SyncGithub = () => {
)

return (
<Button variant="flat" onClick={onRefresh}>
<Button variant="flat" onClick={onRefresh} disabled={incomplete}>
<Icon icon={RefreshCw} size="sm" />
Sync data
</Button>
Expand Down

0 comments on commit de108c5

Please sign in to comment.