From 55e645131fb71659167d7d7732a57af1b104b83a Mon Sep 17 00:00:00 2001 From: Boris Edigariani Date: Tue, 6 Aug 2024 16:05:39 +0400 Subject: [PATCH] feat: added diagram preview and edit functionality --- appsscript.json | 17 --- .../components/edit-diagram-dialog.tsx | 36 +++-- .../components/preview-diagram-dialog.tsx | 70 +++++++++ src/client/preview-diagram-dialog/index.html | 33 +++++ src/client/preview-diagram-dialog/index.jsx | 7 + src/client/preview-diagram-dialog/styles.css | 4 + src/client/sidebar/components/Sidebar.tsx | 139 +++++++++++------- src/server/index.ts | 6 +- src/server/ui.js | 63 ++++---- vite.config.ts | 5 + 10 files changed, 269 insertions(+), 111 deletions(-) create mode 100644 src/client/preview-diagram-dialog/components/preview-diagram-dialog.tsx create mode 100644 src/client/preview-diagram-dialog/index.html create mode 100644 src/client/preview-diagram-dialog/index.jsx create mode 100644 src/client/preview-diagram-dialog/styles.css diff --git a/appsscript.json b/appsscript.json index b52ef40..ac5a99a 100644 --- a/appsscript.json +++ b/appsscript.json @@ -15,22 +15,5 @@ "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/documents" ], - "addOns": { - "common": { - "name": "Mermaid Chart", - "logoUrl": "https://www.mermaidchart.com/img/mermaid-chart-48.png", - "useLocaleFromApp": true, - "universalActions": [ - { - "label": "Learn more about Mermaid Chart", - "openLink": "www.mermaidchart.com" - } - ], - "layoutProperties": { - "primaryColor": "#424242", - "secondaryColor": "#ff3670" - } - } - }, "runtimeVersion": "V8" } diff --git a/src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx b/src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx index 1e35522..4330607 100644 --- a/src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx +++ b/src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx @@ -3,6 +3,8 @@ import { serverFunctions } from '../../utils/serverFunctions'; import { buildUrl, handleDialogClose } from '../../utils/helpers'; import useAuth from '../../hooks/useAuth'; +const editUrl = localStorage.getItem('editUrl'); + const EditDiagramDialog = () => { const { authState, authStatus } = useAuth(); const [diagramsUrl, setDiagramsUrl] = useState(''); @@ -12,20 +14,26 @@ const EditDiagramDialog = () => { const getMetadata = async () => { try { + if (editUrl) { + const iframeUrl = buildUrl(editUrl, authState.token); + setDiagramsUrl(iframeUrl); + localStorage.removeItem('editUrl'); + return; + } const metadata = await serverFunctions.readSelectedImageMetadata(); - if (typeof metadata === 'string') { - const params = new URLSearchParams(metadata); - const projectID = params.get('projectID'); - const documentID = params.get('documentID'); - const major = params.get('major'); - const minor = params.get('minor'); - if (projectID && documentID && major && minor) { - const iframeUrl = buildUrl( - `/app/projects/${projectID}/diagrams/${documentID}/version/v.${major}.${minor}/edit`, - authState.token - ); - setDiagramsUrl(iframeUrl); - } + if (typeof metadata !== 'string') return; + + const params = new URLSearchParams(metadata); + const projectID = params.get('projectID'); + const documentID = params.get('documentID'); + const major = params.get('major'); + const minor = params.get('minor'); + if (projectID && documentID && major && minor) { + const iframeUrl = buildUrl( + `/app/projects/${projectID}/diagrams/${documentID}/version/v.${major}.${minor}/edit`, + authState.token + ); + setDiagramsUrl(iframeUrl); } } catch (error) { console.log(error); @@ -33,7 +41,7 @@ const EditDiagramDialog = () => { }; getMetadata(); - }, [authState]); + }, [authState, editUrl]); useEffect(() => { const handleMessage = async (e: MessageEvent) => { diff --git a/src/client/preview-diagram-dialog/components/preview-diagram-dialog.tsx b/src/client/preview-diagram-dialog/components/preview-diagram-dialog.tsx new file mode 100644 index 0000000..94dccca --- /dev/null +++ b/src/client/preview-diagram-dialog/components/preview-diagram-dialog.tsx @@ -0,0 +1,70 @@ +import { useEffect, useState } from 'react'; +import { buildUrl } from '../../utils/helpers'; +import useAuth from '../../hooks/useAuth'; +import { CircularProgress, Container, Typography } from '@mui/material'; + +const previewUrl = localStorage.getItem('previewUrl'); + +const PreviewDiagramDialog = () => { + const { authState, authStatus } = useAuth(); + const [diagramsUrl, setDiagramsUrl] = useState(''); + + useEffect(() => { + if (!authState?.authorized || !previewUrl) return; + const url = buildUrl(previewUrl, authState.token); + setDiagramsUrl(url); + }, [authState, previewUrl]); + + if (authStatus === 'idle' || authStatus === 'loading') { + return ( + + + + ); + } + + if (authStatus === 'error') { + return ( + + + Error + + + Something went wrong. Please try again later. + + + ); + } + + return ( +
+