import React, { useState } from "react"; import { atom, RecoilState, selector, Snapshot, useRecoilCallback, useRecoilValue, } from "recoil"; import { useSpring } from "@react-spring/web"; import { CLIPS_FRAME_FIELDS, CLIPS_SAMPLE_FIELDS, EMBEDDED_DOCUMENT_FIELD, getFetchFunction, PATCHES_FIELDS, toSnakeCase, } from "@fiftyone/utilities"; import * as atoms from "../../recoil/atoms"; import * as schemaAtoms from "../../recoil/schema"; import * as selectors from "../../recoil/selectors"; import * as viewAtoms from "../../recoil/view"; import { StateResolver, useUnprocessedStateUpdate } from "../../utils/hooks"; import { OBJECT_PATCHES, EVALUATION_PATCHES, CLIPS_VIEWS, } from "../../utils/links"; import Popout from "./Popout"; import { ActionOption } from "./Common"; import { SwitcherDiv, SwitchDiv } from "./utils"; import { State } from "../../recoil/types"; import { filters } from "../../recoil/filters"; import { similarityParameters } from "./Similar"; import { useTheme } from "@fiftyone/components"; export const patching = atom({ key: "patching", default: false, }); export const patchesFields = selector({ key: "patchesFields", get: ({ get }) => { const paths = get(schemaAtoms.labelFields({})); return paths.filter((p) => get( schemaAtoms.meetsType({ path: p, ftype: EMBEDDED_DOCUMENT_FIELD, embeddedDocType: PATCHES_FIELDS, }) ) ); }, }); export const clipsFields = selector({ key: "clipsFields", get: ({ get }) => [ ...get( schemaAtoms.fieldPaths({ space: State.SPACE.FRAME, ftype: EMBEDDED_DOCUMENT_FIELD, embeddedDocType: CLIPS_FRAME_FIELDS, }) ), ...get( schemaAtoms.fieldPaths({ space: State.SPACE.SAMPLE, ftype: EMBEDDED_DOCUMENT_FIELD, embeddedDocType: CLIPS_SAMPLE_FIELDS, }) ), ].sort(), }); const evaluationKeys = selector({ key: "evaluationKeys", get: ({ get }) => { return get(atoms.dataset).evaluations.map(({ key }) => key); }, }); export const sendPatch = async ( snapshot: Snapshot, updateState: (resolve: StateResolver) => void, addStage?: object ) => { const similarity = await snapshot.getPromise(similarityParameters); const subscription = await snapshot.getPromise(selectors.stateSubscription); return getFetchFunction()("POST", "/pin", { filters: await snapshot.getPromise(filters), view: await snapshot.getPromise(viewAtoms.view), dataset: await snapshot.getPromise(selectors.datasetName), sample_ids: await snapshot.getPromise(atoms.selectedSamples), labels: toSnakeCase(await snapshot.getPromise(atoms.selectedLabels)), add_stages: addStage ? [addStage] : null, similarity: similarity ? toSnakeCase(similarity) : null, subscription, }).then((data) => updateState(data)); }; const useToPatches = () => { const updateState = useUnprocessedStateUpdate(); return useRecoilCallback( ({ set, snapshot }) => async (field) => { set(patching, true); sendPatch(snapshot, updateState, { _cls: "fiftyone.core.stages.ToPatches", kwargs: [ ["field", field], ["_state", null], ], }).then(() => set(patching, false)); }, [] ); }; const useToClips = () => { const updateState = useUnprocessedStateUpdate(); return useRecoilCallback( ({ set, snapshot }) => async (field) => { set(patching, true); sendPatch(snapshot, updateState, { _cls: "fiftyone.core.stages.ToClips", kwargs: [ ["field_or_expr", field], ["_state", null], ], }).then(() => set(patching, false)); }, [] ); }; const useToEvaluationPatches = () => { const updateState = useUnprocessedStateUpdate(); return useRecoilCallback( ({ set, snapshot }) => async (evaluation: string) => { set(patching, true); sendPatch(snapshot, updateState, { _cls: "fiftyone.core.stages.ToEvaluationPatches", kwargs: [ ["eval_key", evaluation], ["_state", null], ], }).then(() => set(patching, false)); }, [] ); }; const LabelsClips = ({ close }) => { const fields = useRecoilValue(clipsFields); const toClips = useToClips(); return ( <> {fields.map((field) => { return ( { close(); toClips(field); }} /> ); })} ); }; const LabelsPatches = ({ close }) => { const fields = useRecoilValue(patchesFields); const toPatches = useToPatches(); return ( <> {fields.map((field) => { return ( { close(); toPatches(field); }} /> ); })} ); }; const EvaluationPatches = ({ close }) => { const evaluations = useRecoilValue(evaluationKeys); const toEvaluationPatches = useToEvaluationPatches(); return ( <> {evaluations.map((evaluation) => { return ( { close(); toEvaluationPatches(evaluation); }} /> ); })} ); }; type PatcherProps = { close: () => void; }; const Patcher = ({ bounds, close }: PatcherProps) => { const theme = useTheme(); const isVideo = useRecoilValue(selectors.isVideoDataset) && useRecoilValue(viewAtoms.isRootView); const isClips = useRecoilValue(viewAtoms.isClipsView); const [labels, setLabels] = useState(true); const labelProps = useSpring({ borderBottomColor: labels ? theme.brand : theme.backgroundDark, cursor: labels ? "default" : "pointer", }); const evaluationProps = useSpring({ borderBottomColor: labels ? theme.backgroundDark : theme.brand, cursor: labels ? "pointer" : "default", }); return ( !labels && setLabels(true)} > Labels {!isVideo && ( labels && setLabels(false)} > Evaluations )} {labels && (isVideo || isClips) && } {labels && !isVideo && !isClips && } {!labels && } ); }; export default React.memo(Patcher);