Bin
2025-12-17 2b99d77d73ba568beff0a549534017caaad8a6de
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { CloseOutlined, QuestionCircleOutlined } from "@ant-design/icons";
import { Button, Checkbox, IconChevronLeft, IconChevronRight } from "@humansignal/ui";
import { observer } from "mobx-react";
import type { PropsWithChildren } from "react";
import { createContext, useCallback, useEffect, useRef, useState } from "react";
import { modal } from "../../Common/Modal/Modal";
import { Icon } from "../../Common/Icon/Icon";
import { Tooltip } from "@humansignal/ui";
import { ImagePreview } from "./ImagePreview";
 
import styles from "./GridPreview.module.scss";
 
type Task = {
  id: number;
  data: Record<string, string>;
};
 
type GridViewContextType = {
  tasks: Task[];
  imageField: string | undefined;
  currentTaskId: number | null;
  setCurrentTaskId: (id: number | null) => void;
  hasImage: boolean;
};
 
type TaskModalProps = GridViewContextType & { view: any; imageField: string };
 
export const GridViewContext = createContext<GridViewContextType>({
  tasks: [],
  imageField: undefined,
  currentTaskId: null,
  setCurrentTaskId: () => {},
  hasImage: false,
});
 
const TaskModal = observer(({ view, tasks, imageField, currentTaskId, setCurrentTaskId }: TaskModalProps) => {
  const index = tasks.findIndex((task) => task.id === currentTaskId);
  const task = tasks[index];
 
  const goToNext = useCallback(() => {
    if (index < tasks.length - 1) {
      setCurrentTaskId(tasks[index + 1].id);
    }
  }, [index, tasks]);
 
  const goToPrev = useCallback(() => {
    if (index > 0) {
      setCurrentTaskId(tasks[index - 1].id);
    }
  }, [index, tasks]);
 
  const onSelect = useCallback(() => {
    if (task) {
      view.toggleSelected(task.id);
    }
  }, [task, view]);
 
  const onClose = useCallback(() => {
    setCurrentTaskId(null);
  }, []);
 
  // assign hotkeys
  useEffect(() => {
    const onKeyDown = (event: KeyboardEvent) => {
      if (event.key === "ArrowLeft") {
        goToPrev();
      } else if (event.key === "ArrowRight") {
        goToNext();
      } else if (event.key === " ") {
        onSelect();
        event.preventDefault();
      } else if (event.key === "Escape") {
        onClose();
      } else {
        // pass this event through for other keys
        return;
      }
 
      event.stopPropagation();
    };
 
    document.addEventListener("keydown", onKeyDown);
    return () => document.removeEventListener("keydown", onKeyDown);
  }, [goToNext, goToPrev, onSelect, onClose]);
 
  if (!task) {
    return null;
  }
 
  const tooltip = (
    <div className={styles.tooltip}>
      <p>Preview of the task image to quickly navigate through the tasks and select the ones you want to work on.</p>
      <p>Use [arrow keys] to navigate.</p>
      <p>[Escape] to close the modal.</p>
      <p>[Space] to select/unselect the task.</p>
      <p>Use [scroll] to zoom in/out and [drag] to pan around while image is zoomed in.</p>
    </div>
  );
 
  return (
    <div className={styles.modal}>
      <div className={styles.header}>
        <Checkbox checked={view.selected.isSelected(task.id)} onChange={onSelect}>
          Task {task.id}
        </Checkbox>
        <div className={styles.actions}>
          <Tooltip title={tooltip}>
            <Icon icon={QuestionCircleOutlined} />
          </Tooltip>
          <Icon icon={CloseOutlined} onClick={onClose} />
        </div>
      </div>
      <div className="grid grid-cols-[20px_1fr_20px]">
        <Button
          type="button"
          className="h-full [&_span]:aspect-auto !p-0"
          variant="primary"
          look="string"
          onClick={goToPrev}
          disabled={index === 0}
        >
          <IconChevronLeft />
        </Button>
        <ImagePreview task={task} field={imageField} />
        <Button
          type="button"
          className="h-full [&_span]:aspect-auto !p-0"
          variant="primary"
          look="string"
          onClick={goToNext}
          disabled={index === tasks.length - 1}
        >
          <IconChevronRight />
        </Button>
      </div>
    </div>
  );
});
 
type GridViewProviderProps = PropsWithChildren<{
  data: Task[];
  view: any;
  fields: { alias: string; currentType: string }[];
}>;
 
export const GridViewProvider: React.FC<GridViewProviderProps> = ({ children, data, view, fields }) => {
  const [currentTaskId, setCurrentTaskId] = useState<number | null>(null);
  const modalRef = useRef<{ update: (props: object) => void; close: () => void } | null>(null);
  const imageField = fields.find((f) => f.currentType === "Image")?.alias;
  const hasImage = fields.some((f) => f.currentType === "Image");
 
  const onClose = useCallback(() => {
    modalRef.current = null;
    setCurrentTaskId(null);
  }, []);
 
  useEffect(() => {
    if (currentTaskId === null) {
      modalRef.current?.close();
      return;
    }
 
    if (!imageField) return;
 
    const children = (
      <TaskModal
        view={view}
        tasks={data}
        imageField={imageField}
        currentTaskId={currentTaskId}
        setCurrentTaskId={setCurrentTaskId}
        hasImage={hasImage}
      />
    );
 
    if (!modalRef.current) {
      modalRef.current = modal({
        bare: true,
        title: "Task Preview",
        style: { width: 800 },
        children,
        onHidden: onClose,
      });
    } else {
      modalRef.current.update({ children });
    }
  }, [currentTaskId, data, onClose]);
 
  // close the modal when we leave the view (by browser controls or by hotkeys)
  useEffect(() => () => modalRef.current?.close(), []);
 
  return (
    <GridViewContext.Provider value={{ tasks: data, imageField, currentTaskId, setCurrentTaskId, hasImage }}>
      {children}
    </GridViewContext.Provider>
  );
};