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
+497 -153

No files matched your search

@@ -1,8 +1,25 @@
import React from 'react';
import { makeStyles, Typography, Grid, IconButton } from '@material-ui/core';
import React, { useState } from 'react';
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 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';
const useStyles = makeStyles((theme) => ({
root: {
@@ -10,9 +27,13 @@ const useStyles = makeStyles((theme) => ({
height: '30px',
width: '100%',
color: theme.palette.text.primary,
zIndex: 2,
zIndex: 10,
paddingLeft: '15px',
paddingRight: '10px',
position: 'relative',
'&:hover': {
cursor: 'pointer',
},
},
item: {
margin: '0 6px',
@@ -26,53 +47,128 @@ const useStyles = makeStyles((theme) => ({
padding: '4px',
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 {
key: string;
icon: JSX.Element;
}
interface IProps {
notifications?: INotification[];
}
export const NotificationBar = ({ notifications = [] }: IProps) => {
export const NotificationBar = ({ forceUncollapse }: { forceUncollapse: boolean }) => {
const classes = useStyles();
const history = useHistory();
const { icons, notifications, removeNotification } = useNotifications();
const [uncollapsed, setUncollapsed] = useState<boolean>(false);
return (
<Grid
className={classes.root}
container
justify='space-between'
wrap='nowrap'
>
<Grid container item wrap='nowrap'>
{notifications.map((notification) => (
<Grid
item
key={notification.key}
component={IconButton}
className={classes.icon}
>
{notification.icon}
<>
<Grid
className={classes.root}
container
justify='space-between'
wrap='nowrap'
onClick={() => {
setUncollapsed(!uncollapsed);
}}
>
<Grid container item wrap='nowrap'>
{icons.map((notifIcon) => (
<Grid
item
key={notifIcon.key}
component={IconButton}
className={classes.icon}
>
{notifIcon.icon}
</Grid>
))}
</Grid>
<Grid
container
item
wrap='nowrap'
justify='flex-end'
alignItems='center'
>
<Grid item>
<SignalIcon fontSize='small' />
</Grid>
<Grid item className={classes.item}>
<Typography className={classes.text} variant='button'>
{Default.cellProvider}
</Typography>
</Grid>
<Grid item>
<Battery90Icon
style={{ transform: 'rotate(90deg)', display: 'block' }}
/>
</Grid>
))}
</Grid>
<Grid container item wrap='nowrap' justify='flex-end' alignItems='center'>
<Grid item>
<SignalIcon fontSize='small' />
</Grid>
<Grid item className={classes.item}>
<Typography className={classes.text} variant='button'>
{Default.cellProvider}
</Typography>
</Grid>
<Grid item>
<Battery90Icon
style={{ transform: 'rotate(90deg)', display: 'block' }}
/>
</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>
);
}