fix(workbench): hold refresh feedback
This commit is contained in:
@@ -10,7 +10,7 @@ import {
|
||||
XCircle
|
||||
} from "lucide-react";
|
||||
import { AnimatePresence, motion } from "motion/react";
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import { MAP_TOOL_PANEL_SURFACE_CLASS_NAME, showMapNotice } from "@/features/map/core";
|
||||
import { isGeneratedScheduledConditionSessionId } from "../data/scheduled-conditions";
|
||||
import type {
|
||||
@@ -62,6 +62,7 @@ const CONDITION_CONTENT_SURFACE_CLASS_NAME =
|
||||
"surface-reading rounded-xl border border-slate-200/70";
|
||||
const CONDITION_CONTROL_SURFACE_CLASS_NAME =
|
||||
"surface-control rounded-xl border border-slate-200/70";
|
||||
const MINIMUM_REFRESH_FEEDBACK_MS = 3_000;
|
||||
|
||||
export type ScheduledConditionFeedPresentation =
|
||||
| "desktop-dock"
|
||||
@@ -156,6 +157,7 @@ export function ScheduledConditionFeed({
|
||||
const [scheduleSubmitting, setScheduleSubmitting] = useState(false);
|
||||
const [scheduleError, setScheduleError] = useState<string | null>(null);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const refreshingRef = useRef(false);
|
||||
const mobileSheet = presentation === "mobile-sheet";
|
||||
const desktopDock = presentation === "desktop-dock";
|
||||
const compactPresentation = mobileSheet || desktopDock;
|
||||
@@ -335,19 +337,32 @@ export function ScheduledConditionFeed({
|
||||
}
|
||||
|
||||
async function handleRefresh() {
|
||||
if (!onRefresh || refreshing) return;
|
||||
if (!onRefresh || refreshingRef.current) return;
|
||||
refreshingRef.current = true;
|
||||
setRefreshing(true);
|
||||
const startedAt = Date.now();
|
||||
let refreshError: unknown = null;
|
||||
try {
|
||||
await onRefresh();
|
||||
} catch (error) {
|
||||
refreshError = error;
|
||||
} finally {
|
||||
const remainingFeedbackMs = MINIMUM_REFRESH_FEEDBACK_MS - (Date.now() - startedAt);
|
||||
if (remainingFeedbackMs > 0) {
|
||||
await new Promise((resolve) => window.setTimeout(resolve, remainingFeedbackMs));
|
||||
}
|
||||
refreshingRef.current = false;
|
||||
setRefreshing(false);
|
||||
}
|
||||
if (refreshError) {
|
||||
showMapNotice({
|
||||
id: "scheduled-condition-refresh",
|
||||
tone: "error",
|
||||
title: "工况任务刷新失败",
|
||||
message: error instanceof Error ? error.message : "暂时无法重新获取工况任务。"
|
||||
message: refreshError instanceof Error
|
||||
? refreshError.message
|
||||
: "暂时无法重新获取工况任务。"
|
||||
});
|
||||
} finally {
|
||||
setRefreshing(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user