nitro-react/src/views/room/widgets/camera/CameraWidgetView.tsx

87 lines
3.3 KiB
TypeScript
Raw Normal View History

2021-06-15 22:52:23 +02:00
import { RoomCameraWidgetManagerEvent } from 'nitro-renderer/src/nitro/camera/events/RoomCameraWidgetManagerEvent';
import { FC, useCallback, useEffect, useMemo, useState } from 'react';
import { GetRoomCameraWidgetManager } from '../../../../api';
2021-06-13 07:13:55 +02:00
import { RoomWidgetCameraEvent } from '../../../../events/room-widgets/camera/RoomWidgetCameraEvent';
2021-06-15 22:52:23 +02:00
import { useCameraEvent } from '../../../../hooks/events/nitro/camera/camera-event';
2021-06-13 07:13:55 +02:00
import { useUiEvent } from '../../../../hooks/events/ui/ui-event';
import { CameraWidgetViewProps } from './CameraWidgetView.types';
2021-06-15 19:40:02 +02:00
import { CameraWidgetCaptureView } from './views/capture/CameraWidgetCaptureView';
import { CameraWidgetEditorView } from './views/editor/CameraWidgetEditorView';
2021-06-13 07:13:55 +02:00
export const CameraWidgetView: FC<CameraWidgetViewProps> = props =>
{
2021-06-15 22:52:23 +02:00
const [ effectsReady, setEffectsReady ] = useState(false);
2021-06-15 19:40:02 +02:00
const [ isCaptureVisible, setIsCaptureVisible ] = useState(false);
2021-06-15 22:52:23 +02:00
const [ isEditorVisible, setIsEditorVisible ] = useState(false);
const [ chosenPicture, setChosenPicture ] = useState<HTMLImageElement>(null);
2021-06-15 19:40:02 +02:00
const onNitroEvent = useCallback((event: RoomWidgetCameraEvent) =>
2021-06-13 07:13:55 +02:00
{
switch(event.type)
{
case RoomWidgetCameraEvent.SHOW_CAMERA:
2021-06-15 19:40:02 +02:00
setIsCaptureVisible(true);
2021-06-13 07:13:55 +02:00
return;
case RoomWidgetCameraEvent.HIDE_CAMERA:
2021-06-15 19:40:02 +02:00
setIsCaptureVisible(false);
setIsEditorVisible(false);
2021-06-13 07:13:55 +02:00
return;
case RoomWidgetCameraEvent.TOGGLE_CAMERA:
2021-06-15 19:40:02 +02:00
setIsEditorVisible(false);
setIsCaptureVisible(value => !value);
2021-06-13 07:13:55 +02:00
return;
}
}, []);
2021-06-15 19:40:02 +02:00
useUiEvent(RoomWidgetCameraEvent.SHOW_CAMERA, onNitroEvent);
useUiEvent(RoomWidgetCameraEvent.HIDE_CAMERA, onNitroEvent);
useUiEvent(RoomWidgetCameraEvent.TOGGLE_CAMERA, onNitroEvent);
2021-06-15 22:52:23 +02:00
const availableEffects = useMemo(() =>
{
if(!effectsReady) return null;
return Array.from(GetRoomCameraWidgetManager().effects.values());
}, [ effectsReady ]);
const onRoomCameraWidgetManagerEvent = useCallback((event: RoomCameraWidgetManagerEvent) =>
{
setEffectsReady(true);
}, []);
useCameraEvent(RoomCameraWidgetManagerEvent.INITIALIZED, onRoomCameraWidgetManagerEvent);
useEffect(() =>
{
if(!GetRoomCameraWidgetManager().isLoaded)
{
GetRoomCameraWidgetManager().init();
return;
}
setEffectsReady(true);
}, []);
2021-06-13 07:13:55 +02:00
2021-06-15 19:40:02 +02:00
const processAction = useCallback((type: string, value: any = null) =>
2021-06-13 07:13:55 +02:00
{
switch(type)
{
case 'close':
2021-06-15 19:40:02 +02:00
setIsCaptureVisible(false);
setIsEditorVisible(false);
return;
case 'capture_choose_picture':
setChosenPicture(value);
setIsCaptureVisible(false);
setIsEditorVisible(true);
2021-06-13 07:13:55 +02:00
return;
}
}, []);
2021-06-14 09:49:30 +02:00
2021-06-13 07:13:55 +02:00
return (
2021-06-15 19:40:02 +02:00
( isCaptureVisible && <CameraWidgetCaptureView onCloseClick={ () => processAction('close') } onChoosePicture={ (picture) => processAction('capture_choose_picture', picture) } /> ) ||
( isEditorVisible && <CameraWidgetEditorView onCloseClick={ () => processAction('close') } picture={ chosenPicture } availableEffects={ availableEffects } /> )
2021-06-13 07:13:55 +02:00
);
}