import { useSearchParams } from 'react-router-dom'; import { useEffect, useState } from 'react'; import loadSearch, { SearchResultsType } from '../api/loader/loadSearch'; import VideoList from '../components/VideoList'; import ChannelList from '../components/ChannelList'; import PlaylistList from '../components/PlaylistList'; import SubtitleList from '../components/SubtitleList'; import { ViewStyles } from '../configuration/constants/ViewStyle'; import EmbeddableVideoPlayer from '../components/EmbeddableVideoPlayer'; import SearchExampleQueries from '../components/SearchExampleQueries'; import { useUserConfigStore } from '../stores/UserConfigStore'; import { ApiResponseType } from '../functions/APIClient'; const EmptySearchResponse: ApiResponseType = { data: { results: { video_results: [], channel_results: [], playlist_results: [], fulltext_results: [], }, queryType: 'simple', }, error: undefined, status: 200, }; const Search = () => { const { userConfig } = useUserConfigStore(); const [searchParams] = useSearchParams(); const videoId = searchParams.get('videoId'); const viewVideos = userConfig.view_style_home; const viewChannels = userConfig.view_style_channel; const viewPlaylists = userConfig.view_style_playlist; const gridItems = userConfig.grid_items || 3; const [searchTerm, setSearchTerm] = useState(''); const [debouncedSearchTerm, setDebouncedSearchTerm] = useState(''); const [searchResults, setSearchResults] = useState>(); const [refresh, setRefresh] = useState(false); const { data: searchResultsData } = searchResults ?? {}; const videoList = searchResultsData?.results.video_results; const channelList = searchResultsData?.results.channel_results; const playlistList = searchResultsData?.results.playlist_results; const fulltextList = searchResultsData?.results.fulltext_results; const queryType = searchResultsData?.queryType; const hasSearchQuery = searchTerm.length > 0; const hasVideos = Number(videoList?.length) > 0; const hasChannels = Number(channelList?.length) > 0; const hasPlaylist = Number(playlistList?.length) > 0; const hasFulltext = Number(fulltextList?.length) > 0; const isSimpleQuery = queryType === 'simple'; const isVideoQuery = queryType === 'video' || isSimpleQuery; const isChannelQuery = queryType === 'channel' || isSimpleQuery; const isPlaylistQuery = queryType === 'playlist' || isSimpleQuery; const isFullTextQuery = queryType === 'full' || isSimpleQuery; const isGridView = viewVideos === ViewStyles.grid; const gridView = isGridView ? `boxed-${gridItems}` : ''; const gridViewGrid = isGridView ? `grid-${gridItems}` : ''; useEffect(() => { const handler = setTimeout(() => { setDebouncedSearchTerm(searchTerm); }, 300); return () => { clearTimeout(handler); }; }, [searchTerm]); useEffect(() => { if (debouncedSearchTerm.trim() !== '') { fetchResults(debouncedSearchTerm); } else { setSearchResults(EmptySearchResponse); } }, [debouncedSearchTerm, refresh, videoId]); const fetchResults = async (searchQuery: string) => { const searchResults = await loadSearch(searchQuery); setSearchResults(searchResults); setRefresh(false); }; return ( <> TubeArchivist

Search your Archive

{ setSearchTerm(event.currentTarget.value); }} onKeyDown={event => { if (event.key === 'Enter') { fetchResults(searchTerm); } }} />
{hasSearchQuery && isVideoQuery && (

Video Results

)} {hasSearchQuery && isChannelQuery && (

Channel Results

)} {hasSearchQuery && isPlaylistQuery && (

Playlist Results

)} {hasSearchQuery && isFullTextQuery && (

Fulltext Results

)}
{!hasVideos && !hasChannels && !hasPlaylist && !hasFulltext && }
); }; export default Search;