# All files / src/components EditableValue.tsx

97.72% Statements 86/88
82.5% Branches 33/40
92.85% Functions 26/28
100% Lines 82/82

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
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266

    17x
    17x

    17x
    2x
    1x
    1x
    1x
    1x

    17x
    4x
    4x
    4x
    4x
    4x
    4x
    4x

    4x

    17x
    2x
    2x

    17x
    7x

    2x

    1x

    10x

    45x

    45x
    4x
    3x
    1x
    1x
    1x

    45x
    12x

    5x

    1x

    33x

    52x
    52x
    52x
    52x

    52x
    4x
    4x

    52x
    2x
    2x
    2x
    1x

    1x

    2x
    2x

    52x
    3x
    1x

    52x
    2x
    2x
    2x
    2x

    52x
    5x
    3x
    1x
    2x
    2x
    1x
    1x

    1x
    1x

    52x

    93x

    2x
    3x

    4x

    1x
    1x

    2x
    2x

    2x
    1x

    5x

     import { useState, useCallback, useRef } from 'react'
    import { normalizeUrl, openExternalUrl } from '../utils/url'

    export function UrlValue({
      value,
      onSave,
      onCancel,
      isEditing,
      onStartEdit,
    }: {
      value: string
      onSave: (newValue: string) => void
      onCancel: () => void
      isEditing: boolean
      onStartEdit: () => void
    }) {
      const [editValue, setEditValue] = useState(value)
      const openingRef = useRef(false)

      const handleKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === 'Enter') {
          onSave(editValue)
        E} else if (e.key === 'Escape') {
          setEditValue(value)
          onCancel()
        }
      }

      const handleOpen = useCallback((e: React.MouseEvent) => {
        e.stopPropagation()
        Iif (openingRef.current) return
        openingRef.current = true
        const normalized = normalizeUrl(value)
        try {
          new URL(normalized)
          openExternalUrl(normalized).catch(() => {
            // opener failed — ignore silently
          })
        } catch {
          // malformed URL — do nothing
        } finally {
          setTimeout(() => { openingRef.current = false }, 500)
        }
      }, [value])

      const handleEditClick = useCallback((e: React.MouseEvent) => {
        e.stopPropagation()
        onStartEdit()
      }, [onStartEdit])

      if (isEditing) {
        return (
          <input
            className="w-full rounded border border-ring bg-muted px-2 py-1 text-[12px] text-foreground outline-none focus:border-primary"
            type="text"
            value={editValue}
            onChange={(e) => setEditValue(e.target.value)}
            onKeyDown={handleKeyDown}
            onBlur={() => onSave(editValue)}
            autoFocus
          />
        )
      }

      return (
        <span className="group/url inline-flex min-w-0 items-center gap-1">
          <span
            className="min-w-0 cursor-pointer truncate rounded px-1 py-0.5 text-right text-[12px] text-secondary-foreground transition-colors hover:text-primary hover:underline"
            onClick={handleOpen}
            title={value}
            data-testid="url-link"
          >
            {value || '\u2014'}
          </span>
          <button
            className="shrink-0 border-none bg-transparent p-0 text-[12px] leading-none text-muted-foreground opacity-0 transition-all hover:text-foreground group-hover/url:opacity-100"
            onClick={handleEditClick}
            title="Edit URL"
            data-testid="url-edit-btn"
          >
            &#9998;
          </button>
        </span>
      )
    }

    export function EditableValue({
      value,
      onSave,
      onCancel,
      isEditing,
      onStartEdit
    }: {
      value: string
      onSave: (newValue: string) => void
      onCancel: () => void
      isEditing: boolean
      onStartEdit: () => void
    }) {
      const [editValue, setEditValue] = useState(value)

      const handleKeyDown = (e: React.KeyboardEvent) => {
        if (e.key === 'Enter') {
          onSave(editValue)
        E} else if (e.key === 'Escape') {
          setEditValue(value)
          onCancel()
        }
      }

      if (isEditing) {
        return (
          <input
            className="w-full rounded border border-ring bg-muted px-2 py-1 text-[12px] text-foreground outline-none focus:border-primary"
            type="text"
            value={editValue}
            onChange={(e) => setEditValue(e.target.value)}
            onKeyDown={handleKeyDown}
            onBlur={() => onSave(editValue)}
            autoFocus
          />
        )
      }

      return (
        <span
          className="min-w-0 cursor-pointer truncate rounded px-1 py-0.5 text-right text-[12px] text-secondary-foreground transition-colors hover:bg-muted"
          onClick={onStartEdit}
          title={value || 'Click to edit'}
        >
          {value || '\u2014'}
        </span>
      )
    }

    export function TagPillList({
      items,
      onSave,
      label,
    }: {
      items: string[]
      onSave: (newItems: string[]) => void
      label: string
    }) {
      const [editingIndex, setEditingIndex] = useState<number | null>(null)
      const [editValue, setEditValue] = useState('')
      const [isAddingNew, setIsAddingNew] = useState(false)
      const [newValue, setNewValue] = useState('')

      const handleStartEdit = (index: number) => {
        setEditingIndex(index)
        setEditValue(items[index])
      }

      const handleSaveEdit = () => {
        Eif (editingIndex !== null) {
          const newItems = [...items]
          if (editValue.trim()) {
            newItems[editingIndex] = editValue.trim()
          } else {
            newItems.splice(editingIndex, 1)
          }
          onSave(newItems)
          setEditingIndex(null)
        }
      }

      const handleDeleteItem = (index: number) => {
        const newItems = items.filter((_, i) => i !== index)
        onSave(newItems)
      }

      const handleAddNew = () => {
        Eif (newValue.trim()) {
          onSave([...items, newValue.trim()])
          setNewValue('')
          setIsAddingNew(false)
        }
      }

      const handleKeyDown = (e: React.KeyboardEvent, action: 'edit' | 'add') => {
        if (e.key === 'Enter') {
          if (action === 'edit') handleSaveEdit()
          else handleAddNew()
        E} else if (e.key === 'Escape') {
          if (action === 'edit') {
            setEditingIndex(null)
            setEditValue('')
          } else {
            setIsAddingNew(false)
            setNewValue('')
          }
        }
      }

      return (
        <div className="flex flex-wrap items-center gap-1">
          {items.map((item, idx) =>
            editingIndex === idx ? (
              <input
                key={idx}
                className="rounded-full border border-ring bg-muted px-2 py-0.5 text-[11px] text-foreground outline-none focus:border-primary"
                style={{ width: Math.max(60, editValue.length * 7 + 16) }}
                type="text"
                value={editValue}
                onChange={(e) => setEditValue(e.target.value)}
                onKeyDown={(e) => handleKeyDown(e, 'edit')}
                onBlur={handleSaveEdit}
                autoFocus
              />
            ) : (
              <span
                key={idx}
                className="group/pill inline-flex cursor-pointer items-center gap-0.5 rounded-full py-0.5 pl-2 pr-1 transition-colors"
                style={{
                  backgroundColor: 'var(--accent-blue-light)',
                  color: 'var(--accent-blue)',
                  fontSize: 11,
                  fontWeight: 500,
                }}
                onClick={() => handleStartEdit(idx)}
                title="Click to edit"
              >
                {item}
                <button
                  className="ml-0.5 inline-flex h-3.5 w-3.5 items-center justify-center rounded-full border-none bg-transparent p-0 text-[10px] leading-none opacity-0 transition-all hover:bg-[var(--accent-red-light)] hover:text-[var(--accent-red)] group-hover/pill:opacity-100"
                  style={{ color: 'var(--accent-blue)' }}
                  onClick={(e) => {
                    e.stopPropagation()
                    handleDeleteItem(idx)
                  }}
                  title="Remove"
                >
                  &times;
                </button>
              </span>
            )
          )}
          {isAddingNew ? (
            <input
              className="rounded-full border border-ring bg-muted px-2 py-0.5 text-[11px] text-foreground outline-none focus:border-primary"
              style={{ width: Math.max(60, newValue.length * 7 + 16) }}
              type="text"
              value={newValue}
              onChange={(e) => setNewValue(e.target.value)}
              onKeyDown={(e) => handleKeyDown(e, 'add')}
              onBlur={() => {
                if (newValue.trim()) handleAddNew()
                else { setIsAddingNew(false); setNewValue('') }
              }}
              placeholder={`${label}...`}
              autoFocus
            />
          ) : (
            <button
              className="inline-flex h-5 w-5 items-center justify-center rounded-full border border-dashed border-[var(--accent-blue)] bg-transparent p-0 text-[12px] leading-none text-[var(--accent-blue)] transition-colors hover:bg-[var(--accent-blue-light)]"
              onClick={() => setIsAddingNew(true)}
              title={`Add ${label.toLowerCase()}`}
            >
              +
            </button>
          )}
        </div>
      )
    }