diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 781e876..5707df6 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -2,9 +2,9 @@ import React from 'react'; import './Phone.css'; import './i18n'; import { Route } from 'react-router-dom'; -import { CallModal } from './modal/components/CallModal'; +import { CallModal } from './os/call/components/CallModal'; import { HomeApp } from './apps/home/components/Home'; -import { ThemeProvider, Slide } from '@material-ui/core'; +import { Slide } from '@material-ui/core'; import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard'; import { NotificationBar } from './os/notifications/components/NotificationBar'; import { Navigation } from './os/navigation-bar/components/Navigation'; @@ -21,13 +21,13 @@ import { useMessagesService } from './apps/messages/hooks/useMessageService'; import { useNotesService } from './apps/notes/hooks/useNotesService'; import { usePhotoService } from './apps/camera/hooks/usePhotoService'; import { useSettings } from './apps/settings/hooks/useSettings'; -import { useCallService } from './modal/hooks/useCallService'; -import { useModal } from './modal/hooks/useModal'; +import { useCallService } from './os/call/hooks/useCallService'; +import { useModal } from './os/call/hooks/useModal'; import { useDialService } from './apps/dialer/hooks/useDialService'; import InjectDebugData from './os/debug/InjectDebugData'; import { usePhoneVisibility } from './os/phone/hooks/usePhoneVisibility'; -import { usePhoneTheme } from './os/phone/hooks/usePhoneTheme'; import { Snackbar } from './ui/components/Snackbar'; +import { NotificationAlert } from './os/notifications/components/NotificationAlert'; function Phone() { const { modal } = useModal(); @@ -35,12 +35,7 @@ function Phone() { const [settings] = useSettings(); - const { - bottom, - visibility, - uncollapseNotifications, - clickEventOverride, - } = usePhoneVisibility(); + const { bottom, visibility } = usePhoneVisibility(); useNuiService(); usePhoneService(); @@ -56,58 +51,54 @@ function Phone() { useCallService(); useDialService(); - const currentTheme = usePhoneTheme(); - return ( - - -
-
+ +
+
+
+
-
-
- <> - -
- {modal ? ( - - ) : ( - <> - - {apps.map((App) => ( - - ))} - - )} - -
- - -
+ <> + +
+ {modal ? ( + + ) : ( + <> + + {apps.map((App) => ( + + ))} + + )} + + +
+ +
- - +
+ ); } diff --git a/phone/src/PhoneContainer.tsx b/phone/src/PhoneContainer.tsx new file mode 100644 index 0000000..80a6111 --- /dev/null +++ b/phone/src/PhoneContainer.tsx @@ -0,0 +1,21 @@ +import React from 'react'; +import { ThemeProvider } from '@material-ui/core'; +import { BankNotification } from './apps/bank/components/notification/BankNotification'; +import { NotificationsProvider } from './os/notifications/providers/NotificationsProvider'; +import { usePhoneTheme } from './os/phone/hooks/usePhoneTheme'; +import SnackbarProvider from './ui/providers/SnackbarProvider'; +import Phone from './Phone'; + +export const PhoneContainer = () => { + const currentTheme = usePhoneTheme(); + return ( + + + + + + + + + ); +}; diff --git a/phone/src/apps/dialer/components/DialerApp.tsx b/phone/src/apps/dialer/components/DialerApp.tsx index 4ce7eb0..3610085 100644 --- a/phone/src/apps/dialer/components/DialerApp.tsx +++ b/phone/src/apps/dialer/components/DialerApp.tsx @@ -39,7 +39,7 @@ export const DialerApp = () => { InjectDebugData([ { - app: 'DAILER', + app: 'DIALER', method: 'setHistory', data: [ { diff --git a/phone/src/apps/twitter/components/TwitterApp.tsx b/phone/src/apps/twitter/components/TwitterApp.tsx index 0547669..a23ab4d 100644 --- a/phone/src/apps/twitter/components/TwitterApp.tsx +++ b/phone/src/apps/twitter/components/TwitterApp.tsx @@ -21,100 +21,6 @@ import { useProfile } from '../hooks/useProfile'; import ProfilePrompt from './profile/ProfilePrompt'; import InjectDebugData from '../../../os/debug/InjectDebugData'; -InjectDebugData([ - { - app: 'TWITTER', - method: 'getOrCreateTwitterProfile', - data: { - profile_name: 'Angular_Dev', - bio: 'Angular FTW', - job: 'Googler @ Google', - location: '1600 Amphitheatre Parkway, CA, USA', - avatar_url: 'https://i.tasoagc.dev/i9Ig', - }, - }, - { - app: 'TWITTER', - method: 'createTweetBroadcast', - data: { - id: 111, - profile_id: 111, - profile_name: 'Kidz', - isMine: false, - isReported: false, - message: 'I have tweeted this because Im badass' - } - }, - { - app: 'TWITTER', - method: 'createTweetBroadcast', - data: { - id: 111, - profile_id: 111, - profile_name: 'ItsChip', - isMine: false, - isReported: false, - message: 'I have tweeted this because Im even more badass' - } - }, - { - app: 'TWITTER', - method: 'fetchTweets', - data: [ - { - id: 59, - message: 'XSS\n\nBro', - avatar_url: 'http://i.tasoagc.dev/jtKL', - seconds_since_tweet: 500, - isMine: false, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'jQuery Main', - images: 'http://i.tasoagc.dev/0xdp', - }, - { - id: 2, - message: 'GCPhone will never die', - avatar_url: 'http://i.tasoagc.dev/jtKL', - seconds_since_tweet: 500, - isMine: false, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'GCPhone_v9999', - images: 'http://i.tasoagc.dev/0xdp', - }, - { - id: 1, - message: 'I love angular', - avatar_url: 'https://i.tasoagc.dev/i9Ig', - seconds_since_tweet: 800, - isMine: true, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'Angular_Dev', - images: 'http://i.tasoagc.dev/i9Ig', - }, - { - id: 3, - message: - 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + - 'do you know what shungite is? Anybody know what shungite is. No, no ' + - 'Suge Knight, I think hes locked up in prison.', - avatar_url: 'http://i.tasoagc.dev/0v64', - seconds_since_tweet: 800, - isMine: true, - updateAt: '2021-02-08 14:37:46', - visible: 1, - likes: 50000, - profile_name: 'The_Doc', - }, - ], - }, -]); - const useStyles = makeStyles(() => ({ backgroundModal: { background: 'black', @@ -188,3 +94,103 @@ export const TwitterApp = () => { ); }; + +InjectDebugData([ + { + app: 'TWITTER', + method: 'getOrCreateTwitterProfile', + data: { + profile_name: 'Angular_Dev', + bio: 'Angular FTW', + job: 'Googler @ Google', + location: '1600 Amphitheatre Parkway, CA, USA', + avatar_url: 'https://i.tasoagc.dev/i9Ig', + }, + }, + { + app: 'TWITTER', + method: 'fetchTweets', + data: [ + { + id: 59, + message: 'XSS\n\nBro', + avatar_url: 'http://i.tasoagc.dev/jtKL', + seconds_since_tweet: 500, + isMine: false, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'jQuery Main', + images: 'http://i.tasoagc.dev/0xdp', + }, + { + id: 2, + message: 'GCPhone will never die', + avatar_url: 'http://i.tasoagc.dev/jtKL', + seconds_since_tweet: 500, + isMine: false, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'GCPhone_v9999', + images: 'http://i.tasoagc.dev/0xdp', + }, + { + id: 1, + message: 'I love angular', + avatar_url: 'https://i.tasoagc.dev/i9Ig', + seconds_since_tweet: 800, + isMine: true, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'Angular_Dev', + images: 'http://i.tasoagc.dev/i9Ig', + }, + { + id: 3, + message: + 'Anyways uhm... I bought a whole bunch of shungite, rocks, ' + + 'do you know what shungite is? Anybody know what shungite is. No, no ' + + 'Suge Knight, I think hes locked up in prison.', + avatar_url: 'http://i.tasoagc.dev/0v64', + seconds_since_tweet: 800, + isMine: true, + updateAt: '2021-02-08 14:37:46', + visible: 1, + likes: 50000, + profile_name: 'The_Doc', + }, + ], + }, +]); + +InjectDebugData( + [ + { + app: 'TWITTER', + method: 'createTweetBroadcast', + data: { + id: 111, + profile_id: 111, + profile_name: 'Kidz', + isMine: false, + isReported: false, + message: 'I have tweeted this because Im badass', + }, + }, + { + app: 'TWITTER', + method: 'createTweetBroadcast', + data: { + id: 111, + profile_id: 111, + profile_name: 'ItsChip', + isMine: false, + isReported: false, + message: 'I have tweeted this because Im even more badass', + }, + }, + ], + 2000 +); diff --git a/phone/src/apps/twitter/components/notification/TwitterNotification.test.tsx b/phone/src/apps/twitter/components/notification/TwitterNotification.test.tsx deleted file mode 100644 index 958af69..0000000 --- a/phone/src/apps/twitter/components/notification/TwitterNotification.test.tsx +++ /dev/null @@ -1,47 +0,0 @@ -import React from 'react'; -import { shallow } from 'enzyme'; -import TwitterNotification from './TwitterNotification'; - -jest.mock('react-i18next', () => ({ - useTranslation: () => ({ t: (key) => key }), -})); - -jest.mock('../../../../os/phone/hooks/usePhone', () => ({ - usePhone: jest.fn().mockReturnValue({ - config: { - twitter: { - showNotifications: true, - }, - }, - }), -})); - -jest.mock('../../hooks/useTwitterNotification', () => ({ - useTwitterNotification: jest.fn().mockReturnValue({ - notification: { - profile_name: 'test', - message: 'test message', - }, - }), -})); - -describe('TwitterNotification', () => { - let wrapper; - - beforeEach(() => { - wrapper = shallow(); - }); - it('renders', () => { - expect(wrapper).toBeDefined(); - }); - - it('renders profile name', () => { - expect(wrapper.find('.makeStyles-profileName-5').text()).toEqual('@test'); - }); - - it('renders message', () => { - expect(wrapper.find('.makeStyles-message-7').text()).toEqual( - 'test message' - ); - }); -}); diff --git a/phone/src/apps/twitter/components/notification/TwitterNotification.tsx b/phone/src/apps/twitter/components/notification/TwitterNotification.tsx deleted file mode 100644 index 14d7e31..0000000 --- a/phone/src/apps/twitter/components/notification/TwitterNotification.tsx +++ /dev/null @@ -1,88 +0,0 @@ -import React, { useEffect, useState } from 'react'; -import { useTranslation } from 'react-i18next'; -import { makeStyles, createStyles, Theme } from '@material-ui/core/styles'; -import TwitterIcon from '@material-ui/icons/Twitter'; -import { useTwitterNotification } from '../../hooks/useTwitterNotification'; -import Notification from '../../../../ui/components/Notification'; -import { usePhone } from '../../../../os/phone/hooks/usePhone'; - -const useStyles = makeStyles((theme: Theme) => - createStyles({ - content: { - padding: theme.spacing(2), - overflowY: 'hidden', - }, - title: { - display: 'flex', - flexFlow: 'row nowrap', - justifyContent: 'space-between', - alignItems: 'center', - }, - titleLeft: { - display: 'flex', - flexFlow: 'row nowrap', - justifyContent: 'flex-start', - alignItems: 'center', - }, - icon: { - marginTop: '3px', - marginRight: '5px', - }, - profileName: { - marginBottom: '3px', - }, - justNow: { - fontSize: '18px', - fontStyle: 'italic', - paddingRight: '3px', - }, - message: { - fontSize: '16px', - overflow: 'hidden', - display: '-webkit-box', - WebkitLineClamp: 2, - WebkitBoxOrient: 'vertical', - }, - }) -); - -function TwitterNotification() { - const classes = useStyles(); - const { t } = useTranslation(); - const [visible, setVisible] = useState(false); - const { notification } = useTwitterNotification(); - const { config } = usePhone(); - - useEffect(() => { - if (notification && notification.id) { - setVisible(true); - } - }, [notification]); - - if (!config?.twitter.showNotifications || !notification) return null; - - const profileName = `@${notification.profile_name?.trim()}`; - - return ( - setVisible(false)} - > -
-
-
-
- -
-
{profileName}
-
-
{t('APPS_TWITTER_JUST_NOW')}
-
-
{notification.message}
-
-
- ); -} - -export default TwitterNotification; diff --git a/phone/src/apps/twitter/components/tweet/TweetListContainer.tsx b/phone/src/apps/twitter/components/tweet/TweetListContainer.tsx index a5a74c2..d90d7b5 100644 --- a/phone/src/apps/twitter/components/tweet/TweetListContainer.tsx +++ b/phone/src/apps/twitter/components/tweet/TweetListContainer.tsx @@ -2,15 +2,23 @@ import React, { useEffect } from 'react'; import Nui from '../../../../os/nui-events/utils/Nui'; import { useTweets } from '../../hooks/useTweets'; +import { useTwitterNotifications } from '../../hooks/useTwitterNotifications'; import TweetList from './TweetList'; export function TweetListContainer() { const { tweets } = useTweets(); + const { setUnreadCount } = useTwitterNotifications(); useEffect(() => { Nui.send('phone:fetchTweets', {}); }, []); + useEffect(() => { + if (tweets?.length) { + setUnreadCount(0); + } + }, [setUnreadCount, tweets]); + return ; } diff --git a/phone/src/apps/twitter/hooks/state.ts b/phone/src/apps/twitter/hooks/state.ts index f541623..cd3aa07 100644 --- a/phone/src/apps/twitter/hooks/state.ts +++ b/phone/src/apps/twitter/hooks/state.ts @@ -37,8 +37,8 @@ export const twitterState = { key: 'updateProfileSuccess', default: null, }), - notification: atom({ - key: 'notification', - default: null, + unreadTweetsCount: atom({ + key: 'unreadTweetsCount', + default: 0, }), }; diff --git a/phone/src/apps/twitter/hooks/useTwitterNotification.ts b/phone/src/apps/twitter/hooks/useTwitterNotification.ts deleted file mode 100644 index d01a9ca..0000000 --- a/phone/src/apps/twitter/hooks/useTwitterNotification.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { useRecoilValue } from 'recoil'; - -import { Tweet } from '../../../common/typings/twitter'; -import { twitterState } from './state'; - -interface ITwitterNotification { - notification: Tweet | null; -} - -export const useTwitterNotification = (): ITwitterNotification => { - const notification = useRecoilValue(twitterState.notification); - return { notification }; -}; diff --git a/phone/src/apps/twitter/hooks/useTwitterNotifications.ts b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts new file mode 100644 index 0000000..d6cb625 --- /dev/null +++ b/phone/src/apps/twitter/hooks/useTwitterNotifications.ts @@ -0,0 +1,43 @@ +import { useCallback } from "react"; +import { useTranslation } from "react-i18next"; +import { useHistory } from "react-router-dom"; +import { useRecoilState } from "recoil"; +import { useApp } from "../../../os/apps/hooks/useApps"; +import { useNotifications } from "../../../os/notifications/hooks/useNotifications"; +import { twitterState } from "./state"; + +export const useTwitterNotifications = () => { + const { t } = useTranslation(); + const history = useHistory(); + const { addNotificationAlert } = useNotifications(); + const { icon, notificationIcon } = useApp('TWITTER'); + const [unreadCount, setUnreadCount] = useRecoilState(twitterState.unreadTweetsCount); + const setNotification = useCallback( + ({ profile_name, message }) => { + setUnreadCount((curr) => { + const unread = curr + 1; + addNotificationAlert( + { + app: 'TWITTER', + title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }), + onClick: () => history.push('/twitter'), + content: message, + icon, + notificationIcon, + }, + true, + { + title: t('APPS_TWITTER_UNREAD_MESSAGES', { + count: unread, + }), + content: null, + } + ); + return unread; + }); + }, + [setUnreadCount, addNotificationAlert, t, icon, notificationIcon, history] + ); + + return { setNotification, setUnreadCount, unreadCount } +}; diff --git a/phone/src/apps/twitter/hooks/useTwitterService.ts b/phone/src/apps/twitter/hooks/useTwitterService.ts index d94a7d0..3cf31b7 100644 --- a/phone/src/apps/twitter/hooks/useTwitterService.ts +++ b/phone/src/apps/twitter/hooks/useTwitterService.ts @@ -5,10 +5,7 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent'; import { IMAGE_DELIMITER } from '../utils/images'; import { APP_TWITTER } from '../utils/constants'; import { twitterState } from './state'; -import { useNotifications } from '../../../os/notifications/hooks/useNotifications'; -import { useApp } from '../../../os/apps/hooks/useApps'; -import { useTranslation } from 'react-i18next'; -import { useCallback } from 'react'; +import { useTwitterNotifications } from './useTwitterNotifications'; /** * Perform all necessary processing/transforms from the raw database @@ -32,22 +29,7 @@ function processTweet(tweet) { * there and have moved that logic here instead. */ export const useTwitterService = () => { - const { t } = useTranslation(); - const { addNotification } = useNotifications(); - const { icon, notificationIcon } = useApp('TWITTER'); - - const setNotification = useCallback( - ({ profile_name, message }) => - addNotification({ - app: 'TWITTER', - title: t('APPS_TWITTER_NEW_BROADCAST', { profile_name }), - href: '/twitter', - content: message, - icon, - notificationIcon, - }), - [addNotification, icon, notificationIcon, t] - ); + const { setNotification } = useTwitterNotifications(); const setProfile = useSetRecoilState(twitterState.profile); const setUpdateProfileLoading = useSetRecoilState( diff --git a/phone/src/common/typings/call.ts b/phone/src/common/typings/call.ts index 58cc8db..78c195f 100644 --- a/phone/src/common/typings/call.ts +++ b/phone/src/common/typings/call.ts @@ -1,4 +1,5 @@ export interface CallProps { + active: boolean; accepted: boolean; isTransmitter: boolean; transmitter: string; diff --git a/phone/src/index.tsx b/phone/src/index.tsx index 1834931..bd22b27 100644 --- a/phone/src/index.tsx +++ b/phone/src/index.tsx @@ -6,12 +6,8 @@ import { RecoilRoot } from 'recoil'; import * as Sentry from '@sentry/react'; import { Integrations } from '@sentry/tracing'; import './main.css'; -import Phone from './Phone'; -import TwitterNotification from './apps/twitter/components/notification/TwitterNotification'; -import { BankNotification } from './apps/bank/components/notification/BankNotification'; import PhoneConfig from './config/default.json'; -import SnackbarProvider from './ui/providers/SnackbarProvider'; -import { NotificationsProvider } from './os/notifications/providers/NotificationsProvider'; +import { PhoneContainer } from './PhoneContainer'; // Enable Sentry when config setting is true if (PhoneConfig.SentryErrorMetrics) { @@ -29,13 +25,7 @@ if (PhoneConfig.SentryErrorMetrics) { ReactDOM.render( - - - - - - - + , document.getElementById('root') diff --git a/phone/src/locale/en.json b/phone/src/locale/en.json index a36c0c1..0a4a61e 100644 --- a/phone/src/locale/en.json +++ b/phone/src/locale/en.json @@ -17,6 +17,14 @@ "APPS_SELLOUT": "Market", "APPS_CAMERA": "Camera", + "APPS_DIALER_CURRENT_CALL_TITLE": "Current Phone Call", + "APPS_DIALER_INCOMING_CALL_TITLE": "Incoming Phone Call", + "APPS_DIALER_CURRENT_CALL_WITH": "In call with {{ transmitter }}", + "APPS_DIALER_TRANSMITTER_IS_CALLING": "You have a new call from {{ transmitter }}!", + "APPS_DIALER_ACCEPT_CALL": "Accept", + "APPS_DIALER_REJECT_CALL": "Reject", + "APPS_DIALER_END_CALL": "End", + "APPS_BANK_TRANSFER": "Transfer", "APPS_SETTINGS_OPTIONS_WALLPAPER": "Wallpaper", "APPS_SETTINGS_OPTIONS_FRAME": "Frame", @@ -48,6 +56,7 @@ "APPS_TWITTER_TIME_HOURS": "h", "APPS_TWITTER_TIME_DAYS": "d", "APPS_TWITTER_NEW_BROADCAST": "New tweet from @{{ profile_name }}!", + "APPS_TWITTER_UNREAD_MESSAGES": "You have {{ count }} new tweets in your timeline!", "APPS_BANK_HOME_TITLE": "Welcome", "APPS_BANK_ACCOUNT_LINK": "Account", diff --git a/phone/src/modal/components/CallModal.tsx b/phone/src/modal/components/CallModal.tsx deleted file mode 100644 index 6603f33..0000000 --- a/phone/src/modal/components/CallModal.tsx +++ /dev/null @@ -1,110 +0,0 @@ -import React from 'react'; -import { Fab } from '@material-ui/core'; -import { AppWrapper } from '../../ui/components/AppWrapper'; -import { AppContent } from '../../ui/components/AppContent'; -import { useCall } from '../hooks/useCall'; -import CallIcon from '@material-ui/icons/Call'; -import CallEndIcon from '@material-ui/icons/CallEnd'; -import useStyles from './modal.styles'; -import { useModal } from '../hooks/useModal'; -import Nui from '../../os/nui-events/utils/Nui'; -import CallTimer from './CallTimer'; -import InjectDebugData from '../../os/debug/InjectDebugData'; - -InjectDebugData([ - { - app: 'CALL', - method: 'setCaller', - data: { - accepted: true, - isTransmitter: true, - transmitter: 'Chip', - receiver: 'Taso', - phone_number: '860-4504', - }, - }, -]); - -export const CallModal = () => { - const { call } = useCall(); - const { setModal } = useModal(); - - const classes = useStyles(); - - const handleAcceptCall = () => { - Nui.send('phone:acceptCall', { - transmitterNumber: call.transmitter, - }); - }; - - const handleRejectCall = () => { - setModal(false); - Nui.send('phone:rejectCall', { - phoneNumber: call.transmitter, - }); - }; - - const handleEndCall = () => { - setModal(false); - Nui.send('phone:endCall', { - transmitterNumber: call.transmitter, - }); - }; - - if (call.isTransmitter) { - return ( - - -

{call.receiver}

- -
- - - -
-
-
- ); - } - - return ( - - -

{call.transmitter}

- -
- {!call.accepted ? ( - <> - - - - - - - - ) : ( - - - - )} -
-
-
- ); -}; diff --git a/phone/src/modal/hooks/useCall.ts b/phone/src/modal/hooks/useCall.ts deleted file mode 100644 index 6adc8ea..0000000 --- a/phone/src/modal/hooks/useCall.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { useRecoilState } from 'recoil'; -import { CallProps } from '../../common/typings/call'; -import { callerState } from './state'; - -interface CallHook { - call: CallProps; - setCall: (details: any) => void; -} - -export const useCall = (): CallHook => { - const [call, setCall] = useRecoilState(callerState.currentCall); - return { call, setCall }; -}; diff --git a/phone/src/modal/hooks/useCallService.ts b/phone/src/modal/hooks/useCallService.ts deleted file mode 100644 index 74dbb13..0000000 --- a/phone/src/modal/hooks/useCallService.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { useNuiEvent } from '../../os/nui-events/hooks/useNuiEvent'; -import { useRecoilState, useSetRecoilState } from 'recoil'; -import { callerState } from './state'; - -export const useCallService = () => { - const [caller, setCaller] = useRecoilState(callerState.currentCall); - const setModal = useSetRecoilState(callerState.callModal); - - useNuiEvent('CALL', 'setCaller', setCaller, {}, caller); - useNuiEvent('CALL', 'callModal', setModal); -}; diff --git a/phone/src/os/call/components/CallControls.tsx b/phone/src/os/call/components/CallControls.tsx new file mode 100644 index 0000000..8ef9cf6 --- /dev/null +++ b/phone/src/os/call/components/CallControls.tsx @@ -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 ( + + + + ); + } + return ( + <> + + + + + + + + ); +}; diff --git a/phone/src/os/call/components/CallModal.tsx b/phone/src/os/call/components/CallModal.tsx new file mode 100644 index 0000000..64b0b01 --- /dev/null +++ b/phone/src/os/call/components/CallModal.tsx @@ -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 ( + + + +

{call?.transmitter}

+ +
+
+ +
+
+
+ ); +}; diff --git a/phone/src/os/call/components/CallNotification.tsx b/phone/src/os/call/components/CallNotification.tsx new file mode 100644 index 0000000..d8b8719 --- /dev/null +++ b/phone/src/os/call/components/CallNotification.tsx @@ -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 ( +
+ {children} + + + +
+ ); +}; diff --git a/phone/src/modal/components/CallTimer.tsx b/phone/src/os/call/components/CallTimer.tsx similarity index 100% rename from phone/src/modal/components/CallTimer.tsx rename to phone/src/os/call/components/CallTimer.tsx diff --git a/phone/src/modal/components/modal.styles.ts b/phone/src/os/call/components/modal.styles.ts similarity index 95% rename from phone/src/modal/components/modal.styles.ts rename to phone/src/os/call/components/modal.styles.ts index a8482dc..f4da695 100644 --- a/phone/src/modal/components/modal.styles.ts +++ b/phone/src/os/call/components/modal.styles.ts @@ -11,7 +11,7 @@ const useStyles = makeStyles((theme: Theme) => ({ bottom: '10%', }, actionButton: { - margin: 20, + margin: 10, }, })); diff --git a/phone/src/modal/hooks/state.ts b/phone/src/os/call/hooks/state.ts similarity index 100% rename from phone/src/modal/hooks/state.ts rename to phone/src/os/call/hooks/state.ts diff --git a/phone/src/os/call/hooks/useCall.ts b/phone/src/os/call/hooks/useCall.ts new file mode 100644 index 0000000..a09766b --- /dev/null +++ b/phone/src/os/call/hooks/useCall.ts @@ -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 }; +}; diff --git a/phone/src/os/call/hooks/useCallService.tsx b/phone/src/os/call/hooks/useCallService.tsx new file mode 100644 index 0000000..90a94f7 --- /dev/null +++ b/phone/src/os/call/hooks/useCallService.tsx @@ -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([ + { + 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: ( + + {t('APPS_DIALER_CURRENT_CALL_WITH', { + transmitter: _call.transmitter, + })} + + ), + 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: ( + + {t('APPS_DIALER_TRANSMITTER_IS_CALLING', { + transmitter: _call.transmitter, + })} + + ), + }, + true + ); + } + }; + + useNuiEvent('CALL', 'setCaller', _setCall, call); + useNuiEvent('CALL', 'callModal', setModal); +}; diff --git a/phone/src/modal/hooks/useDuration.ts b/phone/src/os/call/hooks/useDuration.ts similarity index 100% rename from phone/src/modal/hooks/useDuration.ts rename to phone/src/os/call/hooks/useDuration.ts diff --git a/phone/src/modal/hooks/useModal.ts b/phone/src/os/call/hooks/useModal.ts similarity index 100% rename from phone/src/modal/hooks/useModal.ts rename to phone/src/os/call/hooks/useModal.ts diff --git a/phone/src/os/debug/InjectDebugData.ts b/phone/src/os/debug/InjectDebugData.ts index 87ac94f..ad63348 100644 --- a/phone/src/os/debug/InjectDebugData.ts +++ b/phone/src/os/debug/InjectDebugData.ts @@ -1,7 +1,7 @@ -interface DebugEvent { +interface DebugEvent

{ app: string; method: string; - data: any; + data: P; } // TODO: This should be unit tested @@ -27,7 +27,7 @@ interface DebugEvent { * } * ]) **/ -const InjectDebugData = (events: DebugEvent[], timer = 1000) => { +const InjectDebugData =

(events: DebugEvent

[], timer = 1000) => { if (process.env.NODE_ENV === 'development') { for (const event of events) { setTimeout(() => { diff --git a/phone/src/os/keyboard/hooks/useKeyboard.ts b/phone/src/os/keyboard/hooks/useKeyboard.ts index 73c93c7..b2f9dfe 100644 --- a/phone/src/os/keyboard/hooks/useKeyboard.ts +++ b/phone/src/os/keyboard/hooks/useKeyboard.ts @@ -1,7 +1,7 @@ -import { useCallback, useEffect } from 'react'; +import { useCallback, useEffect, useRef } from 'react'; import { useHistory } from 'react-router-dom'; import { atom, useSetRecoilState, useRecoilValue } from 'recoil'; -import Nui from '../../nui-events/utils/Nui'; +import { usePhone } from '../../phone/hooks/usePhone'; const keyboardState = { ArrowRight: atom({ @@ -48,6 +48,7 @@ const isKeyValid = (key) => validKeys.indexOf(key) !== -1; export const useInitKeyboard = () => { const history = useHistory(); + const { closePhone } = usePhone(); const getters = { ArrowRight: useRecoilValue(keyboardState.ArrowRight), ArrowLeft: useRecoilValue(keyboardState.ArrowLeft), @@ -58,21 +59,26 @@ export const useInitKeyboard = () => { Escape: useRecoilValue(keyboardState.Escape), }; + const handlers = useRef(new Map()); const setEscape = useSetRecoilState(keyboardState.Escape); const setBackspace = useSetRecoilState(keyboardState.Backspace); + useEffect( + function registerCustomKeys() { + Object.keys(getters).forEach((g) => { + handlers.current.set(g, getters[g]); + }); + }, + [getters] + ); + useEffect( function handleNUIKeyboardMessage() { function onKeyUp(event) { const { key } = event; - const callback = getters[key]; - if ( - isKeyValid(key) && - callback && - callback.handler && - callback.handler.call - ) { - return callback.handler(event); + const callback = handlers.current.get(key); + if (isKeyValid(key) && callback && callback.call) { + return callback(event); } } @@ -82,35 +88,22 @@ export const useInitKeyboard = () => { [getters] ); - const escapeHandler = useCallback(() => Nui.send('phone:close'), []); - - const backspaceHandler = useCallback((event) => { - if (['input', 'textarea'].includes(event.target.nodeName.toLowerCase())) { - // Dont anything if we are typing something :) - return; - } - history.goBack(); - }, [history]); + const backspaceHandler = useCallback( + (event) => { + if (['input', 'textarea'].includes(event.target.nodeName.toLowerCase())) { + // Dont anything if we are typing something :) + return; + } + history.goBack(); + }, + [history] + ); useEffect( function registerDefaultHandlers() { - setEscape({ handler: escapeHandler }); - setBackspace({ handler: backspaceHandler }); + handlers.current.set('Escape', () => closePhone()); + handlers.current.set('Backspace', backspaceHandler); }, - [setEscape, setBackspace, history, escapeHandler, backspaceHandler] + [setEscape, setBackspace, history, backspaceHandler, closePhone] ); }; - -export const useKeyboard = () => { - const setters = { - ArrowRight: useSetRecoilState(keyboardState.ArrowRight), - ArrowLeft: useSetRecoilState(keyboardState.ArrowLeft), - ArrowUp: useSetRecoilState(keyboardState.ArrowUp), - ArrowDown: useSetRecoilState(keyboardState.ArrowDown), - Enter: useSetRecoilState(keyboardState.Enter), - }; - - return (key, handler) => { - setters[key]({ handler }); - }; -}; diff --git a/phone/src/os/navigation-bar/components/Navigation.tsx b/phone/src/os/navigation-bar/components/Navigation.tsx index 2fc7ef9..92406d8 100644 --- a/phone/src/os/navigation-bar/components/Navigation.tsx +++ b/phone/src/os/navigation-bar/components/Navigation.tsx @@ -8,8 +8,7 @@ import KeyboardArrowLeftIcon from '@material-ui/icons/KeyboardArrowLeft'; import AppsIcon from '@material-ui/icons/Apps'; import RadioButtonUncheckedIcon from '@material-ui/icons/RadioButtonUnchecked'; import { useHistory } from 'react-router-dom'; - -import Nui from '../../nui-events/utils/Nui'; +import { usePhone } from '../../phone/hooks/usePhone'; const useStyles = makeStyles((theme) => ({ root: { @@ -19,10 +18,7 @@ const useStyles = makeStyles((theme) => ({ })); export const Navigation = () => { - const closePhone = () => { - console.log('Phone closed'); - Nui.send('phone:close'); - }; + const { closePhone } = usePhone(); const classes = useStyles(); const history = useHistory(); @@ -38,12 +34,12 @@ export const Navigation = () => { /> closePhone()} + value={closePhone} icon={} /> history.goBack()} + value={history.goBack} icon={} /> diff --git a/phone/src/os/notifications/components/NotificationAlert.tsx b/phone/src/os/notifications/components/NotificationAlert.tsx new file mode 100644 index 0000000..bba4a13 --- /dev/null +++ b/phone/src/os/notifications/components/NotificationAlert.tsx @@ -0,0 +1,46 @@ +import React from 'react'; +import { Box, makeStyles } from '@material-ui/core'; +import { useNotifications } from '../hooks/useNotifications'; +import { Alert, AlertTitle } from '@material-ui/lab'; + +const useStyles = makeStyles((theme) => ({ + snackbar: { + marginTop: '-710px', + display: 'flex', + justifyContent: 'center', + alignItems: 'center', + }, + alert: { + position: 'relative', + cursor: 'pointer', + width: '370px', + height: '120px', + zIndex: 50, + backgroundColor: theme.palette.background.paper, + color: theme.palette.text.primary, + '& .MuiAlert-icon': { + color: theme.palette.text.primary, + }, + }, +})); + +export const NotificationAlert = () => { + const classes = useStyles(); + const { currentAlert } = useNotifications(); + + return ( +

+ {currentAlert ? ( + currentAlert.onClick?.(currentAlert)} + icon={currentAlert.icon || false} + className={classes.alert} + elevation={6} + > + {currentAlert.title} + {currentAlert.content} + + ) : null} +
+ ); +}; diff --git a/phone/src/os/notifications/components/NotificationBar.tsx b/phone/src/os/notifications/components/NotificationBar.tsx index cf4a57d..af0232a 100644 --- a/phone/src/os/notifications/components/NotificationBar.tsx +++ b/phone/src/os/notifications/components/NotificationBar.tsx @@ -8,18 +8,14 @@ import { Paper, Box, List, - ListItem, - ListItemText, Divider, - ListItemAvatar, } from '@material-ui/core'; import SignalIcon from '@material-ui/icons/SignalCellular3Bar'; -import CloseIcon from '@material-ui/icons/Close'; import Battery90Icon from '@material-ui/icons/Battery90'; import ArrowDropUpIcon from '@material-ui/icons/ArrowDropUp'; import Default from '../../../config/default.json'; import { useNotifications } from '../hooks/useNotifications'; -import { useHistory } from 'react-router-dom'; +import { NotificationItem } from './NotificationItem'; const useStyles = makeStyles((theme) => ({ root: { @@ -67,9 +63,8 @@ const useStyles = makeStyles((theme) => ({ }, })); -export const NotificationBar = ({ forceUncollapse }: { forceUncollapse: boolean }) => { +export const NotificationBar = () => { const classes = useStyles(); - const history = useHistory(); const { icons, notifications, removeNotification } = useNotifications(); @@ -120,41 +115,29 @@ export const NotificationBar = ({ forceUncollapse }: { forceUncollapse: boolean - + - {notifications.map(({ href, title, content, icon }, idx) => ( - { - if (href) { - history.push(href); + {notifications.map((notification, idx) => ( + { + e.stopPropagation(); + if (notifications.length === 1) { setUncollapsed(false); + } + removeNotification(idx); + }} + onClickClose={() => { + setUncollapsed(false); + if (!notification.cantClose) { removeNotification(idx); } }} - className={classes.notificationItem} - key={idx} - > - {icon && {icon}} - {title} - { - e.stopPropagation(); - if (notifications.length === 1) { - setUncollapsed(false); - } - removeNotification(idx); - }} - > - - - + {...notification} + /> ))} diff --git a/phone/src/os/notifications/components/NotificationItem.tsx b/phone/src/os/notifications/components/NotificationItem.tsx new file mode 100644 index 0000000..e37d751 --- /dev/null +++ b/phone/src/os/notifications/components/NotificationItem.tsx @@ -0,0 +1,58 @@ +import React from 'react'; +import CloseIcon from '@material-ui/icons/Close'; +import { + IconButton, + ListItem, + ListItemAvatar, + ListItemText, + makeStyles, +} from '@material-ui/core'; +import { INotification } from '../providers/NotificationsProvider'; + +const useStyles = makeStyles((theme) => ({ + closeNotifBtn: { + position: 'absolute', + right: '8px', + top: '8px', + }, + notificationItem: { + position: 'relative', + }, +})); + +export const NotificationItem = ({ + onClose, + onClickClose, + ...notification +}: INotification & { + onClose: (e: any) => void; + onClickClose: (e: any) => void; +}) => { + const { title, icon, content, cantClose, onClick } = notification; + const classes = useStyles(); + return ( + { + if (onClick) { + onClick(notification); + onClickClose(e); + } + }} + className={classes.notificationItem} + > + {icon && {icon}} + {title} + {!cantClose && ( + + + + )} + + ); +}; diff --git a/phone/src/os/notifications/notifications.constants.ts b/phone/src/os/notifications/notifications.constants.ts new file mode 100644 index 0000000..eaf1909 --- /dev/null +++ b/phone/src/os/notifications/notifications.constants.ts @@ -0,0 +1 @@ +export const DEFAULT_ALERT_HIDE_TIME = 3000; diff --git a/phone/src/os/notifications/providers/NotificationsProvider.tsx b/phone/src/os/notifications/providers/NotificationsProvider.tsx index dacd9c5..df61118 100644 --- a/phone/src/os/notifications/providers/NotificationsProvider.tsx +++ b/phone/src/os/notifications/providers/NotificationsProvider.tsx @@ -1,12 +1,22 @@ -import React, { createContext, useState, useCallback, useMemo } from 'react'; +import React, { + createContext, + useState, + useCallback, + useMemo, + useRef, + useEffect, +} from 'react'; +import { DEFAULT_ALERT_HIDE_TIME } from '../notifications.constants'; export interface INotification { app: string; + id?: string; title: string; content?: React.ReactNode; icon?: JSX.Element; notificationIcon?: JSX.Element; - href?: string; + cantClose?: boolean; + onClick?: (notification: INotification) => void; } interface INotificationIcon { @@ -17,39 +27,143 @@ interface INotificationIcon { export const NotificationsContext = createContext<{ notifications: INotification[]; + currentAlert: INotification; icons: INotificationIcon[]; count: number; + removeAlerts(): void; + addOrUpdateNotification(add: INotification, update?: INotification): void; addNotification(value: INotification): void; - updateNotification(idx: number, value: INotification): void; removeNotification(idx: number): void; + updateId(id: string, value: Partial): void; + removeId(id: string): void; + hasNotification(app: string): number; + addNotificationAlert( + n: INotification, + persist: boolean, + update?: Partial + ); }>(null); export function NotificationsProvider({ children }) { const [notifications, setNotifications] = useState([]); + const alertTimeout = useRef(); + const [alerts, setAlerts] = useState< + Array<[INotification, boolean, Partial]> + >([]); + const [currentAlert, setCurrentAlert] = useState(); + const updateNotification = useCallback( (idx: number, value: INotification) => { setNotifications((all) => { const updated = [...all]; - updated.splice(idx, 0, value); + updated.splice(idx, 1, value); return updated; }); }, [] ); - const removeNotification = useCallback((idx) => { + const removeNotification = (idx) => { setNotifications((all) => { - const updated = [...all]; + const updated = all; updated.splice(idx, 1); - return updated; + return [...updated]; }); - }, []); + }; const addNotification = useCallback((value: INotification) => { setNotifications((all) => [value, ...all]); }, []); + /** + * Checks if a notification exists for current app + * and returns its index or -1 if not found. + */ + const hasNotification = useCallback( + (appId: string): number => { + return notifications.findIndex((n) => n.app === appId); + }, + [notifications] + ); + + const addOrUpdateNotification = useCallback( + (add: INotification, update?: INotification) => { + const exists = hasNotification(add.app); + if (update && exists !== -1) { + updateNotification(exists, update); + return; + } + addNotification(add); + }, + [addNotification, hasNotification, updateNotification] + ); + + const _showAlert = useCallback( + (n: INotification, persist: boolean, update: Partial) => { + return new Promise((res) => { + const resolve = () => { + if (persist) { + addOrUpdateNotification(n, update ? { ...n, ...update } : null); + } + res(); + }; + + alertTimeout.current = setTimeout(() => { + resolve(); + // If you change or remove the 300 i'll kill your pet - Kidz /s + // Really, this makes it work nicely with usePhoneVisibility :) + }, DEFAULT_ALERT_HIDE_TIME + 300); + + setCurrentAlert({ + ...n, + onClick: () => { + clearTimeout(alertTimeout.current); + n.onClick?.(n); + resolve(); + }, + }); + }); + }, + [addOrUpdateNotification] + ); + + const addNotificationAlert = ( + n: INotification, + persist?: boolean, + update?: Partial + ) => { + setAlerts((curr) => [...curr, [n, persist, update]]); + }; + + const removeId = (id: string) => { + const idx = notifications.findIndex((n) => n.id === id); + if (idx !== -1) { + removeNotification(idx); + } + }; + + const updateId = (id: string, value: Partial) => { + const idx = notifications.findIndex((n) => n.id === id); + if (idx !== -1) { + const curr = { ...notifications[idx], ...value }; + updateNotification(idx, curr); + } + }; + + useEffect(() => { + if (!currentAlert && alerts.length) { + _showAlert(...alerts[0]).then(() => { + setAlerts((curr) => { + const newQueue = curr; + curr.shift(); + return newQueue; + }); + setCurrentAlert(null); + }); + } + }, [_showAlert, alerts, currentAlert]); + const icons: INotificationIcon[] = useMemo( () => notifications.reduce((icons, curr) => { @@ -64,13 +178,24 @@ export function NotificationsProvider({ children }) { [notifications] ); + const removeAlerts = () => { + setAlerts([]); + setCurrentAlert(null); + }; + return ( >( app: string, method: string, handler: Function, - options: IOptions = {}, - currentState?: Record + currentState?: S, + options: IOptions = defaultOptions ) => { - const savedHandler: MutableRefObject = useRef(); - // Destructure passed options - const { capture, passive, once } = options; // When handler value changes set mutable ref to handler val useEffect(() => { @@ -44,15 +43,13 @@ export const useNuiEvent = ( data: event.data, level: 1, }); - const newData = currentState ? { ...currentState, ...data } : data - savedHandler.current(newData) + const newData = currentState ? { ...currentState, ...data } : data; + savedHandler.current(newData); } }; - // Why are destructing then restructuring option data? - const opts = { capture, passive, once }; - window.addEventListener(eventName, eventListener, opts); + window.addEventListener(eventName, eventListener, options); // Remove Event Listener on component cleanup - return () => window.removeEventListener(eventName, eventListener, opts); - }, [app, method, capture, passive, once, currentState]); + return () => window.removeEventListener(eventName, eventListener, options); + }, [app, method, currentState, options]); }; diff --git a/phone/src/os/phone/hooks/usePhone.ts b/phone/src/os/phone/hooks/usePhone.ts index fee582e..437891a 100644 --- a/phone/src/os/phone/hooks/usePhone.ts +++ b/phone/src/os/phone/hooks/usePhone.ts @@ -1,15 +1,24 @@ -import { useRecoilState } from 'recoil'; -import { phoneState } from './state'; import { IServerConfig } from '../../../common/typings/config'; import ServerConfig from '../../../config.autogen.json'; +import { useNotifications } from '../../notifications/hooks/useNotifications'; +import Nui from '../../nui-events/utils/Nui'; interface IUsePhone { - visibility: boolean; - setVisibility(v: boolean): void; config?: IServerConfig; + openPhone(): void; + closePhone(): void; } export const usePhone = (): IUsePhone => { - const [visibility, setVisibility] = useRecoilState(phoneState.visibility); - return { visibility, setVisibility, config: ServerConfig as any }; + const { removeAlerts } = useNotifications(); + + const closePhone = () => { + removeAlerts(); + Nui.send('phone:close'); + }; + const openPhone = () => { + Nui.send('phone:open'); + }; + + return { config: ServerConfig as any, closePhone, openPhone }; }; diff --git a/phone/src/os/phone/hooks/usePhoneVisibility.ts b/phone/src/os/phone/hooks/usePhoneVisibility.ts index 6d73edb..a3cfc14 100644 --- a/phone/src/os/phone/hooks/usePhoneVisibility.ts +++ b/phone/src/os/phone/hooks/usePhoneVisibility.ts @@ -1,56 +1,53 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; +import { useEffect, useMemo, useRef, useState } from 'react'; +import { useRecoilState } from 'recoil'; import { useSettings } from '../../../apps/settings/hooks/useSettings'; import { useNotifications } from '../../notifications/hooks/useNotifications'; -import { usePhone } from './usePhone'; +import { DEFAULT_ALERT_HIDE_TIME } from '../../notifications/notifications.constants'; +import { phoneState } from './state'; export const usePhoneVisibility = () => { - const { setVisibility, visibility } = usePhone(); - const { count } = useNotifications(); + const [visibility, setVisibility] = useRecoilState(phoneState.visibility); + const { currentAlert } = useNotifications(); const [{ zoom }] = useSettings(); const [notifVisibility, setNotifVisibility] = useState(false); - const isNotificationVisibleOnly = useMemo( - () => !!(!visibility && count > 0), - [visibility, count] - ); + const [isNotificationVisibleOnly, setNotificationVisibleOnly] = useState< + boolean + >(false); + + useEffect(() => { + setNotificationVisibleOnly(!!(!visibility && currentAlert)); + }, [visibility, currentAlert]); const notificationTimer = useRef(); useEffect(() => { if (isNotificationVisibleOnly) { setNotifVisibility(true); - clearTimeout(notificationTimer.current); + if (notificationTimer.current) { + clearTimeout(notificationTimer.current); + notificationTimer.current = undefined; + } notificationTimer.current = setTimeout(() => { - setVisibility(false); setNotifVisibility(false); - }, 5000); + }, DEFAULT_ALERT_HIDE_TIME); } - return () => clearTimeout(notificationTimer.current); + return () => { + clearTimeout(notificationTimer.current); + notificationTimer.current = undefined; + }; }, [isNotificationVisibleOnly, setVisibility]); const bottom = useMemo(() => { if (isNotificationVisibleOnly) { - return `calc(-60% * ${zoom})`; + return `calc(-70% * ${zoom})`; } return 0; }, [isNotificationVisibleOnly, zoom]); - const clickEventOverride = useCallback( - (e) => { - if (isNotificationVisibleOnly) { - e.preventDefault(); - e.stopPropagation(); - setVisibility(true); - } - }, - [setVisibility, isNotificationVisibleOnly] - ); - return { bottom, visibility: notifVisibility || visibility, - uncollapseNotifications: isNotificationVisibleOnly, - clickEventOverride, }; }; diff --git a/phone/src/ui/components/AppContent.tsx b/phone/src/ui/components/AppContent.tsx index 5fb88af..579c2f3 100644 --- a/phone/src/ui/components/AppContent.tsx +++ b/phone/src/ui/components/AppContent.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import { Box, makeStyles, Backdrop, Paper } from '@material-ui/core'; +import { Box, makeStyles, Backdrop, Paper, BoxProps } from '@material-ui/core'; import { AppContentTypes } from '../interface/InterfaceUI'; const useStyles = makeStyles(() => ({ @@ -29,7 +29,7 @@ export const AppContent = ({ backdrop, onClickBackdrop, ...props -}: AppContentTypes) => { +}: AppContentTypes & BoxProps) => { const classes = useStyles(); return ( diff --git a/phone/src/ui/components/GridMenu.tsx b/phone/src/ui/components/GridMenu.tsx index df9610c..bb023c9 100644 --- a/phone/src/ui/components/GridMenu.tsx +++ b/phone/src/ui/components/GridMenu.tsx @@ -1,9 +1,8 @@ -import React, { useEffect } from 'react'; +import React from 'react'; import { AppIcon } from './AppIcon'; import { useTranslation } from 'react-i18next'; import { Grid, makeStyles, Tooltip, Zoom } from '@material-ui/core'; import { Link } from 'react-router-dom'; -import { useKeyboard } from '../../os/keyboard/hooks/useKeyboard'; const useStyles = makeStyles((theme) => ({ root: { @@ -19,16 +18,9 @@ const useStyles = makeStyles((theme) => ({ })); export const GridMenu = ({ items, Component = AppIcon }) => { - const setKey = useKeyboard(); const classes = useStyles(); const { t } = useTranslation(); - useEffect(function registerKeyHandlers() { - setKey('ArrowLeft', () => { - console.log(':v'); - }); - // eslint-disable-next-line - }, []); // TODO: Fix tooltip position to be closer to app icon return ( diff --git a/resources/client/cl_call.ts b/resources/client/cl_call.ts index d82f112..d4bef19 100644 --- a/resources/client/cl_call.ts +++ b/resources/client/cl_call.ts @@ -1,6 +1,6 @@ import events from '../utils/events'; import { ICall } from '../../phone/src/common/typings/call'; -import {phoneCallStartAnim, phoneCallEndAnim} from './functions'; +import { phoneCallStartAnim, phoneCallEndAnim } from './functions'; const exp = (global as any).exports; @@ -21,6 +21,7 @@ onNet( app: 'CALL', method: 'setCaller', data: { + active: true, transmitter: transmitter, receiver: receiver, isTransmitter: isTransmitter, @@ -41,12 +42,13 @@ onNet( events.PHONE_CALL_WAS_ACCEPTED, (channelId: number, currentCall: ICall, isTransmitter: boolean) => { exp['mumble-voip'].SetCallChannel(channelId); - phoneCallStartAnim() // Trigger call animation only if the call was accepted. + phoneCallStartAnim(); // Trigger call animation only if the call was accepted. SendNuiMessage( JSON.stringify({ app: 'CALL', method: 'setCaller', data: { + active: true, transmitter: currentCall.transmitter, receiver: currentCall.receiver, isTransmitter: isTransmitter, @@ -75,13 +77,14 @@ onNet(events.PHONE_CALL_WAS_REJECTED, () => { receiver: null, isTransmitter: null, accepted: false, + active: false, }, }) ); }); onNet(events.PHONE_CALL_SEND_HANGUP_ANIM, () => { - phoneCallEndAnim() + phoneCallEndAnim(); }); RegisterNuiCallbackType(events.PHONE_END_CALL); // Fires when ending an ACTIVE call @@ -104,6 +107,7 @@ onNet(events.PHONE_CALL_WAS_ENDED, () => { receiver: null, isTransmitter: null, accepted: false, + active: false, }, }) ); diff --git a/resources/client/cl_main.ts b/resources/client/cl_main.ts index 7bf8247..6d0a023 100644 --- a/resources/client/cl_main.ts +++ b/resources/client/cl_main.ts @@ -1,51 +1,12 @@ import config from '../utils/config'; import events from '../utils/events'; -import {phoneCloseAnim, phoneOpenAnim, removePhoneProp} from './functions'; +import { phoneCloseAnim, phoneOpenAnim, removePhoneProp } from './functions'; let prop = 0; let isPhoneOpen = false; let propCreated = false; let phoneModel = 'prop_amb_phone'; // Refered to in newphoneProp function. Requires custom phone being streamed. -/* * * * * * * * * * * * * - * - * Register Command and Keybinding - * - * * * * * * * * * * * * */ - -RegisterCommand( - 'phone:close', - async (source: any, args: string[], raw: any) => { - await phoneCloseAnim(); - SetNuiFocus(false, false); - SendNuiMessage( - JSON.stringify({ - app: 'PHONE', - method: 'setVisibility', - data: false, - }) - ); - }, - false -); - -RegisterCommand( - 'phone', - async () => { - //-- Toggles Phone - await Phone(); - }, - false -); - -RegisterCommand( - 'phone:open', - async () => { - await Phone(); - }, - false -); - RegisterKeyMapping('phone', 'Open Phone', 'keyboard', 'f1'); /* * * * * * * * * * * * * @@ -66,7 +27,6 @@ const showPhone = async (): Promise => { data: true, }) ); - sendPhoneConfig(); SetNuiFocus(true, true); }; @@ -84,13 +44,36 @@ const hidePhone = async (): Promise => { SetNuiFocus(false, false); }; -const togglePhoneVisible = async (): Promise => { - if (isPhoneOpen) { +/* * * * * * * * * * * * * + * + * Register Command and Keybinding + * + * * * * * * * * * * * * */ + +RegisterCommand( + 'phone:close', + async () => { await hidePhone(); - } else { + }, + false +); + +RegisterCommand( + 'phone', + async () => { + //-- Toggles Phone + await Phone(); + }, + false +); + +RegisterCommand( + 'phone:open', + async () => { await showPhone(); - } -}; + }, + false +); /* * * * * * * * * * * * * * @@ -99,24 +82,17 @@ const togglePhoneVisible = async (): Promise => { * * * * * * * * * * * * */ async function Phone(): Promise { - fetchOnInitialize() + fetchOnInitialize(); if (config.PhoneAsItem) { // TODO: Do promise callback here // const hasPhoneItem = await emitNetPromise('phone:hasPhoneItem') // if (!hasPhoneItem) return - return await togglePhoneVisible(); } - await togglePhoneVisible(); -} - -function sendPhoneConfig() { - SendNuiMessage( - JSON.stringify({ - app: 'PHONE', - method: 'phoneConfig', - data: config, - }) - ); + if (isPhoneOpen) { + await hidePhone(); + } else { + await showPhone(); + } } AddEventHandler('onResourceStop', function (resource: string) { @@ -144,7 +120,6 @@ onNet('phone:sendCredentials', (number: string) => { ); }); - // DO NOT CHANGE THIS EITHER, PLEASE - CHIP /* * * * * * * * * * * * * @@ -157,7 +132,6 @@ function fetchOnInitialize() { emitNet(events.CONTACTS_GET_CONTACTS); } - RegisterNuiCallbackType(events.OPEN_APP_LISTINGS); on(`__cfx_nui:${events.OPEN_APP_LISTINGS}`, (data: any, cb: Function) => { emitNet(events.SELLOUT_FETCH_LISTING); @@ -191,7 +165,7 @@ on(`__cfx_nui:${events.OPEN_APP_DAILER}`, (data: any, cb: Function) => { RegisterNuiCallbackType('phone:close'); on(`__cfx_nui:phone:close`, async (data: any, cb: Function) => { - await Phone(); + await hidePhone(); cb(); }); // Called for when the phone is closed via the UI.