mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-13 03:13:47 +00:00
8c740b7111
* add dependency adding dependency to parse the excel file in the node side * import file ability to import file - add extension - safeguard file type and size * user is able to upload json or excel * chore: extract logic into self contained function * associate model with version * test id generation * parse excel data * prevent UI crash on bad data * extract validation into self contained function * chore: add more tests for bad data * config: stop from opening browser on start * fix comma in sample db * handle corrupt files * error boundary around main components * jsdocs * increase id length * parse excel time string * feat: excel parsing * validate json db before import * chore: test event validator * feat: make function to clean convert strings * chore: test parser * chore: jsdocs * fix: bug on upload prevent bug where the component would prevent upload of same file twice
139 lines
3.6 KiB
React
139 lines
3.6 KiB
React
import { useMutation, useQueryClient } from 'react-query';
|
|
import { downloadEvents, uploadEvents } from 'app/api/ontimeApi';
|
|
import { EVENTS_TABLE } from 'app/api/apiConstants';
|
|
import DownloadIconBtn from './buttons/DownloadIconBtn';
|
|
import SettingsIconBtn from './buttons/SettingsIconBtn';
|
|
import MaxIconBtn from './buttons/MaxIconBtn';
|
|
import MinIconBtn from './buttons/MinIconBtn';
|
|
import QuitIconBtn from './buttons/QuitIconBtn';
|
|
import style from './MenuBar.module.css';
|
|
import HelpIconBtn from './buttons/HelpIconBtn';
|
|
import UploadIconBtn from './buttons/UploadIconBtn';
|
|
import { useRef } from 'react';
|
|
|
|
export default function MenuBar(props) {
|
|
const { onOpen } = props;
|
|
const hiddenFileInput = useRef(null);
|
|
const queryClient = useQueryClient();
|
|
const uploaddb = useMutation(uploadEvents, {
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries(EVENTS_TABLE);
|
|
},
|
|
});
|
|
|
|
const handleDownload = () => {
|
|
downloadEvents();
|
|
};
|
|
|
|
const handleClick = () => {
|
|
if (hiddenFileInput && hiddenFileInput.current) {
|
|
hiddenFileInput.current.click();
|
|
}
|
|
};
|
|
|
|
const handleUpload = (event) => {
|
|
const fileUploaded = event.target.files[0];
|
|
if (fileUploaded == null) return;
|
|
console.log(fileUploaded);
|
|
|
|
// Limit file size to 1MB
|
|
if (fileUploaded.size > 1000000) {
|
|
console.log('Error: File size limit (1MB) exceeded');
|
|
return;
|
|
}
|
|
|
|
// Check file extension
|
|
if (fileUploaded.name.endsWith('.xlsx')) {
|
|
console.log('excel file');
|
|
} else if (fileUploaded.name.endsWith('.json')) {
|
|
console.log('json file');
|
|
} else {
|
|
console.log('Error: File type unknown');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
uploaddb.mutate(fileUploaded);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
|
|
// reset input value
|
|
hiddenFileInput.current.value = '';
|
|
};
|
|
|
|
const handleIPC = (action) => {
|
|
// Stop crashes when testing locally
|
|
if (window.process?.type === undefined) {
|
|
if (action === 'help') {
|
|
window.open('https://cpvalente.gitbook.io/ontime/');
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (window.process.type === 'renderer') {
|
|
switch (action) {
|
|
case 'min':
|
|
window.ipcRenderer.send('set-window', 'to-tray');
|
|
break;
|
|
case 'max':
|
|
window.ipcRenderer.send('set-window', 'to-max');
|
|
break;
|
|
case 'shutdown':
|
|
window.ipcRenderer.send('shutdown', 'now');
|
|
break;
|
|
case 'help':
|
|
window.ipcRenderer.send('send-to-link', 'help');
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<QuitIconBtn size='lg' clickhandler={() => handleIPC('shutdown')} />
|
|
<MaxIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={() => handleIPC('max')}
|
|
/>
|
|
<MinIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={() => handleIPC('min')}
|
|
/>
|
|
<div className={style.gap} />
|
|
<HelpIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={() => handleIPC('help')}
|
|
/>
|
|
<SettingsIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={onOpen}
|
|
/>
|
|
<div className={style.gap} />
|
|
<input
|
|
type='file'
|
|
style={{ display: 'none' }}
|
|
ref={hiddenFileInput}
|
|
onChange={handleUpload}
|
|
accept='.json, .xlsx'
|
|
/>
|
|
<UploadIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={handleClick}
|
|
/>
|
|
<DownloadIconBtn
|
|
style={{ fontSize: '1.5em' }}
|
|
size='lg'
|
|
clickhandler={handleDownload}
|
|
/>
|
|
</>
|
|
);
|
|
}
|