All files / shared / components ManualTriggerModal.tsx

39.7% Statements 27/68
28.84% Branches 15/52
21.73% Functions 5/23
42.62% Lines 26/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211        45x   45x                                                                                                                                                                                                           45x 45x   122x 122x 122x 122x 122x 122x 91x       91x                                       122x                                                                                                                                          
import { useState, useRef, useEffect } from 'react'
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Check, ChevronsUpDown } from 'lucide-react'

interface Project {
  id: string
  name: string
}

interface Pipeline {
  id: string
  name: string
}

interface ManualTriggerModalProps {
  open: boolean
  projects: Project[]
  pipelines: Pipeline[]
  onProjectChange: (projectId: string) => void
  onSubmit: (pipelineId: string, issueKey?: string, additionalData?: string) => void
  onCancel: () => void
  initialProjectId?: string
  initialPipelineId?: string
}
 
function ComboboxSelect<T extends { id: string; name: string }>({
  items,
  value,
  onChange,
  placeholder,
  disabled,
  label,
}: {
  items: T[]
  value: string
  onChange: (value: string) => void
  placeholder: string
  disabled?: boolean
  label: string
}) {
  const [open, setOpen] = useState(false)
  const [query, setQuery] = useState('')
  const ref = useRef<HTMLDivElement>(null)
 
  const selected = items.find((i) => i.id === value)
 
  const filtered = query
    ? items.filter((i) => i.name.toLowerCase().includes(query.toLowerCase()))
    : items
 
  useEffect(() => {
    if (!open) setQuery('')
  }, [open])
 
  useEffect(() => {
    function handleClickOutside(e: MouseEvent) {
      if (ref.current && !ref.current.contains(e.target as Node)) {
        setOpen(false)
      }
    }
    document.addEventListener('mousedown', handleClickOutside)
    return () => document.removeEventListener('mousedown', handleClickOutside)
  }, [])
 
  return (
    <div ref={ref} className="relative flex flex-col gap-1 text-sm font-medium">
      {label}
      <button
        type="button"
        onClick={() => !disabled && setOpen(!open)}
        disabled={disabled}
        className="flex items-center justify-between rounded-md border border-input bg-background px-3 py-2 text-sm disabled:opacity-50"
      >
        <span className={selected ? '' : 'text-muted-foreground'}>
          {selected?.name ?? placeholder}
        </span>
        <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
      </button>
      {open && (
        <div className="absolute top-full z-30 mt-1 max-h-60 w-full overflow-auto rounded-md border border-border bg-card shadow-lg">
          <div className="sticky top-0 border-b border-border bg-card p-1">
            <input
              className="w-full rounded-md border border-input bg-background px-3 py-1.5 text-sm outline-none"
              placeholder="Search..."
              value={query}
              onChange={(e) => setQuery(e.target.value)}
              autoFocus
            />
          </div>
          {filtered.length === 0 ? (
            <p className="px-3 py-2 text-sm text-muted-foreground">No results</p>
          ) : (
            filtered.map((item) => (
              <button
                key={item.id}
                type="button"
                className="flex w-full items-center gap-2 px-3 py-2 text-left text-sm hover:bg-accent"
                onClick={() => { onChange(item.id); setOpen(false) }}
              >
                <Check
                  className={`h-4 w-4 ${item.id === value ? 'opacity-100' : 'opacity-0'}`}
                />
                {item.name}
              </button>
            ))
          )}
        </div>
      )}
    </div>
  )
}I

export function ManualTriggerModal({
  open,
  Iprojects,
  pipelines,
  onProjectChange,
  onSubmit,
  onCancel,
  initialProjectId,
  initialPipelineId,
}: ManualTriggerModalProps) {
  const [selectedProject, setSelectedProject] = useState(initialProjectId ?? '')
  const [selectedPipeline, setSelectedPipeline] = useState(initialPipelineId ?? '')
  const [issueKey, setIssueKey] = useState('')
  const [additionalData, setAdditionalData] = useState('')

  useEffect(() => {
    if (initialProjectId && !selectedProject) {
      setSelectedProject(initialProjectId)
      onProjectChange(initialProjectId)
    }
    if (initialPipelineId && !selectedPipeline) {
      setSelectedPipeline(initialPipelineId)
    }
  }, [initialProjectId, initialPipelineId, selectedProject, selectedPipeline, onProjectChange])

  function handleProjectChange(value: string) {
    setSelectedProject(value)
    setSelectedPipeline('')
    onProjectChange(value)
  }
 
  function handleSubmit() {
    if (selectedPipeline) {
      onSubmit(selectedPipeline, issueKey || undefined, additionalData || undefined)
    }
  }
 
  return (
    <Dialog open={open} onOpenChange={(o) => !o && onCancel()}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Trigger Run</DialogTitle>
        </DialogHeader>
        <div className="flex flex-col gap-4 py-4">
          <ComboboxSelect
            label="Project"
            items={projects}
            value={selectedProject}
            onChange={handleProjectChange}
            placeholder="Select project..."
          />
          <ComboboxSelect
            label="Pipeline"
            items={pipelines}
            value={selectedPipeline}
            onChange={(v) => setSelectedPipeline(v)}
            placeholder="Select pipeline..."
            disabled={!selectedProject}
          />
          <label className="flex flex-col gap-1 text-sm font-medium">
            Issue Key
            <input
              className="rounded-md border border-input bg-background px-3 py-2 text-sm"
              value={issueKey}
              onChange={(e) => setIssueKey(e.target.value)}
              placeholder="e.g. PROJ-123"
            />
          </label>
          <label className="flex flex-col gap-1 text-sm font-medium">
            Additional Data
            <textarea
              className="rounded-md border border-input bg-background px-3 py-2 text-sm font-mono"
              value={additionalData}
              onChange={(e) => setAdditionalData(e.target.value)}
              placeholder='{"key": "value"}'
              rows={3}
            />
          </label>
        </div>
        <DialogFooter>
          <Button variant="outline" onClick={onCancel}>
            Cancel
          </Button>
          <Button onClick={handleSubmit} disabled={!selectedPipeline}>
            Run
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}