aboutsummaryrefslogtreecommitdiff
path: root/src/components/common/MomentTags.tsx
blob: 37069e189c38fb777604019bc1b9a18cbd111882 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import React, {createRef, RefObject, useEffect, useState} from 'react';
import {Image, View} from 'react-native';
import {MomentTagType, ProfilePreviewType} from '../../types';
import TaggDraggable from '../taggs/TaggDraggable';
import Draggable from './Draggable';

interface MomentTagsProps {
  editing: boolean;
  tags: MomentTagType[];
  setTags: (tag: MomentTagType[]) => void;
  imageRef: RefObject<Image>;
  deleteFromList?: (user: ProfilePreviewType) => void;
  boundaries?: DraggableBoundaries;
}
interface DraggableBoundaries {
  top?: number;
  bottom?: number;
  left?: number;
  right?: number;
}

const MomentTags: React.FC<MomentTagsProps> = ({
  editing,
  tags,
  setTags,
  imageRef,
  deleteFromList,
  boundaries,
}) => {
  const [offset, setOffset] = useState([0, 0]);
  const [imageDimensions, setImageDimensions] = useState([0, 0]);
  const [maxZIndex, setMaxZIndex] = useState(1);
  const [draggableRefs, setDraggableRefs] = useState<RefObject<View>[]>([]);
  // [minXBoundary, maxXBoundary, minYBoundary, maxYBoundary]
  const [boundariesList, setBoundariesList] = useState<number[]>([0, 0, 0, 0]);

  const updateTagPosition = (ref: RefObject<Image>, userId: string) => {
    if (ref !== null && ref.current !== null) {
      ref.current.measure(
        (
          _fx: number, // location of ref relative to parent element
          _fy: number,
          _width: number,
          _height: number,
          px: number, // location of ref relative to entire screen
          py: number,
        ) => {
          const x = ((px - offset[0]) / imageDimensions[0]) * 100;
          const y = ((py - offset[1]) / imageDimensions[1]) * 100;
          setTags(
            tags.map((tag) =>
              tag.user.id === userId
                ? {
                    id: '',
                    x,
                    y,
                    z: maxZIndex - 1, // TODO: change this
                    user: tag.user,
                  }
                : tag,
            ),
          );
        },
      );
    }
  };

  useEffect(() => {
    setDraggableRefs(tags.map((_) => createRef()));
  }, [tags]);

  useEffect(() => {
    setTimeout(
      () => {
        if (imageRef && imageRef.current) {
          imageRef.current.measure(
            (
              fx: number, // location of ref relative to parent element
              fy: number,
              width: number,
              height: number,
              _x: number, // location of ref relative to entire screen
              _y: number,
            ) => {
              setOffset([fx, fy]);
              setImageDimensions([width, height]);
            },
          );
        }

        // Checks for and adds boundaries
        if (boundaries) {
          const newBounds = [...boundariesList];
          if (boundaries.top) {
            newBounds[2] = boundaries.top;
          }
          if (boundaries.bottom) {
            newBounds[3] = boundaries.bottom;
          }
          if (boundaries.left) {
            newBounds[0] = boundaries.left;
          }
          if (boundaries.right) {
            newBounds[1] = boundaries.right;
          }
          setBoundariesList(newBounds);
        }
      },
      editing ? 100 : 0,
    );
  }, []);

  if (!tags) {
    return null;
  }

  return editing && deleteFromList ? (
    <>
      {tags.map((tag, index) => (
        <Draggable
          key={tag.user.id + tag.x + tag.y}
          x={(imageDimensions[0] * tag.x) / 100 + offset[0]}
          y={(imageDimensions[1] * tag.y) / 100 + offset[1]}
          z={tag.z}
          minX={offset[0] + boundariesList[0]}
          minY={offset[1] + boundariesList[2]}
          maxX={imageDimensions[0] + offset[0] - boundariesList[1]}
          maxY={imageDimensions[1] + offset[1] - boundariesList[3]}
          onDragStart={() => {
            const currZIndex = maxZIndex;
            setMaxZIndex(currZIndex + 1);
            return currZIndex;
          }}
          onDragRelease={() =>
            updateTagPosition(draggableRefs[index], tag.user.id)
          }>
          <TaggDraggable
            draggableRef={draggableRefs[index]}
            taggedUser={tag.user}
            editingView={true}
            deleteFromList={() => deleteFromList(tag.user)}
          />
        </Draggable>
      ))}
    </>
  ) : (
    <>
      {tags.map((tag, index) => (
        <Draggable
          key={tag.user.id}
          x={(imageDimensions[0] * tag.x) / 100 + offset[0]}
          y={(imageDimensions[1] * tag.y) / 100 + offset[1]}
          z={tag.z}
          minX={offset[0] + boundariesList[0]}
          minY={offset[1] + boundariesList[2]}
          maxX={imageDimensions[0] + offset[0] - boundariesList[1]}
          maxY={imageDimensions[1] + offset[1] - boundariesList[3]}
          disabled={true}>
          <TaggDraggable
            draggableRef={draggableRefs[index]}
            taggedUser={tag.user}
            editingView={editing}
            deleteFromList={() => null}
          />
        </Draggable>
      ))}
    </>
  );
};

export default MomentTags;