Files
gas-react-sheets/dev/index.js
T
Elisha Nuchi fd73dc051c Webpack 5 support with updated packages (#131)
- Update to Webpack 5
- Use @effortlessmotion dynamic html and inline source plugins
- Remove .clasp.json and use SAMPLE file
- Consistent prettier formatting throughout
- Update packages to latest versions

Co-authored-by: Kevin Malakoff <kmalakoff@gmail.com>
2022-06-20 01:59:26 -04:00

62 lines
1.5 KiB
JavaScript

import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';
import { serverFunctions } from '../src/client/utils/serverFunctions.ts';
const { FILENAME, PORT } = process.env;
const DevServer = () => {
const iframe = React.useRef(null);
useEffect(() => {
const handleRequest = (event) => {
const request = event.data;
const { type, functionName, id, args } = request;
if (type !== 'REQUEST') return;
serverFunctions[functionName](...args)
.then((response) => {
iframe.current.contentWindow.postMessage(
{ type: 'RESPONSE', id, status: 'SUCCESS', response },
`https://localhost:${PORT}`
);
})
.catch((err) => {
iframe.current.contentWindow.postMessage(
{
type: 'RESPONSE',
id,
status: 'ERROR',
response: err,
},
`https://localhost:${PORT}`
);
});
};
window.addEventListener('message', handleRequest, false);
}, []);
return (
<div
// we want our dev environment to fill the dialog window
style={{
width: '100%',
height: '100%',
}}
>
<iframe
style={{
width: '100%',
height: '100%',
border: '0',
position: 'absolute',
}}
ref={iframe}
src={`https://localhost:${PORT}/${FILENAME}-impl.html`}
/>
</div>
);
};
ReactDOM.render(<DevServer />, document.getElementById('index'));