File size: 1,294 Bytes
44e5f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import { useState, useCallback } from "react";
import { createEmptyAnnotations } from "./promptSchema";

// Shared annotation-state logic for the Training and Evaluation pages: an
// empty {events, video} structure derived from an item's applicable axes,
// plus update helpers. `resetFor(item)` re-initializes when the current
// item/video changes (e.g. moving to the next task).
export default function useAnnotations(initialItem) {
  const [annotations, setAnnotations] = useState(() => createEmptyAnnotations(initialItem));

  const updateEventField = useCallback((eventId, axisId, field, value) => {
    setAnnotations((prev) => ({
      ...prev,
      events: {
        ...prev.events,
        [eventId]: {
          ...prev.events[eventId],
          [axisId]: { ...prev.events[eventId][axisId], [field]: value }
        }
      }
    }));
  }, []);

  const updateVideoField = useCallback((axisId, field, value) => {
    setAnnotations((prev) => ({
      ...prev,
      video: {
        ...prev.video,
        [axisId]: { ...prev.video[axisId], [field]: value }
      }
    }));
  }, []);

  const resetFor = useCallback((item) => {
    setAnnotations(createEmptyAnnotations(item));
  }, []);

  return { annotations, updateEventField, updateVideoField, resetFor, setAnnotations };
}