Skip to content

Commit

Permalink
fix: portable text editor crash on React 19
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jun 7, 2024
1 parent 23c42ae commit 62666c8
Show file tree
Hide file tree
Showing 18 changed files with 268 additions and 202 deletions.
1 change: 1 addition & 0 deletions .eslintignore.react-compiler
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ perf/*
scripts/*
test/*
**/__workshop__/*
packages/sanity/playwright-ct/**
13 changes: 13 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ const config = {
'import/consistent-type-specifier-style': ['error', 'prefer-top-level'],
},
},

// Don't lint React Compiler rules on test code
{
files: [
`**/*/test/**/*`,
'**/*/__tests__/**/*',
'**/*.test.{js,ts,tsx}',
'packages/sanity/playwright-ct/**',
],
rules: {
'react-compiler/react-compiler': 'off',
},
},
],
}

Expand Down
4 changes: 2 additions & 2 deletions dev/test-studio/components/workspaceLogos.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {useColorScheme} from 'sanity'
import {useColorSchemeValue} from 'sanity'

export const VercelLogo = () => {
const {scheme} = useColorScheme()
const scheme = useColorSchemeValue()
const fill = scheme === 'dark' ? '#fff' : '#000'

return (
Expand Down
12 changes: 9 additions & 3 deletions dev/test-studio/workshop/WorkshopTool.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Workshop} from '@sanity/ui-workshop'
import {type Tool, useColorScheme, useWorkspace} from 'sanity'
import {type Tool, useColorSchemeSetValue, useColorSchemeValue, useWorkspace} from 'sanity'

import {config} from './config'
import {type WorkshopOptions} from './types'
Expand All @@ -9,7 +9,8 @@ export function WorkshopTool(props: {tool: Tool<WorkshopOptions>}) {
const {tool} = props
const toolName = tool.options?.name || 'workshop'

const {scheme, setScheme} = useColorScheme()
const scheme = useColorSchemeValue()
const setScheme = useColorSchemeSetValue()
const {basePath} = useWorkspace()

const locationStore = useLocationStore({
Expand All @@ -20,7 +21,12 @@ export function WorkshopTool(props: {tool: Tool<WorkshopOptions>}) {
<Workshop
config={config}
locationStore={locationStore}
onSchemeChange={setScheme}
onSchemeChange={
setScheme ||
(() => {
console.warn('Unable to change theme scheme')
})
}
scheme={scheme}
/>
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"check:format": "prettier . --check",
"check:lint": "turbo run lint --continue -- --quiet",
"check:react-exhaustive-deps": "turbo run lint --continue -- --quiet --rule 'react-hooks/exhaustive-deps: [error, {additionalHooks: \"(useAsync|useMemoObservable|useObservableCallback)\"}]'",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 40 .",
"check:react-compiler": "eslint --no-inline-config --no-eslintrc --ext .cjs,.mjs,.js,.jsx,.ts,.tsx --parser @typescript-eslint/parser --plugin react-compiler --rule 'react-compiler/react-compiler: [warn]' --ignore-path .eslintignore.react-compiler --max-warnings 38 .",
"check:test": "run-s test -- --silent",
"check:types": "tsc && turbo run check:types --filter='./packages/*' --filter='./packages/@sanity/*'",
"chore:format:fix": "prettier --cache --write .",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable max-nested-callbacks */
import {type PortableTextBlock} from '@sanity/types'
import {debounce, isEqual} from 'lodash'
import {useMemo, useRef} from 'react'
import {useCallback, useMemo, useRef} from 'react'
import {type Descendant, Editor, type Node, Text, Transforms} from 'slate'
import {useSlate} from 'slate-react'

Expand Down Expand Up @@ -50,20 +50,21 @@ export function useSyncValue(
const slateEditor = useSlate()
const updateValueFunctionRef = useRef<(value: PortableTextBlock[] | undefined) => void>()

const updateValueDebounced = useMemo(() => {
const updateFromCurrentValue = () => {
const currentValue = CURRENT_VALUE.get(portableTextEditor)
if (previousValue.current === currentValue) {
debug('Value is the same object as previous, not need to sync')
return
}
if (updateValueFunctionRef.current && currentValue) {
debug('Updating the value debounced')
updateValueFunctionRef.current(currentValue)
}
const updateFromCurrentValue = useCallback(() => {
const currentValue = CURRENT_VALUE.get(portableTextEditor)
if (previousValue.current === currentValue) {
debug('Value is the same object as previous, not need to sync')
return
}
if (updateValueFunctionRef.current && currentValue) {
debug('Updating the value debounced')
updateValueFunctionRef.current(currentValue)
}
return debounce(updateFromCurrentValue, 1000, {trailing: true, leading: false})
}, [portableTextEditor])
const updateValueDebounced = useMemo(
() => debounce(updateFromCurrentValue, 1000, {trailing: true, leading: false}),
[updateFromCurrentValue],
)

return useMemo(() => {
const updateFunction = (value: PortableTextBlock[] | undefined) => {
Expand Down
Loading

0 comments on commit 62666c8

Please sign in to comment.