Skip to content

Commit

Permalink
Track mirror repetitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Oct 1, 2024
1 parent 096244b commit 4118789
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
2 changes: 1 addition & 1 deletion koala/routes/get-mirroring-cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const getMirrorCards = procedure
.mutation(async ({ ctx }) => {
const cards = await prismaClient.card.findMany({
where: { userId: ctx.user?.id || "000", flagged: false },
orderBy: [{ createdAt: "desc" }],
orderBy: [{ mirrorRepetitionCount: "asc" }],
take: 200,
});
// Order by length of 'term' field:
Expand Down
54 changes: 29 additions & 25 deletions pages/mirror.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useVoiceRecorder } from "@/koala/use-recorder";
import { playAudio } from "@/koala/play-audio";
import { blobToBase64, convertBlobToWav } from "@/koala/record-button";
import { trpc } from "@/koala/trpc-config";
import { useHotkeys } from "@mantine/hooks";

type Card = {
term: string;
Expand Down Expand Up @@ -63,6 +64,31 @@ export const SentenceQuiz = ({
// TRPC mutations
const transcribeAudio = trpc.transcribeAudio.useMutation();

// Voice recorder hook
const voiceRecorder = useVoiceRecorder(handleRecordingResult);

// Handle button click
const handleClick = async () => {
if (successfulAttempts >= 3) {
// Do nothing if already completed
return;
}

if (isRecording) {
// Stop recording
voiceRecorder.stop();
setIsRecording(false);
} else {
playAudio(card.audioUrl);
// Start recording
setIsRecording(true);
voiceRecorder.start();
}
};

// Use hotkeys to trigger handleClick on space bar press
useHotkeys([["space", handleClick]]);

// Reset state variables when the term changes
useEffect(() => {
setSuccessfulAttempts(0);
Expand All @@ -72,7 +98,7 @@ export const SentenceQuiz = ({
}, [card.term]);

// Handle the result after recording is finished
const handleRecordingResult = async (audioBlob: Blob) => {
async function handleRecordingResult(audioBlob: Blob) {
setIsProcessingRecording(true);
try {
// Convert the recorded audio blob to WAV and then to base64
Expand All @@ -97,29 +123,7 @@ export const SentenceQuiz = ({
} finally {
setIsProcessingRecording(false);
}
};

// Voice recorder hook
const voiceRecorder = useVoiceRecorder(handleRecordingResult);

// Handle button click
const handleClick = async () => {
if (successfulAttempts >= 3) {
// Do nothing if already completed
return;
}

if (isRecording) {
// Stop recording
voiceRecorder.stop();
setIsRecording(false);
} else {
playAudio(card.audioUrl);
// Start recording
setIsRecording(true);
voiceRecorder.start();
}
};
}

// Effect to handle successful completion
useEffect(() => {
Expand All @@ -141,7 +145,7 @@ export const SentenceQuiz = ({
failedAttempts={failedAttempts}
handleClick={handleClick}
/>
{successfulAttempts < 3 && <p>{card.term}</p>}
{successfulAttempts === 0 && <p>{card.term}</p>}
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Card" ADD COLUMN "mirrorRepetitionCount" INTEGER DEFAULT 0;
43 changes: 22 additions & 21 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,20 @@ model VerificationToken {
}

model Card {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
flagged Boolean @default(false)
term String
definition String
langCode String
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
flagged Boolean @default(false)
term String
definition String
langCode String
// Gender can be "M"ale, "F"emale, or "N"eutral
gender String @default("N")
createdAt DateTime @default(now())
Quiz Quiz[]
imageBlobId String?
SpeakingCorrection SpeakingCorrection[]
gender String @default("N")
createdAt DateTime @default(now())
Quiz Quiz[]
imageBlobId String?
SpeakingCorrection SpeakingCorrection[]
mirrorRepetitionCount Int? @default(0)
@@unique([userId, term])
}
Expand Down Expand Up @@ -137,13 +138,13 @@ model TrainingData {
}

model SpeakingCorrection {
id Int @id @default(autoincrement())
cardId Int
Card Card @relation(fields: [cardId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
isCorrect Boolean
term String
definition String
userInput String
correction String @default("")
id Int @id @default(autoincrement())
cardId Int
Card Card @relation(fields: [cardId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
isCorrect Boolean
term String
definition String
userInput String
correction String @default("")
}

0 comments on commit 4118789

Please sign in to comment.