Skip to content

Self-hosted homelab infrastructure visualizer — interactive network diagram with live status monitoring

33.4 KB
Raw Blame
import { Fragment, createElement, useState } from 'react'
import modalStyles from './modal-interactive.module.css'
import { RotateCcw, ChevronDown, Palette } from 'lucide-react'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
import { Label } from '@/components/ui/label'
import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue } from '@/components/ui/select'
import { NODE_TYPE_LABELS, type NodeData, type NodeType, type CheckMethod, type NodeTypeStyle } from '@/types'
import { useThemeStore } from '@/stores/themeStore'
import { resolveNodeColors } from '@/utils/nodeColors'
import { ICON_REGISTRY, NODE_TYPE_DEFAULT_ICONS, isBrandIconKey, brandIconSlug, brandIconUrl } from '@/utils/nodeIcons'
import { IconPickerPanel } from './IconPickerPanel'
import { MAX_HANDLES, MAX_PORT_LABEL_LENGTH, MIN_HANDLES, SIDES, clampHandles, handleId, normalizePortLabels, sideDefault, handleCountField, type Side } from '@/utils/handleUtils'
import { isValidParentNode } from '@/utils/virtualEdgeParent'
import { NODE_TYPE_GROUPS, isFurnitureType } from '@/utils/nodeTypeGroups'

// Maps a side to its per-type default field on NodeTypeStyle.
const SIDE_STYLE_KEY: Record<Side, keyof NodeTypeStyle> = {
  top: 'topHandles',
  bottom: 'bottomHandles',
  left: 'leftHandles',
  right: 'rightHandles',
}

/**
 * Compact per-side connection-point control: [− N +] with a typable value.
 * Placed spatially around a node preview (see the Connection Points section).
 */
function CPStepper({ label, side, value, onChange }: {
  label: string
  side: Side
  value: number
  onChange: (v: number) => void
}) {
  const min = MIN_HANDLES
  const labelEl = <span className="text-[10px] text-muted-foreground/80 leading-none">{label}</span>
  const belowLabel = side === 'bottom'
  const btn = 'w-6 h-full flex items-center justify-center text-sm text-muted-foreground hover:text-foreground hover:bg-[#21262d] disabled:opacity-30 disabled:hover:bg-transparent disabled:cursor-default'
  return (
    <div className="flex flex-col items-center gap-1">
      {!belowLabel && labelEl}
      <div className="flex items-center h-7 rounded-md border border-[#30363d] bg-[#0d1117] overflow-hidden">
        <button
          type="button"
          aria-label={`Decrease ${label} connection points`}
          onClick={() => onChange(clampHandles(side, value - 1))}
          disabled={value <= min}
          className={btn}
        >
          −
        </button>
        <input
          type="number"
          min={min}
          max={MAX_HANDLES}
          value={value}
          aria-label={`${label} connection points`}
          onChange={(e) => onChange(clampHandles(side, Number(e.target.value)))}
          className="w-9 h-full bg-transparent text-center text-xs font-mono text-foreground outline-none [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
        />
        <button
          type="button"
          aria-label={`Increase ${label} connection points`}
          onClick={() => onChange(clampHandles(side, value + 1))}
          disabled={value >= MAX_HANDLES}
          className={btn}
        >
          +
        </button>
      </div>
      {belowLabel && labelEl}
    </div>
  )
}

const CHECK_METHODS: CheckMethod[] = ['none', 'ping', 'http', 'https', 'tcp', 'ssh', 'prometheus', 'health']
const CONTAINER_MODE_TYPES: NodeType[] = ['proxmox', 'vm', 'lxc', 'docker_host']
const ZIGBEE_TYPES: NodeType[] = ['zigbee_coordinator', 'zigbee_router', 'zigbee_enddevice']
const ZWAVE_TYPES: NodeType[] = ['zwave_coordinator', 'zwave_router', 'zwave_enddevice']
// Mesh radio devices aren't IP-reachable, so they default to no status check.
const MESH_TYPES: NodeType[] = [...ZIGBEE_TYPES, ...ZWAVE_TYPES]

const CHECK_METHOD_LABELS: Record<CheckMethod, string> = {
  none: 'None',
  ping: 'Ping',
  http: 'HTTP',
  https: 'HTTPS',
  tcp: 'TCP',
  ssh: 'SSH',
  prometheus: 'Prometheus',
  health: 'Health',
}

const DEFAULT_DATA: Partial<NodeData> = {
  type: 'server',
  label: '',
  hostname: '',
  ip: '',
  status: 'unknown',
  check_method: 'ping',
  services: [],
  container_mode: false,
  custom_colors: undefined,
  custom_icon: undefined,
}

interface ParentCandidate {
  id: string
  label: string
  type: NodeType
  /** True when the node has container mode on, so any node can nest inside it. */
  container_mode?: boolean
}

interface NodeModalProps {
  open: boolean
  onClose: () => void
  onSubmit: (data: Partial<NodeData>) => void
  initial?: Partial<NodeData>
  title?: string
  parentCandidates?: ParentCandidate[]
  currentNodeId?: string
  /** Shortcut: open the Custom Style editor for this node's type (canvas-wide). */
  onEditTypeStyle?: (type: NodeType) => void
}

const SIDE_LABELS: Record<Side, string> = { top: 'Top', bottom: 'Bottom', left: 'Left', right: 'Right' }

/**
 * One short text field per connection point, grouped by side, for the label the
 * card prints in place of the point's number — the device's own port numbering
 * (#495). An empty field keeps the number, shown as its placeholder.
 */
function PortLabelFields({ counts, labels, onChange }: {
  counts: Record<Side, number>
  labels: Record<string, string> | undefined
  onChange: (id: string, value: string) => void
}) {
  return (
    <div className="flex flex-col gap-1.5">
      {SIDES.filter((side) => counts[side] > 0).map((side) => (
        <div key={side} className="flex items-start gap-2">
          <span className="w-11 shrink-0 pt-1.5 text-[10px] text-muted-foreground/80">{SIDE_LABELS[side]}</span>
          <div className="flex flex-wrap gap-1">
            {Array.from({ length: counts[side] }, (_, idx) => {
              const id = handleId(side, idx)
              return (
                <input
                  key={id}
                  type="text"
                  value={labels?.[id] ?? ''}
                  placeholder={String(idx + 1)}
                  maxLength={MAX_PORT_LABEL_LENGTH}
                  aria-label={`${SIDE_LABELS[side]} port ${idx + 1} label`}
                  onChange={(e) => onChange(id, e.target.value)}
                  className="w-10 h-6 rounded border border-[#30363d] bg-[#0d1117] text-center text-xs font-mono text-foreground outline-none placeholder:text-muted-foreground/40 focus:border-[#ff6e00]"
                />
              )
            })}
          </div>
        </div>
      ))}
    </div>
  )
}

// NodeModal is always mounted with a key that changes on open/edit, so useState
// initial value is enough - no need for a reset effect.
export function NodeModal({ open, onClose, onSubmit, initial, title = 'Add Node', parentCandidates = [], currentNodeId, onEditTypeStyle }: NodeModalProps) {
  const merged = { ...DEFAULT_DATA, ...initial }
  if (MESH_TYPES.includes((merged.type ?? '') as NodeType)) merged.check_method = 'none'
  const [form, setForm] = useState<Partial<NodeData>>(merged)
  const [iconPickerOpen, setIconPickerOpen] = useState(false)
  const [labelError, setLabelError] = useState(false)
  // Canvas furniture: an annotation, not a device. It has no inventory row, so
  // its text is a `description` on the node rather than the device's `notes`.
  const isFurniture = isFurnitureType(form.type)
  const resolvedNodeColors = resolveNodeColors({ type: form.type ?? 'generic', custom_colors: form.custom_colors })
  const showServicesEnabled = form.custom_colors?.show_services === true
  const hasAppearanceOverrides = Boolean(
    form.custom_colors?.border
    || form.custom_colors?.background
    || form.custom_colors?.icon
  )

  const set = (key: keyof NodeData, value: unknown) =>
    setForm((f) => ({ ...f, [key]: value }))

  const customStyle = useThemeStore((s) => s.customStyle)
  // Effective default count for a side: the per-type style default if set,
  // otherwise the intrinsic side default (top/bottom → 1, left/right → 0).
  const effectiveSideDefault = (side: Side): number => {
    const styleVal = customStyle.nodes[(form.type ?? 'generic') as NodeType]?.[SIDE_STYLE_KEY[side]]
    return clampHandles(side, typeof styleVal === 'number' ? styleVal : sideDefault(side))
  }
  const sideValue = (side: Side): number =>
    clampHandles(side, form[handleCountField(side)] ?? effectiveSideDefault(side))

  const handleSubmit = (e: React.FormEvent) => {
    e.preventDefault()
    if (!form.label?.trim()) {
      setLabelError(true)
      return
    }
    setLabelError(false)
    const selectedType = (form.type ?? 'generic') as NodeType
    const canUseContainerMode = CONTAINER_MODE_TYPES.includes(selectedType)
    let safeParentId = form.parent_id
    if (safeParentId) {
      const parent = parentCandidates.find((n) => n.id === safeParentId)
      if (!parent || !isValidParentNode(selectedType, parent)) safeParentId = undefined
    }
    const isGroupType = selectedType === 'groupRect' || selectedType === 'group'
    onSubmit({
      ...form,
      // Persist the resolved per-side counts so type-style defaults (and
      // untouched sliders) are baked into the node. Skipped for group types.
      ...(isGroupType ? {} : {
        top_handles: sideValue('top'),
        bottom_handles: sideValue('bottom'),
        left_handles: sideValue('left'),
        right_handles: sideValue('right'),
      }),
      // Blank fields fall back to the number, so they are not stored.
      port_labels: normalizePortLabels(form.port_labels),
      parent_id: safeParentId,
      container_mode: canUseContainerMode ? !!form.container_mode : false,
    })
    onClose()
  }

  return (
    <Dialog open={open} onOpenChange={(o) => !o && onClose()}>
      <DialogContent className="bg-[#161b22] border-[#30363d] text-foreground max-w-[calc(100%-2rem)] sm:max-w-3xl max-h-[90vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle className="text-sm font-semibold">{title}</DialogTitle>
        </DialogHeader>

        <form onSubmit={handleSubmit} className="flex flex-col gap-4 mt-2">
          <div className="grid grid-cols-1 md:grid-cols-2 gap-x-6 gap-y-4">
            {/* ── LEFT column: identity & network ── */}
            <div className="flex flex-col gap-4 min-w-0">
            <div className="text-[11px] font-semibold uppercase tracking-wider text-muted-foreground/70 pb-1 border-b border-[#30363d]">Information</div>
            <div className="grid grid-cols-2 gap-3">
            {/* Type + Icon on the same row */}
            <div className="flex flex-col gap-1.5">
              <Label className="text-xs text-muted-foreground">Type</Label>
              <Select value={form.type} onValueChange={(v) => {
                const t = v as NodeType
                setForm((f) => {
                  const next: Partial<NodeData> = { ...f, type: t }
                  if (MESH_TYPES.includes(t)) next.check_method = 'none' as CheckMethod
                  // Drop the parent only if it's no longer a valid target for the
                  // new type — keep container-mode parents (any node can nest).
                  const parent = parentCandidates.find((n) => n.id === f.parent_id)
                  if (f.parent_id && !(parent && isValidParentNode(t, parent))) {
                    next.parent_id = undefined
                  }
                  return next
                })
              }}>
                <SelectTrigger className={`bg-[#21262d] border-[#30363d] text-sm h-8 w-full cursor-pointer ${modalStyles['modal-interactive']} ${modalStyles['modal-radius']}`} aria-label="Node type selector">
                  <SelectValue>{NODE_TYPE_LABELS[(form.type ?? 'server') as NodeType]}</SelectValue>
                </SelectTrigger>
                <SelectContent className="bg-[#21262d] border-[#30363d]">
                  {NODE_TYPE_GROUPS.map((group, i) => (
                    <Fragment key={group.label}>
                      {i > 0 && <SelectSeparator className="bg-[#30363d]" />}
                      <SelectGroup>
                        <SelectLabel className="text-[10px] font-semibold uppercase tracking-wider text-muted-foreground/50 px-2 py-1">
                          {group.label}
                        </SelectLabel>
                        {group.types.map((type) => (
                          <SelectItem key={type} value={type} className="text-sm pl-4">
                            {NODE_TYPE_LABELS[type]}
                          </SelectItem>
                        ))}
                      </SelectGroup>
                    </Fragment>
                  ))}
                </SelectContent>
              </Select>
            </div>

            {/* Icon */}
            <div className="flex flex-col gap-1.5">
              <div className="flex items-center justify-between">
                <Label className="text-xs text-muted-foreground">Icon</Label>
                {form.custom_icon && (
                  <button
                    type="button"
                    onClick={() => { set('custom_icon', undefined); setIconPickerOpen(false) }}
                    className="flex items-center gap-1 text-[10px] text-muted-foreground/60 hover:text-muted-foreground transition-colors"
                  >
                    <RotateCcw size={10} /> Reset
                  </button>
                )}
              </div>
              {/* Trigger button */}
              <button
                type="button"
                onClick={() => setIconPickerOpen((o) => !o)}
                className={`flex items-center justify-between gap-2 h-8 px-3 bg-[#21262d] border border-[#30363d] text-sm transition-colors w-full cursor-pointer ${modalStyles['modal-interactive']} ${modalStyles['modal-radius']}`}
                aria-label="Icon picker trigger"
              >
                <span className="flex items-center gap-2 min-w-0">
                  {(() => {
                    if (isBrandIconKey(form.custom_icon)) {
                      const slug = brandIconSlug(form.custom_icon!)
                      return <><img src={brandIconUrl(slug)} alt={slug} width={13} height={13} className="shrink-0" style={{ width: 13, height: 13, objectFit: 'contain' }} /><span className="text-foreground truncate">{slug}</span></>
                    }
                    const entry = ICON_REGISTRY.find((e) => e.key === form.custom_icon)
                    if (entry) {
                      return <>{createElement(entry.icon, { size: 13, className: 'text-[#00d4ff] shrink-0' })}<span className="text-foreground truncate">{entry.label}</span></>
                    }
                    const defaultIcon = NODE_TYPE_DEFAULT_ICONS[form.type as NodeType] ?? NODE_TYPE_DEFAULT_ICONS.generic
                    return <>{createElement(defaultIcon, { size: 13, className: 'text-muted-foreground shrink-0' })}<span className="text-muted-foreground truncate">Default</span></>
                  })()}
                </span>
                <ChevronDown size={12} className="text-muted-foreground shrink-0" style={{ transform: iconPickerOpen ? 'rotate(180deg)' : undefined, transition: 'transform 0.15s' }} />
              </button>
            </div>
            </div>{/* end Type/Icon subgrid */}

            {/* Inline icon picker - full width, shown below the type+icon row */}
            {iconPickerOpen && (
              <div className="col-span-2">
                <IconPickerPanel
                  value={form.custom_icon}
                  onSelect={(key) => { set('custom_icon', key); setIconPickerOpen(false) }}
                  autoFocus
                />
              </div>
            )}

            {/* Label */}
            <div className="flex flex-col gap-1.5 col-span-2">
              <Label className="text-xs text-muted-foreground">Label *</Label>
              <Input
                value={form.label ?? ''}
                onChange={(e) => { set('label', e.target.value); if (labelError) setLabelError(false) }}
                placeholder="My Server"
                className={`bg-[#21262d] text-sm h-8 ${labelError ? 'border-[#f85149] focus-visible:ring-[#f85149]' : 'border-[#30363d]'} ${modalStyles['modal-radius']}`}
              />
              {labelError && <p className="text-[11px] text-[#f85149]">Label is required</p>}
            </div>

            <div className="grid grid-cols-2 gap-3">
            {/* Hostname */}
            <div className="flex flex-col gap-1.5">
              <Label className="text-xs text-muted-foreground">Hostname</Label>
              <Input
                value={form.hostname ?? ''}
                onChange={(e) => set('hostname', e.target.value)}
                placeholder="server.lan"
                className={`bg-[#21262d] border-[#30363d] font-mono text-sm h-8 ${modalStyles['modal-radius']}`}
              />
            </div>

            {/* IP */}
            <div className="flex flex-col gap-1.5">
              <Label className="text-xs text-muted-foreground">IP Address</Label>
              <Input
                value={form.ip ?? ''}
                onChange={(e) => set('ip', e.target.value)}
                placeholder="192.168.1.x, 2001:db8::1"
                className={`bg-[#21262d] border-[#30363d] font-mono text-sm h-8 ${modalStyles['modal-radius']}`}
              />
              <span className="text-[10px] text-muted-foreground/50">comma-separated</span>
            </div>
            </div>{/* end Hostname/IP subgrid */}

            <div className="grid grid-cols-2 gap-3">
            {/* Check method — hidden for zigbee nodes (always none/online) */}
            {!ZIGBEE_TYPES.includes((form.type ?? '') as NodeType) && (
              <div className="flex flex-col gap-1.5">
                <Label className="text-xs text-muted-foreground">Check Method</Label>
                <Select value={form.check_method ?? 'ping'} onValueChange={(v) => set('check_method', v as CheckMethod)}>
                  <SelectTrigger className={`bg-[#21262d] border-[#30363d] text-sm h-8 cursor-pointer ${modalStyles['modal-interactive']} ${modalStyles['modal-radius']}`} aria-label="Check method selector">
                    <SelectValue>{CHECK_METHOD_LABELS[(form.check_method ?? 'ping') as CheckMethod]}</SelectValue>
                  </SelectTrigger>
                  <SelectContent className="bg-[#21262d] border-[#30363d]">
                    {CHECK_METHODS.map((m) => (
                      <SelectItem key={m} value={m} className="text-sm">{CHECK_METHOD_LABELS[m]}</SelectItem>
                    ))}
                  </SelectContent>
                </Select>
              </div>
            )}

            {/* Check target — hidden for zigbee nodes */}
            {!ZIGBEE_TYPES.includes((form.type ?? '') as NodeType) && (
              <div className="flex flex-col gap-1.5">
                <Label className="text-xs text-muted-foreground">Check Target</Label>
                <Input
                  value={form.check_target ?? ''}
                  onChange={(e) => set('check_target', e.target.value)}
                  placeholder="http://..."
                  className={`bg-[#21262d] border-[#30363d] font-mono text-sm h-8 ${modalStyles['modal-radius']}`}
                />
              </div>
            )}
            </div>{/* end Check method/target subgrid */}

            {/* Parent Container */}
            {(() => {
              const childType = (form.type ?? 'generic') as NodeType
              // Candidates: type-based parents (lxc/vm/docker_container), any
              // container-mode node, and visual groups. The current parent is
              // always kept so an already-nested node can be re-targeted or
              // detached here.
              const validParents = parentCandidates.filter(
                (n) => n.id !== currentNodeId &&
                  (isValidParentNode(childType, n) || n.id === form.parent_id),
              )
              if (validParents.length === 0) return null
              return (
                <div className="flex flex-col gap-1.5 col-span-2">
                  <Label className="text-xs text-muted-foreground">Parent Container</Label>
                  <Select
                    value={form.parent_id ?? 'none'}
                    onValueChange={(v) => set('parent_id', v === 'none' ? undefined : v)}
                  >
                    <SelectTrigger className={`bg-[#21262d] border-[#30363d] text-sm h-8 cursor-pointer ${modalStyles['modal-interactive']} ${modalStyles['modal-radius']}`} aria-label="Parent container selector">
                      <SelectValue>
                        {form.parent_id
                          ? (validParents.find((n) => n.id === form.parent_id)?.label ?? 'None')
                          : 'None'}
                      </SelectValue>
                    </SelectTrigger>
                    <SelectContent className="bg-[#21262d] border-[#30363d]">
                      <SelectItem value="none" className="text-sm">None</SelectItem>
                      {validParents.map((n) => (
                        <SelectItem key={n.id} value={n.id} className="text-sm">{n.label}</SelectItem>
                      ))}
                    </SelectContent>
                  </Select>
                </div>
              )
            })()}

            {/* Container mode */}
            {CONTAINER_MODE_TYPES.includes((form.type ?? 'generic') as NodeType) && (
              <div className="flex items-center justify-between col-span-2 py-1">
                <div className="flex flex-col gap-0.5">
                  <Label className="text-xs text-muted-foreground">Container Mode</Label>
                  <span className="text-[10px] text-muted-foreground/60">
                    Allow other nodes to nest inside this node
                  </span>
                </div>

                <button
                  type="button"
                  role="switch"
                  aria-label="Container Mode"
                  aria-checked={!!form.container_mode}
                  onClick={() => set('container_mode', !form.container_mode)}
                  className={`relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full transition-colors focus:outline-none ${modalStyles['modal-interactive']}`}
                  style={{ background: form.container_mode ? '#ff6e00' : '#30363d' }}
                >
                  <span
                    className="pointer-events-none absolute top-0.5 left-0.5 h-4 w-4 rounded-full bg-white shadow-sm transition-transform duration-200 ease-in-out"
                    style={{
                      transform: form.container_mode ? 'translateX(16px)' : 'translateX(0)'
                    }}
                  />
                </button>
              </div>
            )}
            {/* Notes — or, for canvas furniture, its description.
                Furniture draws no device, so it has no inventory row to keep
                `notes` on; its text lives on the node's own `description`. */}
            <div className="flex flex-col gap-1.5">
              <Label className="text-xs text-muted-foreground">{isFurniture ? 'Description' : 'Notes'}</Label>
              <Textarea
                value={(isFurniture ? form.description : form.notes) ?? ''}
                onChange={(e) => set(isFurniture ? 'description' : 'notes', e.target.value)}
                placeholder={isFurniture ? 'What this is for' : 'Optional notes'}
                rows={3}
                className={`bg-[#21262d] border-[#30363d] text-sm resize-y min-h-16 ${modalStyles['modal-radius']}`}
              />
            </div>
            </div>{/* ── end LEFT column ── */}

            {/* ── RIGHT column: display ── */}
            <div className="flex flex-col gap-4 min-w-0">
            <div className="text-[11px] font-semibold uppercase tracking-wider text-muted-foreground/70 pb-1 border-b border-[#30363d]">Design</div>
            {/* Service visibility */}
            {form.type !== 'groupRect' && form.type !== 'group' && (
              <div className="flex items-start justify-between col-span-2 py-1">
                <div className="flex flex-col gap-0.5">
                  <Label className="text-xs text-muted-foreground">Show Services</Label>
                  <span className="text-[10px] text-muted-foreground/60">Display discovered services on the node card</span>
                </div>
                <button
                  type="button"
                  role="switch"
                  aria-label="Show Services"
                  aria-checked={showServicesEnabled}
                  onClick={() => set('custom_colors', {
                    ...form.custom_colors,
                    show_services: !showServicesEnabled,
                  })}
                  className="relative inline-flex h-5 w-9 mt-1 shrink-0 cursor-pointer rounded-full transition-colors focus:outline-none"
                  style={{ background: showServicesEnabled ? resolvedNodeColors.icon : '#30363d' }}
                >
                  <span
                    className="pointer-events-none absolute top-px left-0.5 h-4 w-4 rounded-full bg-white shadow-sm transition-transform duration-200 ease-in-out"
                    style={{ transform: showServicesEnabled ? 'translateX(16px)' : 'translateX(0)' }}
                  />
                </button>
              </div>
            )}

            {/* Appearance */}
            <div className="flex flex-col gap-2 col-span-2">
              <div className="flex items-center justify-between">
                <Label className="text-xs text-muted-foreground">Appearance</Label>
                {hasAppearanceOverrides && (
                  <button
                    type="button"
                    onClick={() => setForm((f) => {
                      if (!f.custom_colors) return f
                      const { border, background, icon, ...rest } = f.custom_colors
                      void border
                      void background
                      void icon
                      return {
                        ...f,
                        custom_colors: Object.keys(rest).length > 0 ? rest : undefined,
                      }
                    })}
                    className="flex items-center gap-1 text-[10px] text-muted-foreground/60 hover:text-muted-foreground transition-colors"
                  >
                    <RotateCcw size={10} /> Reset to defaults
                  </button>
                )}
              </div>
              <div className="grid grid-cols-3 gap-2">
                {(['border', 'background', 'icon'] as const).map((key) => {
                  const resolved = resolveNodeColors({ type: form.type ?? 'generic', custom_colors: form.custom_colors })
                  const currentValue = resolved[key]
                  const isCustom = !!form.custom_colors?.[key]
                  return (
                    <div key={key} className="flex flex-col gap-1 items-center">
                      <label
                        className={`relative w-full h-7 rounded-md border cursor-pointer overflow-hidden transition-all ${modalStyles['modal-interactive']}`}
                        style={{ borderColor: isCustom ? currentValue : '#30363d' }}
                        title={`${key.charAt(0).toUpperCase() + key.slice(1)}: ${currentValue}`}
                        tabIndex={0}
                        aria-label={`Color picker for ${key}`}
                      >
                        <input
                          type="color"
                          value={currentValue}
                          onChange={(e) => set('custom_colors', { ...form.custom_colors, [key]: e.target.value })}
                          className="absolute inset-0 w-full h-full cursor-pointer opacity-0"
                        />
                        <div className="w-full h-full rounded-sm" style={{ background: currentValue }} />
                      </label>
                      <span className="text-[9px] text-muted-foreground/60 capitalize">{key}</span>
                    </div>
                  )
                })}
              </div>
              <div className="min-h-3.5">
                {!hasAppearanceOverrides && (
                  <p className="text-[10px] text-muted-foreground/50">Using default colors for {NODE_TYPE_LABELS[form.type ?? 'generic']}. Click a swatch to customize.</p>
                )}
              </div>
              {onEditTypeStyle && form.type !== 'group' && form.type !== 'groupRect' && (
                <button
                  type="button"
                  onClick={() => onEditTypeStyle((form.type ?? 'generic') as NodeType)}
                  className="flex items-center gap-1 self-start text-[10px] text-[#00d4ff] hover:underline"
                >
                  <Palette size={10} /> Edit {NODE_TYPE_LABELS[form.type ?? 'generic']} style for all nodes on the canvas
                </button>
              )}
            </div>

            {/* Connection points per side (not for group containers) */}
            {form.type !== 'groupRect' && form.type !== 'group' && (
              <div className="flex flex-col gap-2.5 col-span-2">
                <Label className="text-xs text-muted-foreground">Connection Points</Label>
                {/* Spatial cross: each side's stepper sits where that side is. */}
                <div className="grid grid-cols-[1fr_auto_1fr] items-center justify-items-center gap-x-2 gap-y-2 py-1">
                  <div />
                  <CPStepper label="Top" side="top" value={sideValue('top')}
                    onChange={(v) => set('top_handles', v)} />
                  <div />

                  <CPStepper label="Left" side="left" value={sideValue('left')}
                    onChange={(v) => set('left_handles', v)} />
                  <div
                    className="flex items-center justify-center rounded-md border text-[9px] uppercase tracking-wide font-medium select-none"
                    style={{
                      width: 64, height: 40,
                      borderColor: resolvedNodeColors.border,
                      background: `${resolvedNodeColors.background}`,
                      color: resolvedNodeColors.icon,
                    }}
                  >
                    node
                  </div>
                  <CPStepper label="Right" side="right" value={sideValue('right')}
                    onChange={(v) => set('right_handles', v)} />

                  <div />
                  <CPStepper label="Bottom" side="bottom" value={sideValue('bottom')}
                    onChange={(v) => set('bottom_handles', v)} />
                  <div />
                </div>
                <div className="flex items-center justify-between pt-1">
                  <div className="flex flex-col gap-0.5">
                    <Label className="text-xs text-muted-foreground">Show Port Numbers</Label>
                    <span className="text-[10px] text-muted-foreground/60">Label each connection point</span>
                  </div>
                  <button
                    type="button"
                    role="switch"
                    aria-checked={!!form.show_port_numbers}
                    onClick={() => set('show_port_numbers', !form.show_port_numbers)}
                    className={`relative inline-flex h-5 w-9 shrink-0 cursor-pointer rounded-full transition-colors focus:outline-none ${modalStyles['modal-interactive']}`}
                    tabIndex={0}
                    aria-label="Toggle port numbers"
                    style={{ background: form.show_port_numbers ? '#ff6e00' : '#30363d' }}
                  >
                    <span
                      className="pointer-events-none absolute top-0.5 h-4 w-4 rounded-full bg-white shadow-sm transition-all"
                      style={{ left: form.show_port_numbers ? 'calc(100% - 18px)' : '2px' }}
                    />
                  </button>
                </div>
                {form.show_port_numbers && (
                  <PortLabelFields
                    counts={{ top: sideValue('top'), bottom: sideValue('bottom'), left: sideValue('left'), right: sideValue('right') }}
                    labels={form.port_labels}
                    onChange={(id, value) => setForm((f) => ({ ...f, port_labels: { ...f.port_labels, [id]: value } }))}
                  />
                )}
              </div>
            )}

            </div>{/* ── end RIGHT column ── */}
          </div>

          <div className="flex justify-between gap-2 pt-1">
            {/* Show delete button only for edit mode (not add) */}
            {title !== 'Add Node' ? (
              <Button
                type="button"
                variant="ghost"
                size="sm"
                className="text-[#f85149] hover:text-[#f85149] hover:bg-[#f85149]/10 cursor-pointer"
                onClick={() => {
                  if (window.confirm('Delete this node?')) {
                    onSubmit({ ...form, _delete: true })
                    onClose()
                  }
                }}
                style={{ minWidth: 64 }}
              >
                Delete
              </Button>
            ) : <span />}
            <div className="flex gap-2">
              <Button type="button" variant="ghost" size="sm" className={`cursor-pointer ${modalStyles['modal-cancel-hover']}`} onClick={onClose}>
                Cancel
              </Button>
              <Button
                type="submit"
                size="sm"
                className="bg-[#00d4ff] text-[#0d1117] hover:bg-[#00d4ff]/90 cursor-pointer"
              >
                {title === 'Add Node' ? 'Add' : 'Save'}
              </Button>
            </div>
          </div>
        </form>
      </DialogContent>
    </Dialog>
  )
}