feat: search file
This commit is contained in:
parent
ba6c2d27d5
commit
48908106af
@ -36,8 +36,8 @@ export const putKbById = (data: KbUpdateParams) => PUT(`/plugins/kb/update`, dat
|
|||||||
export const delKbById = (id: string) => DELETE(`/plugins/kb/delete?id=${id}`);
|
export const delKbById = (id: string) => DELETE(`/plugins/kb/delete?id=${id}`);
|
||||||
|
|
||||||
/* kb file */
|
/* kb file */
|
||||||
export const getKbFiles = (kbId: string) =>
|
export const getKbFiles = (data: { kbId: string; searchText: string }) =>
|
||||||
GET<KbFileItemType[]>(`/plugins/kb/file/list`, { kbId });
|
GET<KbFileItemType[]>(`/plugins/kb/file/list`, data);
|
||||||
export const deleteKbFileById = (params: { fileId: string; kbId: string }) =>
|
export const deleteKbFileById = (params: { fileId: string; kbId: string }) =>
|
||||||
DELETE(`/plugins/kb/file/delFileByFileId`, params);
|
DELETE(`/plugins/kb/file/delFileByFileId`, params);
|
||||||
export const getFileInfoById = (fileId: string) =>
|
export const getFileInfoById = (fileId: string) =>
|
||||||
|
|||||||
@ -12,7 +12,9 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
try {
|
try {
|
||||||
await connectToDatabase();
|
await connectToDatabase();
|
||||||
|
|
||||||
const { kbId } = req.query as { kbId: string };
|
let { kbId, searchText } = req.query as { kbId: string; searchText: string };
|
||||||
|
searchText = searchText.replace(/'/g, '');
|
||||||
|
|
||||||
// 凭证校验
|
// 凭证校验
|
||||||
const { userId } = await authUser({ req, authToken: true });
|
const { userId } = await authUser({ req, authToken: true });
|
||||||
|
|
||||||
@ -20,7 +22,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
|
|||||||
const bucket = gridFs.GridFSBucket();
|
const bucket = gridFs.GridFSBucket();
|
||||||
|
|
||||||
const files = await bucket
|
const files = await bucket
|
||||||
.find({ ['metadata.kbId']: kbId })
|
.find({ ['metadata.kbId']: kbId, ...(searchText && { filename: { $regex: searchText } }) })
|
||||||
.sort({ _id: -1 })
|
.sort({ _id: -1 })
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
|
|||||||
@ -42,10 +42,19 @@ const FileCard = ({ kbId }: { kbId: string }) => {
|
|||||||
data: files = [],
|
data: files = [],
|
||||||
refetch,
|
refetch,
|
||||||
isInitialLoading
|
isInitialLoading
|
||||||
} = useQuery(['getFiles', kbId], () => getKbFiles(kbId), {
|
} = useQuery(['getFiles', kbId], () => getKbFiles({ kbId, searchText }), {
|
||||||
refetchInterval: 6000,
|
refetchInterval: 6000,
|
||||||
refetchOnWindowFocus: true
|
refetchOnWindowFocus: true
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const debounceRefetch = useCallback(
|
||||||
|
debounce(() => {
|
||||||
|
refetch();
|
||||||
|
lastSearch.current = searchText;
|
||||||
|
}, 300),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
const formatFiles = useMemo(
|
const formatFiles = useMemo(
|
||||||
() =>
|
() =>
|
||||||
files.map((file) => ({
|
files.map((file) => ({
|
||||||
@ -99,6 +108,7 @@ const FileCard = ({ kbId }: { kbId: string }) => {
|
|||||||
value={searchText}
|
value={searchText}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
setSearchText(e.target.value);
|
setSearchText(e.target.value);
|
||||||
|
debounceRefetch();
|
||||||
}}
|
}}
|
||||||
onBlur={() => {
|
onBlur={() => {
|
||||||
if (searchText === lastSearch.current) return;
|
if (searchText === lastSearch.current) return;
|
||||||
@ -144,11 +154,13 @@ const FileCard = ({ kbId }: { kbId: string }) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<Td display={'flex'} alignItems={'center'}>
|
<Td>
|
||||||
<Image src={file.icon} w={'16px'} mr={2} alt={''} />
|
<Flex alignItems={'center'}>
|
||||||
<Box maxW={['300px', '400px']} className="textEllipsis">
|
<Image src={file.icon} w={'16px'} mr={2} alt={''} />
|
||||||
{t(file.filename)}
|
<Box maxW={['300px', '400px']} className="textEllipsis">
|
||||||
</Box>
|
{t(file.filename)}
|
||||||
|
</Box>
|
||||||
|
</Flex>
|
||||||
</Td>
|
</Td>
|
||||||
<Td fontSize={'md'} fontWeight={'bold'}>
|
<Td fontSize={'md'} fontWeight={'bold'}>
|
||||||
{file.chunkLength}
|
{file.chunkLength}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user