All files / src/pages/flowsheet-page/export ExportIDAES.tsx

0% Statements 0/19
0% Branches 0/4
0% Functions 0/6
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75                                                                                                                                                     
import { Badge } from "@/ahuora-design-system/ui/badge";
import { Button } from "@/ahuora-design-system/ui/button";
import { Textarea } from "@/ahuora-design-system/ui/textarea";
import { useIdaesGeneratePythonCodeCreateMutation } from "@/api/apiStore.gen";
import { useProject, useProjectId } from "@/hooks/project";
import { useState } from "react";
 
export default function GenerateIDAESPython() {
  const [requestGenerate] = useIdaesGeneratePythonCodeCreateMutation();
  const projectName = useProject()?.name;
  const projectId = useProjectId();
  const [result, setResult] = useState<string>(""); // TODO: don't use state for this
 
  const onGenerate = (use_json: boolean) => () => {
    requestGenerate({
      generateIdaesPythonRequest: {
        flowsheet_id: projectId,
        return_json: use_json,
      },
    })
      .unwrap()
      .then((response) => {
        setResult(response);
      })
      .catch((error) => {
        setResult("An error occurred: " + error);
      });
  };
 
  const onDownload = () => {
    const element = document.createElement("a");
    const file = new Blob([result], { type: "text/plain" });
    element.href = URL.createObjectURL(file);
    const name = projectName + "_idaes_model.py";
    element.download = name;
    document.body.appendChild(element); // Required for this to work in FireFox
    element.click();
    document.body.removeChild(element);
  };
 
  return (
    <div className="flex flex-col h-full w-full p-4 gap-4">
      <div className="flex flex-col gap-1">
        <div className="flex flex-row items-center gap-2">
          <p className="text-xl font-bold">Export to IDAES</p>
          <Badge variant="outline" className="rounded-full">
            Experimental
          </Badge>
        </div>
        <p className="text-sm">Export the project to IDAES Python code.</p>
        <p className="text-xs text-muted-foreground">
          This feature is experimental and may not work as expected.
          <br />- Modular property packages are not supported yet.
        </p>
      </div>
      <div className="flex flex-row gap-2 flex-wrap">
        <Button onClick={onGenerate(false)}>Generate IDAES Model</Button>
        <Button variant="outline" onClick={onGenerate(true)}>Generate JSON Model</Button>
        {result && (
          <Button variant="secondary" onClick={onDownload}>
            Download file
          </Button>
        )}
 
      </div>
      {result && (
        <div className="h-80 flex flex-col gap-2">
          <p className="text-sm">Generated Python code:</p>
          <Textarea className="h-full resize-none" value={result} readOnly />
        </div>
      )}
    </div>
  );
}