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 | 68x 68x 9x 7x 5x 5x 8x 5x 5x 10x 10x 10x 14x 10x 10x 10x 13x 8x 13x 5x 5x 13x 1x 1x 2x 1x 1x 13x 1x 1x 2x 1x 1x 13x 1x 2x 1x 1x 13x 2x 2x 2x 2x 13x 1x 3x 12x 2x 1x 1x 2x 10x 10x 14x 14x 1x 1x 1x | import { useState, useCallback } from 'react'
import { Button } from '@/components/ui/button'
import { Skeleton } from '@/components/ui/skeleton'
interface KeyValueEditorProps {
value: Record<string, string> | undefined
onChange?: (value: Record<string, string> | undefined) => void
readOnly?: boolean
isLoading?: boolean
label?: string
}
function entriesFromRecord(
value: Record<string, string> | undefined,
): [string, string][] {
if (!value) return []
return Object.entries(value).sort(([a], [b]) => a.localeCompare(b))
}
function recordFromEntries(entries: [string, string][]): Record<string, string> {
const result: Record<string, string> = {}
for (const [k, v] of entries) {
if (k.trim() !== '') {
result[k] = v
}
}
return result
}
function hasDuplicates(entries: [string, string][]): Set<string> {
const seen = new Set<string>()
const dupes = new Set<string>()
for (const [k] of entries) {
if (k === '') continue
if (seen.has(k)) dupes.add(k)
seen.add(k)
}
return dupes
}
export function KeyValueEditor({
value,
onChange,
readOnly = false,
isLoading = false,
label,
}: KeyValueEditorProps) {
const [entries, setEntries] = useState<[string, string][]>(() =>
entriesFromRecord(value),
)
const emit = useCallback(
(next: [string, string][]) => {
const cleaned = recordFromEntries(next)
onChange?.(Object.keys(cleaned).length > 0 ? cleaned : undefined)
},
[onChange],
)
const handleKeyChange = useCallback(
(index: number, newKey: string) => {
setEntries((prev) => {
const next = prev.map(([k, v], i) =>
i === index ? [newKey, v] as [string, string] : [k, v] as [string, string],
)
emit(next)
return next
})
},
[emit],
)
const handleValueChange = useCallback(
(index: number, newValue: string) => {
setEntries((prev) => {
const next = prev.map(([k, v], i) =>
i === index ? [k, newValue] as [string, string] : [k, v] as [string, string],
)
emit(next)
return next
})
},
[emit],
)
const handleDelete = useCallback(
(index: number) => {
setEntries((prev) => {
const next = prev.filter((_, i) => i !== index)
emit(next)
return next
})
},
[emit],
)
const handleAdd = useCallback(() => {
setEntries((prev) => {
const next = [...prev, ['', ''] as [string, string]]
emit(next)
return next
})
}, [emit])
if (isLoading) {
return (
<div className="flex flex-col gap-2">
{Array.from({ length: 3 }).map((_, i) => (
<Skeleton key={i} className="h-9 w-full" />
))}
</div>
)
}
if (readOnly) {
if (!value || Object.keys(value).length === 0) {
return (
<span className="text-sm text-muted-foreground">
No metadata available
</span>
)
}
return (
<div className="flex flex-col gap-1">
{entriesFromRecord(value).map(([k, v]) => (
<div key={k} className="text-sm">
<span className="font-mono font-semibold text-muted-foreground">
{k}:
</span>{' '}
<span>{v}</span>
</div>
))}
</div>
)
}
const duplicates = hasDuplicates(entries)
return (
<div className="flex flex-col gap-2">
{label && (
<span className="text-sm font-medium">{label}</span>
)}
{entries.map(([k, v], i) => {
const isDuplicate = k !== '' && duplicates.has(k)
return (
<div key={i} className="flex items-center gap-2">
<input
type="text"
value={k}
onChange={(e) => handleKeyChange(i, e.target.value)}
placeholder="Key"
className={'flex h-9 w-40 rounded-md border bg-background px-3 py-2 text-sm shadow-sm outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30' + (isDuplicate ? ' border-destructive focus-visible:border-destructive focus-visible:ring-destructive/20' : ' border-input')}
aria-label="Key"
/>
<input
type="text"
value={v}
onChange={(e) => handleValueChange(i, e.target.value)}
placeholder="Value"
className="flex h-9 flex-1 rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/30"
aria-label="Value"
/>
<Button
variant="ghost"
size="icon-sm"
onClick={() => handleDelete(i)}
aria-label="Delete"
type="button"
>
✕
</Button>
{isDuplicate && (
<span className="text-xs text-destructive whitespace-nowrap">
Duplicate key
</span>
)}
</div>
)
})}
<div>
<Button
variant="outline"
size="sm"
onClick={handleAdd}
type="button"
>
+ Add row
</Button>
</div>
</div>
)
}
|