Feature/floating notifications calling and twitter (#112)

* Add floating notifications :O

* Add phone notifications and refactor calling ui

* This is unsafe

* Fix controls null check

* Remove go to alert

* Fix phone stuck after call

* Remove lazy mount for Phone

* Fix open/close cl_main

* Use effect for notification visibility

* Remove all alerts when phone closes

* Avoid re-render useNuiEvent

* It works now :c
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-02-23 13:19:10 -05:00
1 parent 4633146d40
commit f6d7a3dbe1
42 files changed
+869 -679

No files matched your search

@@ -0,0 +1,66 @@
import React from 'react';
import CallIcon from '@material-ui/icons/Call';
import CallEndIcon from '@material-ui/icons/CallEnd';
import { Fab } from '@material-ui/core';
import { useCall } from '../hooks/useCall';
import useStyles from './modal.styles';
import { useModal } from '../hooks/useModal';
export const CallControls = ({
size,
}: {
size?: 'small' | 'medium' | 'large';
}) => {
const classes = useStyles();
const { setModal } = useModal();
const { call, endCall, acceptCall, rejectCall } = useCall();
if (!call) {
return null;
}
const handleRejectCall = () => {
setModal(false);
rejectCall();
};
const handleEndCall = () => {
setModal(false);
endCall();
};
const { accepted, isTransmitter } = call;
if (accepted || isTransmitter) {
return (
<Fab
size={size}
onClick={handleEndCall}
style={{ backgroundColor: '#e74c3c', color: '#fff' }}
className={classes.actionButton}
>
<CallEndIcon />
</Fab>
);
}
return (
<>
<Fab
size={size}
onClick={handleRejectCall}
style={{ backgroundColor: '#e74c3c', color: '#fff' }}
className={classes.actionButton}
>
<CallEndIcon />
</Fab>
<Fab
size={size}
onClick={acceptCall}
style={{ backgroundColor: '#27ae60', color: '#fff' }}
className={classes.actionButton}
>
<CallIcon />
</Fab>
</>
);
};
@@ -0,0 +1,27 @@
import React from 'react';
import { AppWrapper } from '../../../ui/components/AppWrapper';
import { AppContent } from '../../../ui/components/AppContent';
import { useCall } from '../hooks/useCall';
import useStyles from './modal.styles';
import CallTimer from './CallTimer';
import { CallControls } from './CallControls';
import { Box } from '@material-ui/core';
export const CallModal = () => {
const classes = useStyles();
const { call } = useCall();
return (
<AppWrapper>
<AppContent>
<Box pt={16}>
<h1 style={{ textAlign: 'center' }}>{call?.transmitter}</h1>
<CallTimer isAccepted={call?.accepted} />
</Box>
<div className={classes.actions}>
<CallControls />
</div>
</AppContent>
</AppWrapper>
);
};
@@ -0,0 +1,23 @@
import React from 'react';
import { Box } from '@material-ui/core';
import { CallControls } from './CallControls';
interface ICallNotificationProps {
onAccept(): void;
onReject(): void;
}
export const CallNotification = ({
children,
}: {
children: React.ReactNode;
}) => {
return (
<div style={{ paddingBottom: '48px' }}>
<Box>{children}</Box>
<Box style={{ position: 'absolute', bottom: 0, right: 0 }}>
<CallControls size='small' />
</Box>
</div>
);
};
@@ -0,0 +1,60 @@
import React, { useEffect, useRef } from 'react';
import { useDuration } from '../hooks/useDuration';
const runTimer = (updatedTime, setDuration) => {
let newTime = { ...updatedTime.current };
if (newTime.m >= 60) {
newTime = { ...newTime, h: newTime.h + 1, m: 0 };
}
if (newTime.s >= 60) {
newTime = { ...newTime, m: newTime.m + 1, s: 0 };
}
if (newTime.ms >= 100) {
newTime = { ...newTime, s: newTime.s + 1, ms: 0 };
}
newTime = { ...newTime, ms: newTime.ms + 1 };
updatedTime.current = newTime;
setDuration(newTime);
}
function CallTimer({ isAccepted }) {
const { duration, setDuration } = useDuration();
const updatedTime = useRef({ ms: 0, s: 0, m: 0, h: 0 });
const timer = useRef(null)
useEffect(() => {
if (isAccepted) {
runTimer(updatedTime, setDuration)
timer.current = setInterval(() => {
runTimer(updatedTime, setDuration)
}, 10);
} else {
clearInterval(timer.current);
timer.current = null;
}
return () => clearInterval(timer.current)
}, [isAccepted, setDuration]);
if (!duration) return null;
return (
<div
style={{
textAlign: 'center',
marginTop: '2em',
fontSize: 30,
}}
>
<span>{duration.h >= 10 ? duration.h : '0' + duration.h}</span>
&nbsp;:&nbsp;
<span>{duration.m >= 10 ? duration.m : '0' + duration.m}</span>
&nbsp;:&nbsp;
<span>{duration.s >= 10 ? duration.s : '0' + duration.s}</span>&nbsp;
</div>
);
}
export default CallTimer;
@@ -0,0 +1,18 @@
import { makeStyles, Theme } from '@material-ui/core/styles';
const useStyles = makeStyles((theme: Theme) => ({
actions: {
width: '100%',
margin: 'auto',
position: 'absolute',
display: 'flex',
alignContent: 'center',
justifyContent: 'center',
bottom: '10%',
},
actionButton: {
margin: 10,
},
}));
export default useStyles;
+16
View File
@@ -0,0 +1,16 @@
import { atom } from 'recoil';
export const callerState = {
currentCall: atom({
key: 'currentCall',
default: null
}),
callModal: atom({
key: 'callingModal',
default: false
}),
callDuration: atom({
key: 'callDuration',
default: {ms: 0, s: 0, m: 0, h: 0}
})
};
+36
View File
@@ -0,0 +1,36 @@
import { useRecoilState } from 'recoil';
import { CallProps } from '../../../common/typings/call';
import Nui from '../../nui-events/utils/Nui';
import { callerState } from './state';
interface CallHook {
call: CallProps;
setCall: (details: any) => void;
acceptCall(): void;
rejectCall(): void;
endCall(): void;
}
export const useCall = (): CallHook => {
const [call, setCall] = useRecoilState(callerState.currentCall);
const acceptCall = () => {
Nui.send('phone:acceptCall', {
transmitterNumber: call.transmitter,
});
};
const rejectCall = () => {
Nui.send('phone:rejectCall', {
phoneNumber: call.transmitter,
});
};
const endCall = () => {
Nui.send('phone:endCall', {
transmitterNumber: call.transmitter,
});
};
return { call, setCall, acceptCall, rejectCall, endCall };
};
@@ -0,0 +1,94 @@
import React from 'react';
import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent';
import { useSetRecoilState } from 'recoil';
import { callerState } from './state';
import { useNotifications } from '../../notifications/hooks/useNotifications';
import { useTranslation } from 'react-i18next';
import { CallNotification } from '../components/CallNotification';
import { useCall } from './useCall';
import { CallProps } from '../../../common/typings/call';
import { useApp } from '../../apps/hooks/useApps';
import InjectDebugData from '../../debug/InjectDebugData';
const NOTIFICATION_ID = 'call:current';
InjectDebugData<CallProps>([
{
app: 'CALL',
method: 'setCaller',
data: {
accepted: false,
isTransmitter: false,
transmitter: 'Chip',
receiver: 'Taso',
active: true,
},
},
]);
export const useCallService = () => {
const { t } = useTranslation();
const {
addNotificationAlert,
removeId,
addNotification,
} = useNotifications();
const { call, setCall } = useCall();
const { icon, notificationIcon } = useApp('DIALER');
const setModal = useSetRecoilState(callerState.callModal);
const callNotificationBase = {
app: 'CALL',
id: NOTIFICATION_ID,
cantClose: true,
icon,
notificationIcon,
};
const _setCall = (_call: CallProps) => {
setCall(_call);
if (!_call || !_call.active) {
removeId(NOTIFICATION_ID);
return;
}
if (_call.accepted) {
removeId(NOTIFICATION_ID);
addNotification({
...callNotificationBase,
content: (
<CallNotification>
{t('APPS_DIALER_CURRENT_CALL_WITH', {
transmitter: _call.transmitter,
})}
</CallNotification>
),
title: t('APPS_DIALER_CURRENT_CALL_TITLE'),
});
}
if (!_call.isTransmitter && !_call.accepted) {
removeId(NOTIFICATION_ID);
addNotificationAlert(
{
...callNotificationBase,
title: t('APPS_DIALER_INCOMING_CALL_TITLE', {
transmitter: _call.transmitter,
}),
content: (
<CallNotification>
{t('APPS_DIALER_TRANSMITTER_IS_CALLING', {
transmitter: _call.transmitter,
})}
</CallNotification>
),
},
true
);
}
};
useNuiEvent<CallProps>('CALL', 'setCaller', _setCall, call);
useNuiEvent<boolean>('CALL', 'callModal', setModal);
};
+7
View File
@@ -0,0 +1,7 @@
import { useRecoilState } from 'recoil';
import { callerState } from './state';
export const useDuration = () => {
const [duration, setDuration] = useRecoilState(callerState.callDuration);
return { duration, setDuration}
}
+12
View File
@@ -0,0 +1,12 @@
import { useRecoilState } from 'recoil';
import { callerState } from './state';
interface ModalProps {
modal: boolean;
setModal: (show: boolean) => void;
}
export const useModal = (): ModalProps => {
const [modal, setModal] = useRecoilState(callerState.callModal);
return { modal, setModal }
}