diff --git a/phone/src/Phone.tsx b/phone/src/Phone.tsx index 14d54b9..f01f2c5 100644 --- a/phone/src/Phone.tsx +++ b/phone/src/Phone.tsx @@ -1,8 +1,7 @@ import React, { useMemo } from 'react'; import './Phone.css'; import './i18n'; -import { Route } from 'react-router-dom'; -import MessageIcon from '@material-ui/icons/Email'; +import { Route, useHistory } from 'react-router-dom'; import { CallModal } from './modal/components/CallModal'; import { HomeApp } from './apps/home/components/Home'; import { createMuiTheme, ThemeProvider } from '@material-ui/core'; @@ -32,6 +31,7 @@ import { useCallService } from './modal/hooks/useCallService'; import { useModal } from './modal/hooks/useModal'; import { useDialService } from './apps/dialer/hooks/useDialService'; import InjectDebugData from './os/debug/InjectDebugData'; +import { useQuickAccess } from './os/notifications/hooks/useQuickAccess'; InjectDebugData([ { @@ -42,6 +42,8 @@ InjectDebugData([ ]); function Phone() { + const history = useHistory(); + const quickAccess = useQuickAccess(); useNuiService(); usePhoneService(); const { visibility } = usePhone(); @@ -103,10 +105,15 @@ function Phone() { <> , - }, + ...quickAccess.map((qa) => ({ + key: qa.id, + icon: ( + history.push(qa.path)} + /> + ), + })), ]} />
@@ -115,7 +122,6 @@ function Phone() { ) : ( <> - {allApps.map((App) => ( ))} diff --git a/phone/src/apps/messages/components/modal/MessageModal.tsx b/phone/src/apps/messages/components/modal/MessageModal.tsx index d979b01..09e7960 100644 --- a/phone/src/apps/messages/components/modal/MessageModal.tsx +++ b/phone/src/apps/messages/components/modal/MessageModal.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useRef, useState } from 'react'; import { Slide, Paper, Button, Typography } from '@material-ui/core'; import ArrowBackIcon from '@material-ui/icons/ArrowBack'; @@ -22,8 +22,12 @@ export const MessageModal = () => { const activeMessageGroup = useQueryParams(); const { groupId } = useParams<{ groupId: string }>(); const { messages, setMessages } = useMessages(); + const [minimumLoadPassed, setMimimumLoadPassed] = useState(false); + const minLoadTimeoutRef = useRef(null); + const fetchInterval = useRef(null); + const closeModal = () => { history.push('/messages'); setMessages(null); @@ -33,11 +37,11 @@ export const MessageModal = () => { useEffect(() => { if (!groupId) return; - const timeout = window.setTimeout(() => { + minLoadTimeoutRef.current = setTimeout(() => { setMimimumLoadPassed(true); }, MINIMUM_LOAD_TIME); - const interval = window.setInterval(() => { + fetchInterval.current = setInterval(() => { if (!groupId) return; Nui.send('phone:fetchMessages', { groupId, @@ -45,8 +49,10 @@ export const MessageModal = () => { }, MESSAGES_REFRESH_RATE); return () => { - window.clearInterval(interval); - window.clearTimeout(timeout); + clearInterval(fetchInterval.current); + fetchInterval.current = null; + clearTimeout(minLoadTimeoutRef.current); + minLoadTimeoutRef.current = null; }; }, [groupId]); diff --git a/phone/src/os/apps/hooks/useApps.tsx b/phone/src/os/apps/hooks/useApps.tsx index 43d702d..28d319d 100644 --- a/phone/src/os/apps/hooks/useApps.tsx +++ b/phone/src/os/apps/hooks/useApps.tsx @@ -1,5 +1,4 @@ -import React, {useEffect} from 'react'; -import { useConfig } from '../../../config/hooks/useConfig'; +import React, {useCallback, useEffect, useMemo} from 'react'; import { blue, common, @@ -23,7 +22,7 @@ import { faTwitter } from '@fortawesome/free-brands-svg-icons'; import ContactsIcon from '@material-ui/icons/Contacts'; import SettingsIcon from '@material-ui/icons/Settings'; import MessageIcon from '@material-ui/icons/Message'; -import { atom } from 'recoil'; +import { atom, useRecoilState } from 'recoil'; import { CalculatorIcon } from '../../../apps/calculator/components/CalculatorIcon'; import CameraAltIcon from '@material-ui/icons/CameraAlt'; import { Route } from 'react-router-dom'; @@ -40,6 +39,19 @@ import { NotesApp } from '../../../apps/notes/NotesApp'; import CameraApp from '../../../apps/camera/components/CameraApp'; import Nui from "../../nui-events/utils/Nui"; +export interface IAppConfig { + id: string; + nameLocale: string; + icon: JSX.Element; + notificationIcon: JSX.Element; + backgroundColor: string; + color: string; + path: string; + Route: React.FC; +} + +type AppsRepository = { preinstalled: IAppConfig[] }; + const AppWithStartup = ({ children, id }) => { useEffect(() => { Nui.send(`phone:app:${id}`); @@ -56,7 +68,7 @@ const AppRoute = ({ id, component: Component, ...rest }) => { ); }; -const appsState = atom({ +const appsState = atom({ key: 'apps', default: { preinstalled: [ @@ -64,6 +76,7 @@ const appsState = atom({ id: 'DIALER', nameLocale: 'APPS_DIALER', icon: , + notificationIcon: , backgroundColor: green[600], color: common.white, path: '/phone', @@ -73,16 +86,17 @@ const appsState = atom({ id: 'MESSAGES', nameLocale: 'APPS_MESSAGES', icon: , + notificationIcon: , backgroundColor: amber[700], color: common.white, path: '/messages', - sharePath: '/messages/new', Route: () => , }, { id: 'CONTACTS', nameLocale: 'APPS_CONTACTS', icon: , + notificationIcon: , backgroundColor: blue[500], color: common.white, path: '/contacts', @@ -92,6 +106,7 @@ const appsState = atom({ id: 'CALCULATOR', nameLocale: 'APPS_CALCULATOR', icon: , + notificationIcon: , backgroundColor: purple[500], color: grey[50], path: '/calculator', @@ -101,6 +116,7 @@ const appsState = atom({ id: 'SETTINGS', nameLocale: 'APPS_SETTINGS', icon: , + notificationIcon: , backgroundColor: '#383838', color: grey[50], path: '/settings', @@ -110,6 +126,7 @@ const appsState = atom({ id: 'BANK', nameLocale: 'APPS_BANK', icon: , + notificationIcon: , backgroundColor: blue[900], color: common.white, path: '/bank', @@ -119,6 +136,7 @@ const appsState = atom({ id: 'TWITTER', nameLocale: 'APPS_TWITTER', icon: , + notificationIcon: , backgroundColor: blue[600], color: common.white, path: '/twitter', @@ -128,6 +146,7 @@ const appsState = atom({ id: 'SELLOUT', nameLocale: 'APPS_SELLOUT', icon: , + notificationIcon: , backgroundColor: red[500], color: common.white, path: '/sellout', @@ -137,6 +156,7 @@ const appsState = atom({ id: 'NOTES', nameLocale: 'APPS_NOTES', icon: , + notificationIcon: , backgroundColor: yellow[800], color: common.white, path: '/notes', @@ -146,6 +166,7 @@ const appsState = atom({ id: 'CAMERA', nameLocale: 'APPS_CAMERA', icon: , + notificationIcon: , backgroundColor: grey['A400'], color: common.white, path: '/camera', @@ -155,6 +176,7 @@ const appsState = atom({ id: 'EXAMPLE', nameLocale: 'APPS_EXAMPLE', icon: , + notificationIcon: , backgroundColor: blue[500], color: blue[50], path: '/example', @@ -165,16 +187,13 @@ const appsState = atom({ }); export const useApps = () => { - const [apps, setApps] = useConfig(appsState); - const allApps = [...apps.preinstalled]; - - const getApp = (id) => allApps.find((a) => a.id === id) || null; - + const [apps, setApps] = useRecoilState(appsState); + const allApps = useMemo(() => [...apps.preinstalled], [apps.preinstalled]); + const getApp = useCallback((id: string): IAppConfig => allApps.find((a) => a.id === id) || null, [allApps]); return { apps, allApps, setApps, getApp }; }; -export const useApp = (id) => { +export const useApp = (id: string): IAppConfig => { const { getApp } = useApps(); - return getApp(id); }; diff --git a/phone/src/os/notifications/components/NotificationIcon.tsx b/phone/src/os/notifications/components/NotificationIcon.tsx index d427e4a..6aed85d 100644 --- a/phone/src/os/notifications/components/NotificationIcon.tsx +++ b/phone/src/os/notifications/components/NotificationIcon.tsx @@ -1,5 +1,15 @@ import React from 'react'; +import { IconButton } from '@material-ui/core'; -export const NotificationIcon = ({ Icon }) => { - return ; +interface INotificationIcon { + icon: JSX.Element; + onClick(e: any): void; +} + +export const NotificationIcon = ({ icon, onClick }: INotificationIcon) => { + return ( + + {icon} + + ); }; diff --git a/phone/src/os/notifications/hooks/useQuickAccess.tsx b/phone/src/os/notifications/hooks/useQuickAccess.tsx new file mode 100644 index 0000000..c4eab81 --- /dev/null +++ b/phone/src/os/notifications/hooks/useQuickAccess.tsx @@ -0,0 +1,48 @@ +import { useCallback, useEffect } from 'react'; +import { matchPath, useHistory } from 'react-router-dom'; +import { atom, useRecoilState } from 'recoil'; +import { IAppConfig, useApps } from '../../apps/hooks/useApps'; + +const state = { + recentlyUsed: atom({ + key: 'recentlyUsed', + default: [], + }), +}; + +const MAX_QUICK_ACCESS_ITEMS = 5; + +const unshiftApp = (arr: IAppConfig[], app) => { + const idx = arr.findIndex((a) => a.id === app.id); + if (idx !== -1) { + arr.splice(idx, 1); + } + arr.unshift(app); + return arr; +}; + +export const useQuickAccess = () => { + const history = useHistory(); + const { allApps, getApp } = useApps(); + const [recentlyUsed, setRecentlyUsed] = useRecoilState( + state.recentlyUsed + ); + + useEffect(() => { + setRecentlyUsed([getApp("SETTINGS")]) + }, [getApp, setRecentlyUsed]) + + const listener = useCallback((location) => { + const newState = [...recentlyUsed]; + allApps.forEach((app) => { + if (matchPath(location.pathname, { path: app.path, exact: false })) { + unshiftApp(newState, app); + } + }); + setRecentlyUsed(newState.slice(0, MAX_QUICK_ACCESS_ITEMS)); + }, [allApps, recentlyUsed, setRecentlyUsed]); + + history.listen(listener); + + return recentlyUsed; +};