Feature / Add working notifications with only twitter for now (#102)

* Add working notifications with only twitter for now

* feat(notification-bar): I didnt copy from NP

* Make default browser visibility true

* feat(notifications): Collapse when no more notifications after closing one
This commit is contained in:
J Francisco Rader authored and GitHub committed 2021-02-18 19:21:07 -05:00
1 parent 11af8bfb5a
commit 40ec19a349
19 files changed
+448 -104

No files matched your search

+2 -9
View File
@@ -25,8 +25,6 @@
overflow: hidden; overflow: hidden;
position: absolute; position: absolute;
background-color: black; background-color: black;
/* border-left: 4px solid black;
border-right: 4px solid black; */
bottom: 100px; bottom: 100px;
left: 50px; left: 50px;
right: 50px; right: 50px;
@@ -51,17 +49,12 @@
} }
*::-webkit-scrollbar-track { *::-webkit-scrollbar-track {
box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3); box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
background-color: #f5f5f5; background-color: #666;
} }
*::-webkit-scrollbar { *::-webkit-scrollbar {
width: 3px; width: 3px;
background-color: transparent; background-color: transparent;
} }
*::-webkit-scrollbar-thumb { *::-webkit-scrollbar-thumb {
background-color: #666; background-color: #f5f5f5;
}
.zoomProp {
transform: scale(10);
} }
+26 -32
View File
@@ -7,7 +7,6 @@ import { Alert } from './ui/components/Alert';
import { HomeApp } from './apps/home/components/Home'; import { HomeApp } from './apps/home/components/Home';
import { createMuiTheme, ThemeProvider, Slide } from '@material-ui/core'; import { createMuiTheme, ThemeProvider, Slide } from '@material-ui/core';
import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard'; import { useInitKeyboard } from './os/keyboard/hooks/useKeyboard';
import { NotificationIcon } from './os/notifications/components/NotificationIcon';
import { NotificationBar } from './os/notifications/components/NotificationBar'; import { NotificationBar } from './os/notifications/components/NotificationBar';
import { Navigation } from './os/navigation-bar/components/Navigation'; import { Navigation } from './os/navigation-bar/components/Navigation';
import { useNuiService } from './os/nui-events/hooks/useNuiService'; import { useNuiService } from './os/nui-events/hooks/useNuiService';
@@ -23,25 +22,34 @@ import { useMessagesService } from './apps/messages/hooks/useMessageService';
import { useNotesService } from './apps/notes/hooks/useNotesService'; import { useNotesService } from './apps/notes/hooks/useNotesService';
import { usePhotoService } from './apps/camera/hooks/usePhotoService'; import { usePhotoService } from './apps/camera/hooks/usePhotoService';
import Nui from './os/nui-events/utils/Nui'; import Nui from './os/nui-events/utils/Nui';
import { usePhone } from './os/phone/hooks/usePhone'; import { useSettings } from './apps/settings/hooks/useSettings';
import { settingsState } from './apps/settings/hooks/useSettings';
import config from './config/default.json'; import config from './config/default.json';
import { useRecoilState } from 'recoil';
import { useCallService } from './modal/hooks/useCallService'; 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';
import { useSnackbar } from './ui/hooks/useSnackbar'; import { useSnackbar } from './ui/hooks/useSnackbar';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { usePhoneVisibility } from './os/phone/hooks/usePhoneVisibility';
function Phone() { function Phone() {
const quickAccess = useQuickAccess(); const { t } = useTranslation();
const { alert } = useSnackbar();
const { modal } = useModal();
const { allApps } = useApps();
const [settings] = useSettings();
const {
bottom,
visibility,
uncollapseNotifications,
clickEventOverride,
} = usePhoneVisibility();
useNuiService(); useNuiService();
usePhoneService(); usePhoneService();
const { visibility } = usePhone();
const { allApps } = useApps();
useSimcardService(); useSimcardService();
useContactsService(); useContactsService();
useTwitterService(); useTwitterService();
@@ -53,20 +61,12 @@ function Phone() {
usePhotoService(); usePhotoService();
useCallService(); useCallService();
useDialService(); useDialService();
const { t } = useTranslation()
const { alert } = useSnackbar();
const { modal } = useModal(); // the calling modal
const [settings] = useRecoilState(settingsState);
const currentTheme = useMemo( const currentTheme = useMemo(
() => createMuiTheme(config.themes[settings.theme]), () => createMuiTheme(config.themes[settings.theme]),
[settings.theme] [settings.theme]
); );
document.onkeyup = function (data) { document.onkeyup = function (data) {
if (data.which === 27) { if (data.which === 27) {
Nui.send('phone:close'); Nui.send('phone:close');
@@ -79,10 +79,12 @@ function Phone() {
<div className='PhoneWrapper'> <div className='PhoneWrapper'>
<div> <div>
<div <div
onClick={clickEventOverride}
className='Phone' className='Phone'
style={{ style={{
transformOrigin: 'right bottom', transformOrigin: 'right bottom',
transform: `scale(${settings.zoom}`, transform: `scale(${settings.zoom}`,
bottom,
}} }}
> >
<div <div
@@ -99,17 +101,7 @@ function Phone() {
}} }}
> >
<> <>
<NotificationBar <NotificationBar forceUncollapse={uncollapseNotifications} />
notifications={quickAccess.map((qa) => ({
key: qa.id,
icon: (
<NotificationIcon
icon={qa.notificationIcon}
to={qa.path}
/>
),
}))}
/>
<div className='PhoneAppContainer'> <div className='PhoneAppContainer'>
{modal ? ( {modal ? (
<CallModal /> <CallModal />
@@ -122,14 +114,16 @@ function Phone() {
</> </>
)} )}
{alert ? ( {alert ? (
<div style={{ <div
style={{
marginTop: '-100px', marginTop: '-100px',
display: 'flex', display: 'flex',
justifyContent: 'center', justifyContent: 'center',
alignItems: 'center' alignItems: 'center',
}}> }}
<Alert severity={alert.type} variant="filled"> >
{t("APPS_"+alert.message)} <Alert severity={alert.type} variant='filled'>
{t('APPS_' + alert.message)}
</Alert> </Alert>
</div> </div>
) : null} ) : null}
+2 -2
View File
@@ -5,11 +5,11 @@ import { GridMenu } from '../../../ui/components/GridMenu';
import { useApps } from '../../../os/apps/hooks/useApps'; import { useApps } from '../../../os/apps/hooks/useApps';
export const HomeApp = () => { export const HomeApp = () => {
const { apps } = useApps(); const { allApps } = useApps();
return ( return (
<AppWrapper> <AppWrapper>
<Box width='100%' mt={6} px={1}> <Box width='100%' mt={6} px={1}>
{apps && <GridMenu items={apps.preinstalled} />} {allApps && <GridMenu items={allApps} />}
</Box> </Box>
</AppWrapper> </AppWrapper>
); );
+10 -2
View File
@@ -22,7 +22,15 @@ export const settingsState = atom({
effects_UNSTABLE: [localStorageEffect('settings')], effects_UNSTABLE: [localStorageEffect('settings')],
}); });
interface IPhoneSettings {
wallpaper: string;
frame: string;
theme: string;
zoom: number;
ringtone: string;
ringtoneVol: number;
}
export const useSettings = () => { export const useSettings = () => {
const [settings, setSettings] = useRecoilState(settingsState); return useRecoilState<IPhoneSettings>(settingsState);
return [settings, setSettings];
}; };
@@ -33,6 +33,30 @@ InjectDebugData([
avatar_url: 'https://i.tasoagc.dev/i9Ig', 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', app: 'TWITTER',
method: 'fetchTweets', method: 'fetchTweets',
@@ -5,6 +5,10 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { IMAGE_DELIMITER } from '../utils/images'; import { IMAGE_DELIMITER } from '../utils/images';
import { APP_TWITTER } from '../utils/constants'; import { APP_TWITTER } from '../utils/constants';
import { twitterState } from './state'; 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';
/** /**
* Perform all necessary processing/transforms from the raw database * Perform all necessary processing/transforms from the raw database
@@ -28,6 +32,23 @@ function processTweet(tweet) {
* there and have moved that logic here instead. * there and have moved that logic here instead.
*/ */
export const useTwitterService = () => { 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 setProfile = useSetRecoilState(twitterState.profile); const setProfile = useSetRecoilState(twitterState.profile);
const setUpdateProfileLoading = useSetRecoilState( const setUpdateProfileLoading = useSetRecoilState(
twitterState.updateProfileLoading twitterState.updateProfileLoading
@@ -41,7 +62,6 @@ export const useTwitterService = () => {
const setCreateSuccess = useSetRecoilState( const setCreateSuccess = useSetRecoilState(
twitterState.createTweetSuccessful twitterState.createTweetSuccessful
); );
const setNotification = useSetRecoilState(twitterState.notification);
const _setUpdateProfileSuccess = (isSuccessful) => { const _setUpdateProfileSuccess = (isSuccessful) => {
setUpdateProfileSuccess(isSuccessful); setUpdateProfileSuccess(isSuccessful);
+9 -1
View File
@@ -14,7 +14,15 @@
"printDebugLogs": true, "printDebugLogs": true,
"logLevel": 1 "logLevel": 1
}, },
"frames": ["default.png", "blue.png", "gold.png", "kawaii.png", "minimal.png", "pink.png", "white.png"], "frames": [
"default.png",
"blue.png",
"gold.png",
"kawaii.png",
"minimal.png",
"pink.png",
"white.png"
],
"zoomOptions": [1, 0.9, 0.8], "zoomOptions": [1, 0.9, 0.8],
"ringtones": ["marimba", "pixel"], "ringtones": ["marimba", "pixel"],
"themes": { "themes": {
+4 -1
View File
@@ -10,7 +10,8 @@ import Phone from './Phone';
import TwitterNotification from './apps/twitter/components/notification/TwitterNotification'; import TwitterNotification from './apps/twitter/components/notification/TwitterNotification';
import { BankNotification } from './apps/bank/components/notification/BankNotification'; import { BankNotification } from './apps/bank/components/notification/BankNotification';
import PhoneConfig from './config/default.json'; import PhoneConfig from './config/default.json';
import SnackbarProvider from './ui/components/SnackbarProvider'; import SnackbarProvider from './ui/providers/SnackbarProvider';
import { NotificationsProvider } from './os/notifications/providers/NotificationsProvider';
// Enable Sentry when config setting is true // Enable Sentry when config setting is true
if (PhoneConfig.SentryErrorMetrics) { if (PhoneConfig.SentryErrorMetrics) {
@@ -31,7 +32,9 @@ ReactDOM.render(
<TwitterNotification /> <TwitterNotification />
<BankNotification /> <BankNotification />
<SnackbarProvider> <SnackbarProvider>
<NotificationsProvider>
<Phone /> <Phone />
</NotificationsProvider>
</SnackbarProvider> </SnackbarProvider>
</RecoilRoot> </RecoilRoot>
</HashRouter>, </HashRouter>,
+1
View File
@@ -47,6 +47,7 @@
"APPS_TWITTER_TIME_MINUTES": "m", "APPS_TWITTER_TIME_MINUTES": "m",
"APPS_TWITTER_TIME_HOURS": "h", "APPS_TWITTER_TIME_HOURS": "h",
"APPS_TWITTER_TIME_DAYS": "d", "APPS_TWITTER_TIME_DAYS": "d",
"APPS_TWITTER_NEW_BROADCAST": "New tweet from @{{ profile_name }}!",
"APPS_BANK_HOME_TITLE": "Welcome", "APPS_BANK_HOME_TITLE": "Welcome",
"APPS_BANK_ACCOUNT_LINK": "Account", "APPS_BANK_ACCOUNT_LINK": "Account",
+63 -21
View File
@@ -37,7 +37,8 @@ import { ExampleApp } from '../../../apps/example/components/ExampleApp';
import { SelloutApp } from '../../../apps/sellout/components/SelloutApp'; import { SelloutApp } from '../../../apps/sellout/components/SelloutApp';
import { NotesApp } from '../../../apps/notes/NotesApp'; 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';
import { useNotifications } from '../../notifications/hooks/useNotifications';
export interface IAppConfig { export interface IAppConfig {
id: string; id: string;
@@ -63,7 +64,11 @@ const AppRoute = ({ id, component: Component, ...rest }) => {
return ( return (
<Route <Route
{...rest} {...rest}
render={() => <AppWithStartup id={id}><Component /></AppWithStartup>} render={() => (
<AppWithStartup id={id}>
<Component />
</AppWithStartup>
)}
/> />
); );
}; };
@@ -80,7 +85,9 @@ const appsState = atom<AppsRepository>({
backgroundColor: green[600], backgroundColor: green[600],
color: common.white, color: common.white,
path: '/phone', path: '/phone',
Route: () => <AppRoute id="DIALER" path='/phone' component={DialerApp} />, Route: () => (
<AppRoute id='DIALER' path='/phone' component={DialerApp} />
),
}, },
{ {
id: 'MESSAGES', id: 'MESSAGES',
@@ -90,37 +97,49 @@ const appsState = atom<AppsRepository>({
backgroundColor: amber[700], backgroundColor: amber[700],
color: common.white, color: common.white,
path: '/messages', path: '/messages',
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" />, notificationIcon: <ContactsIcon fontSize='small' />,
backgroundColor: blue[500], backgroundColor: blue[500],
color: common.white, color: common.white,
path: '/contacts', path: '/contacts',
Route: () => <AppRoute id="CONTACTS" path='/contacts' component={ContactsApp} />, Route: () => (
<AppRoute id='CONTACTS' path='/contacts' component={ContactsApp} />
),
}, },
{ {
id: 'CALCULATOR', id: 'CALCULATOR',
nameLocale: 'APPS_CALCULATOR', nameLocale: 'APPS_CALCULATOR',
icon: <CalculatorIcon />, icon: <CalculatorIcon />,
notificationIcon: <CalculatorIcon fontSize="small"/>, notificationIcon: <CalculatorIcon fontSize='small' />,
backgroundColor: purple[500], backgroundColor: purple[500],
color: grey[50], color: grey[50],
path: '/calculator', path: '/calculator',
Route: () => <AppRoute id="CALCULATOR" path='/calculator' component={CalculatorApp} />, Route: () => (
<AppRoute
id='CALCULATOR'
path='/calculator'
component={CalculatorApp}
/>
),
}, },
{ {
id: 'SETTINGS', id: 'SETTINGS',
nameLocale: 'APPS_SETTINGS', nameLocale: 'APPS_SETTINGS',
icon: <SettingsIcon />, icon: <SettingsIcon />,
notificationIcon: <SettingsIcon fontSize="small"/>, notificationIcon: <SettingsIcon fontSize='small' />,
backgroundColor: '#383838', backgroundColor: '#383838',
color: grey[50], color: grey[50],
path: '/settings', path: '/settings',
Route: () => <AppRoute id="SETTINGS" path='/settings' component={SettingsApp} />, Route: () => (
<AppRoute id='SETTINGS' path='/settings' component={SettingsApp} />
),
}, },
{ {
id: 'BANK', id: 'BANK',
@@ -130,37 +149,45 @@ const appsState = atom<AppsRepository>({
backgroundColor: blue[900], backgroundColor: blue[900],
color: common.white, color: common.white,
path: '/bank', path: '/bank',
Route: () => <AppRoute id="BANK" path='/bank' component={BankApp} />, Route: () => <AppRoute id='BANK' path='/bank' component={BankApp} />,
}, },
{ {
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' />, notificationIcon: (
<FontAwesomeIcon icon={faTwitter} fixedWidth size='xs' />
),
backgroundColor: blue[600], backgroundColor: blue[600],
color: common.white, color: common.white,
path: '/twitter', path: '/twitter',
Route: () => <AppRoute id="TWITTER" path='/twitter' component={TwitterApp} />, Route: () => (
<AppRoute id='TWITTER' path='/twitter' component={TwitterApp} />
),
}, },
{ {
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" />, notificationIcon: <FontAwesomeIcon icon={faAd} fixedWidth size='xs' />,
backgroundColor: red[500], backgroundColor: red[500],
color: common.white, color: common.white,
path: '/sellout', path: '/sellout',
Route: () => <AppRoute id="SELLOUT" path='/sellout' component={SelloutApp} />, Route: () => (
<AppRoute id='SELLOUT' path='/sellout' component={SelloutApp} />
),
}, },
{ {
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" />, notificationIcon: (
<FontAwesomeIcon icon={faStickyNote} fixedWidth size='xs' />
),
backgroundColor: yellow[800], backgroundColor: yellow[800],
color: common.white, color: common.white,
path: '/notes', path: '/notes',
Route: () => <AppRoute id="NOTES" path='/notes' component={NotesApp} />, Route: () => <AppRoute id='NOTES' path='/notes' component={NotesApp} />,
}, },
{ {
id: 'CAMERA', id: 'CAMERA',
@@ -170,7 +197,9 @@ const appsState = atom<AppsRepository>({
backgroundColor: grey['A400'], backgroundColor: grey['A400'],
color: common.white, color: common.white,
path: '/camera', path: '/camera',
Route: () => <AppRoute id="CAMERA" path='/camera' component={CameraApp} />, Route: () => (
<AppRoute id='CAMERA' path='/camera' component={CameraApp} />
),
}, },
{ {
id: 'EXAMPLE', id: 'EXAMPLE',
@@ -180,16 +209,29 @@ const appsState = atom<AppsRepository>({
backgroundColor: blue[500], backgroundColor: blue[500],
color: blue[50], color: blue[50],
path: '/example', path: '/example',
Route: () => <AppRoute id="EXAMPLE" path='/example' component={ExampleApp}/>, Route: () => (
<AppRoute id='EXAMPLE' path='/example' component={ExampleApp} />
),
}, },
], ],
}, },
}); });
export const useApps = () => { export const useApps = () => {
const { icons } = useNotifications();
const [apps, setApps] = useRecoilState<AppsRepository>(appsState); const [apps, setApps] = useRecoilState<AppsRepository>(appsState);
const allApps = useMemo(() => [...apps.preinstalled], [apps.preinstalled]); const allApps = useMemo(
const getApp = useCallback((id: string): IAppConfig => allApps.find((a) => a.id === id) || null, [allApps]); () =>
[...apps.preinstalled].map((a) => ({
...a,
notification: icons.find((i) => i.key === a.id),
})),
[apps.preinstalled, icons]
);
const getApp = useCallback(
(id: string): IAppConfig => allApps.find((a) => a.id === id) || null,
[allApps]
);
return { apps, allApps, setApps, getApp }; return { apps, allApps, setApps, getApp };
}; };
@@ -1,8 +1,25 @@
import React from 'react'; import React, { useState } from 'react';
import { makeStyles, Typography, Grid, IconButton } from '@material-ui/core'; import {
makeStyles,
Typography,
Grid,
IconButton,
Slide,
Paper,
Box,
List,
ListItem,
ListItemText,
Divider,
ListItemAvatar,
} from '@material-ui/core';
import SignalIcon from '@material-ui/icons/SignalCellular3Bar'; import SignalIcon from '@material-ui/icons/SignalCellular3Bar';
import CloseIcon from '@material-ui/icons/Close';
import Battery90Icon from '@material-ui/icons/Battery90'; import Battery90Icon from '@material-ui/icons/Battery90';
import ArrowDropUpIcon from '@material-ui/icons/ArrowDropUp';
import Default from '../../../config/default.json'; import Default from '../../../config/default.json';
import { useNotifications } from '../hooks/useNotifications';
import { useHistory } from 'react-router-dom';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
@@ -10,9 +27,13 @@ const useStyles = makeStyles((theme) => ({
height: '30px', height: '30px',
width: '100%', width: '100%',
color: theme.palette.text.primary, color: theme.palette.text.primary,
zIndex: 2, zIndex: 10,
paddingLeft: '15px', paddingLeft: '15px',
paddingRight: '10px', paddingRight: '10px',
position: 'relative',
'&:hover': {
cursor: 'pointer',
},
}, },
item: { item: {
margin: '0 6px', margin: '0 6px',
@@ -26,39 +47,64 @@ const useStyles = makeStyles((theme) => ({
padding: '4px', padding: '4px',
color: theme.palette.text.primary, color: theme.palette.text.primary,
}, },
drawer: {
backgroundColor: theme.palette.background.default,
width: '100%',
position: 'absolute',
top: '30px',
zIndex: 9,
},
closeNotifBtn: {
position: 'absolute',
right: '8px',
top: '8px',
},
notificationItem: {
position: 'relative',
},
collapseBtn: {
margin: '0 auto',
},
})); }));
interface INotification { export const NotificationBar = ({ forceUncollapse }: { forceUncollapse: boolean }) => {
key: string;
icon: JSX.Element;
}
interface IProps {
notifications?: INotification[];
}
export const NotificationBar = ({ notifications = [] }: IProps) => {
const classes = useStyles(); const classes = useStyles();
const history = useHistory();
const { icons, notifications, removeNotification } = useNotifications();
const [uncollapsed, setUncollapsed] = useState<boolean>(false);
return ( return (
<>
<Grid <Grid
className={classes.root} className={classes.root}
container container
justify='space-between' justify='space-between'
wrap='nowrap' wrap='nowrap'
onClick={() => {
setUncollapsed(!uncollapsed);
}}
> >
<Grid container item wrap='nowrap'> <Grid container item wrap='nowrap'>
{notifications.map((notification) => ( {icons.map((notifIcon) => (
<Grid <Grid
item item
key={notification.key} key={notifIcon.key}
component={IconButton} component={IconButton}
className={classes.icon} className={classes.icon}
> >
{notification.icon} {notifIcon.icon}
</Grid> </Grid>
))} ))}
</Grid> </Grid>
<Grid container item wrap='nowrap' justify='flex-end' alignItems='center'> <Grid
container
item
wrap='nowrap'
justify='flex-end'
alignItems='center'
>
<Grid item> <Grid item>
<SignalIcon fontSize='small' /> <SignalIcon fontSize='small' />
</Grid> </Grid>
@@ -74,5 +120,55 @@ export const NotificationBar = ({ notifications = [] }: IProps) => {
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>
<Slide direction='down' in={forceUncollapse || uncollapsed}>
<Paper square className={classes.drawer}>
<Box p={2}>
<List>
<Divider />
{notifications.map(({ href, title, content, icon }, idx) => (
<ListItem
divider
button
onClick={() => {
if (href) {
history.push(href);
setUncollapsed(false);
removeNotification(idx);
}
}}
className={classes.notificationItem}
key={idx}
>
{icon && <ListItemAvatar>{icon}</ListItemAvatar>}
<ListItemText secondary={content}>{title}</ListItemText>
<IconButton
className={classes.closeNotifBtn}
size='small'
onClick={(e) => {
e.stopPropagation();
if (notifications.length === 1) {
setUncollapsed(false);
}
removeNotification(idx);
}}
>
<CloseIcon color='primary' />
</IconButton>
</ListItem>
))}
</List>
</Box>
<Box display='flex'>
<IconButton
className={classes.collapseBtn}
size='small'
onClick={() => setUncollapsed(false)}
>
<ArrowDropUpIcon />
</IconButton>
</Box>
</Paper>
</Slide>
</>
); );
}; };
@@ -0,0 +1,12 @@
import { useContext } from 'react';
import { NotificationsContext } from '../providers/NotificationsProvider';
export const useNotifications = () => {
const context = useContext(NotificationsContext);
if (!context) {
throw new Error(
'useNotifications must be wrapped by NotificationsProvider'
);
}
return context;
};
@@ -0,0 +1,81 @@
import React, { createContext, useState, useCallback, useMemo } from 'react';
export interface INotification {
app: string;
title: string;
content?: React.ReactNode;
icon?: JSX.Element;
notificationIcon?: JSX.Element;
href?: string;
}
interface INotificationIcon {
key: string;
icon: JSX.Element;
badge: number;
}
export const NotificationsContext = createContext<{
notifications: INotification[];
icons: INotificationIcon[];
count: number;
addNotification(value: INotification): void;
updateNotification(idx: number, value: INotification): void;
removeNotification(idx: number): void;
}>(null);
export function NotificationsProvider({ children }) {
const [notifications, setNotifications] = useState<INotification[]>([]);
const updateNotification = useCallback(
(idx: number, value: INotification) => {
setNotifications((all) => {
const updated = [...all];
updated.splice(idx, 0, value);
return updated;
});
},
[]
);
const removeNotification = useCallback((idx) => {
setNotifications((all) => {
const updated = [...all];
updated.splice(idx, 1);
return updated;
});
}, []);
const addNotification = useCallback((value: INotification) => {
setNotifications((all) => [value, ...all]);
}, []);
const icons: INotificationIcon[] = useMemo(
() =>
notifications.reduce((icons, curr) => {
const find = icons.findIndex((i) => i.key === curr.app);
if (find !== -1) {
icons[find].badge++;
return icons;
}
icons.unshift({ key: curr.app, icon: curr.notificationIcon, badge: 1 });
return icons;
}, []),
[notifications]
);
return (
<NotificationsContext.Provider
value={{
notifications,
addNotification,
updateNotification,
removeNotification,
icons,
count: notifications.length,
}}
>
{children}
</NotificationsContext.Provider>
);
}
+4 -3
View File
@@ -1,14 +1,15 @@
import { useRecoilValue } from 'recoil'; import { useRecoilState } from 'recoil';
import { phoneState } from './state'; import { phoneState } from './state';
import { IServerConfig } from '../../../common/typings/config'; import { IServerConfig } from '../../../common/typings/config';
import ServerConfig from '../../../config.autogen.json'; import ServerConfig from '../../../config.autogen.json';
interface IUsePhone { interface IUsePhone {
visibility: boolean; visibility: boolean;
setVisibility(v: boolean): void;
config?: IServerConfig; config?: IServerConfig;
} }
export const usePhone = (): IUsePhone => { export const usePhone = (): IUsePhone => {
const visibility = useRecoilValue(phoneState.visibility); const [visibility, setVisibility] = useRecoilState(phoneState.visibility);
return { visibility, config: ServerConfig as any }; return { visibility, setVisibility, config: ServerConfig as any };
}; };
@@ -1,12 +1,10 @@
import { useSetRecoilState } from 'recoil'; import { useSetRecoilState } from 'recoil';
import { phoneState } from './state'; import { phoneState } from './state';
import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent'; import { useNuiEvent } from '../../nui-events/hooks/useNuiEvent';
import { usePhone } from './usePhone';
export const usePhoneService = () => { export const usePhoneService = () => {
const setVisibility = useSetRecoilState(phoneState.visibility); const setVisibility = useSetRecoilState(phoneState.visibility);
const setPhoneConfig = useSetRecoilState(phoneState.phoneConfig); const setPhoneConfig = useSetRecoilState(phoneState.phoneConfig);
useNuiEvent('PHONE', 'setVisibility', setVisibility); useNuiEvent('PHONE', 'setVisibility', setVisibility);
useNuiEvent('PHONE', 'phoneConfig', setPhoneConfig); useNuiEvent('PHONE', 'phoneConfig', setPhoneConfig);
return usePhone();
}; };
@@ -0,0 +1,56 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useSettings } from '../../../apps/settings/hooks/useSettings';
import { useNotifications } from '../../notifications/hooks/useNotifications';
import { usePhone } from './usePhone';
export const usePhoneVisibility = () => {
const { setVisibility, visibility } = usePhone();
const { count } = useNotifications();
const [{ zoom }] = useSettings();
const [notifVisibility, setNotifVisibility] = useState<boolean>(false);
const isNotificationVisibleOnly = useMemo(
() => !!(!visibility && count > 0),
[visibility, count]
);
const notificationTimer = useRef<NodeJS.Timeout>();
useEffect(() => {
if (isNotificationVisibleOnly) {
setNotifVisibility(true);
clearTimeout(notificationTimer.current);
notificationTimer.current = setTimeout(() => {
setVisibility(false);
setNotifVisibility(false);
}, 5000);
}
return () => clearTimeout(notificationTimer.current);
}, [isNotificationVisibleOnly, setVisibility]);
const bottom = useMemo(() => {
if (isNotificationVisibleOnly) {
return `calc(-60% * ${zoom})`;
}
return 0;
}, [isNotificationVisibleOnly, zoom]);
const clickEventOverride = useCallback(
(e) => {
if (isNotificationVisibleOnly) {
e.preventDefault();
e.stopPropagation();
setVisibility(true);
}
},
[setVisibility, isNotificationVisibleOnly]
);
return {
bottom,
visibility: notifVisibility,
uncollapseNotifications: isNotificationVisibleOnly,
clickEventOverride,
};
};
+14 -2
View File
@@ -2,7 +2,7 @@ import React from 'react';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import Avatar from '@material-ui/core/Avatar'; import Avatar from '@material-ui/core/Avatar';
import { green } from '@material-ui/core/colors'; import { green } from '@material-ui/core/colors';
import { Button } from '@material-ui/core'; import { Badge, Button } from '@material-ui/core';
interface IStyles { interface IStyles {
root: any; root: any;
@@ -28,7 +28,13 @@ const useStyles = makeStyles(
}) })
); );
export const AppIcon = ({ id, name, icon, backgroundColor, color }) => { export const AppIcon = ({
name,
icon,
backgroundColor,
color,
notification,
}) => {
const classes = useStyles({ const classes = useStyles({
backgroundColor: backgroundColor || green[50], backgroundColor: backgroundColor || green[50],
color: color || green[400], color: color || green[400],
@@ -36,9 +42,15 @@ export const AppIcon = ({ id, name, icon, backgroundColor, color }) => {
return ( return (
<Button className={classes.root}> <Button className={classes.root}>
<Badge
color="error"
badgeContent={notification?.badge}
invisible={!notification || notification.badge < 2}
>
<Avatar className={classes.avatar}> <Avatar className={classes.avatar}>
{icon || name[0].toUpperCase()} {icon || name[0].toUpperCase()}
</Avatar> </Avatar>
</Badge>
</Button> </Button>
); );
}; };
+1 -1
View File
@@ -1,5 +1,5 @@
import { useContext } from 'react'; import { useContext } from 'react';
import { SnackbarContext } from '../components/SnackbarProvider'; import { SnackbarContext } from '../providers/SnackbarProvider';
export interface IAlert { export interface IAlert {
@@ -8,26 +8,21 @@ function SnackbarProvider({ children }) {
useEffect(() => { useEffect(() => {
if (alert) { if (alert) {
clearTimeout(timer.current) clearTimeout(timer.current);
timer.current = setTimeout(() => { timer.current = setTimeout(() => {
setAlert(null) setAlert(null);
}, 1000); }, 1000);
} }
return () => clearTimeout(timer.current) return () => clearTimeout(timer.current);
}, [alert]); }, [alert]);
const addAlert = (value) => setAlert(value); const addAlert = (value) => setAlert(value);
const value = { addAlert, alert }
return ( return (
<SnackbarContext.Provider value={value}> <SnackbarContext.Provider value={{ addAlert, alert }}>
<>
{children} {children}
</>
</SnackbarContext.Provider> </SnackbarContext.Provider>
) );
} }
export default SnackbarProvider; export default SnackbarProvider;