Skip to content

Commit

Permalink
fix: allow FormView to be compiled
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 26, 2024
1 parent f3fbe57 commit 706c5f0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 117 deletions.
1 change: 1 addition & 0 deletions packages/sanity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@
"tar-fs": "^2.1.1",
"tar-stream": "^3.1.7",
"use-device-pixel-ratio": "^1.1.0",
"use-effect-event": "^1.0.2",
"use-hot-module-reload": "^2.0.0",
"use-sync-external-store": "^1.2.0",
"vite": "^4.5.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
useDocumentStore,
useTranslation,
} from 'sanity'
import {useEffectEvent} from 'use-effect-event'

import {Delay} from '../../../../components'
import {structureLocaleNamespace} from '../../../../i18n'
Expand All @@ -28,8 +29,6 @@ interface FormViewProps {
margins: [number, number, number, number]
}

const preventDefault = (ev: FormEvent) => ev.preventDefault()

export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormView(props, ref) {
const {hidden, margins} = props

Expand Down Expand Up @@ -103,21 +102,23 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
}, [documentId, documentStore, documentType, patchChannel])

const hasRev = Boolean(value?._rev)
const handleInitialRev = useEffectEvent(() => {
// this is a workaround for an issue that caused the document pushed to withDocument to get
// stuck at the first initial value.
// This effect is triggered only when the document goes from not having a revision, to getting one
// so it will kick in as soon as the document is received from the backend
patchChannel.publish({
type: 'mutation',
patches: [],
snapshot: value,
})
})
useEffect(() => {
if (hasRev) {
// this is a workaround for an issue that caused the document pushed to withDocument to get
// stuck at the first initial value.
// This effect is triggered only when the document goes from not having a revision, to getting one
// so it will kick in as soon as the document is received from the backend
patchChannel.publish({
type: 'mutation',
patches: [],
snapshot: value,
})
handleInitialRev()
}
// React to changes in hasRev only
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [hasRev])
// React to changes in hasRev only, useEffectEvent let's us do this without supressing the linter
}, [handleInitialRev, hasRev])

const [formRef, setFormRef] = useState<null | HTMLDivElement>(null)

Expand Down Expand Up @@ -165,7 +166,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
width={1}
>
<PresenceOverlay margins={margins}>
<Box as="form" onSubmit={preventDefault} ref={setRef}>
<Box as="form" onSubmit={handleSubmit} ref={setRef}>
{connectionState === 'connecting' && !editState?.draft && !editState?.published ? (
<Delay ms={300}>
{/* TODO: replace with loading block */}
Expand Down Expand Up @@ -246,3 +247,7 @@ function prepareRebaseEvent(event: DocumentRebaseEvent): PatchMsg {
),
}
}

function handleSubmit(ev: FormEvent) {
ev.preventDefault()
}
Loading

0 comments on commit 706c5f0

Please sign in to comment.