import { Laptop, Mobile, Server } from '@sd/assets/icons'; import clsx from 'clsx'; import { useEffect, useState } from 'react'; import { Link, NavLink } from 'react-router-dom'; import { arraysEqual, useBridgeQuery, useDebugState, useFeatureFlag, useLibraryQuery, useOnlineLocations } from '@sd/client'; import { Button, Tooltip } from '@sd/ui'; import { AddLocationButton } from '~/app/$libraryId/settings/library/locations/AddLocationButton'; import { Folder, SubtleButton } from '~/components'; import SidebarLink from './Link'; import LocationsContextMenu from './LocationsContextMenu'; import Section from './Section'; import TagsContextMenu from './TagsContextMenu'; type TriggeredContextItem = | { type: 'location'; locationId: number; } | { type: 'tag'; tagId: number; }; const SEE_MORE_LOCATIONS_COUNT = 5; export const LibrarySection = () => { const debugState = useDebugState(); const node = useBridgeQuery(['nodeState']); const locationsQuery = useLibraryQuery(['locations.list'], { keepPreviousData: true }); const tags = useLibraryQuery(['tags.list'], { keepPreviousData: true }); const onlineLocations = useOnlineLocations(); const isPairingEnabled = useFeatureFlag('p2pPairing'); const [triggeredContextItem, setTriggeredContextItem] = useState( null ); const [seeMoreLocations, setSeeMoreLocations] = useState(false); const locations = locationsQuery.data?.slice( 0, seeMoreLocations ? undefined : SEE_MORE_LOCATIONS_COUNT ); useEffect(() => { const outsideClick = () => { document.addEventListener('click', () => { setTriggeredContextItem(null); }); }; outsideClick(); return () => { document.removeEventListener('click', outsideClick); }; }, [triggeredContextItem]); return ( <>
) } > {node.data && ( <> {node.data.name} {debugState.enabled && ( <> Spacephone Titan )} )}
} > {locations?.map((location) => { const online = onlineLocations.some((l) => arraysEqual(location.pub_id, l)); return ( setTriggeredContextItem({ type: 'location', locationId: location.id }) } className={clsx( triggeredContextItem?.type === 'location' && triggeredContextItem.locationId === location.id ? 'border-accent' : 'border-transparent', 'group relative w-full border' )} to={`location/${location.id}`} >
{location.name} ); })} {locationsQuery.data?.[SEE_MORE_LOCATIONS_COUNT] && (
setSeeMoreLocations(!seeMoreLocations)} className="mb-1 ml-2 mt-0.5 cursor-pointer text-center text-tiny font-semibold text-ink-faint/50 transition hover:text-accent" > See {seeMoreLocations ? 'less' : 'more'}
)}
{!!tags.data?.length && (
} >
{tags.data?.slice(0, 6).map((tag) => ( setTriggeredContextItem({ type: 'tag', tagId: tag.id }) } className={clsx( triggeredContextItem?.type === 'tag' && triggeredContextItem?.tagId === tag.id ? 'border-accent' : 'border-transparent', 'border' )} to={`tag/${tag.id}`} >
{tag.name} ))}
)} ); };