2024-02-05 12:01:27 +08:00
|
|
|
import showDeleteConfirm from '@/components/deleting-confirm';
|
2024-03-01 11:28:58 +08:00
|
|
|
import { KnowledgeSearchParams } from '@/constants/knowledge';
|
2024-03-06 19:17:45 +08:00
|
|
|
import { IKnowledge } from '@/interfaces/database/knowledge';
|
2024-02-18 18:18:20 +08:00
|
|
|
import { useCallback, useEffect, useMemo } from 'react';
|
2024-02-05 12:01:27 +08:00
|
|
|
import { useDispatch, useSearchParams, useSelector } from 'umi';
|
2024-02-02 18:49:54 +08:00
|
|
|
|
|
|
|
export const useKnowledgeBaseId = (): string => {
|
|
|
|
const [searchParams] = useSearchParams();
|
|
|
|
const knowledgeBaseId = searchParams.get('id');
|
|
|
|
|
|
|
|
return knowledgeBaseId || '';
|
|
|
|
};
|
2024-02-05 12:01:27 +08:00
|
|
|
|
2024-03-06 19:17:45 +08:00
|
|
|
export const useGetKnowledgeSearchParams = () => {
|
|
|
|
const [currentQueryParameters] = useSearchParams();
|
|
|
|
|
|
|
|
return {
|
|
|
|
documentId:
|
|
|
|
currentQueryParameters.get(KnowledgeSearchParams.DocumentId) || '',
|
|
|
|
knowledgeId:
|
|
|
|
currentQueryParameters.get(KnowledgeSearchParams.KnowledgeId) || '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-02-05 12:01:27 +08:00
|
|
|
export const useDeleteDocumentById = (): {
|
|
|
|
removeDocument: (documentId: string) => Promise<number>;
|
|
|
|
} => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const knowledgeBaseId = useKnowledgeBaseId();
|
|
|
|
|
|
|
|
const removeDocument = (documentId: string) => () => {
|
|
|
|
return dispatch({
|
|
|
|
type: 'kFModel/document_rm',
|
|
|
|
payload: {
|
|
|
|
doc_id: documentId,
|
|
|
|
kb_id: knowledgeBaseId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRmDocument = (documentId: string): Promise<number> => {
|
|
|
|
return showDeleteConfirm({ onOk: removeDocument(documentId) });
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
removeDocument: onRmDocument,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-03-06 19:17:45 +08:00
|
|
|
export const useFetchKnowledgeDetail = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { knowledgeId } = useGetKnowledgeSearchParams();
|
|
|
|
|
|
|
|
const fetchKnowledgeDetail = useCallback(
|
|
|
|
(knowledgeId: string) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'knowledgeModel/getKnowledgeDetail',
|
|
|
|
payload: { kb_id: knowledgeId },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchKnowledgeDetail(knowledgeId);
|
|
|
|
}, [fetchKnowledgeDetail, knowledgeId]);
|
|
|
|
|
|
|
|
return fetchKnowledgeDetail;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useSelectKnowledgeDetail = () => {
|
|
|
|
const knowledge: IKnowledge = useSelector(
|
|
|
|
(state: any) => state.knowledgeModel.knowledge,
|
2024-02-05 12:01:27 +08:00
|
|
|
);
|
|
|
|
|
2024-03-06 19:17:45 +08:00
|
|
|
return knowledge;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useGetDocumentDefaultParser = () => {
|
|
|
|
const item = useSelectKnowledgeDetail();
|
2024-02-05 12:01:27 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
defaultParserId: item?.parser_id ?? '',
|
|
|
|
parserConfig: item?.parser_config ?? '',
|
|
|
|
};
|
|
|
|
};
|
2024-02-07 18:04:25 +08:00
|
|
|
|
|
|
|
export const useDeleteChunkByIds = (): {
|
|
|
|
removeChunk: (chunkIds: string[], documentId: string) => Promise<number>;
|
|
|
|
} => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const removeChunk = useCallback(
|
|
|
|
(chunkIds: string[], documentId: string) => () => {
|
|
|
|
return dispatch({
|
|
|
|
type: 'chunkModel/rm_chunk',
|
|
|
|
payload: {
|
|
|
|
chunk_ids: chunkIds,
|
|
|
|
doc_id: documentId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onRemoveChunk = useCallback(
|
|
|
|
(chunkIds: string[], documentId: string): Promise<number> => {
|
|
|
|
return showDeleteConfirm({ onOk: removeChunk(chunkIds, documentId) });
|
|
|
|
},
|
|
|
|
[removeChunk],
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
removeChunk: onRemoveChunk,
|
|
|
|
};
|
|
|
|
};
|
2024-02-18 18:18:20 +08:00
|
|
|
|
|
|
|
export const useFetchKnowledgeBaseConfiguration = () => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const knowledgeBaseId = useKnowledgeBaseId();
|
|
|
|
|
|
|
|
const fetchKnowledgeBaseConfiguration = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'kSModel/getKbDetail',
|
|
|
|
payload: {
|
|
|
|
kb_id: knowledgeBaseId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}, [dispatch, knowledgeBaseId]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchKnowledgeBaseConfiguration();
|
|
|
|
}, [fetchKnowledgeBaseConfiguration]);
|
|
|
|
};
|
2024-02-20 18:10:20 +08:00
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
export const useFetchKnowledgeList = (
|
|
|
|
shouldFilterListWithoutDocument: boolean = false,
|
|
|
|
): IKnowledge[] => {
|
2024-02-20 18:10:20 +08:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
|
|
|
const knowledgeModel = useSelector((state: any) => state.knowledgeModel);
|
|
|
|
const { data = [] } = knowledgeModel;
|
2024-02-22 17:14:25 +08:00
|
|
|
const list = useMemo(() => {
|
|
|
|
return shouldFilterListWithoutDocument
|
|
|
|
? data.filter((x: IKnowledge) => x.doc_num > 0)
|
|
|
|
: data;
|
|
|
|
}, [data, shouldFilterListWithoutDocument]);
|
2024-02-20 18:10:20 +08:00
|
|
|
|
|
|
|
const fetchList = useCallback(() => {
|
|
|
|
dispatch({
|
|
|
|
type: 'knowledgeModel/getList',
|
|
|
|
});
|
|
|
|
}, [dispatch]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchList();
|
|
|
|
}, [fetchList]);
|
|
|
|
|
2024-02-22 17:14:25 +08:00
|
|
|
return list;
|
2024-02-20 18:10:20 +08:00
|
|
|
};
|
2024-02-28 16:28:33 +08:00
|
|
|
|
|
|
|
export const useSelectFileThumbnails = () => {
|
|
|
|
const fileThumbnails: Record<string, string> = useSelector(
|
|
|
|
(state: any) => state.kFModel.fileThumbnails,
|
|
|
|
);
|
|
|
|
|
|
|
|
return fileThumbnails;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useFetchFileThumbnails = (docIds?: Array<string>) => {
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const fileThumbnails = useSelectFileThumbnails();
|
|
|
|
|
|
|
|
const fetchFileThumbnails = useCallback(
|
|
|
|
(docIds: Array<string>) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'kFModel/fetch_document_thumbnails',
|
|
|
|
payload: { doc_ids: docIds.join(',') },
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[dispatch],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (docIds) {
|
|
|
|
fetchFileThumbnails(docIds);
|
|
|
|
}
|
|
|
|
}, [docIds, fetchFileThumbnails]);
|
|
|
|
|
|
|
|
return { fileThumbnails, fetchFileThumbnails };
|
|
|
|
};
|