2023-05-29 01:24:24 +00:00
|
|
|
import React from "react"
|
|
|
|
|
|
|
|
|
|
|
|
export function useGitHubRepositories(user: string) {
|
|
|
|
const [data, setData] = React.useState<any[]>([])
|
|
|
|
|
2023-12-19 03:07:47 +00:00
|
|
|
const load = React.useCallback(
|
2023-05-29 01:24:24 +00:00
|
|
|
async () => {
|
2023-12-19 03:07:47 +00:00
|
|
|
if(data.length > 0) return
|
2023-05-29 01:24:24 +00:00
|
|
|
let page = 1;
|
|
|
|
let count = 100;
|
|
|
|
while(count == 100) {
|
|
|
|
const resp = await fetch(`https://api.github.com/users/${user}/repos?per_page=100&page=${page}&sort=pushed&direction=desc`)
|
|
|
|
const data = await resp.json()
|
|
|
|
count = data.length
|
|
|
|
page += 1
|
|
|
|
setData((old) => [...old, ...data])
|
|
|
|
}
|
|
|
|
},
|
2023-12-19 03:07:47 +00:00
|
|
|
[data]
|
2023-05-29 01:24:24 +00:00
|
|
|
)
|
|
|
|
|
2023-12-19 03:07:47 +00:00
|
|
|
return {data, load}
|
2023-05-29 01:24:24 +00:00
|
|
|
}
|