|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useId, useMemo, useState } from 'react' |
| 4 | +import { |
| 5 | + ChipDropdown, |
| 6 | + type ChipDropdownOption, |
| 7 | + ChipModal, |
| 8 | + ChipModalBody, |
| 9 | + ChipModalHeader, |
| 10 | + Skeleton, |
| 11 | +} from '@sim/emcn' |
| 12 | +import type { WorkflowDeploymentVersionResponse } from '@/lib/workflows/persistence/utils' |
| 13 | +import { formatVersionLabel } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/components/deploy-modal/components/general/format-version-label' |
| 14 | +import { useDraftWorkflowState } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/hooks/use-draft-workflow-state' |
| 15 | +import { |
| 16 | + CHANGE_LIST_WIDTH_CLASS, |
| 17 | + WorkflowDiffView, |
| 18 | +} from '@/app/workspace/[workspaceId]/w/components/workflow-diff' |
| 19 | +import { useDeploymentVersionState } from '@/hooks/queries/workflows' |
| 20 | + |
| 21 | +/** One side of a comparison: the editor draft or a numbered deployment version. */ |
| 22 | +export type CompareSide = { kind: 'draft' } | { kind: 'version'; version: number } |
| 23 | + |
| 24 | +const DRAFT_OPTION_VALUE = 'draft' |
| 25 | + |
| 26 | +function sideToValue(side: CompareSide): string { |
| 27 | + return side.kind === 'draft' ? DRAFT_OPTION_VALUE : String(side.version) |
| 28 | +} |
| 29 | + |
| 30 | +function valueToSide(value: string): CompareSide { |
| 31 | + return value === DRAFT_OPTION_VALUE |
| 32 | + ? { kind: 'draft' } |
| 33 | + : { kind: 'version', version: Number(value) } |
| 34 | +} |
| 35 | + |
| 36 | +interface CompareVersionsModalProps { |
| 37 | + open: boolean |
| 38 | + onOpenChange: (open: boolean) => void |
| 39 | + workflowId: string |
| 40 | + versions: WorkflowDeploymentVersionResponse[] |
| 41 | + initialBase: CompareSide |
| 42 | + initialTarget: CompareSide |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Full-width comparison of any two versions of this workflow, including the |
| 47 | + * unsaved draft. Both sides are pickable from the header so a reviewer can |
| 48 | + * move between pairs without leaving the view. |
| 49 | + */ |
| 50 | +export function CompareVersionsModal({ |
| 51 | + open, |
| 52 | + onOpenChange, |
| 53 | + workflowId, |
| 54 | + versions, |
| 55 | + initialBase, |
| 56 | + initialTarget, |
| 57 | +}: CompareVersionsModalProps) { |
| 58 | + const descriptionId = useId() |
| 59 | + const [base, setBase] = useState<CompareSide>(initialBase) |
| 60 | + const [target, setTarget] = useState<CompareSide>(initialTarget) |
| 61 | + /* Subscribed only while the modal is open, and only if a side is the draft. */ |
| 62 | + const draftState = useDraftWorkflowState( |
| 63 | + workflowId, |
| 64 | + base.kind === 'draft' || target.kind === 'draft' |
| 65 | + ) |
| 66 | + |
| 67 | + const options = useMemo((): ChipDropdownOption[] => { |
| 68 | + const sorted = [...versions].sort((a, b) => b.version - a.version) |
| 69 | + return [ |
| 70 | + { value: DRAFT_OPTION_VALUE, label: 'Draft' }, |
| 71 | + ...sorted.map((version) => { |
| 72 | + /* A name that merely repeats the number would read as "v2 · v2". */ |
| 73 | + const name = version.name === `v${version.version}` ? null : version.name |
| 74 | + const label = formatVersionLabel(version.version, name) |
| 75 | + return { |
| 76 | + value: String(version.version), |
| 77 | + label: version.isActive ? `${label} (live)` : label, |
| 78 | + } |
| 79 | + }), |
| 80 | + ] |
| 81 | + }, [versions]) |
| 82 | + |
| 83 | + const baseQuery = useDeploymentVersionState( |
| 84 | + workflowId, |
| 85 | + base.kind === 'version' ? base.version : null |
| 86 | + ) |
| 87 | + const targetQuery = useDeploymentVersionState( |
| 88 | + workflowId, |
| 89 | + target.kind === 'version' ? target.version : null |
| 90 | + ) |
| 91 | + |
| 92 | + const baseState = base.kind === 'draft' ? draftState : (baseQuery.data ?? null) |
| 93 | + const targetState = target.kind === 'draft' ? draftState : (targetQuery.data ?? null) |
| 94 | + const isLoading = |
| 95 | + (base.kind === 'version' && baseQuery.isLoading) || |
| 96 | + (target.kind === 'version' && targetQuery.isLoading) |
| 97 | + const loadError = baseQuery.error ?? targetQuery.error |
| 98 | + |
| 99 | + return ( |
| 100 | + <ChipModal |
| 101 | + open={open} |
| 102 | + onOpenChange={onOpenChange} |
| 103 | + srTitle='Compare versions' |
| 104 | + aria-describedby={descriptionId} |
| 105 | + size='full' |
| 106 | + className='h-[92vh] [&>div]:h-full' |
| 107 | + > |
| 108 | + <ChipModalHeader onClose={() => onOpenChange(false)}> |
| 109 | + <div className='flex items-center gap-2'> |
| 110 | + <span>Compare</span> |
| 111 | + <ChipDropdown |
| 112 | + options={options} |
| 113 | + value={sideToValue(base)} |
| 114 | + onChange={(value) => setBase(valueToSide(value))} |
| 115 | + align='start' |
| 116 | + /> |
| 117 | + <span className='text-[var(--text-muted)]'>→</span> |
| 118 | + <ChipDropdown |
| 119 | + options={options} |
| 120 | + value={sideToValue(target)} |
| 121 | + onChange={(value) => setTarget(valueToSide(value))} |
| 122 | + align='start' |
| 123 | + /> |
| 124 | + </div> |
| 125 | + </ChipModalHeader> |
| 126 | + <ChipModalBody fullBleed> |
| 127 | + <p id={descriptionId} className='sr-only'> |
| 128 | + Blocks, fields and connections that differ between the two selected versions. |
| 129 | + </p> |
| 130 | + {loadError ? ( |
| 131 | + <div className='flex h-full items-center justify-center text-[var(--text-error)] text-small'> |
| 132 | + {loadError.message || 'Could not load one of the versions.'} |
| 133 | + </div> |
| 134 | + ) : isLoading || !baseState || !targetState ? ( |
| 135 | + <div className='flex h-full gap-0'> |
| 136 | + <Skeleton className='h-full flex-1 rounded-none' /> |
| 137 | + <Skeleton |
| 138 | + className={`h-full ${CHANGE_LIST_WIDTH_CLASS} rounded-none border-[var(--border)] border-l`} |
| 139 | + /> |
| 140 | + </div> |
| 141 | + ) : ( |
| 142 | + <WorkflowDiffView baseState={baseState} targetState={targetState} /> |
| 143 | + )} |
| 144 | + </ChipModalBody> |
| 145 | + </ChipModal> |
| 146 | + ) |
| 147 | +} |
0 commit comments