refactor: de-couple linking controller from active item (#2108)
This commit is contained in:
33
packages/web/src/javascripts/Hooks/useItemLinks.ts
Normal file
33
packages/web/src/javascripts/Hooks/useItemLinks.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useApplication } from '@/Components/ApplicationProvider'
|
||||
import { useLinkingController } from '@/Controllers/LinkingControllerProvider'
|
||||
import { ContentType, DecryptedItem } from '@standardnotes/snjs'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
export const useItemLinks = (item: DecryptedItem | undefined) => {
|
||||
const application = useApplication()
|
||||
const linkingController = useLinkingController()
|
||||
const { getLinkedNotesForItem, getNotesLinkingToItem, getFilesLinksForItem, getLinkedTagsForItem } = linkingController
|
||||
|
||||
const [, refresh] = useState(Date.now())
|
||||
|
||||
const notesLinkedToItem = getLinkedNotesForItem(item) || []
|
||||
const notesLinkingToItem = getNotesLinkingToItem(item) || []
|
||||
const { filesLinkedToItem, filesLinkingToItem } = getFilesLinksForItem(item)
|
||||
const tagsLinkedToItem = getLinkedTagsForItem(item) || []
|
||||
|
||||
useEffect(
|
||||
() =>
|
||||
application.streamItems([ContentType.Note, ContentType.File, ContentType.Tag], () => {
|
||||
refresh(Date.now())
|
||||
}),
|
||||
[application],
|
||||
)
|
||||
|
||||
return {
|
||||
notesLinkedToItem,
|
||||
notesLinkingToItem,
|
||||
filesLinkedToItem,
|
||||
filesLinkingToItem,
|
||||
tagsLinkedToItem,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user