feat(workbench): add manual task refresh
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
ChevronRight,
|
||||
ClipboardList,
|
||||
History,
|
||||
RefreshCw,
|
||||
X,
|
||||
XCircle
|
||||
} from "lucide-react";
|
||||
@@ -100,6 +101,7 @@ type ScheduledConditionFeedProps = {
|
||||
onTaskReconcile?: (taskId: string, actionId: string, expectedVersion: number) => Promise<unknown>;
|
||||
onLoadTaskEvidenceSnapshot?: (taskId: string, evidenceId: string) => Promise<OperationalTaskDataSnapshot>;
|
||||
onTaskRefresh?: () => void;
|
||||
onRefresh?: () => void | Promise<void>;
|
||||
onCreateSchedule?: (input: ScheduleCreateRequest) => Promise<unknown>;
|
||||
};
|
||||
|
||||
@@ -133,6 +135,7 @@ export function ScheduledConditionFeed({
|
||||
onTaskReconcile,
|
||||
onLoadTaskEvidenceSnapshot,
|
||||
onTaskRefresh,
|
||||
onRefresh,
|
||||
onCreateSchedule
|
||||
}: ScheduledConditionFeedProps) {
|
||||
const [uncontrolledExpanded, setUncontrolledExpanded] = useState(false);
|
||||
@@ -152,6 +155,7 @@ export function ScheduledConditionFeed({
|
||||
const [scheduleNextRunAt, setScheduleNextRunAt] = useState(() => formatLocalDateTimeInput(new Date(Date.now() + 15 * 60_000)));
|
||||
const [scheduleSubmitting, setScheduleSubmitting] = useState(false);
|
||||
const [scheduleError, setScheduleError] = useState<string | null>(null);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
const mobileSheet = presentation === "mobile-sheet";
|
||||
const desktopDock = presentation === "desktop-dock";
|
||||
const compactPresentation = mobileSheet || desktopDock;
|
||||
@@ -330,6 +334,23 @@ export function ScheduledConditionFeed({
|
||||
} finally { setScheduleSubmitting(false); }
|
||||
}
|
||||
|
||||
async function handleRefresh() {
|
||||
if (!onRefresh || refreshing) return;
|
||||
setRefreshing(true);
|
||||
try {
|
||||
await onRefresh();
|
||||
} catch (error) {
|
||||
showMapNotice({
|
||||
id: "scheduled-condition-refresh",
|
||||
tone: "error",
|
||||
title: "工况任务刷新失败",
|
||||
message: error instanceof Error ? error.message : "暂时无法重新获取工况任务。"
|
||||
});
|
||||
} finally {
|
||||
setRefreshing(false);
|
||||
}
|
||||
}
|
||||
|
||||
function handleSelectTimelineCondition(conditionId: string) {
|
||||
updateSelectedConditionId(conditionId);
|
||||
if (!expanded) {
|
||||
@@ -449,6 +470,22 @@ export function ScheduledConditionFeed({
|
||||
<CalendarPlus size={17} aria-hidden="true" />
|
||||
</button>
|
||||
) : null}
|
||||
{onRefresh ? (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={refreshing ? "正在刷新工况任务" : "刷新工况任务"}
|
||||
title={refreshing ? "正在刷新" : "刷新工况任务"}
|
||||
disabled={refreshing}
|
||||
onClick={() => void handleRefresh()}
|
||||
className="grid h-10 w-10 shrink-0 place-items-center rounded-lg text-slate-500 transition-colors active:scale-95 disabled:cursor-wait disabled:text-blue-600 [@media(hover:hover)]:hover:bg-blue-50 [@media(hover:hover)]:hover:text-blue-700"
|
||||
>
|
||||
<RefreshCw
|
||||
size={17}
|
||||
aria-hidden="true"
|
||||
className={cn(refreshing && "animate-spin motion-reduce:animate-none")}
|
||||
/>
|
||||
</button>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
aria-expanded={expanded}
|
||||
|
||||
@@ -20,6 +20,7 @@ export type WorkbenchSyncState = {
|
||||
status: "connecting" | "live" | "reconnecting";
|
||||
error: string | null;
|
||||
acknowledgeEventsThrough: (eventSequence: number) => void;
|
||||
refresh: () => Promise<void>;
|
||||
};
|
||||
|
||||
export function useWorkbenchSync(getAccessToken?: AccessTokenProvider): WorkbenchSyncState {
|
||||
@@ -30,9 +31,15 @@ export function useWorkbenchSync(getAccessToken?: AccessTokenProvider): Workbenc
|
||||
const cursorRef = useRef(0);
|
||||
const statusRef = useRef<WorkbenchSyncState["status"]>("connecting");
|
||||
const runtimeRefreshRef = useRef<number | undefined>(undefined);
|
||||
const refreshSnapshotRef = useRef<(() => Promise<void>) | null>(null);
|
||||
const acknowledgeEventsThrough = useCallback((eventSequence: number) => {
|
||||
setEvents((current) => current.filter((event) => event.event_seq > eventSequence));
|
||||
}, []);
|
||||
const refresh = useCallback(async () => {
|
||||
const refreshSnapshot = refreshSnapshotRef.current;
|
||||
if (!refreshSnapshot) throw new Error("工作台同步尚未就绪");
|
||||
await refreshSnapshot();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
@@ -49,6 +56,7 @@ export function useWorkbenchSync(getAccessToken?: AccessTokenProvider): Workbenc
|
||||
setError(null);
|
||||
initialized = true;
|
||||
};
|
||||
refreshSnapshotRef.current = loadSnapshot;
|
||||
|
||||
const connect = async () => {
|
||||
try {
|
||||
@@ -101,11 +109,12 @@ export function useWorkbenchSync(getAccessToken?: AccessTokenProvider): Workbenc
|
||||
}, RECONCILE_INTERVAL_MS);
|
||||
return () => {
|
||||
controller.abort();
|
||||
refreshSnapshotRef.current = null;
|
||||
if (reconnectTimer !== undefined) window.clearTimeout(reconnectTimer);
|
||||
if (reconcileTimer !== undefined) window.clearInterval(reconcileTimer);
|
||||
if (runtimeRefreshRef.current !== undefined) window.clearTimeout(runtimeRefreshRef.current);
|
||||
};
|
||||
}, [getAccessToken]);
|
||||
|
||||
return { snapshot, events, status, error, acknowledgeEventsThrough };
|
||||
return { snapshot, events, status, error, acknowledgeEventsThrough, refresh };
|
||||
}
|
||||
|
||||
@@ -1336,6 +1336,7 @@ export function MapWorkbenchPage({
|
||||
onTaskReconcile={operationalTasks.reconcileAction}
|
||||
onLoadTaskEvidenceSnapshot={operationalTasks.loadEvidenceSnapshot}
|
||||
onTaskRefresh={operationalTasks.refresh}
|
||||
onRefresh={workbenchSync.refresh}
|
||||
onCreateSchedule={handleCreateMockScheduledTask}
|
||||
/>
|
||||
</div>
|
||||
@@ -1483,6 +1484,7 @@ export function MapWorkbenchPage({
|
||||
onTaskReconcile={operationalTasks.reconcileAction}
|
||||
onLoadTaskEvidenceSnapshot={operationalTasks.loadEvidenceSnapshot}
|
||||
onTaskRefresh={operationalTasks.refresh}
|
||||
onRefresh={workbenchSync.refresh}
|
||||
onCreateSchedule={handleCreateMockScheduledTask}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user