From 3eca56f8c652cfffd11e38bb5716a32a7cce0420 Mon Sep 17 00:00:00 2001
From: Felix Kaspar <ich@felixkaspar.com>
Date: Sat, 18 May 2024 00:09:46 +0200
Subject: [PATCH] first tauri build with new systems deleted old/unused code

---
 client-tauri/package.json                     |  4 +-
 client-tauri/src/App.tsx                      |  8 +--
 .../src/components/DynamicParameterFields.tsx | 63 -------------------
 .../src/components/fields/BuildFields.tsx     |  2 +-
 .../src/components/fields/StringField.tsx     | 12 ----
 client-tauri/src/pages/Dynamic.tsx            |  5 +-
 client-tauri/src/pages/convert/ToPdf.tsx      | 16 -----
 .../src/pages/page-operations/Impose.tsx      | 18 ------
 client-tauri/src/utils/pdf-operations.ts      | 17 -----
 client-tauri/tsconfig.json                    |  3 +-
 .../src/functions/common/getPagesByIndex.ts   |  2 +-
 shared-operations/src/functions/index.ts      |  3 +-
 .../src/workflow/operatorAccessor.ts          |  2 +-
 13 files changed, 17 insertions(+), 138 deletions(-)
 delete mode 100644 client-tauri/src/components/DynamicParameterFields.tsx
 delete mode 100644 client-tauri/src/components/fields/StringField.tsx
 delete mode 100644 client-tauri/src/pages/convert/ToPdf.tsx
 delete mode 100644 client-tauri/src/pages/page-operations/Impose.tsx
 delete mode 100644 client-tauri/src/utils/pdf-operations.ts

diff --git a/client-tauri/package.json b/client-tauri/package.json
index eb95ccaa3..40215a0d5 100644
--- a/client-tauri/package.json
+++ b/client-tauri/package.json
@@ -7,7 +7,9 @@
     "dev": "vite",
     "build": "tsc && vite build",
     "preview": "vite preview",
-    "tauri": "tauri"
+    "tauri-dev (currently broken)": "tauri dev",
+    "tauri-build-debug": "tauri build --debug",
+    "tauri-build": "tauri build"
   },
   "dependencies": {
     "@stirling-pdf/shared-operations": "^0.0.0",
diff --git a/client-tauri/src/App.tsx b/client-tauri/src/App.tsx
index 28d489cd9..fd458fc4c 100644
--- a/client-tauri/src/App.tsx
+++ b/client-tauri/src/App.tsx
@@ -5,8 +5,8 @@ import Home from "./pages/Home";
 import About from "./pages/About";
 import Dashboard from "./pages/Dashboard";
 import Dynamic from "./pages/Dynamic";
-import ToPdf from "./pages/convert/ToPdf";
-import Impose from "./pages/page-operations/Impose";
+// import ToPdf from "./pages/convert/ToPdf";
+// import Impose from "./pages/page-operations/Impose";
 import NoMatch from "./pages/NoMatch";
 import NavBar from "./components/NavBar";
 
@@ -53,11 +53,11 @@ export default function App() {
                     <Route path="*" element={<NoMatch />} />
                 </Route>
                 <Route path="/convert" element={<Layout />}>
-                    <Route path="file-to-pdf" element={<ToPdf />} />
+                    {/* <Route path="file-to-pdf" element={<ToPdf />} /> */}
                     <Route path="*" element={<NoMatch />} />
                 </Route>
                 <Route path="/page-operations" element={<Layout />}>
-                    <Route path="impose" element={<Impose />} />
+                    {/* <Route path="impose" element={<Impose />} /> */}
                     <Route path="*" element={<NoMatch />} />
                 </Route>
             </Routes>
diff --git a/client-tauri/src/components/DynamicParameterFields.tsx b/client-tauri/src/components/DynamicParameterFields.tsx
deleted file mode 100644
index 97437508b..000000000
--- a/client-tauri/src/components/DynamicParameterFields.tsx
+++ /dev/null
@@ -1,63 +0,0 @@
-
-import Form from "react-bootstrap/Form";
-import { useTranslation } from "react-i18next";
-import { FieldConstraint, RecordConstraint } from "@stirling-pdf/shared-operations/src/dynamic-ui/OperatorConstraints";
-
-interface DynamicParameterFieldsProps {
-    constraints: RecordConstraint;
-    parentKeyPath?: string[];
-}
-
-const DynamicParameterFields: React.FC<DynamicParameterFieldsProps> = ({constraints, parentKeyPath=["DPF"]}) => {
-    const { t } = useTranslation();
-
-    return (<>
-        {Object.entries(constraints.record).map(([fieldName, value]) => {
-            console.log(fieldName, value);
-            const globallyUniqueId = joinKeyPath([...parentKeyPath, fieldName]);
-            return <div className='mb-3' key={fieldName} >
-                <label htmlFor={globallyUniqueId}>{t(value.displayNameKey)}</label>
-                {fieldConstraintToElement(fieldName, parentKeyPath, globallyUniqueId, value)}
-            </div>;
-        })}
-    </>);
-};
-
-function joinKeyPath(keyPath: string[]) {
-    return keyPath.join(".");
-}
-
-function fieldConstraintToElement(fieldName: string, parentKeyPath: string[], globallyUniqueId: string, fieldConstraint: FieldConstraint) {
-    if (Array.isArray(fieldConstraint.type)) {
-        if (fieldConstraint.type.every(e => typeof e == "string" || typeof e == "number")) {
-            return (
-                <Form.Select id={globallyUniqueId} name={fieldName}>
-                    <option value="" disabled>Select an option</option>
-                    {fieldConstraint.type.map((option) => <option key={option} value={option}>{option}</option> )}
-                </Form.Select>
-            );
-        } else {
-            return <div key={fieldName}>Error: Field type '{fieldConstraint.type}' not supported</div>;
-        }
-    } else if (typeof fieldConstraint.type == "string") {
-        switch (fieldConstraint.type) {
-        case "file.pdf":
-            return <input id={globallyUniqueId} type="file" name={fieldName} required={fieldConstraint.required} className="form-control required" accept="application/pdf" multiple={false}/>;
-        case "files.pdf":
-            return <input id={globallyUniqueId} type="file" name={fieldName} required={fieldConstraint.required} className="form-control required" accept="application/pdf" multiple={true}/>;
-        case "string":
-            return <input id={globallyUniqueId} type="text" name={fieldName} required={fieldConstraint.required} />;
-        case "number":
-            return <input id={globallyUniqueId} type="number" name={fieldName} required={fieldConstraint.required} />;
-        default:
-            return <div key={fieldName}>Error: Field type '{fieldConstraint.type}' not supported</div>;
-        }
-    } else if (fieldConstraint.type instanceof RecordConstraint) {
-        //return <DynamicParameterFields constraints={fieldConstraint.type} parentKeyPath={[...parentKeyPath, fieldName]}/>
-        return <div key={fieldName}>Error: Field type 'RecordConstraint' not supported yet!</div>;
-    }
-
-    return <div key={fieldName}>Error: Field type '{fieldConstraint.type}' not supported</div>;
-}
-
-export default DynamicParameterFields;
\ No newline at end of file
diff --git a/client-tauri/src/components/fields/BuildFields.tsx b/client-tauri/src/components/fields/BuildFields.tsx
index e07bc8840..c85b1a09a 100644
--- a/client-tauri/src/components/fields/BuildFields.tsx
+++ b/client-tauri/src/components/fields/BuildFields.tsx
@@ -20,7 +20,7 @@ export function BuildFields({ schemaDescription, onSubmit }: BuildFieldsProps) {
             <hr />
             <form onSubmit={(e) => { onSubmit(e); e.preventDefault(); }}>
             {
-                values ? Object.keys(values).map((key, i) => {  
+                values ? Object.keys(values).map((key) => {  
                     return (<GenericField key={key} fieldName={key} joiDefinition={values[key]} />) 
                 }) : undefined
             }
diff --git a/client-tauri/src/components/fields/StringField.tsx b/client-tauri/src/components/fields/StringField.tsx
deleted file mode 100644
index 532092dbc..000000000
--- a/client-tauri/src/components/fields/StringField.tsx
+++ /dev/null
@@ -1,12 +0,0 @@
-interface StringFieldProps {
-    /** The text to display inside the button */
-    validValues: string[];
-    exampleValues: string;
-}
-  
-
-export function StringField({ validValues, exampleValues }: StringFieldProps) {
-    return (
-      <button>{validValues}</button>
-    );
-}
\ No newline at end of file
diff --git a/client-tauri/src/pages/Dynamic.tsx b/client-tauri/src/pages/Dynamic.tsx
index 182931c66..fad5f39f1 100644
--- a/client-tauri/src/pages/Dynamic.tsx
+++ b/client-tauri/src/pages/Dynamic.tsx
@@ -1,6 +1,6 @@
 import { Link } from "react-router-dom";
 
-import { BaseSyntheticEvent, createContext, useRef, useState } from "react";
+import { BaseSyntheticEvent, useRef, useState } from "react";
 import { Operator } from "@stirling-pdf/shared-operations/src/functions";
 import i18next from "i18next";
 import Joi from "@stirling-tools/joi";
@@ -26,6 +26,7 @@ function Dynamic() {
         console.log("Loading namespaces for", selectedValue);
         await i18next.loadNamespaces(selectedValue, (err, t) => {
             if (err) throw err;
+            console.log(t);
         });
         console.log("Loading namespaces done");
 
@@ -110,7 +111,7 @@ function Dynamic() {
             <br />
             <select id="pdfOptions" onChange={selectionChanged}>
                 <option value="none">none</option>
-                { operators.map((operator, i) => {
+                { operators.map((operator) => {
                     return (<option key={operator} value={operator}>{operator}</option>)
                 }) }
             </select>
diff --git a/client-tauri/src/pages/convert/ToPdf.tsx b/client-tauri/src/pages/convert/ToPdf.tsx
deleted file mode 100644
index e193060de..000000000
--- a/client-tauri/src/pages/convert/ToPdf.tsx
+++ /dev/null
@@ -1,16 +0,0 @@
-
-import { isLibreOfficeInstalled } from "../../utils/libre-office-utils";
-
-const hasLibreOffice = await isLibreOfficeInstalled();
-console.log(hasLibreOffice);
-
-function About() {
-    return (
-        <div>
-            <h2>Convert to PDF</h2>
-            {"hasLibreOffice: "+hasLibreOffice}
-        </div>
-    );
-}
-
-export default About;
diff --git a/client-tauri/src/pages/page-operations/Impose.tsx b/client-tauri/src/pages/page-operations/Impose.tsx
deleted file mode 100644
index 9120faeb6..000000000
--- a/client-tauri/src/pages/page-operations/Impose.tsx
+++ /dev/null
@@ -1,18 +0,0 @@
-
-import DynamicParameterFields from "../../components/DynamicParameterFields";
-// import { ImposeParamConstraints } from "@stirling-pdf/shared-operations/src/functions/impose";
-import { useTranslation } from "react-i18next";
-
-function Impose() {
-    const { t } = useTranslation();
-    return (
-        <div>
-            <h2>{t("pageLayout.header")}</h2>
-            <form>
-                {/* <DynamicParameterFields constraints={ImposeParamConstraints}/> */}
-            </form>
-        </div>
-    );
-}
-
-export default Impose;
diff --git a/client-tauri/src/utils/pdf-operations.ts b/client-tauri/src/utils/pdf-operations.ts
deleted file mode 100644
index af772f2bb..000000000
--- a/client-tauri/src/utils/pdf-operations.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-
-import SharedOperations, { OperationsType } from "@stirling-pdf/shared-operations/src";
-import { ImposeParamsType } from "@stirling-pdf/shared-operations/src/functions/impose";
-import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
-
-// Import injected libraries here!
-import * as pdfcpuWrapper from "@stirling-pdf/shared-operations/wasm/pdfcpu/pdfcpu-wrapper-browser.js";
-
-async function impose(params: ImposeParamsType): Promise<PdfFile> {
-    return SharedOperations.impose(params, pdfcpuWrapper);
-}
-
-const toExport: OperationsType = {
-    ...SharedOperations,
-    impose,
-};
-export default toExport;
diff --git a/client-tauri/tsconfig.json b/client-tauri/tsconfig.json
index ce8a07e6d..9bd0514c1 100644
--- a/client-tauri/tsconfig.json
+++ b/client-tauri/tsconfig.json
@@ -22,7 +22,8 @@
   },
   "include": [
     "src",
-    "declarations/*.d.ts"
+    "declarations/*.d.ts",
+    "../shared-operations/declarations/*.d.ts"
   ],
   "references": [{ "path": "./tsconfig.node.json" }]
 }
diff --git a/shared-operations/src/functions/common/getPagesByIndex.ts b/shared-operations/src/functions/common/getPagesByIndex.ts
index 63940824d..ab575dbb5 100644
--- a/shared-operations/src/functions/common/getPagesByIndex.ts
+++ b/shared-operations/src/functions/common/getPagesByIndex.ts
@@ -7,7 +7,7 @@ export async function getPages(file: PdfFile, pageIndexes: number[]): Promise<Pd
     const subDocument = await PDFDocument.create();
 
     // Check that array max number is not larger pdf pages number
-    if(Math.max(...pageIndexes) >= pdfLibDocument.getPageCount()) {
+    if(Math.max(...pageIndexes) > pdfLibDocument.getPageCount()) {
         throw new Error(`The PDF document only has ${pdfLibDocument.getPageCount()} pages and you tried to extract page ${Math.max(...pageIndexes)}`);
     }
 
diff --git a/shared-operations/src/functions/index.ts b/shared-operations/src/functions/index.ts
index 4fa729848..0f2ac4a4c 100644
--- a/shared-operations/src/functions/index.ts
+++ b/shared-operations/src/functions/index.ts
@@ -35,7 +35,8 @@ export class Operator {
     }
 
     async run(input: PdfFile[] | any[], progressCallback: (progress: Progress) => void): Promise<PdfFile[] | any[]> {
-        return [];
+        progressCallback({ curFileProgress: 1, operationProgress: 1 })
+        return input;
     }
 }
 
diff --git a/shared-operations/src/workflow/operatorAccessor.ts b/shared-operations/src/workflow/operatorAccessor.ts
index e2c5f792d..245321170 100644
--- a/shared-operations/src/workflow/operatorAccessor.ts
+++ b/shared-operations/src/workflow/operatorAccessor.ts
@@ -7,7 +7,7 @@ export async function getOperatorByName(name: string): Promise<typeof Operator |
     // Check if exists
     if(!compileTimeOperatorList.includes(name)) return;
 
-    i18next.loadNamespaces(name, (err, t) => { if (err) throw err; });
+    i18next.loadNamespaces(name, (err, t) => { if (err) throw err; console.log(t) });
     const loadedModule = await import("../functions/" + name + ".ts");
     const operator = loadedModule[capitalizeFirstLetter(name)];
     if(!operator) {