From c76be61cef9ec7bc8013d845aa80705d02575152 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 24 Feb 2024 14:41:12 -0800 Subject: Configurable device share expiration --- modern/src/Navigation.jsx | 2 + modern/src/common/components/StatusCard.jsx | 32 +------- modern/src/resources/l10n/en.json | 1 + modern/src/settings/SharePage.jsx | 109 ++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 31 deletions(-) create mode 100644 modern/src/settings/SharePage.jsx (limited to 'modern') diff --git a/modern/src/Navigation.jsx b/modern/src/Navigation.jsx index 731eeb62..2bec6456 100644 --- a/modern/src/Navigation.jsx +++ b/modern/src/Navigation.jsx @@ -55,6 +55,7 @@ import DeviceConnectionsPage from './settings/DeviceConnectionsPage'; import GroupConnectionsPage from './settings/GroupConnectionsPage'; import UserConnectionsPage from './settings/UserConnectionsPage'; import LogsPage from './reports/LogsPage'; +import SharePage from './settings/SharePage'; const Navigation = () => { const navigate = useNavigate(); @@ -122,6 +123,7 @@ const Navigation = () => { } /> } /> } /> + } /> } /> } /> } /> diff --git a/modern/src/common/components/StatusCard.jsx b/modern/src/common/components/StatusCard.jsx index 53048645..a63d0f80 100644 --- a/modern/src/common/components/StatusCard.jsx +++ b/modern/src/common/components/StatusCard.jsx @@ -1,7 +1,6 @@ import React, { useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { useNavigate } from 'react-router-dom'; -import dayjs from 'dayjs'; import Draggable from 'react-draggable'; import { Card, @@ -16,11 +15,6 @@ import { Menu, MenuItem, CardMedia, - Dialog, - TextField, - DialogActions, - DialogContent, - Button, } from '@mui/material'; import makeStyles from '@mui/styles/makeStyles'; import CloseIcon from '@mui/icons-material/Close'; @@ -138,7 +132,6 @@ const StatusCard = ({ deviceId, position, onClose, disableActions, desktopPaddin const [anchorEl, setAnchorEl] = useState(null); const [removing, setRemoving] = useState(false); - const [shared, setShared] = useState(null); const handleRemove = useCatch(async (removed) => { if (removed) { @@ -178,20 +171,6 @@ const StatusCard = ({ deviceId, position, onClose, disableActions, desktopPaddin } }, [navigate, position]); - const handleShare = useCatchCallback(async () => { - const expiration = dayjs().add(1, 'week').toISOString(); - const response = await fetch('/api/devices/share', { - method: 'POST', - body: new URLSearchParams(`deviceId=${deviceId}&expiration=${expiration}`), - }); - if (response.ok) { - const token = await response.text(); - setShared(`${window.location.origin}?token=${token}`); - } else { - throw Error(await response.text()); - } - }, [deviceId, setShared]); - return ( <>
@@ -293,7 +272,7 @@ const StatusCard = ({ deviceId, position, onClose, disableActions, desktopPaddin {t('linkGoogleMaps')} {t('linkAppleMaps')} {t('linkStreetView')} - {!shareDisabled && !user.temporary && {t('deviceShare')}} + {!shareDisabled && !user.temporary && navigate(`/settings/device/${deviceId}/share`)}>{t('deviceShare')}} )} handleRemove(removed)} /> - setShared(null)}> - - e.target.select()} /> - - - - - - ); }; diff --git a/modern/src/resources/l10n/en.json b/modern/src/resources/l10n/en.json index 814bfde0..b91312c7 100644 --- a/modern/src/resources/l10n/en.json +++ b/modern/src/resources/l10n/en.json @@ -101,6 +101,7 @@ "sharedColumns": "Columns", "sharedDropzoneText": "Drag and drop a file here or click", "sharedLogs": "Logs", + "sharedLink": "Link", "calendarSimple": "Simple", "calendarRecurrence": "Recurrence", "calendarOnce": "Once", diff --git a/modern/src/settings/SharePage.jsx b/modern/src/settings/SharePage.jsx new file mode 100644 index 00000000..d16fe44d --- /dev/null +++ b/modern/src/settings/SharePage.jsx @@ -0,0 +1,109 @@ +import React, { useState } from 'react'; +import { useSelector } from 'react-redux'; +import { useNavigate, useParams } from 'react-router-dom'; +import dayjs from 'dayjs'; +import { + Accordion, + AccordionSummary, + AccordionDetails, + Typography, + Container, + TextField, + Button, +} from '@mui/material'; +import ExpandMoreIcon from '@mui/icons-material/ExpandMore'; +import { useTranslation } from '../common/components/LocalizationProvider'; +import PageLayout from '../common/components/PageLayout'; +import SettingsMenu from './components/SettingsMenu'; +import { useCatchCallback } from '../reactHelper'; +import useSettingsStyles from './common/useSettingsStyles'; + +const SharePage = () => { + const navigate = useNavigate(); + const classes = useSettingsStyles(); + const t = useTranslation(); + + const { id } = useParams(); + + const device = useSelector((state) => state.devices.items[id]); + + const [expiration, setExpiration] = useState(dayjs().add(1, 'week').locale('en').format('YYYY-MM-DD')); + const [link, setLink] = useState(); + + const handleShare = useCatchCallback(async () => { + const expirationTime = dayjs(expiration).toISOString(); + const response = await fetch('/api/devices/share', { + method: 'POST', + body: new URLSearchParams(`deviceId=${id}&expiration=${expirationTime}`), + }); + if (response.ok) { + const token = await response.text(); + setLink(`${window.location.origin}?token=${token}`); + } else { + throw Error(await response.text()); + } + }, [id, expiration, setLink]); + + return ( + } breadcrumbs={['deviceShare']}> + + + }> + + {t('sharedRequired')} + + + + + setExpiration(dayjs(e.target.value, 'YYYY-MM-DD').locale('en').format())} + /> + + setLink(e.target.value)} + label={t('sharedLink')} + InputProps={{ + readOnly: true, + }} + /> + + +
+ + +
+
+
+ ); +}; + +export default SharePage; -- cgit v1.2.3