Skip to content

Commit

Permalink
feature(core/form): keep stable child presence list with immutableRec…
Browse files Browse the repository at this point in the history
…oncile
  • Loading branch information
skogsmaskin committed May 30, 2023
1 parent bf77938 commit f4a113f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/sanity/src/core/form/studio/contexts/Presence.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* eslint-disable react/no-unused-prop-types */
import React, {useContext, createContext, useMemo} from 'react'
import React, {useContext, createContext, useMemo, useRef} from 'react'
import {Path} from '@sanity/types'
import {startsWith, isEqual} from '@sanity/util/paths'
import {FormNodePresence} from '../../../presence'
import {immutableReconcile} from '../../store/utils/immutableReconcile'

const PresenceContext = createContext<FormNodePresence[]>([])

Expand All @@ -27,11 +28,13 @@ export function useFormFieldPresence(): FormNodePresence[] {
*/
export function useChildPresence(path: Path, inclusive?: boolean): FormNodePresence[] {
const presence = useFormFieldPresence()
return useMemo(
() =>
presence.filter(
(item) => startsWith(path, item.path) && (inclusive || !isEqual(path, item.path))
),
[inclusive, path, presence]
const prev = useRef(presence)
const next = immutableReconcile(
prev.current,
presence.filter(
(item) => startsWith(path, item.path) && (inclusive || !isEqual(path, item.path))
)
)
prev.current = next
return next
}

0 comments on commit f4a113f

Please sign in to comment.