Files
gas-react-sheets/dev/index.js
T
Elisha Nuchi 531a952df3 Support react-refresh and shared client/server types (#98)
- Adds support for react-refresh through `@pmmwh/react-refresh-webpack-plugin`. Updated webpack config to support this. Means entire app is no longer refreshed on changes, only individual components, and state is maintained.
- React refresh removes need for `google-apps-script-webpack-dev-server` package
- In client code, support for server function autocomplete through use of gas-client 1.0.0. Requires certain files to be typescript .ts files
- Remove need for "global" exports in main server file through updated gas-webpack-plugin
2022-03-14 12:31:52 -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'));