File size: 2,235 Bytes
f422a06
3054c20
 
e4e6a45
3054c20
e4e6a45
 
 
 
 
 
 
f422a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3054c20
 
 
 
 
 
 
 
 
 
 
830bf29
 
3054c20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f422a06
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { IChunk, IKnowledgeFile } from '@/interfaces/database/knowledge';
import { useCallback, useMemo, useState } from 'react';
import { IHighlight } from 'react-pdf-highlighter';
import { useSelector } from 'umi';
import { v4 as uuid } from 'uuid';

export const useSelectDocumentInfo = () => {
  const documentInfo: IKnowledgeFile = useSelector(
    (state: any) => state.chunkModel.documentInfo,
  );
  return documentInfo;
};

export const useSelectChunkList = () => {
  const chunkList: IChunk[] = useSelector(
    (state: any) => state.chunkModel.data,
  );
  return chunkList;
};

export const useHandleChunkCardClick = () => {
  const [selectedChunkId, setSelectedChunkId] = useState<string>('');

  const handleChunkCardClick = useCallback((chunkId: string) => {
    setSelectedChunkId(chunkId);
  }, []);

  return { handleChunkCardClick, selectedChunkId };
};

export const useGetSelectedChunk = (selectedChunkId: string) => {
  const chunkList: IChunk[] = useSelectChunkList();
  return (
    chunkList.find((x) => x.chunk_id === selectedChunkId) ?? ({} as IChunk)
  );
};

export const useGetChunkHighlights = (
  selectedChunkId: string,
): IHighlight[] => {
  const selectedChunk: IChunk = useGetSelectedChunk(selectedChunkId);

  const highlights: IHighlight[] = useMemo(() => {
    return Array.isArray(selectedChunk?.positions) &&
      selectedChunk.positions.every((x) => Array.isArray(x))
      ? selectedChunk?.positions?.map((x) => {
          const actualPositions = x.map((y, index) =>
            index !== 0 ? y / 0.7 : y,
          );
          const boundingRect = {
            width: 849,
            height: 1200,
            x1: actualPositions[1],
            x2: actualPositions[2],
            y1: actualPositions[3],
            y2: actualPositions[4],
          };
          return {
            id: uuid(),
            comment: {
              text: '',
              emoji: '',
            },
            content: { text: selectedChunk.content_with_weight },
            position: {
              boundingRect: boundingRect,
              rects: [boundingRect],
              pageNumber: x[0],
            },
          };
        })
      : [];
  }, [selectedChunk]);

  return highlights;
};