fix(roi-editor):修复 ROI 编辑器中时间段选择器(TimePicker.RangePicker)因连续调用两次状态更新导致的清空问题。
Some checks failed
Python Test / test (push) Has been cancelled

- 新增 `updateWorkingHoursRange` 批量更新函数,将 start/end 作为原子操作同步更新
- 在 onChange 回调中添加 `Array.isArray(dates) && dates.length >= 2` 类型校验
- 避免 React 异步 setState 冲突导致 workingHoursList 意外重置
This commit is contained in:
2026-01-22 17:26:28 +08:00
parent cb46d12cfa
commit 3af7a0f805

View File

@@ -37,12 +37,38 @@ const ROIEditor: React.FC = () => {
const [selectedROI, setSelectedROI] = useState<ROI | null>(null);
const [drawerVisible, setDrawerVisible] = useState(false);
const [form] = Form.useForm();
const [workingHoursList, setWorkingHoursList] = useState<{start: dayjs.Dayjs | null, end: dayjs.Dayjs | null}[]>([]);
const [isDrawing, setIsDrawing] = useState(false);
const [tempPoints, setTempPoints] = useState<number[][]>([]);
const [backgroundImage, setBackgroundImage] = useState<HTMLImageElement | null>(null);
const stageRef = useRef<any>(null);
const addWorkingHours = () => {
setWorkingHoursList([...workingHoursList, { start: null, end: null }]);
};
const removeWorkingHours = (index: number) => {
const newList = workingHoursList.filter((_, i) => i !== index);
setWorkingHoursList(newList);
};
const updateWorkingHours = (index: number, field: 'start' | 'end', value: dayjs.Dayjs | null) => {
const newList = [...workingHoursList];
newList[index] = { ...newList[index], [field]: value };
setWorkingHoursList(newList);
};
const updateWorkingHoursRange = (index: number, start: dayjs.Dayjs | null, end: dayjs.Dayjs | null) => {
setWorkingHoursList(prev => {
const newList = [...prev];
if (newList[index]) {
newList[index] = { start, end };
}
return newList;
});
};
const fetchCameras = async () => {
try {
const res = await axios.get('/api/cameras?enabled_only=true');
@@ -103,9 +129,11 @@ const ROIEditor: React.FC = () => {
const handleSaveROI = async (values: any) => {
if (!selectedCamera || !selectedROI) return;
try {
const workingHours = values.working_hours?.map((item: any) => ({
start: [item.start.hour(), item.start.minute()],
end: [item.end.hour(), item.end.minute()],
const workingHours = workingHoursList
.filter(item => item.start && item.end)
.map(item => ({
start: [item.start!.hour(), item.start!.minute()],
end: [item.end!.hour(), item.end!.minute()],
}));
await axios.put(`/api/camera/${selectedCamera}/roi/${selectedROI.id}`, {
@@ -119,6 +147,7 @@ const ROIEditor: React.FC = () => {
});
message.success('保存成功');
setDrawerVisible(false);
setWorkingHoursList([]);
fetchROIs();
} catch (err: any) {
message.error(`保存失败: ${err.response?.data?.detail || '未知错误'}`);
@@ -226,11 +255,11 @@ const ROIEditor: React.FC = () => {
threshold_sec: roi.threshold_sec,
confirm_sec: roi.confirm_sec,
enabled: roi.enabled,
working_hours: roi.working_hours?.map((wh: WorkingHours) => ({
start: dayjs().hour(wh.start[0]).minute(wh.start[1]),
end: dayjs().hour(wh.end[0]).minute(wh.end[1]),
})),
});
setWorkingHoursList(roi.working_hours?.map((wh: WorkingHours) => ({
start: wh.start ? dayjs().hour(wh.start[0]).minute(wh.start[1]) : null,
end: wh.end ? dayjs().hour(wh.end[0]).minute(wh.end[1]) : null,
})) || []);
setDrawerVisible(true);
}}
onMouseEnter={(e) => {
@@ -387,11 +416,11 @@ const ROIEditor: React.FC = () => {
threshold_sec: roi.threshold_sec,
confirm_sec: roi.confirm_sec,
enabled: roi.enabled,
working_hours: roi.working_hours?.map((wh: WorkingHours) => ({
start: dayjs().hour(wh.start[0]).minute(wh.start[1]),
end: dayjs().hour(wh.end[0]).minute(wh.end[1]),
})),
});
setWorkingHoursList(roi.working_hours?.map((wh: WorkingHours) => ({
start: wh.start ? dayjs().hour(wh.start[0]).minute(wh.start[1]) : null,
end: wh.end ? dayjs().hour(wh.end[0]).minute(wh.end[1]) : null,
})) || []);
setDrawerVisible(true);
}}
>
@@ -426,6 +455,7 @@ const ROIEditor: React.FC = () => {
onClose={() => {
setDrawerVisible(false);
setSelectedROI(null);
setWorkingHoursList([]);
}}
width={400}
>
@@ -458,33 +488,35 @@ const ROIEditor: React.FC = () => {
<InputNumber min={5} style={{ width: '100%' }} />
</Form.Item>
<Divider></Divider>
<Form.List name="working_hours">
{(fields, { add, remove }) => (
<div>
{fields.map((field, index) => (
<Space key={field.key} align="baseline" style={{ display: 'flex', marginBottom: 8 }}>
<Form.Item
{...field}
label={index === 0 ? '时间段' : ''}
style={{ marginBottom: 0 }}
>
<TimePicker.RangePicker format="HH:mm" />
{workingHoursList.map((item, index) => (
<Space key={index} align="baseline" style={{ display: 'flex', marginBottom: 8 }}>
<Form.Item label={index === 0 ? '时间段' : ''} style={{ marginBottom: 0 }}>
<TimePicker.RangePicker
format="HH:mm"
value={item.start && item.end ? [item.start, item.end] : null}
onChange={(dates) => {
if (dates && Array.isArray(dates) && dates.length >= 2 && dates[0] && dates[1]) {
updateWorkingHoursRange(index, dates[0], dates[1]);
} else {
updateWorkingHoursRange(index, null, null);
}
}}
/>
</Form.Item>
<Button
type="link"
danger
onClick={() => remove(field.name)}
onClick={() => removeWorkingHours(index)}
>
</Button>
</Space>
))}
<Button type="dashed" onClick={() => add({ start: null, end: null })} block>
<Button type="dashed" onClick={addWorkingHours} block>
</Button>
</div>
)}
</Form.List>
<Form.Item style={{ fontSize: 12, color: '#999' }}>
使
</Form.Item>
@@ -501,6 +533,7 @@ const ROIEditor: React.FC = () => {
<Button onClick={() => {
setDrawerVisible(false);
setSelectedROI(null);
setWorkingHoursList([]);
}}>
</Button>