forked from lightningpixel/modly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseWaitButton.ts
More file actions
51 lines (45 loc) · 2.77 KB
/
Copy pathuseWaitButton.ts
File metadata and controls
51 lines (45 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { useWorkflowRunStore } from './workflowRunStore'
import type { WaitState } from './workflowRunStore'
export interface WaitButtonModel {
waitState: WaitState | undefined
canContinue: boolean
isRunning: boolean
label: 'Retry' | 'Continue'
buttonClass: string
statusText: string
onContinue: () => void
}
// Shared derivation for the Wait Continue/Retry control, used by both the
// canvas node (WaitNode) and the params panel (WaitParamRow). Keeps the two
// renderings in sync — they differ only in markup/sizing, not in logic.
export function useWaitButton(nodeId: string): WaitButtonModel {
const waitState = useWorkflowRunStore((s) => s.waitStates[nodeId])
const runningBranchId = useWorkflowRunStore((s) => s.runningBranchId)
const status = useWorkflowRunStore((s) => s.runState.status)
const continueRun = useWorkflowRunStore((s) => s.continueRun)
const otherBranchRunning = runningBranchId !== null && runningBranchId !== nodeId
// Pre-phase: shared nodes (e.g. Generate Mesh) still running before any branch hands off.
const inPrePhase = status === 'running' && runningBranchId === null
const isRunning = waitState === 'running'
// A pre-phase failure aborts the run before any handoff (status → 'error' while
// this Wait is still 'pending'): it never became runnable, so no Continue.
const runAborted = status === 'error' && waitState === 'pending'
const canContinue = (waitState === 'pending' || waitState === 'done' || waitState === 'error') && !otherBranchRunning && !inPrePhase && !runAborted
const label: 'Retry' | 'Continue' = waitState === 'done' || waitState === 'error' ? 'Retry' : 'Continue'
const buttonClass = waitState === 'error'
? 'bg-red-500/15 border-red-500/30 text-red-400 hover:bg-red-500/25'
: waitState === 'done'
? 'bg-emerald-500/15 border-emerald-500/30 text-emerald-400 hover:bg-emerald-500/25'
: 'bg-amber-500/15 border-amber-500/30 text-amber-400 hover:bg-amber-500/25'
const statusText =
waitState === 'blocked' ? 'Waiting for the previous Wait to finish…' :
waitState === 'running' ? 'Branch in progress…' :
waitState === 'done' ? 'Branch finished — Retry to re-run.' :
waitState === 'error' ? 'Branch failed — Retry to re-run.' :
waitState === 'pending' && runAborted ? 'Run failed upstream — fix the error and run again.' :
waitState === 'pending' && inPrePhase ? 'Waiting for upstream nodes…' :
waitState === 'pending' && otherBranchRunning ? 'Another branch is running…' :
waitState === 'pending' ? 'Workflow paused — click Continue to run this branch.' :
'Pauses the workflow until you click Continue.'
return { waitState, canContinue, isRunning, label, buttonClass, statusText, onContinue: () => continueRun(nodeId) }
}