|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Card, |
| 4 | + CircularProgress, |
| 5 | + Stack, |
| 6 | + Typography, |
| 7 | +} from "@mui/material"; |
| 8 | +import VisuallyHiddenInput from "./VisuallyHiddenInput"; |
| 9 | +import { useMutation } from "@tanstack/react-query"; |
| 10 | +import { postConvertCrystal, postConvertMolecule } from "../queryfunctions"; |
| 11 | +import { CrystalInput, MoleculeInput } from "../models"; |
| 12 | +import { useState } from "react"; |
| 13 | +import CheckCircleIcon from "@mui/icons-material/CheckCircle"; |
| 14 | +import ErrorIcon from "@mui/icons-material/Error"; |
| 15 | +import GrainIcon from "./icons/GrainIcon"; |
| 16 | +import MoleculeIcon from "./icons/MoleculeIcon"; |
| 17 | + |
| 18 | +function getConvertIcon(state: string, molecule: boolean) { |
| 19 | + if (state == "ok") { |
| 20 | + return <CheckCircleIcon />; |
| 21 | + } else if (state == "failed") { |
| 22 | + return <ErrorIcon />; |
| 23 | + } else if (state == "running") { |
| 24 | + return <CircularProgress size="1em" />; |
| 25 | + } |
| 26 | + |
| 27 | + return molecule ? <MoleculeIcon /> : <GrainIcon />; |
| 28 | +} |
| 29 | + |
| 30 | +export default function ConvertFromCif(props: { |
| 31 | + isFractional: boolean; |
| 32 | + setStructure: (moleculeInput: MoleculeInput | CrystalInput | null) => void; |
| 33 | +}) { |
| 34 | + const [textFile, setTextFile] = useState<string | null>(null); |
| 35 | + const [filename, setFileName] = useState<string | null>(null); |
| 36 | + |
| 37 | + const [convertState, setConvertState] = useState< |
| 38 | + "default" | "ok" | "failed" | "running" |
| 39 | + >("default"); |
| 40 | + |
| 41 | + const mutation = useMutation({ |
| 42 | + mutationFn: props.isFractional ? postConvertCrystal : postConvertMolecule, |
| 43 | + onSuccess: (data) => { |
| 44 | + props.setStructure(data); |
| 45 | + setConvertState("ok"); |
| 46 | + setTimeout(() => setConvertState("default"), 2000); |
| 47 | + }, |
| 48 | + onError: () => { |
| 49 | + setConvertState("failed"); |
| 50 | + setTimeout(() => setConvertState("default"), 2000); |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + // mutation.data |
| 55 | + const handleFile = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 56 | + if (!event.target.files) return; |
| 57 | + const file = event.target.files[0]; |
| 58 | + setFileName(file.name); |
| 59 | + |
| 60 | + if (file.name.endsWith(".cif")) { |
| 61 | + const reader = new FileReader(); |
| 62 | + reader.onload = function () { |
| 63 | + const content = reader.result as string; |
| 64 | + |
| 65 | + setTextFile(content); |
| 66 | + }; |
| 67 | + reader.readAsText(file); // Read the file as text |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + const title = props.isFractional |
| 72 | + ? "Convert from Crystal Cif File" |
| 73 | + : "Convert from Molecular Crystal Cif File"; |
| 74 | + |
| 75 | + return ( |
| 76 | + <Card> |
| 77 | + <Stack> |
| 78 | + <Typography>{title}</Typography> |
| 79 | + <Stack direction="row" spacing="5px" margin="5px"> |
| 80 | + <Button |
| 81 | + variant="contained" |
| 82 | + type="submit" |
| 83 | + role={undefined} |
| 84 | + tabIndex={-1} |
| 85 | + component="label" |
| 86 | + > |
| 87 | + Upload |
| 88 | + <VisuallyHiddenInput |
| 89 | + type="file" |
| 90 | + name="file1" |
| 91 | + onChange={handleFile} |
| 92 | + /> |
| 93 | + </Button> |
| 94 | + <Button |
| 95 | + variant="outlined" |
| 96 | + disabled={textFile == null} |
| 97 | + onClick={() => { |
| 98 | + if (textFile != null) { |
| 99 | + setConvertState("running"); |
| 100 | + mutation.mutate(textFile); |
| 101 | + } |
| 102 | + }} |
| 103 | + endIcon={getConvertIcon(convertState, !props.isFractional)} |
| 104 | + > |
| 105 | + Run Convert |
| 106 | + </Button> |
| 107 | + <Typography>{filename ? filename : "No file"}</Typography> |
| 108 | + </Stack> |
| 109 | + </Stack> |
| 110 | + </Card> |
| 111 | + ); |
| 112 | +} |
0 commit comments