All files / src/pages/flowsheet-page/multi-steady-state ConnectToExcelCommand.tsx

30% Statements 6/20
40% Branches 2/5
0% Functions 0/1
31.57% Lines 6/19

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 76 77 78 79 80                        62x                               7128x 7128x 7128x     6965x     163x                                                                                      
import { defineCommand } from "just-search-it";
import { Plug } from "lucide-react";
import { useState } from "react";
import {
  Dialog,
  DialogContent,
  DialogTrigger,
} from "@/ahuora-design-system/ui/dialog";
import { resolveApiUrl } from "@/api/apiBaseUrl";
import { RegisterCommand } from "@/commands/CommandProvider";
import { useProjectId } from "@/hooks/project";
import { useSearchParam } from "@/hooks/searchParams";
export const ConnectToExcel = defineCommand<[], void>("connectToExcel");
 
async function downloadData(flowsheet_id: number, scenario_id: number) {
  const url = new URL(resolveApiUrl("/api/mss/download_excel_connection"));
  url.searchParams.set("flowsheet", flowsheet_id.toString());
  url.searchParams.set("scenario", scenario_id.toString());
 
  const link = document.createElement("a");
  link.href = url.toString();
  link.download = "excel_connection.odc";
  document.body.appendChild(link);
  link.click();
  link.remove();
}
 
export function ConnectToExcelCommand() {
  const [scenario_id] = useSearchParam("scenario");
  const flowsheet_id = useProjectId();
  const [showPopover, setShowPopover] = useState(false);
 
  if (!flowsheet_id || !scenario_id) {
    return null;
  }
 
  return (
    <>
      <RegisterCommand
        command={ConnectToExcel}
        args={[]}
        name="Connect To Excel"
        description="Downloads an excel connection file"
        group="Export"
        icon={<Plug />}
        action={async () => {
          Iif (!flowsheet_id || !scenario_id) return;
          await downloadData(flowsheet_id, Number(scenario_id));
          window.setTimeout(() => {
            setShowPopover(true);
          }, 500);
        }}
      />
      <Dialog open={showPopover} onOpenChange={setShowPopover}>
        <DialogTrigger className="hidden" />
        <DialogContent className="max-w-[1200] w-[800]">
          <div>
            <p>
              Open the downloaded <strong>excel_connection.odc</strong> file in
              Excel to connect to the flowsheet data.
            </p>
          </div>
          <img
            src="/assets/tutorials/excel-data-integration.png"
            className="w-full"
          />
          <p>
            Click on Cell A1, and use "Refresh" to update all columns in your
            Multi-Steady-State scenario.
          </p>
          <p>
            Note: This feature uses a delegated Excel token with limited access
            to MSS download resources.
          </p>
        </DialogContent>
      </Dialog>
    </>
  );
}