Added fix for Infinite loop when changing value of new navigation item's field value

fixed: https://linear.app/tryghost/issue/ENG-1351/🐛-infinite-loop-when-changing-value-of-new-navigation-items-field
This commit is contained in:
Princi Vershwal 2024-07-04 17:37:33 +05:30 committed by GitHub
parent 3e4b8d825d
commit 3c19d952b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 2 deletions

View File

@ -1,5 +1,6 @@
import {arrayMove} from '@dnd-kit/sortable';
import {useEffect, useState} from 'react';
import _ from 'lodash';
export type SortableIndexedList<Item> = {
items: Array<{ item: Item; id: string }>;
@ -32,7 +33,7 @@ const useSortableIndexedList = <Item extends unknown>({items, setItems, blank, c
allItems.push(newItem);
}
if (JSON.stringify(allItems) !== JSON.stringify(items)) {
if (!_.isEqual(JSON.parse(JSON.stringify(allItems)), JSON.parse(JSON.stringify(items)))) {
setItems(allItems);
}
}, [editableItems, newItem, items, setItems, canAddNewItem]);

View File

@ -31,7 +31,7 @@ const useNavigationEditor = ({items, setItems}: {
const list = useSortableIndexedList<Omit<EditableItem, 'id'>>({
items: items.map(item => ({...item, errors: {}})),
setItems: newItems => setItems(newItems.map(({url, label}) => ({url, label}))),
blank: {label: '', url: '/', errors: {}},
blank: {url: '/',label: '', errors: {}},
canAddNewItem: hasNewItem
});