Skip to content

Commit cb91d3e

Browse files
author
杨纲
committed
fix(start-develop-task-dialog): improve branch selection logic and handle project changes more effectively
1 parent f449107 commit cb91d3e

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

frontend/src/components/console/project/start-develop-task-dialog.tsx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Spinner } from "@/components/ui/spinner"
1212
import { getBrandFromModelName, getInterfaceTypeBadge, getModelHealthBadge, getOwnerTypeBadge, selectHost, selectImage, selectModel } from "@/utils/common"
1313
import { apiRequest } from "@/utils/requestUtils"
1414
import { IconSparkles } from "@tabler/icons-react"
15-
import { useState, useEffect, useMemo, useRef } from "react"
15+
import { useState, useEffect, useMemo, useRef, useCallback } from "react"
1616
import { useNavigate } from "react-router-dom"
1717
import { toast } from "sonner"
1818

@@ -26,7 +26,7 @@ export default function StartDevelopTaskDialog({
2626
open,
2727
onOpenChange,
2828
project
29-
}: StartDevelopTaskDialogProps) {
29+
}: Readonly<StartDevelopTaskDialogProps>) {
3030
const navigate = useNavigate()
3131
const [submitting, setSubmitting] = useState<boolean>(false)
3232
const [branches, setBranches] = useState<string[]>([])
@@ -48,7 +48,7 @@ export default function StartDevelopTaskDialog({
4848
return [economyModel, ...models]
4949
}, [models])
5050

51-
const fetchBranches = async () => {
51+
const fetchBranches = useCallback(async () => {
5252
if (!project?.git_identity_id || !project?.repo_url) {
5353
return
5454
}
@@ -61,14 +61,13 @@ export default function StartDevelopTaskDialog({
6161
}
6262

6363
setLoadingBranches(true)
64-
64+
6565
try {
6666
// 直接使用 full_name 字段
6767
const escapedRepoFullName = project?.full_name || ''
68-
68+
6969
if (!escapedRepoFullName) {
7070
toast.error('无法获取仓库信息')
71-
setLoadingBranches(false)
7271
return
7372
}
7473

@@ -79,15 +78,14 @@ export default function StartDevelopTaskDialog({
7978
if (resp.code === 0 && resp.data) {
8079
const branchList = resp.data.map((b: DomainBranch) => b.name || '').filter(Boolean)
8180
setBranches(branchList)
82-
83-
// 优先选择 main 或 master,否则选择第一个
84-
if (branchList.includes('main')) {
85-
setSelectedBranch('main')
86-
} else if (branchList.includes('master')) {
87-
setSelectedBranch('master')
88-
} else if (branchList.length > 0) {
89-
setSelectedBranch(branchList[0])
90-
}
81+
82+
// 仅在当前选择不存在时才重置,避免刷新覆盖用户选择
83+
setSelectedBranch((prev) => {
84+
if (prev && branchList.includes(prev)) return prev
85+
if (branchList.includes('main')) return 'main'
86+
if (branchList.includes('master')) return 'master'
87+
return branchList[0] || ''
88+
})
9189
} else {
9290
toast.error('获取分支列表失败: ' + resp.message)
9391
}
@@ -98,22 +96,31 @@ export default function StartDevelopTaskDialog({
9896
} finally {
9997
setLoadingBranches(false)
10098
}
101-
}
99+
}, [project])
100+
101+
const prevOpenRef = useRef<boolean>(false)
102+
const prevProjectIdRef = useRef<string | undefined>(undefined)
102103

103-
const prevOpenRef = useRef(false)
104104
useEffect(() => {
105-
if (open) {
106-
const justOpened = !prevOpenRef.current
107-
prevOpenRef.current = true
108-
if (justOpened) {
109-
setUserMessage('')
110-
setSelectedModelId(selectModel(modelsWithEconomy, true))
111-
}
112-
fetchBranches()
113-
} else {
105+
if (!open) {
114106
prevOpenRef.current = false
107+
prevProjectIdRef.current = project?.id
108+
return
109+
}
110+
111+
const projectId = project?.id
112+
const firstOpen = !prevOpenRef.current
113+
const projectChanged = projectId !== prevProjectIdRef.current
114+
115+
if (firstOpen || projectChanged) {
116+
setUserMessage('')
117+
setSelectedModelId(selectModel(modelsWithEconomy, true))
118+
fetchBranches()
115119
}
116-
}, [open, project, modelsWithEconomy])
120+
121+
prevOpenRef.current = true
122+
prevProjectIdRef.current = projectId
123+
}, [open, project?.id, modelsWithEconomy, fetchBranches])
117124

118125
const handleSubmit = async () => {
119126
if (!userMessage.trim()) {

0 commit comments

Comments
 (0)