utility-function-extractor/packages/webpage-refactor/components/homepage.js

159 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-06-17 17:44:08 +00:00
import React, { useState } from "react";
2022-06-17 18:47:10 +00:00
import { mergeSort } from "utility-tools";
2022-06-17 17:44:08 +00:00
import { ProgressIndicator } from "./progressIndicator.js";
import { DisplayElementForComparison } from "./displayElementForComparison.js";
import { ComparisonActuator } from "./comparisonActuator.js";
import { AdvancedOptions } from "./advancedOptions.js";
2022-06-17 22:48:11 +00:00
import { Graph } from "./graph/graph.js";
2022-06-18 18:18:15 +00:00
import { pushToMongo } from "../lib/pushToMongo.js";
2022-06-17 18:47:10 +00:00
import { resolveToNumIfPossible } from "../lib/squiggle.js";
2022-06-17 17:44:08 +00:00
export function Homepage({ listOfElementsInit }) {
const SLICE = false;
2022-06-17 17:44:08 +00:00
/* Statefull elements */
2022-06-17 18:47:10 +00:00
// list of elements
const [listOfElements, setListOfElements] = useState(
2022-06-18 18:18:15 +00:00
SLICE ? listOfElementsInit.slice(0, 4) : listOfElementsInit
2022-06-17 18:47:10 +00:00
);
// number of steps
2022-06-17 17:44:08 +00:00
const numStepsInit = 0;
const [numStepsNow, setNumStepsNow] = useState(numStepsInit);
2022-06-17 18:47:10 +00:00
// is list ordered?
const [isListOrdered, setIsListOrdered] = useState(false);
2022-06-18 18:18:15 +00:00
const [mergeSortOrder, setMergeSortOrder] = useState([]);
2022-06-17 17:44:08 +00:00
2022-06-17 18:47:10 +00:00
// list of comparisons
const [links, setLinks] = useState([]);
const addLink = (link, links) => setLinks([...links, link]);
// paired being currently compared
2022-06-17 17:44:08 +00:00
const pairCurrentlyBeingComparedInit = [
listOfElementsInit[0],
listOfElementsInit[1],
];
const [pairCurrentlyBeingCompared, setPairCurrentlyBeingCompared] = useState(
pairCurrentlyBeingComparedInit
);
// dataset changer
const onChangeOfDataset = (newListOfElements) => {
setListOfElements(newListOfElements);
setLinks([]);
setPairCurrentlyBeingCompared([newListOfElements[0], newListOfElements[1]]);
setNumStepsNow(0);
setIsListOrdered(false);
2022-06-17 17:44:08 +00:00
};
2022-06-17 18:47:10 +00:00
// process next step
const mergeSortStep = ({ list, links }) => {
let mergeSortOutput = mergeSort({
list: list,
links: links,
});
setNumStepsNow(numStepsNow + 1);
if (mergeSortOutput.finishedOrderingList == false) {
let newPairToCompare = mergeSortOutput.uncomparedElements;
setIsListOrdered(false);
setPairCurrentlyBeingCompared(newPairToCompare);
} else {
setIsListOrdered(true);
2022-06-18 18:18:15 +00:00
setMergeSortOrder(mergeSortOutput.orderedList);
pushToMongo({ mergeSortOutput, links });
// alert(JSON.stringify(mergeSortOutput, null, 4));
// chooseNextPairToCompareRandomly({ listOfElements });
// return 1;
}
};
// full
2022-06-17 18:47:10 +00:00
const moveToNextStep = async ({
listOfElements,
pairCurrentlyBeingCompared,
comparisonString,
whileChangingStuff,
newLinksFromChangingStuff,
2022-06-17 18:47:10 +00:00
}) => {
// In the normal course of things:
if (!whileChangingStuff) {
let newLink = {
source: pairCurrentlyBeingCompared[0].name,
target: pairCurrentlyBeingCompared[1].name,
squiggleString: comparisonString,
};
let numOption = await resolveToNumIfPossible(comparisonString);
if (numOption.asNum == false) {
alert(JSON.stringify(numOption.errorMsg));
} else if (numOption.asNum == true) {
// addLink({ ...newLink, distance: numOption.num }, links);
/// let newLinks = [...links, { ...newLink, distance: numOption.num }];
newLink = { ...newLink, distance: numOption.num };
addLink(newLink, links);
let newLinks = [...links, newLink];
console.log("links: ", links);
mergeSortStep({ list: listOfElements, links: newLinks });
2022-06-17 18:47:10 +00:00
}
} else {
mergeSortStep({
list: listOfElements,
links: newLinksFromChangingStuff,
});
setNumStepsNow(0); // almost no guarantees of how many left.
2022-06-17 18:47:10 +00:00
}
// When changing comparisons:
2022-06-17 18:47:10 +00:00
};
2022-06-17 17:44:08 +00:00
return (
2022-06-18 03:38:06 +00:00
<div className="block w-full items-center sm:w-full mt-10">
2022-06-17 17:44:08 +00:00
<ProgressIndicator
numStepsNow={numStepsNow}
numElements={listOfElements.length}
/>
{/* Comparisons section */}
2022-06-18 03:20:44 +00:00
<div className={"" /*isListOrdered ? "hidden" : ""*/}>
2022-06-18 03:38:06 +00:00
<div className="flex justify-evenly mt-10">
2022-06-17 17:44:08 +00:00
<DisplayElementForComparison
element={pairCurrentlyBeingCompared[0]}
></DisplayElementForComparison>
<ComparisonActuator
2022-06-17 18:47:10 +00:00
listOfElements={listOfElements}
pairCurrentlyBeingCompared={pairCurrentlyBeingCompared}
moveToNextStep={moveToNextStep}
2022-06-18 18:18:15 +00:00
isListOrdered={isListOrdered}
2022-06-17 17:44:08 +00:00
/>
<DisplayElementForComparison
element={pairCurrentlyBeingCompared[1]}
></DisplayElementForComparison>
</div>
</div>
2022-06-18 03:20:44 +00:00
{/* <Graph />
*/}
<Graph
listOfElements={listOfElements}
links={links}
isListOrdered={isListOrdered}
mergeSortOrder={mergeSortOrder}
/>
2022-06-18 03:20:44 +00:00
{/* Advanced options section */}
<div>
<AdvancedOptions
links={links}
setLinks={setLinks}
listOfElements={listOfElements}
moveToNextStep={moveToNextStep}
onChangeOfDataset={onChangeOfDataset}
/>
2022-06-17 17:44:08 +00:00
</div>
</div>
);
}