import { FeatureName } from '@/Controllers/FeatureName' import { FeaturesController } from '@/Controllers/FeaturesController' import { FilesController } from '@/Controllers/FilesController' import { LinkingController } from '@/Controllers/LinkingController' import { classNames } from '@standardnotes/utils' import { getLinkingSearchResults } from '@/Utils/Items/Search/getSearchResults' import { observer } from 'mobx-react-lite' import { ChangeEventHandler, useEffect, useRef, useState } from 'react' import { useApplication } from '../ApplicationView/ApplicationProvider' import ClearInputButton from '../ClearInputButton/ClearInputButton' import Icon from '../Icon/Icon' import DecoratedInput from '../Input/DecoratedInput' import LinkedItemSearchResults from './LinkedItemSearchResults' import { LinkedItemsSectionItem } from './LinkedItemsSectionItem' const LinkedItemsPanel = ({ linkingController, filesController, featuresController, isOpen, }: { linkingController: LinkingController filesController: FilesController featuresController: FeaturesController isOpen: boolean }) => { const { tags, linkedFiles, filesLinkingToActiveItem, notesLinkedToItem, notesLinkingToActiveItem, allItemLinks: allLinkedItems, linkItemToSelectedItem, unlinkItemFromSelectedItem, activateItem, createAndAddNewTag, isEntitledToNoteLinking, activeItem, } = linkingController const { entitledToFiles } = featuresController const application = useApplication() const fileInputRef = useRef(null) const searchInputRef = useRef(null) const [searchQuery, setSearchQuery] = useState('') const isSearching = !!searchQuery.length const { linkedResults, unlinkedItems, shouldShowCreateTag } = getLinkingSearchResults( searchQuery, application, activeItem, ) useEffect(() => { if (isOpen && searchInputRef.current) { searchInputRef.current.focus() } }, [isOpen]) const handleFileInputChange: ChangeEventHandler = (event) => { const files = event.currentTarget.files if (!files) { return } for (let i = 0; i < files.length; i++) { void filesController.uploadNewFile(files[i]).then((uploadedFiles) => { if (uploadedFiles) { void linkItemToSelectedItem(uploadedFiles[0]) } }) } } const selectAndUploadFiles = () => { if (!entitledToFiles) { void featuresController.showPremiumAlert(FeatureName.Files) return } if (!fileInputRef.current) { return } fileInputRef.current.click() } return (
{ setSearchQuery('') searchInputRef.current?.focus() }} /> ), ]} />
{isSearching ? ( <> {(!!unlinkedItems.length || shouldShowCreateTag) && (
Unlinked
{ setSearchQuery('') searchInputRef.current?.focus() }} />
)} {!!linkedResults.length && (
Linked
{linkedResults.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
)} ) : ( <> {!!tags.length && (
Linked Tags
{tags.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
)}
Linked Files
{linkedFiles.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
{!!filesLinkingToActiveItem.length && (
Files Linking To Current File
{filesLinkingToActiveItem.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
)} {!!notesLinkedToItem.length && (
Linked Notes
{notesLinkedToItem.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
)} {!!notesLinkingToActiveItem.length && (
Notes Linking To This Note
{notesLinkingToActiveItem.map((link) => ( unlinkItemFromSelectedItem(link.item)} activateItem={activateItem} handleFileAction={filesController.handleFileAction} /> ))}
)} )}
) } export default observer(LinkedItemsPanel)