Add quick access and improve Apps config

This commit is contained in:
J Francisco Rader committed 2021-02-08 00:44:57 -05:00
1 parent 6ae60b377c
commit c149a1f49e
5 files changed
+115 -26

No files matched your search

+13 -7
View File
@@ -1,8 +1,7 @@
import React, { useMemo } from 'react'; import React, { useMemo } from 'react';
import './Phone.css'; import './Phone.css';
import './i18n'; import './i18n';
import { Route } from 'react-router-dom'; import { Route, useHistory } from 'react-router-dom';
import MessageIcon from '@material-ui/icons/Email';
import { CallModal } from './modal/components/CallModal'; import { CallModal } from './modal/components/CallModal';
import { HomeApp } from './apps/home/components/Home'; import { HomeApp } from './apps/home/components/Home';
import { createMuiTheme, ThemeProvider } from '@material-ui/core'; import { createMuiTheme, ThemeProvider } from '@material-ui/core';
@@ -32,6 +31,7 @@ import { useCallService } from './modal/hooks/useCallService';
import { useModal } from './modal/hooks/useModal'; import { useModal } from './modal/hooks/useModal';
import { useDialService } from './apps/dialer/hooks/useDialService'; import { useDialService } from './apps/dialer/hooks/useDialService';
import InjectDebugData from './os/debug/InjectDebugData'; import InjectDebugData from './os/debug/InjectDebugData';
import { useQuickAccess } from './os/notifications/hooks/useQuickAccess';
InjectDebugData([ InjectDebugData([
{ {
@@ -42,6 +42,8 @@ InjectDebugData([
]); ]);
function Phone() { function Phone() {
const history = useHistory();
const quickAccess = useQuickAccess();
useNuiService(); useNuiService();
usePhoneService(); usePhoneService();
const { visibility } = usePhone(); const { visibility } = usePhone();
@@ -103,10 +105,15 @@ function Phone() {
<> <>
<NotificationBar <NotificationBar
notifications={[ notifications={[
{ ...quickAccess.map((qa) => ({
key: 'newMessage', key: qa.id,
icon: <NotificationIcon Icon={MessageIcon} />, icon: (
}, <NotificationIcon
icon={qa.notificationIcon}
onClick={() => history.push(qa.path)}
/>
),
})),
]} ]}
/> />
<div className='PhoneAppContainer'> <div className='PhoneAppContainer'>
@@ -115,7 +122,6 @@ function Phone() {
) : ( ) : (
<> <>
<Route exact path='/' component={HomeApp} /> <Route exact path='/' component={HomeApp} />
{allApps.map((App) => ( {allApps.map((App) => (
<App.Route key={App.id} /> <App.Route key={App.id} />
))} ))}
@@ -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 { Slide, Paper, Button, Typography } from '@material-ui/core';
import ArrowBackIcon from '@material-ui/icons/ArrowBack'; import ArrowBackIcon from '@material-ui/icons/ArrowBack';
@@ -22,8 +22,12 @@ export const MessageModal = () => {
const activeMessageGroup = useQueryParams<MessageGroup>(); const activeMessageGroup = useQueryParams<MessageGroup>();
const { groupId } = useParams<{ groupId: string }>(); const { groupId } = useParams<{ groupId: string }>();
const { messages, setMessages } = useMessages(); const { messages, setMessages } = useMessages();
const [minimumLoadPassed, setMimimumLoadPassed] = useState(false); const [minimumLoadPassed, setMimimumLoadPassed] = useState(false);
const minLoadTimeoutRef = useRef(null);
const fetchInterval = useRef(null);
const closeModal = () => { const closeModal = () => {
history.push('/messages'); history.push('/messages');
setMessages(null); setMessages(null);
@@ -33,11 +37,11 @@ export const MessageModal = () => {
useEffect(() => { useEffect(() => {
if (!groupId) return; if (!groupId) return;
const timeout = window.setTimeout(() => { minLoadTimeoutRef.current = setTimeout(() => {
setMimimumLoadPassed(true); setMimimumLoadPassed(true);
}, MINIMUM_LOAD_TIME); }, MINIMUM_LOAD_TIME);
const interval = window.setInterval(() => { fetchInterval.current = setInterval(() => {
if (!groupId) return; if (!groupId) return;
Nui.send('phone:fetchMessages', { Nui.send('phone:fetchMessages', {
groupId, groupId,
@@ -45,8 +49,10 @@ export const MessageModal = () => {
}, MESSAGES_REFRESH_RATE); }, MESSAGES_REFRESH_RATE);
return () => { return () => {
window.clearInterval(interval); clearInterval(fetchInterval.current);
window.clearTimeout(timeout); fetchInterval.current = null;
clearTimeout(minLoadTimeoutRef.current);
minLoadTimeoutRef.current = null;
}; };
}, [groupId]); }, [groupId]);
+31 -12
View File
@@ -1,5 +1,4 @@
import React, {useEffect} from 'react'; import React, {useCallback, useEffect, useMemo} from 'react';
import { useConfig } from '../../../config/hooks/useConfig';
import { import {
blue, blue,
common, common,
@@ -23,7 +22,7 @@ import { faTwitter } from '@fortawesome/free-brands-svg-icons';
import ContactsIcon from '@material-ui/icons/Contacts'; import ContactsIcon from '@material-ui/icons/Contacts';
import SettingsIcon from '@material-ui/icons/Settings'; import SettingsIcon from '@material-ui/icons/Settings';
import MessageIcon from '@material-ui/icons/Message'; import MessageIcon from '@material-ui/icons/Message';
import { atom } from 'recoil'; import { atom, useRecoilState } from 'recoil';
import { CalculatorIcon } from '../../../apps/calculator/components/CalculatorIcon'; import { CalculatorIcon } from '../../../apps/calculator/components/CalculatorIcon';
import CameraAltIcon from '@material-ui/icons/CameraAlt'; import CameraAltIcon from '@material-ui/icons/CameraAlt';
import { Route } from 'react-router-dom'; import { Route } from 'react-router-dom';
@@ -40,6 +39,19 @@ import { NotesApp } from '../../../apps/notes/NotesApp';
import CameraApp from '../../../apps/camera/components/CameraApp'; import CameraApp from '../../../apps/camera/components/CameraApp';
import Nui from "../../nui-events/utils/Nui"; 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 }) => { const AppWithStartup = ({ children, id }) => {
useEffect(() => { useEffect(() => {
Nui.send(`phone:app:${id}`); Nui.send(`phone:app:${id}`);
@@ -56,7 +68,7 @@ const AppRoute = ({ id, component: Component, ...rest }) => {
); );
}; };
const appsState = atom({ const appsState = atom<AppsRepository>({
key: 'apps', key: 'apps',
default: { default: {
preinstalled: [ preinstalled: [
@@ -64,6 +76,7 @@ const appsState = atom({
id: 'DIALER', id: 'DIALER',
nameLocale: 'APPS_DIALER', nameLocale: 'APPS_DIALER',
icon: <FontAwesomeIcon icon={faPhoneAlt} size='xs' />, icon: <FontAwesomeIcon icon={faPhoneAlt} size='xs' />,
notificationIcon: <FontAwesomeIcon icon={faPhoneAlt} size='xs' />,
backgroundColor: green[600], backgroundColor: green[600],
color: common.white, color: common.white,
path: '/phone', path: '/phone',
@@ -73,16 +86,17 @@ const appsState = atom({
id: 'MESSAGES', id: 'MESSAGES',
nameLocale: 'APPS_MESSAGES', nameLocale: 'APPS_MESSAGES',
icon: <MessageIcon fontSize='default' />, icon: <MessageIcon fontSize='default' />,
notificationIcon: <MessageIcon fontSize='small' />,
backgroundColor: amber[700], backgroundColor: amber[700],
color: common.white, color: common.white,
path: '/messages', path: '/messages',
sharePath: '/messages/new',
Route: () => <AppRoute id="MESSAGE" path='/messages' component={MessagesApp} />, Route: () => <AppRoute id="MESSAGE" path='/messages' component={MessagesApp} />,
}, },
{ {
id: 'CONTACTS', id: 'CONTACTS',
nameLocale: 'APPS_CONTACTS', nameLocale: 'APPS_CONTACTS',
icon: <ContactsIcon />, icon: <ContactsIcon />,
notificationIcon: <ContactsIcon fontSize="small" />,
backgroundColor: blue[500], backgroundColor: blue[500],
color: common.white, color: common.white,
path: '/contacts', path: '/contacts',
@@ -92,6 +106,7 @@ const appsState = atom({
id: 'CALCULATOR', id: 'CALCULATOR',
nameLocale: 'APPS_CALCULATOR', nameLocale: 'APPS_CALCULATOR',
icon: <CalculatorIcon />, icon: <CalculatorIcon />,
notificationIcon: <CalculatorIcon fontSize="small"/>,
backgroundColor: purple[500], backgroundColor: purple[500],
color: grey[50], color: grey[50],
path: '/calculator', path: '/calculator',
@@ -101,6 +116,7 @@ const appsState = atom({
id: 'SETTINGS', id: 'SETTINGS',
nameLocale: 'APPS_SETTINGS', nameLocale: 'APPS_SETTINGS',
icon: <SettingsIcon />, icon: <SettingsIcon />,
notificationIcon: <SettingsIcon fontSize="small"/>,
backgroundColor: '#383838', backgroundColor: '#383838',
color: grey[50], color: grey[50],
path: '/settings', path: '/settings',
@@ -110,6 +126,7 @@ const appsState = atom({
id: 'BANK', id: 'BANK',
nameLocale: 'APPS_BANK', nameLocale: 'APPS_BANK',
icon: <FontAwesomeIcon icon={faWonSign} size='xs' />, icon: <FontAwesomeIcon icon={faWonSign} size='xs' />,
notificationIcon: <FontAwesomeIcon icon={faWonSign} size='xs' />,
backgroundColor: blue[900], backgroundColor: blue[900],
color: common.white, color: common.white,
path: '/bank', path: '/bank',
@@ -119,6 +136,7 @@ const appsState = atom({
id: 'TWITTER', id: 'TWITTER',
nameLocale: 'APPS_TWITTER', nameLocale: 'APPS_TWITTER',
icon: <FontAwesomeIcon icon={faTwitter} fixedWidth size='xs' />, icon: <FontAwesomeIcon icon={faTwitter} fixedWidth size='xs' />,
notificationIcon: <FontAwesomeIcon icon={faTwitter} fixedWidth size='xs' />,
backgroundColor: blue[600], backgroundColor: blue[600],
color: common.white, color: common.white,
path: '/twitter', path: '/twitter',
@@ -128,6 +146,7 @@ const appsState = atom({
id: 'SELLOUT', id: 'SELLOUT',
nameLocale: 'APPS_SELLOUT', nameLocale: 'APPS_SELLOUT',
icon: <FontAwesomeIcon icon={faAd} fixedWidth />, icon: <FontAwesomeIcon icon={faAd} fixedWidth />,
notificationIcon: <FontAwesomeIcon icon={faAd} fixedWidth size="xs" />,
backgroundColor: red[500], backgroundColor: red[500],
color: common.white, color: common.white,
path: '/sellout', path: '/sellout',
@@ -137,6 +156,7 @@ const appsState = atom({
id: 'NOTES', id: 'NOTES',
nameLocale: 'APPS_NOTES', nameLocale: 'APPS_NOTES',
icon: <FontAwesomeIcon icon={faStickyNote} fixedWidth />, icon: <FontAwesomeIcon icon={faStickyNote} fixedWidth />,
notificationIcon: <FontAwesomeIcon icon={faStickyNote} fixedWidth size="xs" />,
backgroundColor: yellow[800], backgroundColor: yellow[800],
color: common.white, color: common.white,
path: '/notes', path: '/notes',
@@ -146,6 +166,7 @@ const appsState = atom({
id: 'CAMERA', id: 'CAMERA',
nameLocale: 'APPS_CAMERA', nameLocale: 'APPS_CAMERA',
icon: <CameraAltIcon fontSize='large' />, icon: <CameraAltIcon fontSize='large' />,
notificationIcon: <CameraAltIcon fontSize='small' />,
backgroundColor: grey['A400'], backgroundColor: grey['A400'],
color: common.white, color: common.white,
path: '/camera', path: '/camera',
@@ -155,6 +176,7 @@ const appsState = atom({
id: 'EXAMPLE', id: 'EXAMPLE',
nameLocale: 'APPS_EXAMPLE', nameLocale: 'APPS_EXAMPLE',
icon: <FontAwesomeIcon icon={faPlaneArrival} size='sm' />, icon: <FontAwesomeIcon icon={faPlaneArrival} size='sm' />,
notificationIcon: <FontAwesomeIcon icon={faPlaneArrival} size='xs' />,
backgroundColor: blue[500], backgroundColor: blue[500],
color: blue[50], color: blue[50],
path: '/example', path: '/example',
@@ -165,16 +187,13 @@ const appsState = atom({
}); });
export const useApps = () => { export const useApps = () => {
const [apps, setApps] = useConfig(appsState); const [apps, setApps] = useRecoilState<AppsRepository>(appsState);
const allApps = [...apps.preinstalled]; const allApps = useMemo(() => [...apps.preinstalled], [apps.preinstalled]);
const getApp = useCallback((id: string): IAppConfig => allApps.find((a) => a.id === id) || null, [allApps]);
const getApp = (id) => allApps.find((a) => a.id === id) || null;
return { apps, allApps, setApps, getApp }; return { apps, allApps, setApps, getApp };
}; };
export const useApp = (id) => { export const useApp = (id: string): IAppConfig => {
const { getApp } = useApps(); const { getApp } = useApps();
return getApp(id); return getApp(id);
}; };
@@ -1,5 +1,15 @@
import React from 'react'; import React from 'react';
import { IconButton } from '@material-ui/core';
export const NotificationIcon = ({ Icon }) => { interface INotificationIcon {
return <Icon fontSize='small' />; icon: JSX.Element;
onClick(e: any): void;
}
export const NotificationIcon = ({ icon, onClick }: INotificationIcon) => {
return (
<IconButton size="small" onClick={onClick}>
{icon}
</IconButton>
);
}; };
@@ -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<IAppConfig[]>(
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;
};