You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hooks.ts 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { useOneNamespaceEffectsLoading } from '@/hooks/store-hooks';
  2. import { IChunk, IKnowledgeFile } from '@/interfaces/database/knowledge';
  3. import { buildChunkHighlights } from '@/utils/documentUtils';
  4. import { useCallback, useMemo, useState } from 'react';
  5. import { IHighlight } from 'react-pdf-highlighter';
  6. import { useSelector } from 'umi';
  7. import { ChunkTextMode } from './constant';
  8. export const useSelectDocumentInfo = () => {
  9. const documentInfo: IKnowledgeFile = useSelector(
  10. (state: any) => state.chunkModel.documentInfo,
  11. );
  12. return documentInfo;
  13. };
  14. export const useSelectChunkList = () => {
  15. const chunkList: IChunk[] = useSelector(
  16. (state: any) => state.chunkModel.data,
  17. );
  18. return chunkList;
  19. };
  20. export const useHandleChunkCardClick = () => {
  21. const [selectedChunkId, setSelectedChunkId] = useState<string>('');
  22. const handleChunkCardClick = useCallback((chunkId: string) => {
  23. setSelectedChunkId(chunkId);
  24. }, []);
  25. return { handleChunkCardClick, selectedChunkId };
  26. };
  27. export const useGetSelectedChunk = (selectedChunkId: string) => {
  28. const chunkList: IChunk[] = useSelectChunkList();
  29. return (
  30. chunkList.find((x) => x.chunk_id === selectedChunkId) ?? ({} as IChunk)
  31. );
  32. };
  33. export const useGetChunkHighlights = (selectedChunkId: string) => {
  34. const [size, setSize] = useState({ width: 849, height: 1200 });
  35. const selectedChunk: IChunk = useGetSelectedChunk(selectedChunkId);
  36. const highlights: IHighlight[] = useMemo(() => {
  37. return buildChunkHighlights(selectedChunk, size);
  38. }, [selectedChunk, size]);
  39. const setWidthAndHeight = (width: number, height: number) => {
  40. setSize((pre) => {
  41. if (pre.height !== height || pre.width !== width) {
  42. return { height, width };
  43. }
  44. return pre;
  45. });
  46. };
  47. return { highlights, setWidthAndHeight };
  48. };
  49. export const useSelectChunkListLoading = () => {
  50. return useOneNamespaceEffectsLoading('chunkModel', [
  51. 'create_hunk',
  52. 'chunk_list',
  53. 'switch_chunk',
  54. ]);
  55. };
  56. // Switch chunk text to be fully displayed or ellipse
  57. export const useChangeChunkTextMode = () => {
  58. const [textMode, setTextMode] = useState<ChunkTextMode>(ChunkTextMode.Full);
  59. const changeChunkTextMode = useCallback((mode: ChunkTextMode) => {
  60. setTextMode(mode);
  61. }, []);
  62. return { textMode, changeChunkTextMode };
  63. };