2022-02-21 22:24:13 +01:00
|
|
|
import * as React from 'react';
|
|
|
|
import {useEffect, useRef, useState} from 'react';
|
|
|
|
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 IconButton from "@mui/material/IconButton";
|
|
|
|
import MoreVertIcon from "@mui/icons-material/MoreVert";
|
2022-02-24 02:30:12 +01:00
|
|
|
import api from "../app/Api";
|
2022-03-03 22:52:07 +01:00
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
2022-02-21 22:24:13 +01:00
|
|
|
|
|
|
|
// Originally from https://mui.com/components/menus/#MenuListComposition.js
|
2022-03-03 22:52:07 +01:00
|
|
|
const SubscribeSettings = (props) => {
|
2022-02-21 22:24:13 +01:00
|
|
|
const [open, setOpen] = useState(false);
|
|
|
|
const anchorRef = useRef(null);
|
|
|
|
|
|
|
|
const handleToggle = () => {
|
|
|
|
setOpen((prevOpen) => !prevOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleClose = (event) => {
|
|
|
|
if (anchorRef.current && anchorRef.current.contains(event.target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setOpen(false);
|
2022-02-23 05:22:30 +01:00
|
|
|
};
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
const handleClearAll = async (event) => {
|
2022-02-24 18:26:07 +01:00
|
|
|
handleClose(event);
|
2022-03-02 22:16:30 +01:00
|
|
|
console.log(`[IconSubscribeSettings] Deleting all notifications from ${props.subscription.id}`);
|
2022-03-03 22:52:07 +01:00
|
|
|
await subscriptionManager.deleteNotifications(props.subscription.id);
|
2022-02-24 18:26:07 +01:00
|
|
|
};
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
const handleUnsubscribe = async (event) => {
|
2022-02-23 05:22:30 +01:00
|
|
|
handleClose(event);
|
2022-03-03 22:52:07 +01:00
|
|
|
await subscriptionManager.remove(props.subscription.id);
|
2022-02-24 02:30:12 +01:00
|
|
|
props.onUnsubscribe(props.subscription.id);
|
2022-02-21 22:24:13 +01:00
|
|
|
};
|
|
|
|
|
2022-02-23 05:22:30 +01:00
|
|
|
const handleSendTestMessage = () => {
|
|
|
|
const baseUrl = props.subscription.baseUrl;
|
|
|
|
const topic = props.subscription.topic;
|
2022-03-02 03:23:12 +01:00
|
|
|
api.publish(baseUrl, topic,
|
2022-02-26 05:25:04 +01:00
|
|
|
`This is a test notification sent by the ntfy Web UI at ${new Date().toString()}.`); // FIXME result ignored
|
2022-02-23 05:22:30 +01:00
|
|
|
setOpen(false);
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
const handleListKeyDown = (event) => {
|
2022-02-21 22:24:13 +01:00
|
|
|
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 (
|
|
|
|
<>
|
|
|
|
<IconButton
|
|
|
|
color="inherit"
|
|
|
|
size="large"
|
|
|
|
edge="end"
|
|
|
|
ref={anchorRef}
|
|
|
|
id="composition-button"
|
|
|
|
onClick={handleToggle}
|
|
|
|
>
|
|
|
|
<MoreVertIcon/>
|
|
|
|
</IconButton>
|
|
|
|
<Popper
|
|
|
|
open={open}
|
|
|
|
anchorEl={anchorRef.current}
|
|
|
|
role={undefined}
|
|
|
|
placement="bottom-start"
|
|
|
|
transition
|
|
|
|
disablePortal
|
|
|
|
>
|
|
|
|
{({TransitionProps, placement}) => (
|
|
|
|
<Grow
|
|
|
|
{...TransitionProps}
|
|
|
|
style={{
|
|
|
|
transformOrigin:
|
|
|
|
placement === 'bottom-start' ? 'left top' : 'left bottom',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Paper>
|
|
|
|
<ClickAwayListener onClickAway={handleClose}>
|
|
|
|
<MenuList
|
|
|
|
autoFocusItem={open}
|
|
|
|
id="composition-menu"
|
|
|
|
onKeyDown={handleListKeyDown}
|
|
|
|
>
|
2022-02-23 05:22:30 +01:00
|
|
|
<MenuItem onClick={handleSendTestMessage}>Send test notification</MenuItem>
|
2022-02-24 18:26:07 +01:00
|
|
|
<MenuItem onClick={handleClearAll}>Clear all notifications</MenuItem>
|
2022-02-23 05:22:30 +01:00
|
|
|
<MenuItem onClick={handleUnsubscribe}>Unsubscribe</MenuItem>
|
2022-02-21 22:24:13 +01:00
|
|
|
</MenuList>
|
|
|
|
</ClickAwayListener>
|
|
|
|
</Paper>
|
|
|
|
</Grow>
|
|
|
|
)}
|
|
|
|
</Popper>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-03 22:52:07 +01:00
|
|
|
export default SubscribeSettings;
|