2
0

fix(editor): 🚸 Improve equality check

This commit is contained in:
Baptiste Arnaud
2022-03-10 15:11:42 +01:00
parent 533fdb1b3a
commit 5228cff468
2 changed files with 45 additions and 17 deletions

View File

@ -5,7 +5,7 @@ import { css } from '@codemirror/lang-css'
import { javascript } from '@codemirror/lang-javascript' import { javascript } from '@codemirror/lang-javascript'
import { html } from '@codemirror/lang-html' import { html } from '@codemirror/lang-html'
import { useEffect, useRef, useState } from 'react' import { useEffect, useRef, useState } from 'react'
import { useDebounce } from 'use-debounce' import { useDebouncedCallback } from 'use-debounce'
type Props = { type Props = {
value: string value: string
@ -22,12 +22,22 @@ export const CodeEditor = ({
}: Props & Omit<BoxProps, 'onChange'>) => { }: Props & Omit<BoxProps, 'onChange'>) => {
const editorContainer = useRef<HTMLDivElement | null>(null) const editorContainer = useRef<HTMLDivElement | null>(null)
const editorView = useRef<EditorView | null>(null) const editorView = useRef<EditorView | null>(null)
const [plainTextValue, setPlainTextValue] = useState(value) const [, setPlainTextValue] = useState(value)
const [debouncedValue] = useDebounce( const debounced = useDebouncedCallback(
plainTextValue, (value) => {
setPlainTextValue(value)
onChange && onChange(value)
},
process.env.NEXT_PUBLIC_E2E_TEST ? 0 : 1000 process.env.NEXT_PUBLIC_E2E_TEST ? 0 : 1000
) )
useEffect(
() => () => {
debounced.flush()
},
[debounced]
)
useEffect(() => { useEffect(() => {
if (!editorView.current || !isReadOnly) return if (!editorView.current || !isReadOnly) return
editorView.current.dispatch({ editorView.current.dispatch({
@ -40,12 +50,6 @@ export const CodeEditor = ({
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]) }, [value])
useEffect(() => {
if (!onChange || debouncedValue === value) return
onChange(debouncedValue)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [debouncedValue])
useEffect(() => { useEffect(() => {
const editor = initEditor(value) const editor = initEditor(value)
return () => { return () => {
@ -58,7 +62,7 @@ export const CodeEditor = ({
if (!editorContainer.current) return if (!editorContainer.current) return
const updateListenerExtension = EditorView.updateListener.of((update) => { const updateListenerExtension = EditorView.updateListener.of((update) => {
if (update.docChanged && onChange) if (update.docChanged && onChange)
setPlainTextValue(update.state.doc.toJSON().join('\n')) debounced(update.state.doc.toJSON().join('\n'))
}) })
const extensions = [ const extensions = [
updateListenerExtension, updateListenerExtension,

View File

@ -49,6 +49,7 @@ import { deepEqual } from 'fast-equals'
import { stringify } from 'qs' import { stringify } from 'qs'
import { isChoiceInput, isConditionStep, sendRequest, omit } from 'utils' import { isChoiceInput, isConditionStep, sendRequest, omit } from 'utils'
import cuid from 'cuid' import cuid from 'cuid'
import { diff } from 'deep-object-diff'
export type TypebotInDashboard = Pick< export type TypebotInDashboard = Pick<
Typebot, Typebot,
@ -253,12 +254,35 @@ export const checkIfTypebotsAreEqual = (typebotA: Typebot, typebotB: Typebot) =>
export const checkIfPublished = ( export const checkIfPublished = (
typebot: Typebot, typebot: Typebot,
publicTypebot: PublicTypebot publicTypebot: PublicTypebot,
) => debug?: boolean
deepEqual(typebot.blocks, publicTypebot.blocks) && ) => {
deepEqual(typebot.settings, publicTypebot.settings) && if (debug)
deepEqual(typebot.theme, publicTypebot.theme) && console.log(
deepEqual(typebot.variables, publicTypebot.variables) diff(
JSON.parse(JSON.stringify(typebot.blocks)),
JSON.parse(JSON.stringify(publicTypebot.blocks))
)
)
return (
deepEqual(
JSON.parse(JSON.stringify(typebot.blocks)),
JSON.parse(JSON.stringify(publicTypebot.blocks))
) &&
deepEqual(
JSON.parse(JSON.stringify(typebot.settings)),
JSON.parse(JSON.stringify(publicTypebot.settings))
) &&
deepEqual(
JSON.parse(JSON.stringify(typebot.theme)),
JSON.parse(JSON.stringify(publicTypebot.theme))
) &&
deepEqual(
JSON.parse(JSON.stringify(typebot.variables)),
JSON.parse(JSON.stringify(publicTypebot.variables))
)
)
}
export const parseDefaultPublicId = (name: string, id: string) => export const parseDefaultPublicId = (name: string, id: string) =>
toKebabCase(name) + `-${id?.slice(-7)}` toKebabCase(name) + `-${id?.slice(-7)}`