1
1
import { useTheme } from "@fiftyone/components" ;
2
- import { AbstractLooker } from "@fiftyone/looker" ;
3
- import { BaseState } from "@fiftyone/looker/src/state" ;
2
+ import type { ImageLooker } from "@fiftyone/looker" ;
4
3
import * as fos from "@fiftyone/state" ;
5
- import { useEventHandler , useOnSelectLabel } from "@fiftyone/state" ;
6
- import React , { useEffect , useMemo , useRef , useState } from "react" ;
7
- import { useErrorHandler } from "react-error-boundary" ;
8
- import { useRecoilCallback , useRecoilValue , useSetRecoilState } from "recoil" ;
9
- import { v4 as uuid } from "uuid" ;
10
- import { useModalContext } from "./hooks" ;
4
+ import React , { useMemo } from "react" ;
5
+ import { useRecoilCallback , useRecoilValue } from "recoil" ;
11
6
import { ImaVidLookerReact } from "./ImaVidLooker" ;
12
-
13
- export const useLookerOptionsUpdate = ( ) => {
14
- return useRecoilCallback (
15
- ( { snapshot, set } ) =>
16
- async ( update : object , updater ?: ( updated : { } ) => void ) => {
17
- const currentOptions = await snapshot . getPromise (
18
- fos . savedLookerOptions
19
- ) ;
20
-
21
- const panels = await snapshot . getPromise ( fos . lookerPanels ) ;
22
- const updated = {
23
- ...currentOptions ,
24
- ...update ,
25
- showJSON : panels . json . isOpen ,
26
- showHelp : panels . help . isOpen ,
27
- } ;
28
- set ( fos . savedLookerOptions , updated ) ;
29
- if ( updater ) updater ( updated ) ;
30
- }
31
- ) ;
32
- } ;
7
+ import { VideoLookerReact } from "./VideoLooker" ;
8
+ import useLooker from "./use-looker" ;
33
9
34
10
export const useShowOverlays = ( ) => {
35
11
return useRecoilCallback ( ( { set } ) => async ( event : CustomEvent ) => {
@@ -47,137 +23,27 @@ export const useClearSelectedLabels = () => {
47
23
} ;
48
24
49
25
interface LookerProps {
50
- sample ?: fos . ModalSample ;
51
- onClick ?: React . MouseEventHandler < HTMLDivElement > ;
26
+ sample : fos . ModalSample ;
52
27
}
53
28
54
- const ModalLookerNoTimeline = React . memo (
55
- ( { sample : sampleDataWithExtraParams } : LookerProps ) => {
56
- const [ id ] = useState ( ( ) => uuid ( ) ) ;
57
- const colorScheme = useRecoilValue ( fos . colorScheme ) ;
58
-
59
- const { sample } = sampleDataWithExtraParams ;
60
-
61
- const theme = useTheme ( ) ;
62
- const initialRef = useRef < boolean > ( true ) ;
63
- const lookerOptions = fos . useLookerOptions ( true ) ;
64
- const [ reset , setReset ] = useState ( false ) ;
65
- const selectedMediaField = useRecoilValue ( fos . selectedMediaField ( true ) ) ;
66
- const setModalLooker = useSetRecoilState ( fos . modalLooker ) ;
67
-
68
- const createLooker = fos . useCreateLooker ( true , false , {
69
- ...lookerOptions ,
70
- } ) ;
71
-
72
- const { setActiveLookerRef } = useModalContext ( ) ;
73
-
74
- const looker = React . useMemo (
75
- ( ) => createLooker . current ( sampleDataWithExtraParams ) ,
76
- [ reset , createLooker , selectedMediaField ]
77
- ) as AbstractLooker < BaseState > ;
78
-
79
- useEffect ( ( ) => {
80
- setModalLooker ( looker ) ;
81
- } , [ looker ] ) ;
82
-
83
- useEffect ( ( ) => {
84
- if ( looker ) {
85
- setActiveLookerRef ( looker as fos . Lookers ) ;
86
- }
87
- } , [ looker ] ) ;
88
-
89
- useEffect ( ( ) => {
90
- ! initialRef . current && looker . updateOptions ( lookerOptions ) ;
91
- } , [ lookerOptions ] ) ;
92
-
93
- useEffect ( ( ) => {
94
- ! initialRef . current && looker . updateSample ( sample ) ;
95
- } , [ sample , colorScheme ] ) ;
96
-
97
- useEffect ( ( ) => {
98
- return ( ) => looker ?. destroy ( ) ;
99
- } , [ looker ] ) ;
100
-
101
- const handleError = useErrorHandler ( ) ;
102
-
103
- const updateLookerOptions = useLookerOptionsUpdate ( ) ;
104
- useEventHandler ( looker , "options" , ( e ) => updateLookerOptions ( e . detail ) ) ;
105
- useEventHandler ( looker , "showOverlays" , useShowOverlays ( ) ) ;
106
- useEventHandler ( looker , "reset" , ( ) => {
107
- setReset ( ( c ) => ! c ) ;
108
- } ) ;
109
-
110
- const jsonPanel = fos . useJSONPanel ( ) ;
111
- const helpPanel = fos . useHelpPanel ( ) ;
112
-
113
- useEventHandler ( looker , "select" , useOnSelectLabel ( ) ) ;
114
- useEventHandler ( looker , "error" , ( event ) => handleError ( event . detail ) ) ;
115
- useEventHandler (
116
- looker ,
117
- "panels" ,
118
- async ( { detail : { showJSON, showHelp, SHORTCUTS } } ) => {
119
- if ( showJSON ) {
120
- jsonPanel [ showJSON ] ( sample ) ;
121
- }
122
- if ( showHelp ) {
123
- if ( showHelp == "close" ) {
124
- helpPanel . close ( ) ;
125
- } else {
126
- helpPanel [ showHelp ] ( shortcutToHelpItems ( SHORTCUTS ) ) ;
127
- }
128
- }
129
-
130
- updateLookerOptions ( { } , ( updatedOptions ) =>
131
- looker . updateOptions ( updatedOptions )
132
- ) ;
133
- }
134
- ) ;
135
-
136
- useEffect ( ( ) => {
137
- initialRef . current = false ;
138
- } , [ ] ) ;
139
-
140
- useEffect ( ( ) => {
141
- looker . attach ( id ) ;
142
- } , [ looker , id ] ) ;
143
-
144
- useEventHandler ( looker , "clear" , useClearSelectedLabels ( ) ) ;
145
-
146
- const hoveredSample = useRecoilValue ( fos . hoveredSample ) ;
147
-
148
- useEffect ( ( ) => {
149
- const hoveredSampleId = hoveredSample ?. _id ;
150
- looker . updater ( ( state ) => ( {
151
- ...state ,
152
- shouldHandleKeyEvents : hoveredSampleId === sample . _id ,
153
- options : {
154
- ...state . options ,
155
- } ,
156
- } ) ) ;
157
- } , [ hoveredSample , sample , looker ] ) ;
158
-
159
- const ref = useRef < HTMLDivElement > ( null ) ;
160
- useEffect ( ( ) => {
161
- ref . current ?. dispatchEvent (
162
- new CustomEvent ( `looker-attached` , { bubbles : true } )
163
- ) ;
164
- } , [ ref ] ) ;
165
-
166
- return (
167
- < div
168
- ref = { ref }
169
- id = { id }
170
- data-cy = "modal-looker-container"
171
- style = { {
172
- width : "100%" ,
173
- height : "100%" ,
174
- background : theme . background . level2 ,
175
- position : "relative" ,
176
- } }
177
- />
178
- ) ;
179
- }
180
- ) ;
29
+ const ModalLookerNoTimeline = React . memo ( ( props : LookerProps ) => {
30
+ const { id, ref } = useLooker < ImageLooker > ( props ) ;
31
+ const theme = useTheme ( ) ;
32
+
33
+ return (
34
+ < div
35
+ ref = { ref }
36
+ id = { id }
37
+ data-cy = "modal-looker-container"
38
+ style = { {
39
+ width : "100%" ,
40
+ height : "100%" ,
41
+ background : theme . background . level2 ,
42
+ position : "relative" ,
43
+ } }
44
+ />
45
+ ) ;
46
+ } ) ;
181
47
182
48
export const ModalLooker = React . memo (
183
49
( { sample : propsSampleData } : LookerProps ) => {
@@ -197,21 +63,16 @@ export const ModalLooker = React.memo(
197
63
const shouldRenderImavid = useRecoilValue (
198
64
fos . shouldRenderImaVidLooker ( true )
199
65
) ;
66
+ const video = useRecoilValue ( fos . isVideoDataset ) ;
200
67
201
68
if ( shouldRenderImavid ) {
202
69
return < ImaVidLookerReact sample = { sample } /> ;
203
70
}
204
71
72
+ if ( video ) {
73
+ return < VideoLookerReact sample = { sample } /> ;
74
+ }
75
+
205
76
return < ModalLookerNoTimeline sample = { sample } /> ;
206
77
}
207
78
) ;
208
-
209
- export function shortcutToHelpItems ( SHORTCUTS ) {
210
- return Object . values (
211
- Object . values ( SHORTCUTS ) . reduce ( ( acc , v ) => {
212
- acc [ v . shortcut ] = v ;
213
-
214
- return acc ;
215
- } , { } )
216
- ) ;
217
- }
0 commit comments