Skip to content

Commit

Permalink
[desk-tool] Use presence when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuslundgard authored and rexxars committed Oct 6, 2020
1 parent ae902fc commit ce48f97
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface DocumentPaneProps {
onExpand?: () => void
onCollapse?: () => void
paneKey: string
presence: any
schemaType: any
title?: string
type: any
Expand All @@ -54,7 +53,6 @@ export function DocumentPane(props: DocumentPaneProps) {
// options,
paneKey,
title: paneTitle,
presence,
schemaType,
value,
views = []
Expand Down Expand Up @@ -148,7 +146,6 @@ export function DocumentPane(props: DocumentPaneProps) {
onSetActiveView={handleSetActiveView}
onSplitPane={handleSplitPane}
paneTitle={paneTitle}
presence={presence}
schemaType={schemaType}
toggleInspect={toggleInspect}
value={value}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react'
import {useDocumentPresence} from '@sanity/base/hooks'
import {
useConnectionState,
useDocumentOperation,
Expand Down Expand Up @@ -50,7 +49,6 @@ export const DocumentPaneProvider = withInitialValue((props: Props) => {
const {patch}: any = useDocumentOperation(documentIdRaw, documentTypeName)
const editState: any = useEditState(documentIdRaw, documentTypeName)
const {markers} = useValidationStatus(documentIdRaw, documentTypeName)
const presence = useDocumentPresence(documentIdRaw)
const connectionState = useConnectionState(documentIdRaw, documentTypeName)
const schemaType = schema.get(documentTypeName)

Expand Down Expand Up @@ -118,7 +116,6 @@ export const DocumentPaneProvider = withInitialValue((props: Props) => {
initialValue={initialValue}
markers={markers}
onChange={onChange}
presence={presence}
schemaType={schemaType}
value={value}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ interface DocumentPanelProps {
isHistoryOpen: boolean
markers: any
menuItemGroups: MenuItemGroup[]
presence?: any
onChange: (patches: any[]) => void
onCloseView: () => void
onSetActiveView: (id: string | null) => void
Expand Down Expand Up @@ -111,7 +110,6 @@ export function DocumentPanel(props: DocumentPanelProps) {
initialValue={props.initialValue}
markers={props.markers}
onChange={props.onChange}
presence={props.presence}
readOnly={historyDisplayed === 'from'}
ref={formRef}
schemaType={props.schemaType}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-explicit-any */

import React, {FormEvent} from 'react'
import {useDocumentPresence} from '@sanity/base/hooks'
import {FormBuilder} from 'part:@sanity/form-builder'
import documentStore from 'part:@sanity/base/datastore/document'
import {tap} from 'rxjs/operators'
import React, {FormEvent, useEffect, useMemo, useRef, memo} from 'react'
import {Subscription} from 'rxjs'
import {tap} from 'rxjs/operators'

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

Expand All @@ -27,60 +27,62 @@ interface Props {
readOnly: boolean
schema: Schema
type: SchemaType
presence: any
}

export class EditForm extends React.PureComponent<Props> {
subscription?: Subscription
patchChannel = FormBuilder.createPatchChannel()
export const EditForm = memo((props: Props) => {
const presence = useDocumentPresence(props.id)
const subscriptionRef = useRef<Subscription | null>(null)
const patchChannel = useMemo(() => FormBuilder.createPatchChannel(), [])

componentDidMount() {
this.subscription = documentStore.pair
.documentEvents(this.props.id, this.props.type.name)
useEffect(() => {
subscriptionRef.current = documentStore.pair
.documentEvents(props.id, props.type.name)
.pipe(
tap((event: any) => {
this.patchChannel.receiveEvent(event)
patchChannel.receiveEvent(event)
})
)
.subscribe()
}

componentWillUnmount() {
if (this.subscription) this.subscription.unsubscribe()
}
return () => {
if (subscriptionRef.current) {
subscriptionRef.current.unsubscribe()
subscriptionRef.current = null
}
}
}, [])

render() {
const {
filterField,
focusPath,
markers,
value,
onBlur,
onFocus,
onChange,
readOnly,
schema,
type,
presence
} = this.props
const {
filterField,
focusPath,
markers,
value,
onBlur,
onFocus,
onChange,
readOnly,
schema,
type
} = props

return (
<form onSubmit={preventDefault}>
<FormBuilder
schema={schema}
patchChannel={this.patchChannel}
value={value || {_type: type}}
type={type}
presence={presence}
filterField={filterField}
readOnly={readOnly}
onBlur={onBlur}
onFocus={onFocus}
focusPath={focusPath}
onChange={onChange}
markers={markers}
/>
</form>
)
}
}
return (
<form onSubmit={preventDefault}>
<FormBuilder
schema={schema}
patchChannel={patchChannel}
value={value || {_type: type}}
type={type}
presence={presence}
filterField={filterField}
readOnly={readOnly}
onBlur={onBlur}
onFocus={onFocus}
focusPath={focusPath}
onChange={onChange}
markers={markers}
/>
</form>
)
})

EditForm.displayName = 'EditForm'
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface Props {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
markers: Array<{path: any[]}>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
presence: any
initialFocusPath: unknown[] | null
}

Expand Down Expand Up @@ -109,7 +108,7 @@ export class FormView extends React.PureComponent<Props> {
}

render() {
const {id, value, initialValue, markers, presence, schemaType} = this.props
const {id, value, initialValue, markers, schemaType} = this.props
const {focusPath, filterField} = this.state
const readOnly = this.isReadOnly()
const documentId = value && value._id && value._id.replace(/^drafts\./, '')
Expand Down Expand Up @@ -142,7 +141,6 @@ export class FormView extends React.PureComponent<Props> {
readOnly={readOnly}
schema={schema}
type={schemaType}
presence={presence}
/>
</PresenceOverlay>

Expand Down

0 comments on commit ce48f97

Please sign in to comment.