Skip to content

Commit

Permalink
Fixed learning related bugs
Browse files Browse the repository at this point in the history
- fixed repetition accuracy and repetition numbers
- fixed isFinished being set to true if there is still a card but react state hasn't updated yet (current solution: isFinished is being set to false again, but it is fast enough to not be visible)
- fixed DeckPreview showing always one as to review count
  • Loading branch information
h16nning committed Feb 8, 2024
1 parent c0b61f3 commit b398716
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/components/deck/DeckPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default function DeckPreview({ deck, i }: DeckPreviewProps) {
<Group gap="xs" wrap="nowrap">
{states.review > 0 ? (
<Badge variant="dot" color="blue">
{t("deck.review-cards-label", { count: -1 })}
{t("deck.review-cards-label", { count: states.review })}
</Badge>
) : (
<></>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import classes from "./FinishedLearningView.module.css";
import React, { useEffect } from "react";
import {
Button,
Center,
Expand Down
5 changes: 3 additions & 2 deletions src/components/learning/LearnView/LearnView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function LearnView() {
}, []);

const [showingAnswer, setShowingAnswer] = useState<boolean>(false);
const [debouncedFinish] = useDebouncedValue(controller.isFinished, 1000);
const [debouncedFinish] = useDebouncedValue(controller.isFinished, 50);

const answerButtonPressed = useCallback(
async (rating: Rating) => {
Expand Down Expand Up @@ -105,8 +105,9 @@ function LearnView() {
showingAnswer={showingAnswer}
setShowingAnswer={setShowingAnswer}
/>

<Modal
opened={controller.isFinished}
opened={debouncedFinish}
onClose={() => navigate("/home")}
fullScreen
closeOnClickOutside={false}
Expand Down
34 changes: 20 additions & 14 deletions src/components/learning/LearnView/LearnViewFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ function LearnViewFooter({
showingAnswer,
setShowingAnswer,
}: LearnViewFooterProps) {
useHotkeys([
["1", () => answer(Rating.Again)],
["2", () => answer(Rating.Hard)],
["3", () => answer(Rating.Good)],
["4", () => answer(Rating.Easy)],
[
"Space",
() => (!showingAnswer ? setShowingAnswer(true) : answer(Rating.Good)),
],
[
"Enter",
() => (!showingAnswer ? setShowingAnswer(true) : answer(Rating.Good)),
],
]);
useHotkeys(
!controller.isFinished
? [
["1", () => answer(Rating.Again)],
["2", () => answer(Rating.Hard)],
["3", () => answer(Rating.Good)],
["4", () => answer(Rating.Easy)],
[
"Space",
() =>
!showingAnswer ? setShowingAnswer(true) : answer(Rating.Good),
],
[
"Enter",
() =>
!showingAnswer ? setShowingAnswer(true) : answer(Rating.Good),
],
]
: []
);

return (
<Group className={classes.footerContainer} justify="center">
Expand Down
40 changes: 21 additions & 19 deletions src/logic/learn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ export function useLearning(
nextCard();
}
}
if (
timeCriticalCards.length +
newCards.length +
toReviewCards.length +
learnedCards.length >
0
) {
setIsFinished(false);
}
}, [
providedCards,
currentCard,
Expand All @@ -131,6 +140,7 @@ export function useLearning(

//Tries to set currentCard to the next card
const nextCard = useCallback(() => {
if (isFinished) return;
//If there are time critical cards that are due, set the first one as currentCard
if (
timeCriticalCards.length > 0 &&
Expand Down Expand Up @@ -167,14 +177,7 @@ export function useLearning(
} else {
setIsFinished(true);
}
}, [
timeCriticalCards,
newCards,
toReviewCards,
learnedCards,
options,
setCurrentCard,
]);
}, [timeCriticalCards, newCards, toReviewCards, learnedCards, options]);

//Providing information about how all 4 ratings would affect the current card
//Shown on buttons
Expand All @@ -190,7 +193,6 @@ export function useLearning(
const answer = useCallback(
(rating: Rating) => {
if (currentCard && currentCardRepeatInfo) {
console.log(currentCardRepeatInfo[rating]);
updateCardModel(currentCard, currentCardRepeatInfo[rating].card);
if (currentCardRepeatInfo[rating].card.scheduled_days === 0) {
setTimeCriticalCards((tcCards) =>
Expand All @@ -199,8 +201,8 @@ export function useLearning(
{ ...currentCard, model: currentCardRepeatInfo[rating].card },
].sort((a, b) => a.model.due.getTime() - b.model.due.getTime())
);
setRatingsList((rList) => [...rList, rating]);
}
setRatingsList((rList) => [...rList, rating]);
} else {
throw new Error("Card or cardModelInfo is missing");
}
Expand All @@ -225,17 +227,17 @@ export function useLearning(
};
}

//POSSIBLY NOT ACCURATE | NOT REVIEWED AGAIN AFTER #33
export function useRepetitionAccuracy(ratingsList: number[]): number | null {
const [accuracy, setAccuracy] = useState<number | null>(null);

useEffect(() => {
export function useRepetitionAccuracy(ratingsList: number[]): number {
return useMemo(() => {
if (ratingsList.length !== 0) {
let sum = 0;
ratingsList.forEach((rating) => (sum += rating / Rating.Good));
setAccuracy(Math.round((sum / ratingsList.length) * 1000) / 10);
ratingsList.forEach((rating) => {
return (sum += (rating - 1) / 2);
});
return Math.round((sum / ratingsList.length) * 1000) / 10;
} else {
console.log("No ratings available");
return NaN;
}
}, [ratingsList]);

return accuracy;
}

0 comments on commit b398716

Please sign in to comment.