1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-11-03 18:34:15 +01:00
ntfy/web/src/components/ActionBar.js

195 lines
8.2 KiB
JavaScript
Raw Normal View History

2022-02-25 18:46:22 +01:00
import AppBar from "@mui/material/AppBar";
import Navigation from "./Navigation";
import Toolbar from "@mui/material/Toolbar";
import IconButton from "@mui/material/IconButton";
import MenuIcon from "@mui/icons-material/Menu";
import Typography from "@mui/material/Typography";
import * as React from "react";
2022-03-06 04:33:34 +01:00
import {useEffect, useRef, useState} from "react";
2022-02-28 22:56:38 +01:00
import Box from "@mui/material/Box";
2022-03-11 04:58:24 +01:00
import {formatShortDateTime, shuffle, topicShortUrl} from "../app/utils";
2022-03-06 04:33:34 +01:00
import {useLocation, useNavigate} from "react-router-dom";
import ClickAwayListener from '@mui/material/ClickAwayListener';
import Grow from '@mui/material/Grow';
import Paper from '@mui/material/Paper';
import Popper from '@mui/material/Popper';
import MenuItem from '@mui/material/MenuItem';
import MenuList from '@mui/material/MenuList';
import MoreVertIcon from "@mui/icons-material/MoreVert";
2022-03-08 22:56:41 +01:00
import NotificationsIcon from '@mui/icons-material/Notifications';
import NotificationsOffIcon from '@mui/icons-material/NotificationsOff';
2022-03-06 04:33:34 +01:00
import api from "../app/Api";
import routes from "./routes";
2022-03-06 04:33:34 +01:00
import subscriptionManager from "../app/SubscriptionManager";
2022-03-10 21:37:50 +01:00
import logo from "../img/ntfy.svg";
2022-02-25 18:46:22 +01:00
const ActionBar = (props) => {
2022-03-04 22:10:04 +01:00
const location = useLocation();
let title = "ntfy";
if (props.selected) {
title = topicShortUrl(props.selected.baseUrl, props.selected.topic);
2022-03-04 22:10:04 +01:00
} else if (location.pathname === "/settings") {
title = "Settings";
}
2022-02-25 18:46:22 +01:00
return (
2022-02-26 20:22:21 +01:00
<AppBar position="fixed" sx={{
width: '100%',
2022-02-26 20:36:23 +01:00
zIndex: { sm: 1250 }, // > Navigation (1200), but < Dialog (1300)
2022-02-26 20:22:21 +01:00
ml: { sm: `${Navigation.width}px` }
}}>
2022-02-25 18:46:22 +01:00
<Toolbar sx={{pr: '24px'}}>
<IconButton
color="inherit"
edge="start"
onClick={props.onMobileDrawerToggle}
sx={{ mr: 2, display: { sm: 'none' } }}
>
<MenuIcon />
</IconButton>
2022-03-09 21:58:21 +01:00
<Box component="img" src={logo} sx={{
2022-02-28 22:56:38 +01:00
display: { xs: 'none', sm: 'block' },
marginRight: '10px',
height: '28px'
}}/>
2022-02-26 20:22:21 +01:00
<Typography variant="h6" noWrap component="div" sx={{ flexGrow: 1 }}>
{title}
</Typography>
2022-03-08 22:56:41 +01:00
{props.selected &&
<SettingsIcons
subscription={props.selected}
onUnsubscribe={props.onUnsubscribe}
/>}
2022-02-25 18:46:22 +01:00
</Toolbar>
</AppBar>
);
};
2022-03-06 04:33:34 +01:00
// Originally from https://mui.com/components/menus/#MenuListComposition.js
2022-03-08 22:56:41 +01:00
const SettingsIcons = (props) => {
2022-03-06 04:33:34 +01:00
const navigate = useNavigate();
const [open, setOpen] = useState(false);
const anchorRef = useRef(null);
2022-03-08 22:56:41 +01:00
const subscription = props.subscription;
2022-03-06 04:33:34 +01:00
2022-03-08 22:56:41 +01:00
const handleToggleOpen = () => {
2022-03-06 04:33:34 +01:00
setOpen((prevOpen) => !prevOpen);
};
2022-03-08 22:56:41 +01:00
const handleToggleMute = async () => {
const mutedUntil = (subscription.mutedUntil) ? 0 : 1; // Make this a timestamp in the future
await subscriptionManager.setMutedUntil(subscription.id, mutedUntil);
}
2022-03-06 04:33:34 +01:00
const handleClose = (event) => {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return;
}
setOpen(false);
};
const handleClearAll = async (event) => {
handleClose(event);
console.log(`[ActionBar] Deleting all notifications from ${props.subscription.id}`);
await subscriptionManager.deleteNotifications(props.subscription.id);
};
const handleUnsubscribe = async (event) => {
console.log(`[ActionBar] Unsubscribing from ${props.subscription.id}`);
handleClose(event);
await subscriptionManager.remove(props.subscription.id);
const newSelected = await subscriptionManager.first(); // May be undefined
if (newSelected) {
navigate(routes.forSubscription(newSelected));
2022-03-06 22:35:31 +01:00
} else {
navigate(routes.root);
2022-03-06 04:33:34 +01:00
}
};
const handleSendTestMessage = () => {
const baseUrl = props.subscription.baseUrl;
const topic = props.subscription.topic;
2022-03-11 04:58:24 +01:00
const tags = shuffle([
"grinning", "octopus", "upside_down_face", "palm_tree", "maple_leaf", "apple", "skull", "warning", "jack_o_lantern",
"de-server-1", "backups", "cron-script", "script-error", "phils-automation", "mouse", "go-rocks", "hi-ben"])
.slice(0, Math.round(Math.random() * 4));
const priority = shuffle([1, 2, 3, 4, 5])[0];
const title = shuffle([
"",
"",
"", // Higher chance of no title
"Oh my, another test message?",
"Titles are optional, did you know that?",
"ntfy is open source, and will always be free. Cool, right?",
"I don't really like apples",
"My favorite TV show is The Wire. You should watch it!"
])[0];
const message = shuffle([
`Hello friend, this is a test notification from ntfy web. It's ${formatShortDateTime(Date.now())} right now. Is that early or late?`,
`So I heard you like ntfy? If that's true, go to GitHub and star it, or to the Play store and rate it. Thanks! Oh yeah, this is a test notification.`,
`It's almost like you want to hear what I have to say. I'm not even a machine. I'm just a sentence that Phil typed on a random Thursday.`,
`Alright then, it's ${formatShortDateTime(Date.now())} already. Boy oh boy, where did the time go? I hope you're alright, friend.`,
`There are nine million bicycles in Beijing That's a fact; It's a thing we can't deny. I wonder if that's true ...`,
`I'm really excited that you're trying out ntfy. Did you know that there are a few public topics, such as ntfy.sh/stats and ntfy.sh/annoucements.`,
`It's interesting to hear what people use ntfy for. I've heard people talk about using it for so many cool things. What do you use it for?`
])[0];
api.publish(baseUrl, topic, message, title, priority, tags);
2022-03-06 04:33:34 +01:00
setOpen(false);
}
const handleListKeyDown = (event) => {
if (event.key === 'Tab') {
event.preventDefault();
setOpen(false);
} else if (event.key === 'Escape') {
setOpen(false);
}
}
// return focus to the button when we transitioned from !open -> open
const prevOpen = useRef(open);
useEffect(() => {
if (prevOpen.current === true && open === false) {
anchorRef.current.focus();
}
prevOpen.current = open;
}, [open]);
return (
<>
2022-03-08 22:56:41 +01:00
<IconButton color="inherit" size="large" edge="end" onClick={handleToggleMute} sx={{marginRight: 0}}>
{subscription.mutedUntil ? <NotificationsOffIcon/> : <NotificationsIcon/>}
</IconButton>
<IconButton color="inherit" size="large" edge="end" ref={anchorRef} onClick={handleToggleOpen}>
2022-03-06 04:33:34 +01:00
<MoreVertIcon/>
</IconButton>
<Popper
open={open}
anchorEl={anchorRef.current}
role={undefined}
placement="bottom-start"
transition
disablePortal
>
{({TransitionProps, placement}) => (
<Grow
{...TransitionProps}
2022-03-08 22:56:41 +01:00
style={{transformOrigin: placement === 'bottom-start' ? 'left top' : 'left bottom'}}
2022-03-06 04:33:34 +01:00
>
<Paper>
<ClickAwayListener onClickAway={handleClose}>
2022-03-09 21:58:21 +01:00
<MenuList autoFocusItem={open} onKeyDown={handleListKeyDown}>
2022-03-06 04:33:34 +01:00
<MenuItem onClick={handleSendTestMessage}>Send test notification</MenuItem>
<MenuItem onClick={handleClearAll}>Clear all notifications</MenuItem>
<MenuItem onClick={handleUnsubscribe}>Unsubscribe</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</>
);
};
2022-02-25 18:46:22 +01:00
export default ActionBar;