Skip to content

Commit ed87e2a

Browse files
authoredMar 4, 2024
fix(sanity): preserve form (as readOnly) when reconnecting (#5884)
1 parent 023e7e6 commit ed87e2a

File tree

4 files changed

+44
-54
lines changed

4 files changed

+44
-54
lines changed
 

‎packages/sanity/src/structure/panes/document/documentPanel/documentViews/FormView.tsx

+38-48
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
FormBuilder,
1111
type FormDocumentValue,
1212
fromMutationPatches,
13-
LoadingBlock,
1413
type PatchMsg,
1514
PresenceOverlay,
1615
useDocumentPresence,
@@ -48,6 +47,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
4847
ready,
4948
formState,
5049
onFocus,
50+
connectionState,
5151
onBlur,
5252
onSetCollapsedPath,
5353
onPathOpen,
@@ -148,10 +148,6 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
148148
// [documentId]
149149
// )
150150

151-
if (!ready) {
152-
return <LoadingBlock showText />
153-
}
154-
155151
return (
156152
<Container
157153
hidden={hidden}
@@ -163,49 +159,7 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
163159
>
164160
<PresenceOverlay margins={margins}>
165161
<Box as="form" onSubmit={preventDefault} ref={setRef}>
166-
{ready ? (
167-
formState === null || hidden ? (
168-
<Box padding={2}>
169-
<Text>{t('document-view.form-view.form-hidden')}</Text>
170-
</Box>
171-
) : (
172-
<>
173-
<FormHeader
174-
documentId={documentId}
175-
schemaType={formState.schemaType}
176-
title={title}
177-
/>
178-
<FormBuilder
179-
__internal_fieldActions={fieldActions}
180-
__internal_patchChannel={patchChannel}
181-
collapsedFieldSets={collapsedFieldSets}
182-
collapsedPaths={collapsedPaths}
183-
focusPath={formState.focusPath}
184-
changed={formState.changed}
185-
focused={formState.focused}
186-
groups={formState.groups}
187-
id="root"
188-
members={formState.members}
189-
onChange={onChange}
190-
onFieldGroupSelect={onSetActiveFieldGroup}
191-
onPathBlur={onBlur}
192-
onPathFocus={onFocus}
193-
onPathOpen={onPathOpen}
194-
onSetFieldSetCollapsed={onSetCollapsedFieldSet}
195-
onSetPathCollapsed={onSetCollapsedPath}
196-
presence={presence}
197-
readOnly={formState.readOnly}
198-
schemaType={formState.schemaType}
199-
validation={validation}
200-
value={
201-
// note: the form state doesn't have a typed concept of a "document" value
202-
// but these should be compatible
203-
formState.value as FormDocumentValue
204-
}
205-
/>
206-
</>
207-
)
208-
) : (
162+
{connectionState === 'connecting' ? (
209163
<Delay ms={300}>
210164
{/* TODO: replace with loading block */}
211165
<Flex align="center" direction="column" height="fill" justify="center">
@@ -217,6 +171,42 @@ export const FormView = forwardRef<HTMLDivElement, FormViewProps>(function FormV
217171
</Box>
218172
</Flex>
219173
</Delay>
174+
) : formState === null || hidden ? (
175+
<Box padding={2}>
176+
<Text>{t('document-view.form-view.form-hidden')}</Text>
177+
</Box>
178+
) : (
179+
<>
180+
<FormHeader documentId={documentId} schemaType={formState.schemaType} title={title} />
181+
<FormBuilder
182+
__internal_fieldActions={fieldActions}
183+
__internal_patchChannel={patchChannel}
184+
collapsedFieldSets={collapsedFieldSets}
185+
collapsedPaths={collapsedPaths}
186+
focusPath={formState.focusPath}
187+
changed={formState.changed}
188+
focused={formState.focused}
189+
groups={formState.groups}
190+
id="root"
191+
members={formState.members}
192+
onChange={onChange}
193+
onFieldGroupSelect={onSetActiveFieldGroup}
194+
onPathBlur={onBlur}
195+
onPathFocus={onFocus}
196+
onPathOpen={onPathOpen}
197+
onSetFieldSetCollapsed={onSetCollapsedFieldSet}
198+
onSetPathCollapsed={onSetCollapsedPath}
199+
presence={presence}
200+
readOnly={connectionState === 'reconnecting' || formState.readOnly}
201+
schemaType={formState.schemaType}
202+
validation={validation}
203+
value={
204+
// note: the form state doesn't have a typed concept of a "document" value
205+
// but these should be compatible
206+
formState.value as FormDocumentValue
207+
}
208+
/>
209+
</>
220210
)}
221211
</Box>
222212
</PresenceOverlay>

‎packages/sanity/src/structure/panes/document/documentPanel/header/DocumentHeaderTitle.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {useDocumentPane} from '../../useDocumentPane'
66

77
export function DocumentHeaderTitle(): ReactElement {
88
const {connectionState, schemaType, title, value: documentValue} = useDocumentPane()
9-
const subscribed = Boolean(documentValue) && connectionState === 'connected'
9+
const subscribed = Boolean(documentValue) && connectionState !== 'connecting'
1010

1111
const {error, value} = useValuePreview({
1212
enabled: subscribed,
@@ -15,7 +15,7 @@ export function DocumentHeaderTitle(): ReactElement {
1515
})
1616
const {t} = useTranslation(structureLocaleNamespace)
1717

18-
if (connectionState !== 'connected') {
18+
if (connectionState === 'connecting') {
1919
return <></>
2020
}
2121

‎packages/sanity/src/structure/panes/document/documentPanel/header/DocumentPanelHeader.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const DocumentPanelHeader = memo(
3939
menuItemGroups,
4040
schemaType,
4141
timelineStore,
42-
ready,
42+
connectionState,
4343
views,
4444
unstable_languageFilter,
4545
} = useDocumentPane()
@@ -96,7 +96,7 @@ export const DocumentPanelHeader = memo(
9696
<PaneHeader
9797
border
9898
ref={ref}
99-
loading={!ready}
99+
loading={connectionState === 'connecting'}
100100
title={<DocumentHeaderTitle />}
101101
tabs={showTabs && <DocumentHeaderTabs />}
102102
tabIndex={tabIndex}

‎packages/sanity/src/structure/panes/document/useDocumentTitle.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ interface UseDocumentTitle {
2323
*/
2424
export function useDocumentTitle(): UseDocumentTitle {
2525
const {connectionState, schemaType, title, value: documentValue} = useDocumentPane()
26-
const subscribed = Boolean(documentValue) && connectionState === 'connected'
26+
const subscribed = Boolean(documentValue) && connectionState !== 'connecting'
2727

2828
const {error, value} = useValuePreview({
2929
enabled: subscribed,
3030
schemaType,
3131
value: documentValue,
3232
})
3333

34-
if (connectionState !== 'connected') {
34+
if (connectionState === 'connecting') {
3535
return {error: undefined, title: undefined}
3636
}
3737

0 commit comments

Comments
 (0)
Please sign in to comment.