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

130 lines
5.7 KiB
TypeScript
Raw Normal View History

2021-06-20 16:04:31 +02:00
import { RoomWidgetCameraConfigurationComposer, RoomWidgetCameraConfigurationEvent } from 'nitro-renderer';
2021-06-15 22:52:23 +02:00
import { RoomCameraWidgetManagerEvent } from 'nitro-renderer/src/nitro/camera/events/RoomCameraWidgetManagerEvent';
2021-06-20 16:04:31 +02:00
import { IRoomCameraWidgetSelectedEffect } from 'nitro-renderer/src/nitro/camera/IRoomCameraWidgetSelectedEffect';
2021-06-15 22:52:23 +02:00
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';
2021-06-20 16:04:31 +02:00
import { CreateMessageHook, SendMessageHook } from '../../../../hooks/messages/message-event';
2021-06-13 07:13:55 +02:00
import { CameraWidgetViewProps } from './CameraWidgetView.types';
2021-06-20 09:39:47 +02:00
import { CameraWidgetContextProvider } from './context/CameraWidgetContext';
2021-06-15 19:40:02 +02:00
import { CameraWidgetCaptureView } from './views/capture/CameraWidgetCaptureView';
2021-06-20 16:04:31 +02:00
import { CameraWidgetCheckoutView } from './views/checkout/CameraWidgetCheckoutView';
2021-06-15 19:40:02 +02:00
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-20 16:04:31 +02:00
const [ isCaptureVisible, setIsCaptureVisible ] = useState(false);
const [ isEditorVisible, setIsEditorVisible ] = useState(false);
const [ isCheckoutVisible, setIsCheckoutVisible ] = useState(false);
2021-06-20 09:39:47 +02:00
2021-06-20 16:04:31 +02:00
const [ myLevel, setMyLevel ] = useState(10);
2021-06-20 09:39:47 +02:00
const [ cameraRoll, setCameraRoll ] = useState<HTMLImageElement[]>([]);
const [ selectedPictureIndex, setSelectedPictureIndex ] = useState(-1);
const [ selectedEffects, setSelectedEffects ] = useState<IRoomCameraWidgetSelectedEffect[]>([]);
2021-06-20 16:04:31 +02:00
const [ isZoomed, setIsZoomed ] = useState(false);
const [ price, setPrice ] = useState<{credits: Number, points: Number, pointsType: number}>(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-20 16:04:31 +02:00
setIsCheckoutVisible(false);
2021-06-13 07:13:55 +02:00
return;
case RoomWidgetCameraEvent.TOGGLE_CAMERA:
2021-06-15 19:40:02 +02:00
setIsCaptureVisible(value => !value);
2021-06-20 16:04:31 +02:00
setIsEditorVisible(false);
setIsCheckoutVisible(false);
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
2021-06-20 16:04:31 +02:00
useEffect(() =>
{
if(price) return;
SendMessageHook(new RoomWidgetCameraConfigurationComposer());
}, [ price ]);
const onCameraConfigurationEvent = useCallback((event: RoomWidgetCameraConfigurationEvent) =>
{
const parser = event.getParser();
setPrice({ credits: parser.credits, points: parser.points, pointsType: parser.pointsType });
2021-06-30 18:05:10 +02:00
}, []);
2021-06-20 16:04:31 +02:00
CreateMessageHook(RoomWidgetCameraConfigurationEvent, onCameraConfigurationEvent);
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);
2021-06-20 16:04:31 +02:00
setIsCheckoutVisible(false);
2021-06-15 19:40:02 +02:00
return;
2021-06-20 09:39:47 +02:00
case 'capture_edit':
2021-06-15 19:40:02 +02:00
setIsCaptureVisible(false);
setIsEditorVisible(true);
2021-06-13 07:13:55 +02:00
return;
2021-06-20 09:39:47 +02:00
case 'editor_cancel':
setIsCaptureVisible(true);
setIsEditorVisible(false);
2021-06-20 16:04:31 +02:00
setIsCheckoutVisible(false);
return;
case 'editor_checkout':
setIsEditorVisible(false);
setIsCheckoutVisible(true);
2021-06-20 09:39:47 +02:00
return;
2021-06-13 07:13:55 +02:00
}
}, []);
2021-06-14 09:49:30 +02:00
2021-06-13 07:13:55 +02:00
return (
2021-06-20 16:04:31 +02:00
<CameraWidgetContextProvider value={ { cameraRoll, setCameraRoll, selectedPictureIndex, setSelectedPictureIndex, selectedEffects, setSelectedEffects, isZoomed, setIsZoomed } }>
{ isCaptureVisible && <CameraWidgetCaptureView onCloseClick={ () => processAction('close') } onEditClick={ () => processAction('capture_edit') } /> }
{ isEditorVisible && <CameraWidgetEditorView myLevel={ myLevel } onCloseClick={ () => processAction('close') } onCancelClick={ () => processAction('editor_cancel') } onCheckoutClick={ () => processAction('editor_checkout') } availableEffects={ availableEffects } /> }
{ isCheckoutVisible && <CameraWidgetCheckoutView onCloseClick={ () => processAction('close') } onCancelClick={ () => processAction('editor_cancel') } price={ price }></CameraWidgetCheckoutView> }
2021-06-20 09:39:47 +02:00
</CameraWidgetContextProvider>
2021-06-13 07:13:55 +02:00
);
}