diff --git a/src/app/actions/fileEntity.ts b/src/app/actions/fileEntity.ts index 4091863..2dfc5f5 100644 --- a/src/app/actions/fileEntity.ts +++ b/src/app/actions/fileEntity.ts @@ -81,67 +81,6 @@ export async function removeUserFile(fileId: string, fullDelete: number) { } } -export async function searchUserFiles(fileId: string) { - const token = await getSessionData('token'); - - try { - const response = await fetch(`${API_BASE_URL}/api/v1/files/${fileId}/similar?similarityLevels=DUPLICATE,SIMILARITY&auth_token=${token}`, { - method: 'GET' - }); - - if (response.ok) { - let parsed = await response.json(); - - if (parsed.message_code === 0) { - return parsed.message_body; - } else { - throw parsed; - } - } else { - throw (`${response.status}`); - } - - } catch (error) { - return error - } -} - -export async function searchGlobalFiles(fileId: string) { - const token = await getSessionData('token'); - - try { - const response = await fetch(`${API_BASE_URL}/api/v1/data`, { - method: 'POST', - body: JSON.stringify({ - version: 1, - msg_id: 20007, - message_body: { - file_id: fileId, - } - }), - headers: { - 'Content-Type': 'application/json', - 'Accept': 'application/json' - } - }); - - if (response.ok) { - let parsed = await response.json(); - - if (parsed.message_code === 0) { - return parsed.message_body; - } else { - throw parsed; - } - } else { - throw (`${response.status}`); - } - - } catch (error) { - return error - } -} - export async function viewFileInfo(fileId: string) { const token = await getSessionData('token'); diff --git a/src/app/actions/searchActions.ts b/src/app/actions/searchActions.ts new file mode 100644 index 0000000..e4689fc --- /dev/null +++ b/src/app/actions/searchActions.ts @@ -0,0 +1,90 @@ +'use server' + +import { getSessionData } from '@/app/actions/session'; +import { API_BASE_URL } from '@/app/actions/definitions'; + +export async function searchUserFiles(fileId: string) { + const token = await getSessionData('token'); + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/files/${fileId}/similar?similarityLevels=DUPLICATE,SIMILARITY&auth_token=${token}`, { + method: 'GET' + }); + + if (response.ok) { + let parsed = await response.json(); + + if (parsed.message_code === 0) { + return parsed.message_body; + } else { + throw parsed; + } + } else { + throw (`${response.status}`); + } + + } catch (error) { + return error + } +} + +export async function searchGlobalFiles(fileId: string) { + const token = await getSessionData('token'); + + try { + const response = await fetch(`${API_BASE_URL}/api/v1/data`, { + method: 'POST', + body: JSON.stringify({ + version: 1, + msg_id: 20007, + message_body: { + file_id: fileId, + } + }), + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }); + + if (response.ok) { + let parsed = await response.json(); + + if (parsed.message_code === 0) { + return parsed.message_body; + } else { + throw parsed; + } + } else { + throw (`${response.status}`); + } + + } catch (error) { + return error + } +} + +export async function getSearchStats() { + const token = await getSessionData('token'); + + try { + const response = await fetch(`${API_BASE_URL}/api/check/file_stats`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'Authorization': `Bearer ${token}`, + } + }); + + + + if (response.ok) { + let parsed = await response.json(); + + return parsed; + } + } catch (error) { + console.error('error'); + } +} \ No newline at end of file diff --git a/src/app/ui/search/file-search-panel.tsx b/src/app/ui/search/file-search-panel.tsx index 3b1dfd1..75f0c01 100644 --- a/src/app/ui/search/file-search-panel.tsx +++ b/src/app/ui/search/file-search-panel.tsx @@ -1,8 +1,8 @@ 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/fileEntity'; -import { fa } from 'zod/v4/locales'; +import { searchUserFiles, searchGlobalFiles } from '@/app/actions/searchActions'; +import { useQueryClient } from '@tanstack/react-query'; export function FileSearchPanel({ fileId, ref }: { fileId: string | null, ref: any }) { const [searchedUserFiles, setSearchedUserFiles] = useState([]); @@ -10,6 +10,7 @@ export function FileSearchPanel({ fileId, ref }: { fileId: string | null, ref: a const [searchedGlobalFiles, setSearchedGlobalFiles] = useState([]); const [searchedGlobalFilesShowNull, setSearchedGlobalFilesShowNull] = useState(false); const [loading, setLoading] = useState(false); + const queryClient = useQueryClient(); useImperativeHandle(ref, () => ({ clearList: () => { @@ -45,6 +46,7 @@ export function FileSearchPanel({ fileId, ref }: { fileId: string | null, ref: a try { let result = await searchGlobalFiles(fileId); + console.log(result); if (result.images.length) { setSearchedGlobalFiles(result.images); } else { @@ -53,6 +55,8 @@ export function FileSearchPanel({ fileId, ref }: { fileId: string | null, ref: a } catch (error) { } finally { + await queryClient.invalidateQueries({ queryKey: ['userData'] }); + await queryClient.invalidateQueries({ queryKey: ['userSearchData'] }); setLoading(false); } diff --git a/src/app/ui/search/search-stats.tsx b/src/app/ui/search/search-stats.tsx index 9e33d02..5bb8466 100644 --- a/src/app/ui/search/search-stats.tsx +++ b/src/app/ui/search/search-stats.tsx @@ -1,4 +1,26 @@ +'use client' + +import { useQuery } from '@tanstack/react-query'; +import { getSearchStats } from '@/app/actions/searchActions'; +import { useEffect } from 'react'; + export function SearchStats() { + const { + data: userSearchData, + isLoading, + isError, + error, + } = useQuery({ + queryKey: ['userSearchData'], + queryFn: () => { + return getSearchStats(); + }, + }); + + useEffect(() => { + console.log(userSearchData); + }, [userSearchData]); + return (
@@ -7,23 +29,37 @@ export function SearchStats() {
Изображения - 0 + + {userSearchData?.image?.count ? userSearchData?.image?.count : 0} +
+
Видео - 0 + + {userSearchData?.video?.count ? userSearchData?.video?.count : 0} +
+
Аудио - 0 + + {userSearchData?.audio?.count ? userSearchData?.audi?.count : 0} +
+
Документы - 0 + + {userSearchData?.document?.count ? userSearchData?.document?.count : 0} +
+
Всего поисков - 0 + + {userSearchData?.allCountForPeriod ? userSearchData?.allCountForPeriod : 0} +
)