Skip to content

Commit

Permalink
refactor(presentation): use typed connection status
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Dec 10, 2024
1 parent e88f99d commit 8ef93fb
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 16 deletions.
1 change: 1 addition & 0 deletions packages/presentation/src/PresentationTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export default function PresentationTool(props: {
)
})

// @todo This won't work for multiple window contexts?
comlink.on('visual-editing/refreshing', (data) => {
if (data.source === 'manual') {
clearTimeout(refreshRef.current)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type {Status} from '@sanity/comlink'
import {memo, startTransition, useEffect, useMemo, useState} from 'react'
import {type SanityDocument} from 'sanity'
import {getPublishedId, useEditState} from '../internals'
import type {VisualEditingConnection} from '../types'
import type {ConnectionStatus, VisualEditingConnection} from '../types'

export interface PostMessageRefreshMutationsProps {
id: string
type: string
comlink: VisualEditingConnection
previewKitConnection: Status
loadersConnection: Status
previewKitConnection: ConnectionStatus
loadersConnection: ConnectionStatus
}

function PostMessageRefreshMutations(props: PostMessageRefreshMutationsProps): React.ReactNode {
Expand Down
1 change: 1 addition & 0 deletions packages/presentation/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './plugin'
export type {
CombinedSearchParams,
ConnectionStatus,
ContextFn,
DocumentLocation,
DocumentLocationResolver,
Expand Down
15 changes: 8 additions & 7 deletions packages/presentation/src/preview/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type {Status} from '@sanity/comlink'
import {
Button,
Card,
Expand Down Expand Up @@ -34,7 +33,12 @@ import {
type DispatchPresentationAction,
type PresentationState,
} from '../reducers/presentationReducer'
import type {HeaderOptions, PresentationPerspective, PresentationViewport} from '../types'
import type {
ConnectionStatus,
HeaderOptions,
PresentationPerspective,
PresentationViewport,
} from '../types'
import {usePresentationTool} from '../usePresentationTool'
import {IFrame} from './IFrame'
import {usePresentationPreviewHeader} from './PreviewHeader'
Expand All @@ -48,12 +52,12 @@ export interface PreviewProps extends Pick<PresentationState, 'iframe' | 'visual
dispatch: DispatchPresentationAction
header?: HeaderOptions
initialUrl: URL
loadersConnection: Status
loadersConnection: ConnectionStatus
navigatorEnabled: boolean
onPathChange: (nextPath: string) => void
onRefresh: (fallback: () => void) => void
openPopup: (url: string) => void
overlaysConnection: Status
overlaysConnection: ConnectionStatus
perspective: PresentationPerspective
previewUrl?: string
setPerspective: (perspective: 'previewDrafts' | 'published') => void
Expand Down Expand Up @@ -148,9 +152,6 @@ export const Preview = memo(
}, MAX_TIME_TO_OVERLAYS_CONNECTION)
return () => clearTimeout(timeout)
}
if (overlaysConnection === 'disconnected') {
setSomethingIsWrong(true)
}
return
}, [loading, overlaysConnection, refreshing, showOverlaysConnectionStatus])

Expand Down
2 changes: 2 additions & 0 deletions packages/presentation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,5 @@ export type VisualEditingConnection = ChannelInstance<
* @internal
*/
export type LoaderConnection = ChannelInstance<LoaderControllerMsg, LoaderNodeMsg>

export type ConnectionStatus = 'connected' | 'connecting' | 'reconnecting' | 'idle'
12 changes: 7 additions & 5 deletions packages/presentation/src/useStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {Status, StatusEvent} from '@sanity/comlink'
import type {StatusEvent} from '@sanity/comlink'
import {useCallback, useMemo, useState} from 'react'
import type {ConnectionStatus} from './types'

/**
* A hook that manages and returns the connection status of multiple channels
Expand All @@ -16,10 +17,10 @@ import {useCallback, useMemo, useState} from 'react'
* The function to update the status takes a `StatusEvent` object which includes
* the channel and the status
*/
export function useStatus(): [string, (event: StatusEvent) => void] {
export function useStatus(): [ConnectionStatus, (event: StatusEvent) => void] {
// State to keep track of the status of each channel
const [statusMap, setStatusMap] = useState(
new Map<string, {status: Status; hasConnected: boolean}>(),
new Map<string, {status: ConnectionStatus; hasConnected: boolean}>(),
)

// Memoized computation of the overall status based on the status of individual channels
Expand All @@ -30,7 +31,7 @@ export function useStatus(): [string, (event: StatusEvent) => void] {
return 'connected'
}
// If the initial connection is being established, return `connecting` status
const handshaking = values.filter(({status}) => status === 'handshaking')
const handshaking = values.filter(({status}) => status === 'connecting')
if (handshaking.length) {
return handshaking.some(({hasConnected}) => !hasConnected) ? 'connecting' : 'reconnecting'
}
Expand All @@ -49,7 +50,8 @@ export function useStatus(): [string, (event: StatusEvent) => void] {
// Update the status and connection flag for the channel
const hasConnected =
next.get(event.connection)?.hasConnected || event.status === 'connected'
next.set(event.connection, {status: event.status, hasConnected})
const status = event.status === 'handshaking' ? 'connecting' : event.status
next.set(event.connection, {status, hasConnected})
}
return next
})
Expand Down

0 comments on commit 8ef93fb

Please sign in to comment.