Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/cloud/components/EventSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { getUniqueFolderAndDocIdsFromResourcesIds } from '../lib/utils/patterns'
import { getAccessToken, useElectron } from '../lib/stores/electron'
import { useNotifications } from '../../design/lib/stores/notifications'
import { useComments } from '../lib/stores/comments'
import { useBlocks } from '../lib/stores/blocks'

interface EventSourceProps {
teamId: string
Expand Down Expand Up @@ -67,6 +68,7 @@ const EventSource = ({ teamId }: EventSourceProps) => {
} = useGlobalData()
const { commentsEventListener } = useComments()
const { notificationsEventListener } = useNotifications()
const { blockEventListener } = useBlocks()

const setupEventSource = useCallback(
(url: string) => {
Expand Down Expand Up @@ -452,6 +454,11 @@ const EventSource = ({ teamId }: EventSourceProps) => {
case 'notificationViewed':
notificationsEventListener(event)
break
case 'blockCreated':
case 'blockDeleted':
case 'blockUpdated':
blockEventListener(event)
break
}
updateAppEventsMap([event.id, event])
}
Expand All @@ -474,6 +481,7 @@ const EventSource = ({ teamId }: EventSourceProps) => {
smartFolderDeleteHandler,
updateAppEventsMap,
notificationsEventListener,
blockEventListener,
])

return null
Expand Down
3 changes: 3 additions & 0 deletions src/cloud/interfaces/db/appEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export type SseEventType =
| 'smartFolderDelete'
| 'notificationCreated'
| 'notificationViewed'
| 'blockCreated'
| 'blockUpdated'
| 'blockDeleted'

export interface SerializableAppEventProps {
id: string
Expand Down
26 changes: 23 additions & 3 deletions src/cloud/lib/stores/blocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createStoreContext } from '../../utils/context'
import { useRef, useCallback } from 'react'
import { useRef, useCallback, useEffect } from 'react'
import { Block, getBlockTree } from '../../../api/blocks'
import { useToast } from '../../../../design/lib/stores/toast'
import { SerializedAppEvent } from '../../../interfaces/db/appEvents'

type BlocksObserver = (blocks: Block) => void

Expand All @@ -11,7 +12,7 @@ function useBlocksStore() {
const treeObservers = useRef<Map<string, Set<BlocksObserver>>>(new Map())

const getBlocks = useCallback(
async (rootBlock: string) => {
async (rootBlock: string, suppressError = false) => {
try {
const blocks = await getBlockTree(rootBlock)
treeCache.current.set(rootBlock, blocks)
Expand All @@ -21,7 +22,9 @@ function useBlocksStore() {
}
return true
} catch (err) {
pushApiErrorMessage(err)
if (!suppressError) {
pushApiErrorMessage(err)
}
return false
}
},
Expand All @@ -46,9 +49,26 @@ function useBlocksStore() {
[getBlocks]
)

const getBlocksRef = useRef(getBlocks)
useEffect(() => {
getBlocksRef.current = getBlocks
}, [getBlocks])
const blockEventListener = useCallback(async (event: SerializedAppEvent) => {
switch (event.type) {
case 'blockCreated':
case 'blockUpdated':
case 'blockDeleted': {
if (typeof event.data.rootBlockId === 'string') {
getBlocksRef.current(event.data.rootBlockId, true)
}
}
}
}, [])

return {
observeDocBlocks,
getBlocks,
blockEventListener,
}
}

Expand Down