Skip to content

Commit 6af1aed

Browse files
committed
feat: implemented ui updates and new features
1 parent b8e1821 commit 6af1aed

File tree

25 files changed

+702
-230
lines changed

25 files changed

+702
-230
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,35 @@
11
import { useEffect, useState } from 'react';
22
import { buildUrl, handleDialogClose } from '../../utils/helpers';
33
import { serverFunctions } from '../../utils/serverFunctions';
4+
import useAuth from '../../hooks/useAuth';
5+
import { CircularProgress, Container, Typography } from '@mui/material';
46

5-
interface AuthState {
6-
authorized: boolean;
7-
token?: string;
8-
message?: string;
9-
}
10-
11-
type Status = 'idle' | 'loading' | 'success' | 'error';
12-
13-
const Dialog = () => {
7+
const CreateDiagramDialog = () => {
8+
const { authState, authStatus } = useAuth();
149
const [diagramsUrl, setDiagramsUrl] = useState('');
15-
const [, setAuthState] = useState<null | AuthState>(null);
16-
const [authStatus, setAuthStatus] = useState<Status>('idle');
1710

1811
useEffect(() => {
19-
const getAuth = async () => {
20-
setAuthStatus('loading');
21-
try {
22-
const state = await serverFunctions.getAuthorizationState();
23-
setAuthState(state);
24-
setAuthStatus('success');
25-
26-
if (state.authorized) {
27-
const url = buildUrl('/app/plugins/confluence/select', state.token);
28-
setDiagramsUrl(url);
29-
}
30-
} catch (error) {
31-
console.log('Error getting auth data', error);
32-
setAuthStatus('error');
33-
}
34-
};
35-
36-
getAuth();
37-
}, []);
12+
if (!authState?.authorized) return;
13+
// const url = buildUrl('/app/plugins/confluence/select', state.token);
14+
const url = buildUrl(
15+
'/app/diagrams/new?pluginSource=googledocs',
16+
authState.token
17+
);
18+
setDiagramsUrl(url);
19+
}, [authState]);
3820

3921
useEffect(() => {
4022
const handleMessage = async (e: MessageEvent) => {
4123
const action = e.data.action;
42-
console.log('action', action, e.data);
4324
if (action === 'save') {
4425
const data = e.data.data;
45-
console.log(data);
4626
const metadata = new URLSearchParams({
4727
projectID: data.projectID,
4828
documentID: data.documentID,
4929
major: data.major,
5030
minor: data.minor,
5131
});
32+
5233
try {
5334
await serverFunctions.insertBase64ImageWithMetadata(
5435
data.diagramImage,
@@ -68,8 +49,41 @@ const Dialog = () => {
6849
};
6950
}, []);
7051

71-
if (authStatus !== 'success') {
72-
return null;
52+
if (authStatus === 'idle' || authStatus === 'loading') {
53+
return (
54+
<Container
55+
sx={{
56+
display: 'flex',
57+
flexDirection: 'column',
58+
alignItems: 'center',
59+
justifyContent: 'center',
60+
height: '96.5vh',
61+
}}
62+
>
63+
<CircularProgress />
64+
</Container>
65+
);
66+
}
67+
68+
if (authStatus === 'error') {
69+
return (
70+
<Container
71+
sx={{
72+
display: 'flex',
73+
flexDirection: 'column',
74+
alignItems: 'center',
75+
justifyContent: 'center',
76+
height: '96.5vh',
77+
}}
78+
>
79+
<Typography variant="h5" gutterBottom my={2} textAlign="center">
80+
Error
81+
</Typography>
82+
<Typography paragraph textAlign="center">
83+
Something went wrong. Please try again later.
84+
</Typography>
85+
</Container>
86+
);
7387
}
7488

7589
return (
@@ -87,4 +101,4 @@ const Dialog = () => {
87101
);
88102
};
89103

90-
export default Dialog;
104+
export default CreateDiagramDialog;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ReactDOM from 'react-dom';
2+
import CreateDiagramDialog from './components/create-diagram-dialog';
3+
import './styles.css';
4+
5+
ReactDOM.render(<CreateDiagramDialog />, document.getElementById('index'));

src/client/dialog/index.jsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/client/edit-dialog/components/EditDialog.tsx renamed to src/client/edit-diagram-dialog/components/edit-diagram-dialog.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import { serverFunctions } from '../../utils/serverFunctions';
33
import { buildUrl, handleDialogClose } from '../../utils/helpers';
44
import useAuth from '../../hooks/useAuth';
55

6-
const EditDialog = () => {
6+
const EditDiagramDialog = () => {
77
const { authState, authStatus } = useAuth();
88
const [diagramsUrl, setDiagramsUrl] = useState('');
99

1010
useEffect(() => {
11-
if (!authState) return;
11+
if (!authState?.authorized) return;
12+
1213
const getMetadata = async () => {
1314
try {
1415
const metadata = await serverFunctions.readSelectedImageMetadata();
15-
console.log(metadata, 'metadata');
1616
if (typeof metadata === 'string') {
1717
const params = new URLSearchParams(metadata);
1818
const projectID = params.get('projectID');
1919
const documentID = params.get('documentID');
2020
const major = params.get('major');
2121
const minor = params.get('minor');
22-
if (projectID && documentID && major && minor && authState?.token) {
22+
if (projectID && documentID && major && minor) {
2323
const iframeUrl = buildUrl(
2424
`/app/projects/${projectID}/diagrams/${documentID}/version/v.${major}.${minor}/edit`,
2525
authState.token
@@ -31,6 +31,7 @@ const EditDialog = () => {
3131
console.log(error);
3232
}
3333
};
34+
3435
getMetadata();
3536
}, [authState]);
3637

@@ -84,4 +85,4 @@ const EditDialog = () => {
8485
);
8586
};
8687

87-
export default EditDialog;
88+
export default EditDiagramDialog;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import ReactDOM from 'react-dom';
2+
import EditDiagramDialog from './components/edit-diagram-dialog';
3+
4+
import './styles.css';
5+
6+
ReactDOM.render(<EditDiagramDialog />, document.getElementById('index'));

src/client/edit-dialog/index.jsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/client/hooks/useAuth.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import { useCallback, useEffect, useState } from 'react';
22
import { serverFunctions } from '../utils/serverFunctions';
33

4-
interface AuthState {
5-
authorized: boolean;
6-
token?: string;
7-
message?: string;
4+
type Status = 'idle' | 'loading' | 'success' | 'error';
5+
6+
interface AuthorizedState {
7+
authorized: true;
8+
token: string;
89
}
910

10-
type Status = 'idle' | 'loading' | 'success' | 'error';
11+
interface UnauthorizedState {
12+
authorized: false;
13+
}
14+
15+
type AuthState = AuthorizedState | UnauthorizedState;
1116

1217
const useAuth = () => {
1318
const [authState, setAuthState] = useState<null | AuthState>(null);
@@ -17,7 +22,7 @@ const useAuth = () => {
1722
setAuthStatus('loading');
1823
try {
1924
const state = await serverFunctions.getAuthorizationState();
20-
setAuthState(state);
25+
setAuthState(state as AuthState);
2126
setAuthStatus('success');
2227
} catch (error) {
2328
console.log('Error getting auth data', error);
@@ -30,11 +35,15 @@ const useAuth = () => {
3035
}, [getAuth]);
3136

3237
const signOut = async () => {
33-
serverFunctions.resetOAuth();
34-
setTimeout(getAuth, 2000);
38+
try {
39+
await serverFunctions.resetOAuth();
40+
setTimeout(getAuth, 500);
41+
} catch (error) {
42+
console.error('Error revoking OAuth:', error);
43+
}
3544
};
3645

37-
return { authState, authStatus, signOut };
46+
return { authState, authStatus, getAuth, signOut };
3847
};
3948

4049
export default useAuth;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { useEffect, useState } from 'react';
2+
import { buildUrl, handleDialogClose } from '../../utils/helpers';
3+
import { serverFunctions } from '../../utils/serverFunctions';
4+
import useAuth from '../../hooks/useAuth';
5+
import { CircularProgress, Container, Typography } from '@mui/material';
6+
7+
const SelectDiagramDialog = () => {
8+
const { authState, authStatus } = useAuth();
9+
const [diagramsUrl, setDiagramsUrl] = useState('');
10+
11+
useEffect(() => {
12+
if (!authState?.authorized) return;
13+
// const url = buildUrl('/app/plugins/confluence/select', state.token);
14+
const url = buildUrl(
15+
'/app/plugins/select?pluginSource=googledocs',
16+
authState.token
17+
);
18+
setDiagramsUrl(url);
19+
}, [authState]);
20+
21+
useEffect(() => {
22+
const handleMessage = async (e: MessageEvent) => {
23+
const action = e.data.action;
24+
if (action === 'save') {
25+
const data = e.data.data;
26+
const metadata = new URLSearchParams({
27+
projectID: data.projectID,
28+
documentID: data.documentID,
29+
major: data.major,
30+
minor: data.minor,
31+
});
32+
33+
try {
34+
await serverFunctions.insertBase64ImageWithMetadata(
35+
data.diagramImage,
36+
metadata.toString()
37+
);
38+
handleDialogClose();
39+
} catch (error) {
40+
console.error('Error inserting image with metadata', error);
41+
}
42+
}
43+
};
44+
45+
window.addEventListener('message', handleMessage);
46+
47+
return () => {
48+
window.removeEventListener('message', handleMessage);
49+
};
50+
}, []);
51+
52+
if (authStatus === 'idle' || authStatus === 'loading') {
53+
return (
54+
<Container
55+
sx={{
56+
display: 'flex',
57+
flexDirection: 'column',
58+
alignItems: 'center',
59+
justifyContent: 'center',
60+
height: '96.5vh',
61+
}}
62+
>
63+
<CircularProgress />
64+
</Container>
65+
);
66+
}
67+
68+
if (authStatus === 'error') {
69+
return (
70+
<Container
71+
sx={{
72+
display: 'flex',
73+
flexDirection: 'column',
74+
alignItems: 'center',
75+
justifyContent: 'center',
76+
height: '96.5vh',
77+
}}
78+
>
79+
<Typography variant="h5" gutterBottom my={2} textAlign="center">
80+
Error
81+
</Typography>
82+
<Typography paragraph textAlign="center">
83+
Something went wrong. Please try again later.
84+
</Typography>
85+
</Container>
86+
);
87+
}
88+
89+
return (
90+
<div style={{ padding: '3px', overflowX: 'hidden', height: '100%' }}>
91+
<iframe
92+
src={diagramsUrl}
93+
title="diagrams"
94+
style={{
95+
border: 'none',
96+
width: '100%',
97+
height: '96.5vh',
98+
}}
99+
/>
100+
</div>
101+
);
102+
};
103+
104+
export default SelectDiagramDialog;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<base target="_top" />
5+
<!-- Add any external scripts and stylesheets here -->
6+
<script
7+
crossorigin
8+
src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"
9+
></script>
10+
<script
11+
crossorigin
12+
src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"
13+
></script>
14+
<script
15+
crossorigin
16+
src="https://unpkg.com/@mui/material@5.11.11/umd/material-ui.production.min.js"
17+
></script>
18+
<script
19+
crossorigin
20+
src="https://unpkg.com/gas-client@1.1.1/dist/index.js"
21+
></script>
22+
<script
23+
crossorigin
24+
src="https://unpkg.com/@types/react@18.2.66/index.d.ts"
25+
></script>
26+
</head>
27+
<body>
28+
<section id="index">
29+
<script type="module" src="./index.jsx"></script>
30+
<!-- bundled js and css will get inlined here during build-->
31+
</section>
32+
</body>
33+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ReactDOM from 'react-dom';
2+
import SelectDiagramDialog from './components/select-diagram-dialog';
3+
import './styles.css';
4+
5+
ReactDOM.render(<SelectDiagramDialog />, document.getElementById('index'));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* needed to make consistent test snapshots across OSs */
2+
body {
3+
font-family: Arial !important;
4+
}

0 commit comments

Comments
 (0)