Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem History #39

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e53a0e9
Remove logical expression parsing
Elscrux Oct 25, 2023
9341dcd
Remove negated variable token and use separate tokens for negate and …
Elscrux Oct 25, 2023
60085dc
Add more logical expression validations
Elscrux Oct 25, 2023
5d550c8
Refactor text synchronization
Elscrux Nov 2, 2023
820b1f5
Add fetchSolution API call
Elscrux Nov 2, 2023
6b52dd6
Add history
Elscrux Nov 2, 2023
a220fbc
Give the ListItem key a proper value
Elscrux Nov 2, 2023
23b0da7
Add tooltip to be able to see the full text for long problems
Elscrux Nov 9, 2023
4d6fbe0
Add load spinner for loading solutions too
Elscrux Nov 9, 2023
59786da
Add cookie library
Elscrux Nov 9, 2023
dcc7725
Add cookie to store history
Elscrux Nov 9, 2023
98bc3c2
Fix using wrong variable for MaxCut text
Elscrux Nov 9, 2023
297877a
Use different cookies for different problem types and useEffect loop
Elscrux Nov 9, 2023
ce58623
Don't try to load an existing solution when clicking then Go button
Elscrux Nov 9, 2023
7b723a0
Revert "Add cookie library"
Elscrux Nov 14, 2023
4e254b8
Replace cookie approach with local storage
Elscrux Nov 14, 2023
1af6a83
Reduce History api to one function onRequestRollback
Elscrux Jan 23, 2024
b1d51b2
Rename content to problemInput
Elscrux Jan 23, 2024
a516efc
Rename setText to onTextChanged
Elscrux Jan 23, 2024
fb71ba4
Extract history storage to own file
Elscrux Jan 23, 2024
3be04be
Rename getSolution to startSolving
Elscrux Jan 23, 2024
53e60be
Rename ProblemTypeSolutionId to SolutionIds
Elscrux Jan 23, 2024
2f5b411
Remove code that is commented out
Elscrux Jan 23, 2024
de8cfb8
Remove unused import
Elscrux Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract history storage to own file
Elscrux committed Jan 23, 2024
commit fb71ba4d01ce58972366a80e85c495284d9c1f97
2 changes: 1 addition & 1 deletion src/components/solvers/History.tsx
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import {
Stack,
Tooltip,
} from "@chakra-ui/react";
import { ProblemState } from "./ProgressHandler";
import { ProblemState } from "./HistoryStorage";

interface HistoryProps<T> {
problemStates: ProblemState<T>[];
30 changes: 30 additions & 0 deletions src/components/solvers/HistoryStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ProblemTypeSolutionId } from "./ProgressHandler";

export interface ProblemState<T> {
/**
* Problem input
*/
problemInput: T;
/**
* Ids of the solutions of the problem input per problem type
*/
solutionIds: ProblemTypeSolutionId;
}

export function getHistory<T>(problemTypes: string[]): ProblemState<T>[] {
let historyItem = localStorage.getItem(getStoreId(problemTypes));
if (historyItem === null) return [];

return JSON.parse(historyItem);
}

export function storeHistory<T>(
problemTypes: string[],
history: ProblemState<T>[]
) {
localStorage.setItem(getStoreId(problemTypes), JSON.stringify(history));
}

export function getStoreId(problemTypes: string[]) {
return `problemStates-${problemTypes}`;
}
29 changes: 7 additions & 22 deletions src/components/solvers/ProgressHandler.tsx
Original file line number Diff line number Diff line change
@@ -6,20 +6,10 @@ import { fetchSolution, postProblem } from "../../api/ToolboxAPI";
import { Container } from "../Container";
import { GoButton } from "./buttons/GoButton";
import { History } from "./History";
import { getHistory, ProblemState, storeHistory } from "./HistoryStorage";
import { SolutionView } from "./SolutionView";
import { SolverPicker } from "./SolverPicker";

export interface ProblemState<T> {
/**
* Problem input
*/
problemInput: T;
/**
* Ids of the solutions of the problem input per problem type
*/
solutionIds: ProblemTypeSolutionId;
}

export type ProblemTypeSolutionId = {
[problemTypeId: string]: number;
};
@@ -36,8 +26,6 @@ export interface ProgressHandlerProps<T> {
export const ProgressHandler = <T extends {}>(
props: ProgressHandlerProps<T>
) => {
const historyItemName: string = `problemStates-${props.problemTypes}`;

const [wasClicked, setClicked] = useState<boolean>(false);
const [finished, setFinished] = useState<boolean>(false);
const [solutions, setSolutions] = useState<Solution[]>();
@@ -50,19 +38,16 @@ export const ProgressHandler = <T extends {}>(
useEffect(() => {
// Handle problem states from local storage
if (problemStates.length == 0) {
let historyItem = localStorage.getItem(historyItemName);
if (historyItem == null) return;

let storageStates = JSON.parse(historyItem);
if (storageStates.length > 0) {
setProblemStates(storageStates);
let history = getHistory<T>(props.problemTypes);
if (history.length > 0) {
setProblemStates(history);
}
}
}, [problemStates, historyItemName]);
}, [problemStates, props.problemTypes]);

useEffect(() => {
localStorage.setItem(historyItemName, JSON.stringify(problemStates));
}, [historyItemName, problemStates]);
storeHistory(props.problemTypes, problemStates);
}, [problemStates, props.problemTypes]);

async function getSolution() {
setClicked(true);