Files
gas-react-sheets/src/client/dialog-demo-mui/components/SheetEditor.jsx
T
Elisha Nuchi 31befe1d68 V3: Migrate to Vite (#221)
This is version 3 of the boilerplate, which makes significant changes to the build script and local development tooling. It replaces Webpack with Vite, which required some significant changes to how the local development setup works with Google Apps Script.

A new dev-server-wrapper.html file is used as the wrapper app in development, which is a standalone html + js file instead of a React app. This reduces the need for a complex build step for the wrapper app. Instead the file is just copied over for each entrypoing and the file location is changed with a "replace" step.

Due to the way Vite works, the html templates are changed a bit to support local development.

Vite doesn't easily allow completely separate multiple builds, so some Vite plugins and custom plugins are written to support multiple entrypoints (sidebars/dialogs). The externalization for large packages is now handled in the Vite config instead of a plugin, and by manually adding in the script tags into the index.html templates.

Additional changes here:
- yarn is now used instead of npm as package manager
- eslint configs are updated to support vite
- GitHub workflows for tests are updated to use yarn and other minor changes such as node versions and OS.
- A VS Code launch.json configuration is provided
- Dev wrapper setup has been updated (see above description)
- ES Modules (import/export) updated throughout
- Unneeded packages supporting webpack configurations are removed. Many packages have been upgraded. NPM scripts have been updated to use yarn.
- Removed `import React` due to Vite config.
- Added script tags to each template since webpack externalization plugin is no longer used
- Added <script type="module" src="./index.jsx"></script> to index.html templates to support Vite local development
- Changed src/server/sheets.js to .ts typescript as exemplar
- Updated tests to support yarn commands, Vite changes, and listening for Vite stdout triggers.
- Add Vite-style tsconfig.json
- README is updated
2024-05-20 00:25:31 -04:00

71 lines
2.1 KiB
React
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useState, useEffect } from 'react';
import { Button, Typography } from '@mui/material';
import FormInput from './FormInput';
import SheetTable from './SheetTable';
// This is a wrapper for google.script.run that lets us use promises.
import { serverFunctions } from '../../utils/serverFunctions';
const SheetEditor = () => {
const [names, setNames] = useState([]);
useEffect(() => {
serverFunctions.getSheetsData().then(setNames).catch(alert);
}, []);
const deleteSheet = (sheetIndex) => {
serverFunctions.deleteSheet(sheetIndex).then(setNames).catch(alert);
};
const setActiveSheet = (sheetName) => {
serverFunctions.setActiveSheet(sheetName).then(setNames).catch(alert);
};
const submitNewSheet = async (newSheetName) => {
try {
const response = await serverFunctions.addSheet(newSheetName);
setNames(response);
} catch (error) {
// eslint-disable-next-line no-alert
alert(error);
}
};
return (
<div style={{ padding: '3px', overflowX: 'hidden' }}>
<Typography variant="h4" gutterBottom>
☀️ MUI demo! ☀️
</Typography>
<Typography variant="body1" gutterBottom sx={{ marginBottom: '30px' }}>
This is a sample app that uses the <code>mui</code> library
to help us build a simple React app. Enter a name for a new sheet, hit
enter and the new sheet will be created. Click the red button next to the sheet name to
delete it.
</Typography>
<FormInput submitNewSheet={submitNewSheet} />
{names.length > 0 &&
<SheetTable rows={names.map((name) => {
return {
sheetName: name.name,
goToButton: <Button
variant="outlined"
disabled={name?.isActive}
onClick={() => setActiveSheet(name.name)}
>Go To Sheet</Button>,
deleteButton: <Button
variant="contained"
color="error"
onClick={() => deleteSheet(name.index)}
>Delete</Button>
}
})} />}
</div>
);
};
export default SheetEditor;