Skip to content

Commit

Permalink
fix: ensure comlink stays connected if navigate changes (#2082)
Browse files Browse the repository at this point in the history
* chore: find cause

* fix: handle comlink disconnect

* chore: remove debug code
  • Loading branch information
stipsan authored Nov 4, 2024
1 parent e2e5574 commit c9e738d
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 170 deletions.
21 changes: 8 additions & 13 deletions packages/presentation/src/PresentationTool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from 'sanity'
import {useRouter, type RouterContextValue} from 'sanity/router'
import {styled} from 'styled-components'
import {useEffectEvent} from 'use-effect-event'
import {
COMMENTS_INSPECTOR_NAME,
DEFAULT_TOOL_NAME,
Expand Down Expand Up @@ -241,6 +242,10 @@ export default function PresentationTool(props: {
}
}, [controller, popups])

const handleNavigate = useEffectEvent<typeof navigate>(
(nextState, nextSearchState, forceReplace) =>
navigate(nextState, nextSearchState, forceReplace),
)
useEffect(() => {
if (!controller) return

Expand All @@ -257,7 +262,7 @@ export default function PresentationTool(props: {

comlink.on('visual-editing/focus', (data) => {
if (!('id' in data)) return
navigate({
handleNavigate({
type: data.type,
id: data.id,
path: data.path,
Expand All @@ -267,7 +272,7 @@ export default function PresentationTool(props: {
comlink.on('visual-editing/navigate', (data) => {
const {title, url} = data
if (frameStateRef.current.url !== url) {
navigate({}, {preview: url})
handleNavigate({}, {preview: url})
}
frameStateRef.current = {title, url}
})
Expand Down Expand Up @@ -308,12 +313,11 @@ export default function PresentationTool(props: {

const stop = comlink.start()
setVisualEditingComlink(comlink)

return () => {
stop()
setVisualEditingComlink(null)
}
}, [controller, navigate, setDocumentsOnPage, setOverlaysConnection, targetOrigin])
}, [controller, handleNavigate, setDocumentsOnPage, setOverlaysConnection, targetOrigin])

useEffect(() => {
if (!controller) return
Expand Down Expand Up @@ -442,15 +446,6 @@ export default function PresentationTool(props: {
unstable_navigator,
})

// Handle edge case where the `&rev=` parameter gets "stuck"
const idRef = useRef<string | undefined>(params.id)
useEffect(() => {
if (params.rev && idRef.current && params.id !== idRef.current) {
navigate({}, {rev: undefined})
}
idRef.current = params.id
})

const refreshRef = useRef<number>()
const handleRefresh = useCallback(
(fallback: () => void) => {
Expand Down
Loading

0 comments on commit c9e738d

Please sign in to comment.