2023-04-18 05:21:16 +02:00
|
|
|
import {Box, Button, Chip, Dialog, DialogContent, DialogTitle, Grid, IconButton, Input, Tab, Tabs} from "@mui/material";
|
2023-04-18 03:41:36 +02:00
|
|
|
import {driverNames} from "../../libs/driverNames";
|
2023-04-18 05:21:16 +02:00
|
|
|
import {Add, Edit} from "@mui/icons-material";
|
|
|
|
import {useState} from "react";
|
2023-04-18 04:51:08 +02:00
|
|
|
import {Calculator} from "./Calculator";
|
|
|
|
import {PresetSnapshot} from "../../consts/presets";
|
2023-04-18 09:23:55 +02:00
|
|
|
import {useDispatch, useSelector} from "react-redux";
|
|
|
|
import {addSlot, removeSlot, renameSlot} from "../../libs/reducers/configReducer";
|
2023-04-18 03:41:36 +02:00
|
|
|
|
2023-04-18 09:23:55 +02:00
|
|
|
export function TabManager() {
|
|
|
|
const config = useSelector(state => state.config)
|
|
|
|
const { slots } = config;
|
|
|
|
const dispatch = useDispatch()
|
2023-04-18 04:28:34 +02:00
|
|
|
const [tab, setTab] = useState(0);
|
2023-04-18 03:41:36 +02:00
|
|
|
const [editText, setEditText] = useState("");
|
2023-04-18 05:21:16 +02:00
|
|
|
const [openRenameSlot, setOpenRenameSlot] = useState(null);
|
2023-04-18 03:41:36 +02:00
|
|
|
|
2023-04-18 05:21:16 +02:00
|
|
|
const saveSlotEdit = () => {
|
2023-04-18 09:23:55 +02:00
|
|
|
dispatch(renameSlot({ id: openRenameSlot.id, slotTitle: editText }))
|
2023-04-18 05:21:16 +02:00
|
|
|
setOpenRenameSlot(null);
|
|
|
|
}
|
|
|
|
|
2023-04-18 03:41:36 +02:00
|
|
|
return (
|
2023-04-18 04:51:08 +02:00
|
|
|
<div>
|
|
|
|
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
|
2023-04-18 05:21:16 +02:00
|
|
|
{
|
|
|
|
openRenameSlot !== null && (
|
|
|
|
<Dialog
|
|
|
|
open
|
|
|
|
onClose={saveSlotEdit}
|
|
|
|
aria-labelledby="alert-dialog-title"
|
|
|
|
aria-describedby="alert-dialog-description"
|
|
|
|
>
|
|
|
|
<DialogTitle id="alert-dialog-title">Renaming Slot {openRenameSlot.id}: <b>{openRenameSlot.slotTitle}</b></DialogTitle>
|
|
|
|
<DialogContent>
|
|
|
|
<div>
|
|
|
|
<Input value={editText} onChange={e => setEditText(e.target.value)} sx={{ width: "100%" }} />
|
|
|
|
</div>
|
|
|
|
<div style={{ marginTop: 20 }}>
|
|
|
|
<Grid container spacing={1}>
|
|
|
|
{
|
|
|
|
driverNames.map(
|
|
|
|
d => <Grid item key={d}><Chip label={d} onClick={() => setEditText(d)} /></Grid>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Grid>
|
|
|
|
</div>
|
|
|
|
<div style={{ marginTop: 20, textAlign: "right" }}>
|
2023-04-18 16:44:57 +02:00
|
|
|
{
|
|
|
|
slots.length > 1 && (
|
|
|
|
<Button sx={{m: 1}} variant="contained" color="error" onClick={
|
|
|
|
() => {
|
|
|
|
setTab(tab > 0 ? tab - 1 : 0);
|
|
|
|
dispatch(removeSlot({id: openRenameSlot.id}));
|
|
|
|
setOpenRenameSlot(null);
|
|
|
|
}
|
|
|
|
}>Delete this Slot</Button>
|
|
|
|
)
|
|
|
|
}
|
2023-04-18 05:21:16 +02:00
|
|
|
<Button sx={{m: 1}} variant="contained" color="primary" onClick={saveSlotEdit}>Save Changes</Button>
|
|
|
|
</div>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)
|
|
|
|
}
|
2023-04-18 04:51:08 +02:00
|
|
|
<Tabs
|
|
|
|
value={tab}
|
|
|
|
onChange={(_, f) => {
|
|
|
|
setTab(f)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{
|
|
|
|
slots.map(
|
|
|
|
(s, _idx) => <Tab label={
|
|
|
|
<div>{s.slotTitle}
|
|
|
|
<IconButton size="small" sx={{ ml: 1, padding: 0 }} onClick={() => {
|
|
|
|
setEditText(s.slotTitle);
|
2023-04-18 05:21:16 +02:00
|
|
|
setOpenRenameSlot(s);
|
2023-04-18 04:51:08 +02:00
|
|
|
}}><Edit /></IconButton>
|
|
|
|
</div>
|
|
|
|
} value={_idx} key={_idx}/>
|
|
|
|
)
|
|
|
|
}
|
2023-04-18 09:23:55 +02:00
|
|
|
<Button size="small" sx={{ padding: 0, float: "right" }} onClick={() => dispatch(addSlot())}><Add /></Button>
|
2023-04-18 04:51:08 +02:00
|
|
|
</Tabs>
|
|
|
|
</Box>
|
|
|
|
{
|
2023-04-18 11:00:08 +02:00
|
|
|
config.slots.length > 0 && (
|
2023-04-18 04:51:08 +02:00
|
|
|
<Calculator
|
2023-04-18 11:00:08 +02:00
|
|
|
key={tab}
|
|
|
|
slot={config.slots[tab]}
|
|
|
|
target={config.slots[tab].slotNaming}
|
2023-04-18 04:51:08 +02:00
|
|
|
preset={PresetSnapshot}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</div>
|
2023-04-18 03:41:36 +02:00
|
|
|
);
|
|
|
|
}
|