import clsx from 'clsx'; import React, { useCallback, useRef, useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { FaSearch } from 'react-icons/fa'; import { PiPlus } from 'react-icons/pi'; import { PiSelectionAll, PiSelectionAllFill } from 'react-icons/pi'; import { PiDotsThreeCircle } from 'react-icons/pi'; import { MdOutlineMenu } from 'react-icons/md'; import { IoMdCloseCircle } from 'react-icons/io'; import { useEnv } from '@/context/EnvContext'; import { useThemeStore } from '@/store/themeStore'; import { useTranslation } from '@/hooks/useTranslation'; import { useLibraryStore } from '@/store/libraryStore'; import { useTrafficLight } from '@/hooks/useTrafficLight'; import { useResponsiveSize } from '@/hooks/useResponsiveSize'; import { debounce } from '@/utils/debounce'; import useShortcuts from '@/hooks/useShortcuts'; import WindowButtons from '@/components/WindowButtons'; import Dropdown from '@/components/Dropdown'; import SettingsMenu from './SettingsMenu'; import ImportMenu from './ImportMenu'; import ViewMenu from './ViewMenu'; interface LibraryHeaderProps { isSelectMode: boolean; isSelectAll: boolean; onPullLibrary: () => void; onImportBooksFromFiles: () => void; onImportBooksFromDirectory?: () => void; onOpenCatalogManager: () => void; onToggleSelectMode: () => void; onSelectAll: () => void; onDeselectAll: () => void; } const LibraryHeader: React.FC = ({ isSelectMode, isSelectAll, onPullLibrary, onImportBooksFromFiles, onImportBooksFromDirectory, onOpenCatalogManager, onToggleSelectMode, onSelectAll, onDeselectAll, }) => { const _ = useTranslation(); const router = useRouter(); const searchParams = useSearchParams(); const { appService } = useEnv(); const { systemUIVisible, statusBarHeight } = useThemeStore(); const { currentBookshelf } = useLibraryStore(); const { isTrafficLightVisible } = useTrafficLight(); const [searchQuery, setSearchQuery] = useState(searchParams?.get('q') ?? ''); const headerRef = useRef(null); const iconSize18 = useResponsiveSize(18); const { safeAreaInsets: insets } = useThemeStore(); useShortcuts({ onToggleSelectMode, }); // eslint-disable-next-line react-hooks/exhaustive-deps const debouncedUpdateQueryParam = useCallback( debounce((value: string) => { const params = new URLSearchParams(searchParams?.toString()); if (value) { params.set('q', value); } else { params.delete('q'); } router.push(`?${params.toString()}`); }, 500), [searchParams], ); const handleSearchChange = (e: React.ChangeEvent) => { const newQuery = e.target.value; setSearchQuery(newQuery); debouncedUpdateQueryParam(newQuery); }; const windowButtonVisible = appService?.hasWindowBar && !isTrafficLightVisible; const currentBooksCount = currentBookshelf.reduce( (acc, item) => acc + ('books' in item ? item.books.length : 1), 0, ); if (!insets) return null; const isMobile = appService?.isMobile || window.innerWidth <= 640; return (
1 ? _('Search in {{count}} Book(s)...', { count: currentBooksCount, }) : _('Search Books...') } onChange={handleSearchChange} spellCheck='false' className={clsx( 'search-input input h-9 w-full rounded-full pr-[30%] ps-10 sm:h-7', 'bg-base-300/45 border-0', 'font-sans text-sm font-light', 'placeholder:text-base-content/50 truncate', 'focus:outline-none focus:ring-0', )} />
{searchQuery && ( )} } > {isMobile ? null : ( )}
{isSelectMode ? (
) : (
} > } > {appService?.hasWindowBar && ( )}
)}
); }; export default LibraryHeader;