fix: 修复待办跳转定位错误员工 — 等待debouncedSearch匹配后才打开详情,避免旧数据误选
This commit is contained in:
@@ -70,16 +70,17 @@ export default function Roster() {
|
|||||||
const pagination = rosterData?.pagination || { page, pageSize, total: 0, totalPages: 0 }
|
const pagination = rosterData?.pagination || { page, pageSize, total: 0, totalPages: 0 }
|
||||||
|
|
||||||
// 从 URL query 参数自动定位员工(从待办跳转时)
|
// 从 URL query 参数自动定位员工(从待办跳转时)
|
||||||
|
const employeeParam = searchParams.get('employee')
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const emp = searchParams.get('employee')
|
if (!employeeParam) return
|
||||||
if (emp) {
|
setSearch(employeeParam)
|
||||||
setSearch(emp)
|
// 只有当 debouncedSearch 与参数一致时,搜索结果才真正匹配
|
||||||
// 搜索结果加载后自动打开第一个匹配的员工
|
if (debouncedSearch === employeeParam && !isLoading && employees.length > 0 && !selectedId) {
|
||||||
if (!isLoading && employees.length > 0 && !selectedId) {
|
|
||||||
setSelectedId(employees[0].id)
|
setSelectedId(employees[0].id)
|
||||||
|
// 清除 URL 参数,避免后续搜索时重复触发
|
||||||
|
setSearchParams({}, { replace: true })
|
||||||
}
|
}
|
||||||
}
|
}, [employeeParam, debouncedSearch, isLoading, employees, selectedId, setSearchParams])
|
||||||
}, [searchParams, isLoading, employees, selectedId])
|
|
||||||
|
|
||||||
const addMutation = useMutation({
|
const addMutation = useMutation({
|
||||||
mutationFn: (data: any) => api.post('/employees', data),
|
mutationFn: (data: any) => api.post('/employees', data),
|
||||||
|
|||||||
Reference in New Issue
Block a user