import { SearchedUserFilesList } from '@/app/ui/search/searched-user-files-list'; import { SearchedGlobalFilesList } from '@/app/ui/search/searched-global-files-list'; import { useCallback, useState, useImperativeHandle } from 'react'; import { searchUserFiles, searchGlobalFiles } from '@/app/actions/searchActions'; import { useQueryClient } from '@tanstack/react-query'; import { Pagination } from '@/app/components/Pagination'; import { useTranslations } from 'next-intl'; import { AllowedExtensions } from '@/app/ui/search/section-search-file'; export interface SearchItem { url: string, pageTitle: string, height: number, width: number, host: string, pageUrl: string } export interface FileInfo { fileId: string, fileSize: number, hammingDistance: number, originalFileName: string, ownerId: number, similarityLevel: string, status: string, supportId: number, uploadDate: string, url: string } export function FileSearchPanel( { fileId, ref, allowedExtensions, fileType } : { fileId: string | null, ref: any, allowedExtensions: AllowedExtensions, fileType: string | null } ) { const [searchedUserFiles, setSearchedUserFiles] = useState([]); const [searchedUserFilesShowNull, setSearchedUserFilesShowNull] = useState(false); const [searchedGlobalFiles, setSearchedGlobalFiles] = useState([]); const [searchedGlobalFilesShowNull, setSearchedGlobalFilesShowNull] = useState(false); const [loading, setLoading] = useState(false); const [paginationLoading, setPaginationLoading] = useState(false); const [showGlobalSearch, setShowGlobalSearch] = useState(false); const queryClient = useQueryClient(); const [errorMessage, setErrorMessage] = useState(null); const [globalSearchErrorMessage, setGlobalSearchErrorMessage] = useState(null); const t = useTranslations('Global'); const [searchTotalPages, setSearchTotalPages] = useState(0); const [searchCurrentPages, setSearchCurrentPages] = useState(1); useImperativeHandle(ref, () => ({ clearList: () => { setSearchedUserFiles([]); setSearchedGlobalFiles([]); setSearchedUserFilesShowNull(false); setSearchedGlobalFilesShowNull(false); setShowGlobalSearch(false); setSearchCurrentPages(1); setSearchTotalPages(0); } })); const handlerSearchUserFile = useCallback(async (fileId: string, page: number): Promise => { setErrorMessage(null); setGlobalSearchErrorMessage(null); try { let result = await searchUserFiles(fileId); if (result.errorMessage) { throw result.errorMessage } if (result?.content?.length) { setSearchedUserFiles(result.content); const hasOwned = result?.content.some((item: { owner: boolean }) => { if (item.owner) { return true; } return false; }); if (hasOwned) { if (fileType === 'image') { setShowGlobalSearch(true); handlerSearchGlobalFile(fileId, page); } } } else { setSearchedUserFilesShowNull(true); if (fileType === 'image') { setShowGlobalSearch(true); handlerSearchGlobalFile(fileId, page); } } } catch (error) { if (error === 'file-for-search-unsupported') { setErrorMessage(error); } else { setErrorMessage('search-error'); } } finally { await queryClient.invalidateQueries({ queryKey: ['userData'] }); await queryClient.invalidateQueries({ queryKey: ['userSearchData'] }); } }, [fileId]) const handlerSearchGlobalFile = useCallback(async (fileId: string, page: number, actionFromPagination?: boolean): Promise => { setGlobalSearchErrorMessage(null); if (!actionFromPagination) { setLoading(true); setSearchedGlobalFilesShowNull(false); setSearchedGlobalFiles([]); } else { setPaginationLoading(true); } try { let result = await searchGlobalFiles(fileId, page); if (result.error) { throw result.error; } if (result.totalResults) { setSearchedGlobalFiles(result.images); setSearchTotalPages(result.totalPages); setSearchCurrentPages(page); } else { setSearchedGlobalFilesShowNull(true); } } catch (error) { if (error) { setGlobalSearchErrorMessage(error as string); } else { setGlobalSearchErrorMessage('error'); } } finally { /* await queryClient.invalidateQueries({ queryKey: ['userData'] }); await queryClient.invalidateQueries({ queryKey: ['userSearchData'] }); */ setLoading(false); setPaginationLoading(false); } }, [fileId]) return ( <>
{errorMessage && (
{t(errorMessage)}
)}
{(searchedUserFiles.length !== 0) && ( )} {searchedUserFilesShowNull && (
{t('search-results-title')}
{t('files-found', { count: searchedUserFiles.length })}

{t('no-similar-files-title')}

{t('no-similar-files-description')}

{t('hint-title')} {t('hint-text')}
)} {showGlobalSearch && (
{t('global-search-title')}
{t('global-search-description')}
{t('global-search-badge')}
{t('global-search-info-title')}
{t('global-search-info-text')}
{t('searching-internet')}
{t('found-on-internet')} {paginationLoading && (
)}
{(searchedGlobalFilesShowNull && !loading) && (

{t('no-similar-global-title')}

{t('no-similar-global-description')}

)} {globalSearchErrorMessage && ( <>
{t('search-error')}
)}
)} ); }