App.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import * as React from 'react';
  2. import {useState} from 'react';
  3. import Container from '@mui/material/Container';
  4. import Typography from '@mui/material/Typography';
  5. import Box from '@mui/material/Box';
  6. import Link from '@mui/material/Link';
  7. import Subscription from './Subscription';
  8. import WsConnection from './WsConnection';
  9. import {createTheme, styled, ThemeProvider} from '@mui/material/styles';
  10. import CssBaseline from '@mui/material/CssBaseline';
  11. import MuiDrawer from '@mui/material/Drawer';
  12. import MuiAppBar from '@mui/material/AppBar';
  13. import Toolbar from '@mui/material/Toolbar';
  14. import ChatBubbleOutlineIcon from '@mui/icons-material/ChatBubbleOutline';
  15. import List from '@mui/material/List';
  16. import Divider from '@mui/material/Divider';
  17. import IconButton from '@mui/material/IconButton';
  18. import Badge from '@mui/material/Badge';
  19. import Grid from '@mui/material/Grid';
  20. import Paper from '@mui/material/Paper';
  21. import MenuIcon from '@mui/icons-material/Menu';
  22. import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
  23. import NotificationsIcon from '@mui/icons-material/Notifications';
  24. import ListItemIcon from "@mui/material/ListItemIcon";
  25. import ListItemText from "@mui/material/ListItemText";
  26. import ListItemButton from "@mui/material/ListItemButton";
  27. import SettingsIcon from "@mui/icons-material/Settings";
  28. import AddIcon from "@mui/icons-material/Add";
  29. import Card from "@mui/material/Card";
  30. import {Button, CardActions, CardContent, Stack} from "@mui/material";
  31. function Copyright(props) {
  32. return (
  33. <Typography variant="body2" color="text.secondary" align="center" {...props}>
  34. {'Copyright © '}
  35. <Link color="inherit" href="https://mui.com/">
  36. Your Website
  37. </Link>{' '}
  38. {new Date().getFullYear()}
  39. {'.'}
  40. </Typography>
  41. );
  42. }
  43. const drawerWidth = 240;
  44. const AppBar = styled(MuiAppBar, {
  45. shouldForwardProp: (prop) => prop !== 'open',
  46. })(({ theme, open }) => ({
  47. zIndex: theme.zIndex.drawer + 1,
  48. transition: theme.transitions.create(['width', 'margin'], {
  49. easing: theme.transitions.easing.sharp,
  50. duration: theme.transitions.duration.leavingScreen,
  51. }),
  52. ...(open && {
  53. marginLeft: drawerWidth,
  54. width: `calc(100% - ${drawerWidth}px)`,
  55. transition: theme.transitions.create(['width', 'margin'], {
  56. easing: theme.transitions.easing.sharp,
  57. duration: theme.transitions.duration.enteringScreen,
  58. }),
  59. }),
  60. }));
  61. const Drawer = styled(MuiDrawer, { shouldForwardProp: (prop) => prop !== 'open' })(
  62. ({ theme, open }) => ({
  63. '& .MuiDrawer-paper': {
  64. position: 'relative',
  65. whiteSpace: 'nowrap',
  66. width: drawerWidth,
  67. transition: theme.transitions.create('width', {
  68. easing: theme.transitions.easing.sharp,
  69. duration: theme.transitions.duration.enteringScreen,
  70. }),
  71. boxSizing: 'border-box',
  72. ...(!open && {
  73. overflowX: 'hidden',
  74. transition: theme.transitions.create('width', {
  75. easing: theme.transitions.easing.sharp,
  76. duration: theme.transitions.duration.leavingScreen,
  77. }),
  78. width: theme.spacing(7),
  79. [theme.breakpoints.up('sm')]: {
  80. width: theme.spacing(9),
  81. },
  82. }),
  83. },
  84. }),
  85. );
  86. const mdTheme = createTheme();
  87. const SubscriptionNav = (props) => {
  88. const subscriptions = props.subscriptions;
  89. return (
  90. <div className="subscriptionList">
  91. {Object.keys(subscriptions).map(id =>
  92. <SubscriptionItem
  93. key={id}
  94. subscription={subscriptions[id]}
  95. selected={props.selectedSubscription === subscriptions[id]}
  96. onClick={() => props.handleSubscriptionClick(id)}
  97. />)
  98. }
  99. </div>
  100. );
  101. }
  102. const SubscriptionItem = (props) => {
  103. const subscription = props.subscription;
  104. return (
  105. <ListItemButton onClick={props.onClick}>
  106. <ListItemIcon>
  107. <ChatBubbleOutlineIcon />
  108. </ListItemIcon>
  109. <ListItemText primary={subscription.shortUrl()} />
  110. </ListItemButton>
  111. );
  112. }
  113. const NotificationList = (props) => {
  114. return (
  115. <Stack spacing={3} className="notificationList">
  116. {props.notifications.map(notification =>
  117. <NotificationItem key={notification.id} notification={notification}/>)}
  118. </Stack>
  119. );
  120. }
  121. const NotificationItem = (props) => {
  122. const notification = props.notification;
  123. return (
  124. <Card sx={{ minWidth: 275 }}>
  125. <CardContent>
  126. <Typography sx={{ fontSize: 14 }} color="text.secondary" gutterBottom>
  127. {notification.time}
  128. </Typography>
  129. {notification.title && <Typography variant="h5" component="div">
  130. title: {notification.title}
  131. </Typography>}
  132. <Typography variant="body1">
  133. msg: {notification.message}
  134. </Typography>
  135. </CardContent>
  136. <CardActions>
  137. <Button size="small">Learn More</Button>
  138. </CardActions>
  139. </Card>
  140. );
  141. }
  142. const defaultBaseUrl = "https://ntfy.sh"
  143. const SubscriptionAddForm = (props) => {
  144. const [topic, setTopic] = useState("");
  145. const handleSubmit = (ev) => {
  146. ev.preventDefault();
  147. props.onSubmit(new Subscription(defaultBaseUrl, topic));
  148. setTopic('');
  149. }
  150. return (
  151. <form onSubmit={handleSubmit}>
  152. <input
  153. type="text"
  154. value={topic}
  155. onChange={ev => setTopic(ev.target.value)}
  156. placeholder="Topic name, e.g. phil_alerts"
  157. required
  158. />
  159. </form>
  160. );
  161. }
  162. const App = () => {
  163. const [open, setOpen] = React.useState(true);
  164. const [subscriptions, setSubscriptions] = useState({});
  165. const [selectedSubscription, setSelectedSubscription] = useState(null);
  166. const [connections, setConnections] = useState({});
  167. const subscriptionChanged = (subscription) => {
  168. setSubscriptions(prev => ({...prev, [subscription.id]: subscription})); // Fake-replace
  169. };
  170. const addSubscription = (subscription) => {
  171. const connection = new WsConnection(subscription, subscriptionChanged);
  172. setSubscriptions(prev => ({...prev, [subscription.id]: subscription}));
  173. setConnections(prev => ({...prev, [connection.id]: connection}));
  174. connection.start();
  175. };
  176. const handleSubscriptionClick = (subscriptionId) => {
  177. console.log(`handleSubscriptionClick ${subscriptionId}`)
  178. setSelectedSubscription(subscriptions[subscriptionId]);
  179. };
  180. const notifications = (selectedSubscription !== null) ? selectedSubscription.notifications : [];
  181. const toggleDrawer = () => {
  182. setOpen(!open);
  183. };
  184. return (
  185. <ThemeProvider theme={mdTheme}>
  186. <Box sx={{ display: 'flex' }}>
  187. <CssBaseline />
  188. <AppBar position="absolute" open={open}>
  189. <Toolbar
  190. sx={{
  191. pr: '24px', // keep right padding when drawer closed
  192. }}
  193. color="primary"
  194. >
  195. <IconButton
  196. edge="start"
  197. color="inherit"
  198. aria-label="open drawer"
  199. onClick={toggleDrawer}
  200. sx={{
  201. marginRight: '36px',
  202. ...(open && { display: 'none' }),
  203. }}
  204. >
  205. <MenuIcon />
  206. </IconButton>
  207. <Typography
  208. component="h1"
  209. variant="h6"
  210. color="inherit"
  211. noWrap
  212. sx={{ flexGrow: 1 }}
  213. >
  214. ntfy
  215. </Typography>
  216. <IconButton color="inherit">
  217. <Badge badgeContent={4} color="secondary">
  218. <NotificationsIcon />
  219. </Badge>
  220. </IconButton>
  221. </Toolbar>
  222. </AppBar>
  223. <Drawer variant="permanent" open={open}>
  224. <Toolbar
  225. sx={{
  226. display: 'flex',
  227. alignItems: 'center',
  228. justifyContent: 'flex-end',
  229. px: [1],
  230. }}
  231. >
  232. <IconButton onClick={toggleDrawer}>
  233. <ChevronLeftIcon />
  234. </IconButton>
  235. </Toolbar>
  236. <Divider />
  237. <List component="nav">
  238. <SubscriptionNav
  239. subscriptions={subscriptions}
  240. selectedSubscription={selectedSubscription}
  241. handleSubscriptionClick={handleSubscriptionClick}
  242. />
  243. <Divider sx={{ my: 1 }} />
  244. <ListItemButton>
  245. <ListItemIcon>
  246. <SettingsIcon />
  247. </ListItemIcon>
  248. <ListItemText primary="Settings" />
  249. </ListItemButton>
  250. <ListItemButton>
  251. <ListItemIcon>
  252. <AddIcon />
  253. </ListItemIcon>
  254. <ListItemText primary="Add subscription" />
  255. </ListItemButton>
  256. </List>
  257. </Drawer>
  258. <Box
  259. component="main"
  260. sx={{
  261. backgroundColor: (theme) =>
  262. theme.palette.mode === 'light'
  263. ? theme.palette.grey[100]
  264. : theme.palette.grey[900],
  265. flexGrow: 1,
  266. height: '100vh',
  267. overflow: 'auto',
  268. }}
  269. >
  270. <Toolbar />
  271. <Container maxWidth="lg" sx={{ mt: 4, mb: 4 }}>
  272. <Grid container spacing={3}>
  273. <SubscriptionAddForm onSubmit={addSubscription}/>
  274. <NotificationList notifications={notifications}/>
  275. {/* Recent Orders */}
  276. <Grid item xs={12}>
  277. <Paper sx={{ p: 2, display: 'flex', flexDirection: 'column' }}>
  278. </Paper>
  279. </Grid>
  280. </Grid>
  281. <Copyright sx={{ pt: 4 }} />
  282. </Container>
  283. </Box>
  284. </Box>
  285. </ThemeProvider>
  286. );
  287. }
  288. export default App;