mirror of
https://github.com/billsonnn/nitro-react.git
synced 2024-11-23 06:40:50 +01:00
Update DoorbellWidgetView
This commit is contained in:
commit
f1fd556b42
@ -1,22 +0,0 @@
|
||||
import { RoomWidgetUpdateEvent } from './RoomWidgetUpdateEvent';
|
||||
|
||||
export class RoomWidgetDoorbellEvent extends RoomWidgetUpdateEvent
|
||||
{
|
||||
public static RINGING: string = 'RWDE_RINGING';
|
||||
public static REJECTED: string = 'RWDE_REJECTED';
|
||||
public static ACCEPTED: string = 'RWDE_ACCEPTED';
|
||||
|
||||
private _userName: string = '';
|
||||
|
||||
constructor(type: string, userName: string)
|
||||
{
|
||||
super(type);
|
||||
|
||||
this._userName = userName;
|
||||
}
|
||||
|
||||
public get userName(): string
|
||||
{
|
||||
return this._userName;
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ export * from './RoomDimmerPreset';
|
||||
export * from './RoomObjectItem';
|
||||
export * from './RoomWidgetAvatarInfoEvent';
|
||||
export * from './RoomWidgetChooserContentEvent';
|
||||
export * from './RoomWidgetDoorbellEvent';
|
||||
export * from './RoomWidgetFloodControlEvent';
|
||||
export * from './RoomWidgetObjectNameEvent';
|
||||
export * from './RoomWidgetPollUpdateEvent';
|
||||
|
@ -1,60 +0,0 @@
|
||||
import { NitroEvent, RoomSessionDoorbellEvent, RoomWidgetEnum } from '@nitrots/nitro-renderer';
|
||||
import { RoomWidgetDoorbellEvent, RoomWidgetUpdateEvent } from '../events';
|
||||
import { RoomWidgetLetUserInMessage, RoomWidgetMessage } from '../messages';
|
||||
import { RoomWidgetHandler } from './RoomWidgetHandler';
|
||||
|
||||
export class DoorbellWidgetHandler extends RoomWidgetHandler
|
||||
{
|
||||
public processEvent(event: NitroEvent): void
|
||||
{
|
||||
const doorbellEvent = (event as RoomSessionDoorbellEvent);
|
||||
|
||||
switch(event.type)
|
||||
{
|
||||
case RoomSessionDoorbellEvent.DOORBELL:
|
||||
this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.RINGING, doorbellEvent.userName));
|
||||
return;
|
||||
case RoomSessionDoorbellEvent.RSDE_REJECTED:
|
||||
this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.REJECTED, doorbellEvent.userName));
|
||||
return;
|
||||
case RoomSessionDoorbellEvent.RSDE_ACCEPTED:
|
||||
this.container.eventDispatcher.dispatchEvent(new RoomWidgetDoorbellEvent(RoomWidgetDoorbellEvent.ACCEPTED, doorbellEvent.userName));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public processWidgetMessage(message: RoomWidgetMessage): RoomWidgetUpdateEvent
|
||||
{
|
||||
switch(message.type)
|
||||
{
|
||||
case RoomWidgetLetUserInMessage.LET_USER_IN:
|
||||
const letUserInMessage = (message as RoomWidgetLetUserInMessage);
|
||||
|
||||
this.container.roomSession.sendDoorbellApprovalMessage(letUserInMessage.userName, letUserInMessage.canEnter);
|
||||
break;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public get type(): string
|
||||
{
|
||||
return RoomWidgetEnum.DOORBELL;
|
||||
}
|
||||
|
||||
public get eventTypes(): string[]
|
||||
{
|
||||
return [
|
||||
RoomSessionDoorbellEvent.DOORBELL,
|
||||
RoomSessionDoorbellEvent.RSDE_REJECTED,
|
||||
RoomSessionDoorbellEvent.RSDE_ACCEPTED
|
||||
];
|
||||
}
|
||||
|
||||
public get messageTypes(): string[]
|
||||
{
|
||||
return [
|
||||
RoomWidgetLetUserInMessage.LET_USER_IN
|
||||
];
|
||||
}
|
||||
}
|
@ -1,4 +1,3 @@
|
||||
export * from './DoorbellWidgetHandler';
|
||||
export * from './FurniChooserWidgetHandler';
|
||||
export * from './FurnitureContextMenuWidgetHandler';
|
||||
export * from './FurnitureCreditWidgetHandler';
|
||||
|
@ -1,27 +0,0 @@
|
||||
import { RoomWidgetMessage } from './RoomWidgetMessage';
|
||||
|
||||
export class RoomWidgetLetUserInMessage extends RoomWidgetMessage
|
||||
{
|
||||
public static LET_USER_IN: string = 'RWLUIM_LET_USER_IN';
|
||||
|
||||
private _userName: string;
|
||||
private _canEnter: boolean;
|
||||
|
||||
constructor(userName: string, canEnter: boolean)
|
||||
{
|
||||
super(RoomWidgetLetUserInMessage.LET_USER_IN);
|
||||
|
||||
this._userName = userName;
|
||||
this._canEnter = canEnter;
|
||||
}
|
||||
|
||||
public get userName(): string
|
||||
{
|
||||
return this._userName;
|
||||
}
|
||||
|
||||
public get canEnter(): boolean
|
||||
{
|
||||
return this._canEnter;
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ export * from './RoomWidgetDimmerPreviewMessage';
|
||||
export * from './RoomWidgetDimmerSavePresetMessage';
|
||||
export * from './RoomWidgetFurniActionMessage';
|
||||
export * from './RoomWidgetFurniToWidgetMessage';
|
||||
export * from './RoomWidgetLetUserInMessage';
|
||||
export * from './RoomWidgetMessage';
|
||||
export * from './RoomWidgetPollMessage';
|
||||
export * from './RoomWidgetPresentOpenMessage';
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { EventDispatcher, IRoomSession, NitroRectangle, RoomGeometry, RoomVariableEnum, Vector3d } from '@nitrots/nitro-renderer';
|
||||
import { FC, useEffect, useRef, useState } from 'react';
|
||||
import { DispatchMouseEvent, DispatchTouchEvent, DoorbellWidgetHandler, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCreditWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurnitureInternalLinkHandler, FurnitureMannequinWidgetHandler, FurniturePresentWidgetHandler, FurnitureRoomLinkHandler, FurnitureYoutubeDisplayWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, PollWidgetHandler, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler, WordQuizWidgetHandler } from '../../api';
|
||||
import { DispatchMouseEvent, DispatchTouchEvent, FurniChooserWidgetHandler, FurnitureContextMenuWidgetHandler, FurnitureCreditWidgetHandler, FurnitureCustomStackHeightWidgetHandler, FurnitureDimmerWidgetHandler, FurnitureExternalImageWidgetHandler, FurnitureInternalLinkHandler, FurnitureMannequinWidgetHandler, FurniturePresentWidgetHandler, FurnitureRoomLinkHandler, FurnitureYoutubeDisplayWidgetHandler, GetNitroInstance, GetRoomEngine, InitializeRoomInstanceRenderingCanvas, IRoomWidgetHandlerManager, PollWidgetHandler, RoomWidgetAvatarInfoHandler, RoomWidgetChatHandler, RoomWidgetChatInputHandler, RoomWidgetHandlerManager, RoomWidgetInfostandHandler, RoomWidgetRoomToolsHandler, RoomWidgetUpdateRoomViewEvent, UserChooserWidgetHandler, WordQuizWidgetHandler } from '../../api';
|
||||
import { Base } from '../../common';
|
||||
import { RoomColorView } from './RoomColorView';
|
||||
import { RoomContextProvider } from './RoomContext';
|
||||
@ -38,7 +38,6 @@ export const RoomView: FC<RoomViewProps> = props =>
|
||||
widgetHandlerManager.registerHandler(new RoomWidgetChatInputHandler());
|
||||
widgetHandlerManager.registerHandler(new RoomWidgetChatHandler());
|
||||
widgetHandlerManager.registerHandler(new UserChooserWidgetHandler());
|
||||
widgetHandlerManager.registerHandler(new DoorbellWidgetHandler());
|
||||
widgetHandlerManager.registerHandler(new WordQuizWidgetHandler());
|
||||
widgetHandlerManager.registerHandler(new PollWidgetHandler());
|
||||
|
||||
|
@ -1,63 +1,18 @@
|
||||
import { FC, useCallback, useState } from 'react';
|
||||
import { LocalizeText, RoomWidgetDoorbellEvent, RoomWidgetLetUserInMessage } from '../../../../api';
|
||||
import { FC, useEffect, useState } from 'react';
|
||||
import { LocalizeText } from '../../../../api';
|
||||
import { Base, Button, Column, Flex, Grid, NitroCardContentView, NitroCardHeaderView, NitroCardView } from '../../../../common';
|
||||
import { UseEventDispatcherHook } from '../../../../hooks';
|
||||
import { useRoomContext } from '../../RoomContext';
|
||||
import { useDoorbellWidget } from '../../../../hooks';
|
||||
|
||||
export const DoorbellWidgetView: FC<{}> = props =>
|
||||
{
|
||||
const [ isVisible, setIsVisible ] = useState(false);
|
||||
const [ users, setUsers ] = useState<string[]>([]);
|
||||
const { eventDispatcher = null, widgetHandler = null } = useRoomContext();
|
||||
const { users = [], answer = null } = useDoorbellWidget();
|
||||
|
||||
const addUser = useCallback((userName: string) =>
|
||||
useEffect(() =>
|
||||
{
|
||||
if(users.indexOf(userName) >= 0) return;
|
||||
|
||||
setUsers([ ...users, userName ]);
|
||||
setIsVisible(true);
|
||||
setIsVisible(!!users.length);
|
||||
}, [ users ]);
|
||||
|
||||
const removeUser = useCallback((userName: string) =>
|
||||
{
|
||||
const index = users.indexOf(userName);
|
||||
|
||||
if(index === -1) return;
|
||||
|
||||
const newUsers = [ ...users ];
|
||||
|
||||
newUsers.splice(index, 1);
|
||||
|
||||
setUsers(newUsers);
|
||||
|
||||
if(!newUsers.length) setIsVisible(false);
|
||||
}, [ users ]);
|
||||
|
||||
const onRoomWidgetDoorbellEvent = useCallback((event: RoomWidgetDoorbellEvent) =>
|
||||
{
|
||||
switch(event.type)
|
||||
{
|
||||
case RoomWidgetDoorbellEvent.RINGING:
|
||||
addUser(event.userName);
|
||||
return;
|
||||
case RoomWidgetDoorbellEvent.REJECTED:
|
||||
case RoomWidgetDoorbellEvent.ACCEPTED:
|
||||
removeUser(event.userName);
|
||||
return;
|
||||
}
|
||||
}, [ addUser, removeUser ]);
|
||||
|
||||
UseEventDispatcherHook(RoomWidgetDoorbellEvent.RINGING, eventDispatcher, onRoomWidgetDoorbellEvent);
|
||||
UseEventDispatcherHook(RoomWidgetDoorbellEvent.REJECTED, eventDispatcher, onRoomWidgetDoorbellEvent);
|
||||
UseEventDispatcherHook(RoomWidgetDoorbellEvent.ACCEPTED, eventDispatcher, onRoomWidgetDoorbellEvent);
|
||||
|
||||
const answer = useCallback((userName: string, flag: boolean) =>
|
||||
{
|
||||
widgetHandler.processWidgetMessage(new RoomWidgetLetUserInMessage(userName, flag));
|
||||
|
||||
removeUser(userName);
|
||||
}, [ widgetHandler, removeUser ]);
|
||||
|
||||
if(!isVisible) return null;
|
||||
|
||||
return (
|
||||
@ -66,7 +21,7 @@ export const DoorbellWidgetView: FC<{}> = props =>
|
||||
<NitroCardContentView overflow="hidden" gap={ 0 }>
|
||||
<Column gap={ 2 }>
|
||||
<Grid gap={ 1 } className="text-black fw-bold border-bottom px-1 pb-1">
|
||||
<Base className="g-col-6">Username</Base>
|
||||
<Base className="g-col-6">{ LocalizeText('generic.username') }</Base>
|
||||
<Base className="g-col-6"></Base>
|
||||
</Grid>
|
||||
</Column>
|
||||
|
@ -8,6 +8,7 @@ export * from './inventory';
|
||||
export * from './messages';
|
||||
export * from './navigator';
|
||||
export * from './purse';
|
||||
export * from './rooms';
|
||||
export * from './session';
|
||||
export * from './UseMountEffect';
|
||||
export * from './useSharedVisibility';
|
||||
|
1
src/hooks/rooms/index.ts
Normal file
1
src/hooks/rooms/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './widgets';
|
1
src/hooks/rooms/widgets/index.ts
Normal file
1
src/hooks/rooms/widgets/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './useDoorbellWidget';
|
58
src/hooks/rooms/widgets/useDoorbellWidget.ts
Normal file
58
src/hooks/rooms/widgets/useDoorbellWidget.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { RoomSessionDoorbellEvent } from '@nitrots/nitro-renderer';
|
||||
import { useCallback, useState } from 'react';
|
||||
import { GetRoomSession } from '../../../api';
|
||||
import { UseRoomSessionManagerEvent } from '../../events';
|
||||
|
||||
const useDoorbellWidgetState = () =>
|
||||
{
|
||||
const [ users, setUsers ] = useState<string[]>([]);
|
||||
|
||||
const addUser = useCallback((userName: string) =>
|
||||
{
|
||||
if(users.indexOf(userName) >= 0) return;
|
||||
|
||||
setUsers([ ...users, userName ]);
|
||||
}, [ users ]);
|
||||
|
||||
const removeUser = useCallback((userName: string) =>
|
||||
{
|
||||
const index = users.indexOf(userName);
|
||||
|
||||
if(index === -1) return;
|
||||
|
||||
const newUsers = [ ...users ];
|
||||
|
||||
newUsers.splice(index, 1);
|
||||
|
||||
setUsers(newUsers);
|
||||
}, [ users ]);
|
||||
|
||||
const answer = useCallback((userName: string, flag: boolean) =>
|
||||
{
|
||||
GetRoomSession().sendDoorbellApprovalMessage(userName, flag);
|
||||
|
||||
removeUser(userName);
|
||||
}, [ removeUser ]);
|
||||
|
||||
const onRoomSessionDoorbellEvent = useCallback((event: RoomSessionDoorbellEvent) =>
|
||||
{
|
||||
switch(event.type)
|
||||
{
|
||||
case RoomSessionDoorbellEvent.DOORBELL:
|
||||
addUser(event.userName);
|
||||
return;
|
||||
case RoomSessionDoorbellEvent.RSDE_REJECTED:
|
||||
case RoomSessionDoorbellEvent.RSDE_ACCEPTED:
|
||||
removeUser(event.userName);
|
||||
return;
|
||||
}
|
||||
}, [ addUser, removeUser ]);
|
||||
|
||||
UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.DOORBELL, onRoomSessionDoorbellEvent);
|
||||
UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.RSDE_REJECTED, onRoomSessionDoorbellEvent);
|
||||
UseRoomSessionManagerEvent(RoomSessionDoorbellEvent.RSDE_ACCEPTED, onRoomSessionDoorbellEvent);
|
||||
|
||||
return { users, addUser, removeUser, answer };
|
||||
}
|
||||
|
||||
export const useDoorbellWidget = useDoorbellWidgetState;
|
Loading…
Reference in New Issue
Block a user