2023-02-01 15:39:30 +13:00
|
|
|
import * as React from "react";
|
|
|
|
import { useContext, useState } from "react";
|
2023-05-24 22:25:20 +12:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
TextField,
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogContentText,
|
|
|
|
DialogTitle,
|
|
|
|
Chip,
|
|
|
|
InputAdornment,
|
|
|
|
Portal,
|
|
|
|
Snackbar,
|
|
|
|
useMediaQuery,
|
|
|
|
MenuItem,
|
|
|
|
IconButton,
|
2023-05-25 07:36:01 +12:00
|
|
|
ListItemIcon,
|
|
|
|
ListItemText,
|
|
|
|
Divider,
|
2023-05-24 22:25:20 +12:00
|
|
|
} from "@mui/material";
|
2023-02-01 15:39:30 +13:00
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useNavigate } from "react-router-dom";
|
2023-05-25 07:36:01 +12:00
|
|
|
import {
|
|
|
|
Check,
|
|
|
|
Clear,
|
|
|
|
ClearAll,
|
|
|
|
Edit,
|
|
|
|
EnhancedEncryption,
|
|
|
|
Lock,
|
|
|
|
LockOpen,
|
|
|
|
NotificationsOff,
|
|
|
|
RemoveCircle,
|
|
|
|
Send,
|
|
|
|
} from "@mui/icons-material";
|
2023-02-01 15:39:30 +13:00
|
|
|
import theme from "./theme";
|
2023-06-02 23:22:54 +12:00
|
|
|
import subscriptionManager from "../app/SubscriptionManager";
|
2023-02-01 15:39:30 +13:00
|
|
|
import DialogFooter from "./DialogFooter";
|
2023-02-21 14:06:49 +13:00
|
|
|
import accountApi, { Role } from "../app/AccountApi";
|
2023-02-01 15:39:30 +13:00
|
|
|
import session from "../app/Session";
|
|
|
|
import routes from "./routes";
|
|
|
|
import PopupMenu from "./PopupMenu";
|
|
|
|
import { formatShortDateTime, shuffle } from "../app/utils";
|
|
|
|
import api from "../app/Api";
|
|
|
|
import { AccountContext } from "./App";
|
2023-02-03 09:19:37 +13:00
|
|
|
import { ReserveAddDialog, ReserveDeleteDialog, ReserveEditDialog } from "./ReserveDialogs";
|
|
|
|
import { UnauthorizedError } from "../app/errors";
|
2023-05-25 07:36:01 +12:00
|
|
|
import notifier from "../app/Notifier";
|
2023-02-01 15:39:30 +13:00
|
|
|
|
2023-02-11 15:19:44 +13:00
|
|
|
export const SubscriptionPopup = (props) => {
|
2023-02-01 15:39:30 +13:00
|
|
|
const { t } = useTranslation();
|
|
|
|
const { account } = useContext(AccountContext);
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const [displayNameDialogOpen, setDisplayNameDialogOpen] = useState(false);
|
|
|
|
const [reserveAddDialogOpen, setReserveAddDialogOpen] = useState(false);
|
|
|
|
const [reserveEditDialogOpen, setReserveEditDialogOpen] = useState(false);
|
|
|
|
const [reserveDeleteDialogOpen, setReserveDeleteDialogOpen] = useState(false);
|
|
|
|
const [showPublishError, setShowPublishError] = useState(false);
|
|
|
|
const { subscription } = props;
|
|
|
|
const placement = props.placement ?? "left";
|
|
|
|
const reservations = account?.reservations || [];
|
|
|
|
|
2023-02-11 15:19:44 +13:00
|
|
|
const showReservationAdd = config.enable_reservations && !subscription?.reservation && account?.stats.reservations_remaining > 0;
|
2023-02-12 08:32:50 +13:00
|
|
|
const showReservationAddDisabled =
|
|
|
|
!showReservationAdd &&
|
|
|
|
config.enable_reservations &&
|
|
|
|
!subscription?.reservation &&
|
|
|
|
(config.enable_payments || account?.stats.reservations_remaining === 0);
|
2023-02-11 15:19:44 +13:00
|
|
|
const showReservationEdit = config.enable_reservations && !!subscription?.reservation;
|
|
|
|
const showReservationDelete = config.enable_reservations && !!subscription?.reservation;
|
2023-02-01 15:39:30 +13:00
|
|
|
|
|
|
|
const handleChangeDisplayName = async () => {
|
|
|
|
setDisplayNameDialogOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleReserveAdd = async () => {
|
|
|
|
setReserveAddDialogOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleReserveEdit = async () => {
|
|
|
|
setReserveEditDialogOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleReserveDelete = async () => {
|
|
|
|
setReserveDeleteDialogOpen(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleSendTestMessage = async () => {
|
2023-05-25 07:36:01 +12:00
|
|
|
const { baseUrl, topic } = props.subscription;
|
2023-02-01 15:39:30 +13: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([
|
|
|
|
"",
|
2023-05-24 07:13:01 +12:00
|
|
|
"",
|
2023-02-01 15:39:30 +13:00
|
|
|
"", // 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!",
|
|
|
|
"You can attach files and URLs to messages too",
|
|
|
|
"You can delay messages up to 3 days",
|
|
|
|
])[0];
|
|
|
|
const nowSeconds = Math.round(Date.now() / 1000);
|
|
|
|
const message = shuffle([
|
|
|
|
`Hello friend, this is a test notification from ntfy web. It's ${formatShortDateTime(nowSeconds)} 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(nowSeconds)} 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/announcements.`,
|
|
|
|
`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];
|
|
|
|
try {
|
|
|
|
await api.publish(baseUrl, topic, message, {
|
|
|
|
title,
|
|
|
|
priority,
|
|
|
|
tags,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2023-02-03 09:19:37 +13:00
|
|
|
console.log(`[SubscriptionPopup] Error publishing message`, e);
|
2023-02-01 15:39:30 +13:00
|
|
|
setShowPublishError(true);
|
|
|
|
}
|
2023-05-24 07:13:01 +12:00
|
|
|
};
|
2023-02-01 15:39:30 +13:00
|
|
|
|
|
|
|
const handleClearAll = async () => {
|
2023-02-03 09:19:37 +13:00
|
|
|
console.log(`[SubscriptionPopup] Deleting all notifications from ${props.subscription.id}`);
|
2023-02-01 15:39:30 +13:00
|
|
|
await subscriptionManager.deleteNotifications(props.subscription.id);
|
|
|
|
};
|
|
|
|
|
2023-02-03 09:19:37 +13:00
|
|
|
const handleUnsubscribe = async () => {
|
|
|
|
console.log(`[SubscriptionPopup] Unsubscribing from ${props.subscription.id}`, props.subscription);
|
2023-05-25 07:36:01 +12:00
|
|
|
await subscriptionManager.remove(props.subscription);
|
2023-02-13 08:09:44 +13:00
|
|
|
if (session.exists() && !subscription.internal) {
|
2023-02-01 15:39:30 +13:00
|
|
|
try {
|
2023-02-13 08:09:44 +13:00
|
|
|
await accountApi.deleteSubscription(props.subscription.baseUrl, props.subscription.topic);
|
2023-02-01 15:39:30 +13:00
|
|
|
} catch (e) {
|
2023-02-03 09:19:37 +13:00
|
|
|
console.log(`[SubscriptionPopup] Error unsubscribing`, e);
|
|
|
|
if (e instanceof UnauthorizedError) {
|
2023-02-01 15:39:30 +13:00
|
|
|
session.resetAndRedirect(routes.login);
|
|
|
|
}
|
2023-05-24 07:13:01 +12:00
|
|
|
}
|
|
|
|
}
|
2023-02-01 15:39:30 +13:00
|
|
|
const newSelected = await subscriptionManager.first(); // May be undefined
|
|
|
|
if (newSelected && !newSelected.internal) {
|
|
|
|
navigate(routes.forSubscription(newSelected));
|
2023-05-24 07:13:01 +12:00
|
|
|
} else {
|
2023-02-01 15:39:30 +13:00
|
|
|
navigate(routes.app);
|
2023-05-24 07:13:01 +12:00
|
|
|
}
|
2023-02-01 15:39:30 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<PopupMenu horizontal={placement} anchorEl={props.anchor} open={!!props.anchor} onClose={props.onClose}>
|
2023-05-25 07:36:01 +12:00
|
|
|
<NotificationToggle subscription={subscription} />
|
|
|
|
<Divider />
|
|
|
|
<MenuItem onClick={handleChangeDisplayName}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<Edit fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_change_display_name")}
|
|
|
|
</MenuItem>
|
|
|
|
{showReservationAdd && (
|
|
|
|
<MenuItem onClick={handleReserveAdd}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<Lock fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
{t("action_bar_reservation_add")}
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
2023-02-08 17:18:41 +13:00
|
|
|
{showReservationAddDisabled && (
|
|
|
|
<MenuItem sx={{ cursor: "default" }}>
|
2023-05-25 07:36:01 +12:00
|
|
|
<ListItemIcon>
|
|
|
|
<Lock fontSize="small" color="disabled" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
2023-02-08 17:18:41 +13:00
|
|
|
<span style={{ opacity: 0.3 }}>{t("action_bar_reservation_add")}</span>
|
2023-02-11 15:19:44 +13:00
|
|
|
<ReserveLimitChip />
|
2023-02-08 17:18:41 +13:00
|
|
|
</MenuItem>
|
2023-02-01 15:39:30 +13:00
|
|
|
)}
|
2023-05-25 07:36:01 +12:00
|
|
|
{showReservationEdit && (
|
|
|
|
<MenuItem onClick={handleReserveEdit}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<EnhancedEncryption fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_reservation_edit")}
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
{showReservationDelete && (
|
|
|
|
<MenuItem onClick={handleReserveDelete}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<LockOpen fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_reservation_delete")}
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
<MenuItem onClick={handleSendTestMessage}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<Send fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_send_test_notification")}
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem onClick={handleClearAll}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<ClearAll fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_clear_notifications")}
|
|
|
|
</MenuItem>
|
|
|
|
<MenuItem onClick={handleUnsubscribe}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<RemoveCircle fontSize="small" />
|
|
|
|
</ListItemIcon>
|
|
|
|
|
|
|
|
{t("action_bar_unsubscribe")}
|
|
|
|
</MenuItem>
|
2023-02-01 15:39:30 +13:00
|
|
|
</PopupMenu>
|
|
|
|
<Portal>
|
|
|
|
<Snackbar
|
|
|
|
open={showPublishError}
|
|
|
|
autoHideDuration={3000}
|
|
|
|
onClose={() => setShowPublishError(false)}
|
|
|
|
message={t("message_bar_error_publishing")}
|
|
|
|
/>
|
|
|
|
<DisplayNameDialog open={displayNameDialogOpen} subscription={subscription} onClose={() => setDisplayNameDialogOpen(false)} />
|
|
|
|
{showReservationAdd && (
|
|
|
|
<ReserveAddDialog
|
|
|
|
open={reserveAddDialogOpen}
|
|
|
|
topic={subscription.topic}
|
|
|
|
reservations={reservations}
|
|
|
|
onClose={() => setReserveAddDialogOpen(false)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showReservationEdit && (
|
|
|
|
<ReserveEditDialog
|
|
|
|
open={reserveEditDialogOpen}
|
|
|
|
reservation={subscription.reservation}
|
|
|
|
reservations={props.reservations}
|
|
|
|
onClose={() => setReserveEditDialogOpen(false)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showReservationDelete && (
|
|
|
|
<ReserveDeleteDialog
|
|
|
|
open={reserveDeleteDialogOpen}
|
|
|
|
topic={subscription.topic}
|
|
|
|
onClose={() => setReserveDeleteDialogOpen(false)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Portal>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DisplayNameDialog = (props) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const { subscription } = props;
|
2023-02-03 09:19:37 +13:00
|
|
|
const [error, setError] = useState("");
|
2023-02-01 15:39:30 +13:00
|
|
|
const [displayName, setDisplayName] = useState(subscription.displayName ?? "");
|
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down("sm"));
|
|
|
|
|
|
|
|
const handleSave = async () => {
|
|
|
|
await subscriptionManager.setDisplayName(subscription.id, displayName);
|
2023-02-13 08:09:44 +13:00
|
|
|
if (session.exists() && !subscription.internal) {
|
2023-02-01 15:39:30 +13:00
|
|
|
try {
|
|
|
|
console.log(`[SubscriptionSettingsDialog] Updating subscription display name to ${displayName}`);
|
2023-02-13 08:09:44 +13:00
|
|
|
await accountApi.updateSubscription(subscription.baseUrl, subscription.topic, { display_name: displayName });
|
2023-02-01 15:39:30 +13:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`[SubscriptionSettingsDialog] Error updating subscription`, e);
|
2023-02-03 09:19:37 +13:00
|
|
|
if (e instanceof UnauthorizedError) {
|
2023-02-01 15:39:30 +13:00
|
|
|
session.resetAndRedirect(routes.login);
|
2023-02-03 09:19:37 +13:00
|
|
|
} else {
|
|
|
|
setError(e.message);
|
|
|
|
return;
|
2023-02-01 15:39:30 +13:00
|
|
|
}
|
2023-05-24 07:13:01 +12:00
|
|
|
}
|
2023-02-01 15:39:30 +13:00
|
|
|
}
|
|
|
|
props.onClose();
|
2023-05-24 07:13:01 +12:00
|
|
|
};
|
2023-02-01 15:39:30 +13:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={props.open} onClose={props.onClose} maxWidth="sm" fullWidth fullScreen={fullScreen}>
|
|
|
|
<DialogTitle>{t("display_name_dialog_title")}</DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogContentText>{t("display_name_dialog_description")}</DialogContentText>
|
|
|
|
<TextField
|
|
|
|
autoFocus
|
|
|
|
placeholder={t("display_name_dialog_placeholder")}
|
|
|
|
value={displayName}
|
|
|
|
onChange={(ev) => setDisplayName(ev.target.value)}
|
|
|
|
type="text"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
inputProps={{
|
|
|
|
maxLength: 64,
|
|
|
|
"aria-label": t("display_name_dialog_placeholder"),
|
2023-05-25 13:32:15 +12:00
|
|
|
}}
|
|
|
|
InputProps={{
|
2023-02-01 15:39:30 +13:00
|
|
|
endAdornment: (
|
|
|
|
<InputAdornment position="end">
|
|
|
|
<IconButton onClick={() => setDisplayName("")} edge="end">
|
|
|
|
<Clear />
|
|
|
|
</IconButton>
|
|
|
|
</InputAdornment>
|
|
|
|
),
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</DialogContent>
|
2023-02-03 09:19:37 +13:00
|
|
|
<DialogFooter status={error}>
|
2023-02-01 15:39:30 +13:00
|
|
|
<Button onClick={props.onClose}>{t("common_cancel")}</Button>
|
|
|
|
<Button onClick={handleSave}>{t("common_save")}</Button>
|
|
|
|
</DialogFooter>
|
|
|
|
</Dialog>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-25 07:36:01 +12:00
|
|
|
const checkedItem = (
|
|
|
|
<ListItemIcon>
|
|
|
|
<Check />
|
|
|
|
</ListItemIcon>
|
|
|
|
);
|
|
|
|
|
|
|
|
const NotificationToggle = ({ subscription }) => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
2023-06-02 23:22:54 +12:00
|
|
|
const handleToggleBackground = async () => {
|
2023-05-25 07:36:01 +12:00
|
|
|
try {
|
2023-06-02 23:22:54 +12:00
|
|
|
await subscriptionManager.toggleBackgroundNotifications(subscription);
|
2023-05-25 07:36:01 +12:00
|
|
|
} catch (e) {
|
|
|
|
console.error("[NotificationToggle] Error setting notification type", e);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const unmute = async () => {
|
|
|
|
await subscriptionManager.setMutedUntil(subscription.id, 0);
|
|
|
|
};
|
|
|
|
|
2023-06-02 23:22:54 +12:00
|
|
|
if (subscription.mutedUntil === 1) {
|
2023-05-25 07:36:01 +12:00
|
|
|
return (
|
|
|
|
<MenuItem onClick={unmute}>
|
|
|
|
<ListItemIcon>
|
|
|
|
<NotificationsOff />
|
|
|
|
</ListItemIcon>
|
|
|
|
{t("notification_toggle_unmute")}
|
|
|
|
</MenuItem>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2023-06-02 23:22:54 +12:00
|
|
|
{notifier.pushPossible() && (
|
2023-05-25 07:36:01 +12:00
|
|
|
<>
|
2023-06-02 23:22:54 +12:00
|
|
|
<MenuItem>
|
|
|
|
{subscription.webPushEnabled === 1 && checkedItem}
|
|
|
|
<ListItemText inset={subscription.webPushEnabled !== 1} onClick={handleToggleBackground}>
|
|
|
|
{t("notification_toggle_background")}
|
|
|
|
</ListItemText>
|
|
|
|
</MenuItem>
|
2023-05-25 07:36:01 +12:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-02-11 15:19:44 +13:00
|
|
|
export const ReserveLimitChip = () => {
|
|
|
|
const { account } = useContext(AccountContext);
|
2023-02-21 14:06:49 +13:00
|
|
|
if (account?.role === Role.ADMIN || account?.stats.reservations_remaining > 0) {
|
2023-02-11 15:19:44 +13:00
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
if (config.enable_payments) {
|
2023-02-12 08:32:50 +13:00
|
|
|
return account?.limits.reservations > 0 ? <LimitReachedChip /> : <ProChip />;
|
2023-02-11 15:19:44 +13:00
|
|
|
}
|
|
|
|
if (account) {
|
|
|
|
return <LimitReachedChip />;
|
2023-05-24 07:13:01 +12:00
|
|
|
}
|
2023-02-11 15:19:44 +13:00
|
|
|
return <></>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const LimitReachedChip = () => {
|
|
|
|
const { t } = useTranslation();
|
|
|
|
return (
|
|
|
|
<Chip
|
|
|
|
label={t("action_bar_reservation_limit_reached")}
|
|
|
|
variant="outlined"
|
|
|
|
color="primary"
|
|
|
|
sx={{
|
|
|
|
opacity: 0.8,
|
|
|
|
borderWidth: "2px",
|
|
|
|
height: "24px",
|
|
|
|
marginLeft: "5px",
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-24 20:20:15 +12:00
|
|
|
export const ProChip = () => (
|
|
|
|
<Chip
|
|
|
|
label="ntfy Pro"
|
|
|
|
variant="outlined"
|
|
|
|
color="primary"
|
|
|
|
sx={{
|
|
|
|
opacity: 0.8,
|
|
|
|
fontWeight: "bold",
|
|
|
|
borderWidth: "2px",
|
|
|
|
height: "24px",
|
|
|
|
marginLeft: "5px",
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|