Skip to content

Commit

Permalink
chore: repolist coding
Browse files Browse the repository at this point in the history
  • Loading branch information
baiwusanyu-c committed Apr 11, 2023
1 parent e4b8265 commit f7bf2ad
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
27 changes: 23 additions & 4 deletions packages/extension/chrome-option/components/Repo-List.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Button, Input, Spin, Tooltip } from 'antd'
import { getRebaseRepo } from '@pr-checker/fetchGit'
import { getAllRepo, getIssuesPR } from '@pr-checker/fetchGit'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useThrottleFn } from 'ahooks'
// TODO: merge data
// TODO: logo data
interface IRepoListProps {
opType: string
Expand All @@ -24,9 +23,9 @@ export const RepoList = (props: IRepoListProps) => {
const repoListCache = useRef<IRepoWithPRs[]>([])
const [loading, setLoading] = useState(false)
useEffect(() => {
setLoading(true)
if (props.opType === 'rebase') {
setLoading(true)
getRebaseRepo(props.token, props.userName)
getIssuesPR(props.token, props.userName)
.then((res) => {
// 使用 map 避免重复遍历
const repos = new Map<string, IRepoWithPRs>()
Expand All @@ -52,6 +51,25 @@ export const RepoList = (props: IRepoListProps) => {
setLoading(false)
})
}
// merge 模式 我们只获取仓库信息
if (props.opType === 'merge') {
getAllRepo(props.token).then((res) => {
const hasIssuesRepo = res.filter(val => val.open_issues_count > 0 && !val.fork)
const repos = new Map<string, IRepoWithPRs>()
hasIssuesRepo.forEach((val) => {
const repoName = val.url.split('/').pop()!
const repoUName = val.url.split('repos/').pop()!
const repoURL = val.url
repos.set(repoURL, { name: repoName, url: repoURL, uname: repoUName, pullRequests: [val] })
})
// 将 map 转成数组,并设置到 state 里
const resRepo = [...repos.values()]
setRepoList(resRepo)
repoListCache.current = resRepo
}).finally(() => {
setLoading(false)
})
}
}, [props.opType, props.token, props.userName])

const [activeIndex, setActiveIndex] = useState(-1)
Expand Down Expand Up @@ -85,6 +103,7 @@ export const RepoList = (props: IRepoListProps) => {
return item.name.toLowerCase().includes(searchParams.toLowerCase())
})
setRepoList(filterRes)
setActiveIndex(-1)
}

const { run } = useThrottleFn(
Expand Down
22 changes: 13 additions & 9 deletions packages/extension/chrome-popup/view/PopupPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ export const PopupPage = () => {
const [opType, setOpType] = useState('')
const { setItem, CACHE_KEYS, getItem } = useStorage()

const onFinish = useCallback(async(values) => {
// 存储操作类型和 TOKEN
await Promise.all([
setItem(CACHE_KEYS.OP_TYPE, opType),
setItem(CACHE_KEYS.TOKEN, values.token),
])
chrome.runtime.openOptionsPage()
}, [CACHE_KEYS.OP_TYPE, CACHE_KEYS.TOKEN, opType, setItem])

const [loading, setLoading] = useState(false)
const [showInput, setShowInput] = useState(true)
const [userInfo, setUserInfo] = useState({
Expand Down Expand Up @@ -67,6 +58,19 @@ export const PopupPage = () => {
await setItem(CACHE_KEYS.OP_TYPE, opType)
chrome.runtime.openOptionsPage()
}, [CACHE_KEYS.OP_TYPE, setItem])

const onFinish = useCallback(async(values) => {
const userInfo = await getItem(CACHE_KEYS.USER_INFO)
if (!userInfo)
await getUserData(values.token)

// 存储操作类型和 TOKEN
await Promise.all([
setItem(CACHE_KEYS.OP_TYPE, opType),
setItem(CACHE_KEYS.TOKEN, values.token),
])
chrome.runtime.openOptionsPage()
}, [CACHE_KEYS.OP_TYPE, CACHE_KEYS.TOKEN, CACHE_KEYS.USER_INFO, opType, setItem, getItem, getUserData])
return (
<div className="h-240px w-380px p-4 login" style={{ backgroundImage: `url(${loginBg})` }}>
<div className="flex items-center justify-between mb-2">
Expand Down
15 changes: 14 additions & 1 deletion packages/fetch-git/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function getUserInfo(token: string) {
return res
}

export async function getRebaseRepo(token: string, username: string) {
export async function getIssuesPR(token: string, username: string) {
const res = await request(`${baseUrl}/search/issues`, {
method: 'GET',
token,
Expand All @@ -21,3 +21,16 @@ export async function getRebaseRepo(token: string, username: string) {
})
return res
}

export async function getAllRepo(token: string) {
const res = await request(`${baseUrl}/user/repos`, {
method: 'GET',
token,
params: {
type: 'owner',
per_page: 1000,
},
})
return res
}

0 comments on commit f7bf2ad

Please sign in to comment.