|
1 | 1 | import { |
2 | | - Button, |
3 | | - Input, |
| 2 | + Accordion, |
| 3 | + AccordionDetails, |
| 4 | + AccordionSummary, |
| 5 | + Divider, |
4 | 6 | Modal, |
5 | 7 | ModalClose, |
6 | 8 | ModalDialog, |
7 | 9 | Typography, |
8 | 10 | } from "@mui/joy"; |
9 | | -import { listen } from "@tauri-apps/api/event"; |
10 | | -import { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
11 | | -import { invoke } from "@tauri-apps/api/core"; |
12 | | -import { Operation } from "../utilities/operations"; |
| 11 | +import { OperationState } from "../utilities/operations"; |
| 12 | +import "./OperationView.css"; |
| 13 | +import { SuccessIcon, ErrorIcon, StyledLoadingIcon } from "react-toast-plus"; |
| 14 | +import { PanoramaFishEye } from "@mui/icons-material"; |
13 | 15 |
|
14 | | -interface OperationViewProps { |
15 | | - operation_info: { operation: Operation; params: { [key: string]: any } }; |
16 | | -} |
17 | | - |
18 | | -export default ({ operation_info }: OperationViewProps) => { |
19 | | - const operation = operation_info.operation; |
20 | | - const params = operation_info.params; |
21 | | - const id = useMemo<string>(() => operation.id + "_operation", [operation]); |
22 | | - const [open, setOpen] = useState(true); |
23 | | - |
24 | | - const listenerAdded = useRef(false); |
25 | | - const unlisten = useRef<() => void>(() => {}); |
26 | | - |
27 | | - useEffect(() => { |
28 | | - if (!listenerAdded.current) { |
29 | | - (async () => { |
30 | | - const unlistenFn = await listen(id, (event) => {}); |
31 | | - unlisten.current = unlistenFn; |
32 | | - })(); |
33 | | - listenerAdded.current = true; |
34 | | - } |
35 | | - return () => { |
36 | | - unlisten.current(); |
37 | | - }; |
38 | | - }, []); |
39 | | - |
40 | | - const startOperation = useCallback(async () => { |
41 | | - invoke(id); |
42 | | - }, []); |
| 16 | +export default ({ |
| 17 | + operationState, |
| 18 | + closeMenu, |
| 19 | +}: { |
| 20 | + operationState: OperationState; |
| 21 | + closeMenu: () => void; |
| 22 | +}) => { |
| 23 | + const operation = operationState.current; |
| 24 | + const failed = operationState.failed.length > 0; |
| 25 | + const done = |
| 26 | + failed || operationState.completed.length == operation.steps.length; |
43 | 27 |
|
44 | 28 | return ( |
45 | 29 | <Modal |
46 | | - open={open} |
| 30 | + open={true} |
47 | 31 | onClose={() => { |
48 | | - setOpen(false); |
| 32 | + if (done) closeMenu(); |
49 | 33 | }} |
50 | 34 | > |
51 | 35 | <ModalDialog> |
52 | | - <ModalClose /> |
53 | | - <Typography level="h3">Can I put my balls in your jaws?</Typography> |
| 36 | + {done && <ModalClose />} |
| 37 | + <div> |
| 38 | + <Typography level="h3">{operation?.title}</Typography> |
| 39 | + <Typography level="body-lg"> |
| 40 | + {done |
| 41 | + ? failed |
| 42 | + ? "Operation failed. Please see steps for details." |
| 43 | + : "Operation completed!" |
| 44 | + : "Please wait..."} |
| 45 | + </Typography> |
| 46 | + </div> |
| 47 | + <Divider /> |
| 48 | + <div className="operation-content"> |
| 49 | + {operation.steps.map((step) => { |
| 50 | + let failed = operationState.failed.find((f) => f.stepId == step.id); |
| 51 | + let completed = operationState.completed.includes(step.id); |
| 52 | + let started = operationState.started.includes(step.id); |
| 53 | + let notStarted = !failed && !completed && !started; |
| 54 | + return ( |
| 55 | + <div className="operation-step"> |
| 56 | + <div className="operation-step-icon"> |
| 57 | + {failed && <ErrorIcon />} |
| 58 | + {!failed && completed && <SuccessIcon />} |
| 59 | + {!failed && !completed && started && <StyledLoadingIcon />} |
| 60 | + {notStarted && ( |
| 61 | + <PanoramaFishEye |
| 62 | + sx={{ |
| 63 | + width: "100%", |
| 64 | + height: "100%", |
| 65 | + color: "neutral.500", |
| 66 | + }} |
| 67 | + /> |
| 68 | + )} |
| 69 | + </div> |
| 70 | + |
| 71 | + <div className="operation-step-internal"> |
| 72 | + <Typography |
| 73 | + textColor={notStarted ? "neutral.500" : undefined} |
| 74 | + > |
| 75 | + {step.title} |
| 76 | + </Typography> |
| 77 | + {failed && ( |
| 78 | + <Accordion sx={{ marginTop: 0 }}> |
| 79 | + <AccordionSummary> |
| 80 | + <Typography level="body-sm">Show Details</Typography> |
| 81 | + </AccordionSummary> |
| 82 | + <AccordionDetails> |
| 83 | + <pre className="operation-extra-details"> |
| 84 | + {failed.extraDetails} |
| 85 | + </pre> |
| 86 | + </AccordionDetails> |
| 87 | + </Accordion> |
| 88 | + )} |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ); |
| 92 | + })} |
| 93 | + </div> |
54 | 94 | </ModalDialog> |
55 | 95 | </Modal> |
56 | 96 | ); |
|
0 commit comments