|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useCallback, useEffect, useRef, useState } from 'react' |
| 4 | +import { createLogger } from '@sim/logger' |
| 5 | +import { |
| 6 | + Button, |
| 7 | + Modal, |
| 8 | + ModalBody, |
| 9 | + ModalContent, |
| 10 | + ModalFooter, |
| 11 | + ModalHeader, |
| 12 | + Textarea, |
| 13 | +} from '@/components/emcn' |
| 14 | + |
| 15 | +const logger = createLogger('VersionDescriptionModal') |
| 16 | + |
| 17 | +interface VersionDescriptionModalProps { |
| 18 | + open: boolean |
| 19 | + onOpenChange: (open: boolean) => void |
| 20 | + workflowId: string |
| 21 | + version: number |
| 22 | + versionName: string |
| 23 | + currentDescription: string | null | undefined |
| 24 | + onSave: () => Promise<void> |
| 25 | +} |
| 26 | + |
| 27 | +export function VersionDescriptionModal({ |
| 28 | + open, |
| 29 | + onOpenChange, |
| 30 | + workflowId, |
| 31 | + version, |
| 32 | + versionName, |
| 33 | + currentDescription, |
| 34 | + onSave, |
| 35 | +}: VersionDescriptionModalProps) { |
| 36 | + const [description, setDescription] = useState('') |
| 37 | + const [isSaving, setIsSaving] = useState(false) |
| 38 | + const [error, setError] = useState<string | null>(null) |
| 39 | + const [showUnsavedChangesAlert, setShowUnsavedChangesAlert] = useState(false) |
| 40 | + |
| 41 | + const initialDescriptionRef = useRef('') |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (open) { |
| 45 | + const initialDescription = currentDescription || '' |
| 46 | + setDescription(initialDescription) |
| 47 | + initialDescriptionRef.current = initialDescription |
| 48 | + setError(null) |
| 49 | + } |
| 50 | + }, [open, currentDescription]) |
| 51 | + |
| 52 | + const hasChanges = description.trim() !== initialDescriptionRef.current.trim() |
| 53 | + |
| 54 | + const handleCloseAttempt = useCallback(() => { |
| 55 | + if (hasChanges && !isSaving) { |
| 56 | + setShowUnsavedChangesAlert(true) |
| 57 | + } else { |
| 58 | + onOpenChange(false) |
| 59 | + } |
| 60 | + }, [hasChanges, isSaving, onOpenChange]) |
| 61 | + |
| 62 | + const handleDiscardChanges = useCallback(() => { |
| 63 | + setShowUnsavedChangesAlert(false) |
| 64 | + setDescription(initialDescriptionRef.current) |
| 65 | + onOpenChange(false) |
| 66 | + }, [onOpenChange]) |
| 67 | + |
| 68 | + const handleSave = useCallback(async () => { |
| 69 | + if (!workflowId) return |
| 70 | + |
| 71 | + setIsSaving(true) |
| 72 | + setError(null) |
| 73 | + try { |
| 74 | + const res = await fetch(`/api/workflows/${workflowId}/deployments/${version}`, { |
| 75 | + method: 'PATCH', |
| 76 | + headers: { 'Content-Type': 'application/json' }, |
| 77 | + body: JSON.stringify({ description: description.trim() || null }), |
| 78 | + }) |
| 79 | + |
| 80 | + if (res.ok) { |
| 81 | + await onSave() |
| 82 | + onOpenChange(false) |
| 83 | + } else { |
| 84 | + const data = await res.json().catch(() => ({})) |
| 85 | + const message = data.error || 'Failed to save description' |
| 86 | + setError(message) |
| 87 | + logger.error('Failed to save description:', message) |
| 88 | + } |
| 89 | + } catch (err) { |
| 90 | + const message = err instanceof Error ? err.message : 'An unexpected error occurred' |
| 91 | + setError(message) |
| 92 | + logger.error('Error saving description:', err) |
| 93 | + } finally { |
| 94 | + setIsSaving(false) |
| 95 | + } |
| 96 | + }, [workflowId, version, description, onSave, onOpenChange]) |
| 97 | + |
| 98 | + return ( |
| 99 | + <> |
| 100 | + <Modal open={open} onOpenChange={(openState) => !openState && handleCloseAttempt()}> |
| 101 | + <ModalContent className='max-w-[480px]'> |
| 102 | + <ModalHeader> |
| 103 | + <span>Version Description</span> |
| 104 | + </ModalHeader> |
| 105 | + <ModalBody className='space-y-[12px]'> |
| 106 | + <p className='text-[12px] text-[var(--text-secondary)]'> |
| 107 | + {currentDescription ? 'Edit' : 'Add'} a description for{' '} |
| 108 | + <span className='font-medium text-[var(--text-primary)]'>{versionName}</span> |
| 109 | + </p> |
| 110 | + <Textarea |
| 111 | + placeholder='Describe the changes in this deployment version...' |
| 112 | + className='min-h-[120px] resize-none' |
| 113 | + value={description} |
| 114 | + onChange={(e) => setDescription(e.target.value)} |
| 115 | + maxLength={500} |
| 116 | + /> |
| 117 | + <div className='flex items-center justify-between'> |
| 118 | + {error ? <p className='text-[12px] text-[var(--text-error)]'>{error}</p> : <div />} |
| 119 | + <p className='text-[11px] text-[var(--text-tertiary)]'>{description.length}/500</p> |
| 120 | + </div> |
| 121 | + </ModalBody> |
| 122 | + <ModalFooter> |
| 123 | + <Button variant='default' onClick={handleCloseAttempt} disabled={isSaving}> |
| 124 | + Cancel |
| 125 | + </Button> |
| 126 | + <Button variant='tertiary' onClick={handleSave} disabled={isSaving || !hasChanges}> |
| 127 | + {isSaving ? 'Saving...' : 'Save'} |
| 128 | + </Button> |
| 129 | + </ModalFooter> |
| 130 | + </ModalContent> |
| 131 | + </Modal> |
| 132 | + |
| 133 | + <Modal open={showUnsavedChangesAlert} onOpenChange={setShowUnsavedChangesAlert}> |
| 134 | + <ModalContent className='max-w-[400px]'> |
| 135 | + <ModalHeader> |
| 136 | + <span>Unsaved Changes</span> |
| 137 | + </ModalHeader> |
| 138 | + <ModalBody> |
| 139 | + <p className='text-[14px] text-[var(--text-secondary)]'> |
| 140 | + You have unsaved changes. Are you sure you want to discard them? |
| 141 | + </p> |
| 142 | + </ModalBody> |
| 143 | + <ModalFooter> |
| 144 | + <Button variant='default' onClick={() => setShowUnsavedChangesAlert(false)}> |
| 145 | + Keep Editing |
| 146 | + </Button> |
| 147 | + <Button variant='destructive' onClick={handleDiscardChanges}> |
| 148 | + Discard Changes |
| 149 | + </Button> |
| 150 | + </ModalFooter> |
| 151 | + </ModalContent> |
| 152 | + </Modal> |
| 153 | + </> |
| 154 | + ) |
| 155 | +} |
0 commit comments