Fix bug where editing any field closed the form
This commit is contained in:
committed by
Linus Rath
parent
ad80aa23ca
commit
e86183b44a
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { Plus, Trash2, RotateCcw, ChevronDown, ChevronRight } from 'lucide-react';
|
import { Plus, Trash2, RotateCcw, ChevronDown, ChevronRight } from 'lucide-react';
|
||||||
import type { JmapServerEntry } from '@/lib/admin/jmap-servers';
|
import type { JmapServerEntry } from '@/lib/admin/jmap-servers';
|
||||||
|
|
||||||
@@ -77,30 +77,17 @@ function emptyDraft(): RowDraft {
|
|||||||
|
|
||||||
export function JmapServersSection({ value, source, onChange, onRevert }: Props) {
|
export function JmapServersSection({ value, source, onChange, onRevert }: Props) {
|
||||||
const [drafts, setDrafts] = useState<RowDraft[]>(() => value.map(entryToDraft));
|
const [drafts, setDrafts] = useState<RowDraft[]>(() => value.map(entryToDraft));
|
||||||
|
const lastEmittedRef = useRef(value);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Re-sync from props when the underlying config value changes (e.g. revert,
|
if (value === lastEmittedRef.current) return;
|
||||||
// initial load). Skip when drafts already represent the same array to avoid
|
setDrafts(value.map(entryToDraft))
|
||||||
// clobbering in-progress edits.
|
|
||||||
setDrafts((prev) => {
|
|
||||||
if (prev.length === value.length) {
|
|
||||||
const same = prev.every((d, i) => {
|
|
||||||
const e = value[i];
|
|
||||||
return d.id === e.id && d.url === e.url && d.label === e.label;
|
|
||||||
});
|
|
||||||
if (same) return prev;
|
|
||||||
}
|
|
||||||
return value.map(entryToDraft);
|
|
||||||
});
|
|
||||||
}, [value]);
|
}, [value]);
|
||||||
|
|
||||||
function commit(next: RowDraft[]) {
|
function commit(next: RowDraft[]) {
|
||||||
setDrafts(next);
|
setDrafts(next);
|
||||||
const entries: JmapServerEntry[] = [];
|
const entries = next.map(draftToEntry).filter((e): e is JmapServerEntry => e !== null);
|
||||||
for (const d of next) {
|
lastEmittedRef.current = entries;
|
||||||
const e = draftToEntry(d);
|
|
||||||
if (e) entries.push(e);
|
|
||||||
}
|
|
||||||
onChange(entries);
|
onChange(entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user