refactor: remove Anki import/export and simplify card system

- Remove Anki apkg import/export functionality
- Remove OCR feature module
- Remove note and note-type modules
- Simplify card/deck modules (remove spaced repetition complexity)
- Update translator and dictionary features
- Clean up unused translations and update i18n files
- Simplify prisma schema
This commit is contained in:
2026-03-17 20:24:42 +08:00
parent 95ce49378b
commit de7c1321c2
77 changed files with 2767 additions and 8107 deletions

View File

@@ -15,14 +15,14 @@ import { DictionaryEntry } from "./DictionaryEntry";
import { LanguageSelector } from "./LanguageSelector";
import { authClient } from "@/lib/auth-client";
import { actionGetDecksByUserId } from "@/modules/deck/deck-action";
import { actionCreateNote } from "@/modules/note/note-action";
import { actionCreateCard } from "@/modules/card/card-action";
import { actionGetNoteTypesByUserId, actionCreateDefaultBasicNoteType } from "@/modules/note-type/note-type-action";
import type { TSharedDeck } from "@/shared/anki-type";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
import type { CardType } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
import { getNativeName } from "./stores/dictionaryStore";
interface DictionaryClientProps {
initialDecks: TSharedDeck[];
initialDecks: ActionOutputDeck[];
}
export function DictionaryClient({ initialDecks }: DictionaryClientProps) {
@@ -45,8 +45,7 @@ export function DictionaryClient({ initialDecks }: DictionaryClientProps) {
} = useDictionaryStore();
const { data: session } = authClient.useSession();
const [decks, setDecks] = useState<TSharedDeck[]>(initialDecks);
const [defaultNoteTypeId, setDefaultNoteTypeId] = useState<number | null>(null);
const [decks, setDecks] = useState<ActionOutputDeck[]>(initialDecks);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
@@ -65,29 +64,7 @@ export function DictionaryClient({ initialDecks }: DictionaryClientProps) {
if (session?.user?.id) {
actionGetDecksByUserId(session.user.id).then((result) => {
if (result.success && result.data) {
setDecks(result.data as TSharedDeck[]);
}
});
}
}, [session?.user?.id]);
useEffect(() => {
if (session?.user?.id) {
actionGetNoteTypesByUserId().then(async (result) => {
if (result.success && result.data) {
const basicNoteType = result.data.find(
(nt) => nt.name === "Basic Vocabulary"
);
if (basicNoteType) {
setDefaultNoteTypeId(basicNoteType.id);
} else if (result.data.length > 0) {
setDefaultNoteTypeId(result.data[0].id);
} else {
const createResult = await actionCreateDefaultBasicNoteType();
if (createResult.success && createResult.data) {
setDefaultNoteTypeId(createResult.data.id);
}
}
setDecks(result.data);
}
});
}
@@ -116,11 +93,10 @@ export function DictionaryClient({ initialDecks }: DictionaryClientProps) {
toast.error(t("pleaseCreateFolder"));
return;
}
if (!defaultNoteTypeId) {
toast.error("No note type available. Please try again.");
if (!searchResult?.entries?.length) {
toast.error("No dictionary item to save. Please search first.");
return;
}
if (!searchResult?.entries?.length) return;
const deckSelect = document.getElementById("deck-select") as HTMLSelectElement;
const deckId = deckSelect?.value ? Number(deckSelect.value) : decks[0]?.id;
@@ -132,43 +108,38 @@ export function DictionaryClient({ initialDecks }: DictionaryClientProps) {
setIsSaving(true);
const definition = searchResult.entries
.map((e) => e.definition)
.join(" | ");
const ipa = searchResult.entries[0]?.ipa || "";
const example = searchResult.entries
.map((e) => e.example)
.filter(Boolean)
.join(" | ") || "";
try {
const noteResult = await actionCreateNote({
noteTypeId: defaultNoteTypeId,
fields: [searchResult.standardForm, definition, ipa, example],
tags: ["dictionary"],
const hasIpa = searchResult.entries.some((e) => e.ipa);
const hasSpaces = searchResult.standardForm.includes(" ");
let cardType: CardType = "WORD";
if (!hasIpa) {
cardType = "SENTENCE";
} else if (hasSpaces) {
cardType = "PHRASE";
}
const ipa = searchResult.entries.find((e) => e.ipa)?.ipa || null;
const meanings = searchResult.entries.map((e) => ({
partOfSpeech: e.partOfSpeech || null,
definition: e.definition,
example: e.example || null,
}));
const cardResult = await actionCreateCard({
deckId,
word: searchResult.standardForm,
ipa,
queryLang: getNativeName(queryLang),
cardType,
meanings,
});
if (!noteResult.success || !noteResult.data) {
toast.error(t("saveFailed"));
if (!cardResult.success) {
toast.error(cardResult.message || t("saveFailed"));
setIsSaving(false);
return;
}
const noteId = BigInt(noteResult.data.id);
await actionCreateCard({
noteId,
deckId,
ord: 0,
});
await actionCreateCard({
noteId,
deckId,
ord: 1,
});
const deckName = decks.find((d) => d.id === deckId)?.name || "Unknown";
toast.success(t("savedToFolder", { folderName: deckName }));
} catch (error) {

View File

@@ -2,17 +2,17 @@ import { DictionaryClient } from "./DictionaryClient";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { actionGetDecksByUserId } from "@/modules/deck/deck-action";
import type { TSharedDeck } from "@/shared/anki-type";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
export default async function DictionaryPage() {
const session = await auth.api.getSession({ headers: await headers() });
let decks: TSharedDeck[] = [];
let decks: ActionOutputDeck[] = [];
if (session?.user?.id) {
const result = await actionGetDecksByUserId(session.user.id as string);
if (result.success && result.data) {
decks = result.data as TSharedDeck[];
decks = result.data;
}
}

View File

@@ -1,286 +0,0 @@
"use client";
import { useState, useCallback, useRef } from "react";
import { useTranslations } from "next-intl";
import { PageLayout } from "@/components/ui/PageLayout";
import { PrimaryButton, LightButton } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { Select } from "@/design-system/base/select";
import { Card } from "@/design-system/base/card";
import { toast } from "sonner";
import { Upload, FileImage, Loader2 } from "lucide-react";
import { actionProcessOCR } from "@/modules/ocr/ocr-action";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
interface ActionOutputProcessOCR {
success: boolean;
message: string;
data?: {
pairsCreated: number;
sourceLanguage?: string;
targetLanguage?: string;
};
}
interface OCRClientProps {
initialDecks: ActionOutputDeck[];
}
export function OCRClient({ initialDecks }: OCRClientProps) {
const t = useTranslations("ocr");
const fileInputRef = useRef<HTMLInputElement>(null);
const [decks, setDecks] = useState<ActionOutputDeck[]>(initialDecks);
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
const [selectedDeckId, setSelectedDeckId] = useState<number | null>(
initialDecks.length > 0 ? initialDecks[0].id : null
);
const [sourceLanguage, setSourceLanguage] = useState<string>("");
const [targetLanguage, setTargetLanguage] = useState<string>("");
const [isProcessing, setIsProcessing] = useState(false);
const [ocrResult, setOcrResult] = useState<ActionOutputProcessOCR | null>(null);
const handleFileChange = useCallback((file: File | null) => {
if (!file) return;
if (!file.type.startsWith("image/")) {
toast.error(t("invalidFileType"));
return;
}
const url = URL.createObjectURL(file);
setPreviewUrl(url);
setSelectedFile(file);
setOcrResult(null);
}, [t]);
const handleDrop = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
const file = e.dataTransfer.files[0];
handleFileChange(file);
}, [handleFileChange]);
const handleDragOver = useCallback((e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
}, []);
const fileToBase64 = async (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const result = reader.result as string;
const base64 = result.split(",")[1];
resolve(base64);
};
reader.onerror = reject;
reader.readAsDataURL(file);
});
};
const handleProcess = async () => {
if (!selectedFile) {
toast.error(t("noImage"));
return;
}
if (!selectedDeckId) {
toast.error(t("noDeck"));
return;
}
setIsProcessing(true);
setOcrResult(null);
try {
const base64 = await fileToBase64(selectedFile);
const result = await actionProcessOCR({
imageBase64: base64,
deckId: selectedDeckId,
sourceLanguage: sourceLanguage || undefined,
targetLanguage: targetLanguage || undefined,
});
if (result.success && result.data) {
setOcrResult(result);
const deckName = decks.find(d => d.id === selectedDeckId)?.name || "";
toast.success(t("ocrSuccess", { count: result.data.pairsCreated, deck: deckName }));
} else {
toast.error(result.message || t("ocrFailed"));
}
} catch {
toast.error(t("processingFailed"));
} finally {
setIsProcessing(false);
}
};
const handleSave = async () => {
if (!ocrResult || !selectedDeckId) {
toast.error(t("noResultsToSave"));
return;
}
try {
const deckName = decks.find(d => d.id === selectedDeckId)?.name || "Unknown";
toast.success(t("savedToDeck", { deckName }));
} catch (error) {
toast.error(t("saveFailed"));
}
};
const clearImage = () => {
if (previewUrl) {
URL.revokeObjectURL(previewUrl);
}
setPreviewUrl(null);
setSelectedFile(null);
setOcrResult(null);
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};
return (
<PageLayout variant="centered-card">
<div className="text-center mb-8">
<h1 className="text-4xl md:text-5xl font-bold text-gray-800 mb-4">
{t("title")}
</h1>
<p className="text-gray-700 text-lg">
{t("description")}
</p>
</div>
<Card variant="bordered" padding="lg">
<div className="space-y-6">
{/* Upload Section */}
<div>
<h2 className="text-xl font-semibold text-gray-800 mb-4">
{t("uploadSection")}
</h2>
<div
onClick={() => fileInputRef.current?.click()}
className="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center cursor-pointer hover:border-primary-500 hover:bg-primary-50 transition-colors"
>
{previewUrl ? (
<div className="space-y-4">
<img
src={previewUrl}
alt="Preview"
className="mx-auto max-w-full h-64 object-contain rounded-lg"
/>
<p className="text-gray-600">{t("changeImage")}</p>
</div>
) : (
<div className="space-y-4">
<Upload className="mx-auto h-12 w-12 text-gray-400" />
<p className="text-gray-600">{t("dropOrClick")}</p>
<p className="text-sm text-gray-500">{t("supportedFormats")}</p>
</div>
)}
</div>
<input
ref={fileInputRef}
type="file"
accept="image/*"
onChange={(e) => handleFileChange(e.target.files?.[0] || null)}
className="hidden"
/>
</div>
{/* Deck Selection */}
<div>
<h2 className="text-xl font-semibold text-gray-800 mb-4">
{t("deckSelection")}
</h2>
<Select
value={selectedDeckId?.toString() || ""}
onChange={(e) => setSelectedDeckId(Number(e.target.value))}
className="w-full"
>
<option value="">{t("selectDeck")}</option>
{decks.map((deck) => (
<option key={deck.id} value={deck.id}>
{deck.name}
</option>
))}
</Select>
</div>
{/* Language Hints */}
<div>
<h2 className="text-xl font-semibold text-gray-800 mb-4">
{t("languageHints")}
</h2>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<Input
type="text"
placeholder={t("sourceLanguagePlaceholder")}
value={sourceLanguage}
onChange={(e) => setSourceLanguage(e.target.value)}
className="w-full"
/>
<Input
type="text"
placeholder={t("targetLanguagePlaceholder")}
value={targetLanguage}
onChange={(e) => setTargetLanguage(e.target.value)}
className="w-full"
/>
</div>
</div>
{/* Process Button */}
<div className="flex justify-center">
<PrimaryButton
onClick={handleProcess}
disabled={!selectedFile || !selectedDeckId || isProcessing}
loading={isProcessing}
className="px-8 py-3 text-lg"
>
{t("processButton")}
</PrimaryButton>
</div>
{/* Results Preview */}
{ocrResult && ocrResult.data && (
<div>
<h2 className="text-xl font-semibold text-gray-800 mb-4">
{t("resultsPreview")}
</h2>
<div className="bg-gray-50 rounded-lg p-4">
<div className="space-y-3">
<div className="text-center py-4">
<p className="text-gray-800">{t("extractedPairs", { count: ocrResult.data.pairsCreated })}</p>
</div>
</div>
{ocrResult.data.sourceLanguage && (
<div className="mt-4 text-sm text-gray-500">
{t("detectedSourceLanguage")}: {ocrResult.data.sourceLanguage}
</div>
)}
{ocrResult.data.targetLanguage && (
<div className="mt-1 text-sm text-gray-500">
{t("detectedTargetLanguage")}: {ocrResult.data.targetLanguage}
</div>
)}
</div>
<div className="mt-4 flex justify-center">
<LightButton
onClick={handleSave}
disabled={!selectedDeckId}
className="px-6 py-2"
>
{t("saveButton")}
</LightButton>
</div>
</div>
)}
</div>
</Card>
</PageLayout>
);
}

View File

@@ -1,20 +0,0 @@
import { OCRClient } from "./OCRClient";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { actionGetDecksByUserId } from "@/modules/deck/deck-action";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
export default async function OCRPage() {
const session = await auth.api.getSession({ headers: await headers() });
let decks: ActionOutputDeck[] = [];
if (session?.user?.id) {
const result = await actionGetDecksByUserId(session.user.id as string);
if (result.success && result.data) {
decks = result.data;
}
}
return <OCRClient initialDecks={decks} />;
}

View File

@@ -1,6 +1,7 @@
"use client";
import { LightButton, IconClick } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { Textarea } from "@/design-system/base/textarea";
import { IMAGES } from "@/config/images";
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
@@ -18,6 +19,20 @@ import { genIPA, genLanguage } from "@/modules/translator/translator-action";
import { PageLayout } from "@/components/ui/PageLayout";
import { getTTSUrl, TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
const TTS_LANGUAGES = [
{ value: "Auto", labelKey: "auto" },
{ value: "Chinese", labelKey: "chinese" },
{ value: "English", labelKey: "english" },
{ value: "Japanese", labelKey: "japanese" },
{ value: "Korean", labelKey: "korean" },
{ value: "French", labelKey: "french" },
{ value: "German", labelKey: "german" },
{ value: "Italian", labelKey: "italian" },
{ value: "Spanish", labelKey: "spanish" },
{ value: "Portuguese", labelKey: "portuguese" },
{ value: "Russian", labelKey: "russian" },
] as const;
export default function TextSpeakerPage() {
const t = useTranslations("text_speaker");
const textareaRef = useRef<HTMLTextAreaElement>(null);
@@ -30,6 +45,8 @@ export default function TextSpeakerPage() {
const [autopause, setAutopause] = useState(true);
const textRef = useRef("");
const [language, setLanguage] = useState<string | null>(null);
const [selectedLanguage, setSelectedLanguage] = useState<string>("Auto");
const [customLanguage, setCustomLanguage] = useState<string>("");
const [ipa, setIPA] = useState<string>("");
const objurlRef = useRef<string | null>(null);
const [processing, setProcessing] = useState(false);
@@ -93,8 +110,15 @@ export default function TextSpeakerPage() {
} else {
// 第一次播放
try {
let theLanguage = language;
if (!theLanguage) {
let theLanguage: string;
if (customLanguage.trim()) {
theLanguage = customLanguage.trim();
} else if (selectedLanguage !== "Auto") {
theLanguage = selectedLanguage;
} else if (language) {
theLanguage = language;
} else {
const tmp_language = await genLanguage(textRef.current.slice(0, 30));
setLanguage(tmp_language);
theLanguage = tmp_language;
@@ -102,7 +126,6 @@ export default function TextSpeakerPage() {
theLanguage = theLanguage.toLowerCase().replace(/[^a-z]/g, '').replace(/^./, match => match.toUpperCase());
// 检查语言是否在 TTS 支持列表中
const supportedLanguages: TTS_SUPPORTED_LANGUAGES[] = [
"Auto", "Chinese", "English", "German", "Italian", "Portuguese",
"Spanish", "Japanese", "Korean", "French", "Russian"
@@ -138,6 +161,8 @@ export default function TextSpeakerPage() {
const handleInputChange = (e: ChangeEvent<HTMLTextAreaElement>) => {
textRef.current = e.target.value.trim();
setLanguage(null);
setSelectedLanguage("Auto");
setCustomLanguage("");
setIPA("");
if (objurlRef.current) URL.revokeObjectURL(objurlRef.current);
objurlRef.current = null;
@@ -318,6 +343,40 @@ export default function TextSpeakerPage() {
alt="save"
className={`${saving ? "bg-gray-200" : ""}`}
></IconClick>
{/* 语言选择器 */}
<div className="w-full flex flex-row flex-wrap gap-2 justify-center items-center">
<span className="text-sm text-gray-600">{t("language")}</span>
{TTS_LANGUAGES.slice(0, 6).map((lang) => (
<LightButton
key={lang.value}
selected={!customLanguage && selectedLanguage === lang.value}
onClick={() => {
setSelectedLanguage(lang.value);
setCustomLanguage("");
if (objurlRef.current) URL.revokeObjectURL(objurlRef.current);
objurlRef.current = null;
setPause(true);
}}
size="sm"
>
{t(`languages.${lang.labelKey}`)}
</LightButton>
))}
<Input
variant="bordered"
size="sm"
value={customLanguage}
onChange={(e) => {
setCustomLanguage(e.target.value);
setSelectedLanguage("Auto");
if (objurlRef.current) URL.revokeObjectURL(objurlRef.current);
objurlRef.current = null;
setPause(true);
}}
placeholder={t("customLanguage")}
className="w-auto min-w-[120px]"
/>
</div>
{/* 功能开关按钮 */}
<div className="w-full flex flex-row flex-wrap gap-2 justify-center items-center">
<LightButton

View File

@@ -1,17 +1,23 @@
"use client";
import { LightButton, PrimaryButton, IconClick } from "@/design-system/base/button";
import { Select } from "@/design-system/base/select";
import { LightButton, PrimaryButton, IconClick, CircleButton } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { Textarea } from "@/design-system/base/textarea";
import { Select } from "@/design-system/base/select";
import { IMAGES } from "@/config/images";
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
import { useTranslations } from "next-intl";
import { useRef, useState } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import { actionTranslateText } from "@/modules/translator/translator-action";
import { actionCreateCard } from "@/modules/card/card-action";
import { actionGetDecksByUserId } from "@/modules/deck/deck-action";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
import type { CardType } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
import { getTTSUrl, TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
import { TSharedTranslationResult } from "@/shared/translator-type";
import { Plus } from "lucide-react";
import { authClient } from "@/lib/auth-client";
const SOURCE_LANGUAGES = [
{ value: "Auto", labelKey: "auto" },
@@ -40,12 +46,21 @@ const TARGET_LANGUAGES = [
{ value: "Russian", labelKey: "russian" },
] as const;
// Estimated button width in pixels (including gap)
const BUTTON_WIDTH = 80;
const LABEL_WIDTH = 100;
const INPUT_WIDTH = 140;
const IPA_BUTTON_WIDTH = 100;
export default function TranslatorPage() {
const t = useTranslations("translator");
const taref = useRef<HTMLTextAreaElement>(null);
const sourceContainerRef = useRef<HTMLDivElement>(null);
const targetContainerRef = useRef<HTMLDivElement>(null);
const [sourceLanguage, setSourceLanguage] = useState<string>("Auto");
const [targetLanguage, setTargetLanguage] = useState<string>("Chinese");
const [customSourceLanguage, setCustomSourceLanguage] = useState<string>("");
const [customTargetLanguage, setCustomTargetLanguage] = useState<string>("");
const [translationResult, setTranslationResult] = useState<TSharedTranslationResult | null>(null);
const [needIpa, setNeedIpa] = useState(true);
@@ -55,38 +70,72 @@ export default function TranslatorPage() {
sourceLanguage: string;
targetLanguage: string;
} | null>(null);
const [sourceButtonCount, setSourceButtonCount] = useState(2);
const [targetButtonCount, setTargetButtonCount] = useState(2);
const { load, play } = useAudioPlayer();
const lastTTS = useRef({
text: "",
url: "",
});
const { data: session } = authClient.useSession();
const [decks, setDecks] = useState<ActionOutputDeck[]>([]);
const [showSaveModal, setShowSaveModal] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const tts = async (text: string, locale: string) => {
if (lastTTS.current.text !== text) {
try {
// Map language name to TTS format
let theLanguage = locale.toLowerCase().replace(/[^a-z]/g, '').replace(/^./, match => match.toUpperCase());
// Check if language is in TTS supported list
const supportedLanguages: TTS_SUPPORTED_LANGUAGES[] = [
"Auto", "Chinese", "English", "German", "Italian", "Portuguese",
"Spanish", "Japanese", "Korean", "French", "Russian"
];
if (!supportedLanguages.includes(theLanguage as TTS_SUPPORTED_LANGUAGES)) {
theLanguage = "Auto";
useEffect(() => {
if (session?.user?.id) {
actionGetDecksByUserId(session.user.id).then((result) => {
if (result.success && result.data) {
setDecks(result.data);
}
const url = await getTTSUrl(text, theLanguage as TTS_SUPPORTED_LANGUAGES);
await load(url);
await play();
lastTTS.current.text = text;
lastTTS.current.url = url;
} catch (error) {
toast.error("Failed to generate audio");
}
});
}
};
}, [session?.user?.id]);
// Calculate how many buttons to show based on container width
const calculateButtonCount = useCallback((containerWidth: number, hasIpa: boolean) => {
// Reserve space for label, input, and IPA button (for source)
const reservedWidth = LABEL_WIDTH + INPUT_WIDTH + (hasIpa ? IPA_BUTTON_WIDTH : 0);
const availableWidth = containerWidth - reservedWidth;
return Math.max(0, Math.floor(availableWidth / BUTTON_WIDTH));
}, []);
useEffect(() => {
const updateButtonCounts = () => {
if (sourceContainerRef.current) {
const width = sourceContainerRef.current.offsetWidth;
setSourceButtonCount(calculateButtonCount(width, true));
}
if (targetContainerRef.current) {
const width = targetContainerRef.current.offsetWidth;
setTargetButtonCount(calculateButtonCount(width, false));
}
};
updateButtonCounts();
window.addEventListener("resize", updateButtonCounts);
return () => window.removeEventListener("resize", updateButtonCounts);
}, [calculateButtonCount]);
const tts = useCallback(async (text: string, locale: string) => {
try {
// Map language name to TTS format
let theLanguage = locale.toLowerCase().replace(/[^a-z]/g, '').replace(/^./, match => match.toUpperCase());
// Check if language is in TTS supported list
const supportedLanguages: TTS_SUPPORTED_LANGUAGES[] = [
"Auto", "Chinese", "English", "German", "Italian", "Portuguese",
"Spanish", "Japanese", "Korean", "French", "Russian"
];
if (!supportedLanguages.includes(theLanguage as TTS_SUPPORTED_LANGUAGES)) {
theLanguage = "Auto";
}
const url = await getTTSUrl(text, theLanguage as TTS_SUPPORTED_LANGUAGES);
await load(url);
await play();
} catch (error) {
toast.error("Failed to generate audio");
}
}, [load, play]);
const translate = async () => {
if (!taref.current || processing) return;
@@ -94,12 +143,13 @@ export default function TranslatorPage() {
setProcessing(true);
const sourceText = taref.current.value;
const effectiveSourceLanguage = customSourceLanguage.trim() || sourceLanguage;
const effectiveTargetLanguage = customTargetLanguage.trim() || targetLanguage;
// 判断是否需要强制重新翻译
const forceRetranslate =
lastTranslation?.sourceText === sourceText &&
lastTranslation?.sourceLanguage === sourceLanguage &&
lastTranslation?.sourceLanguage === effectiveSourceLanguage &&
lastTranslation?.targetLanguage === effectiveTargetLanguage;
try {
@@ -108,14 +158,14 @@ export default function TranslatorPage() {
targetLanguage: effectiveTargetLanguage,
forceRetranslate,
needIpa,
sourceLanguage: sourceLanguage === "Auto" ? undefined : sourceLanguage,
sourceLanguage: effectiveSourceLanguage === "Auto" ? undefined : effectiveSourceLanguage,
});
if (result.success && result.data) {
setTranslationResult(result.data);
setLastTranslation({
sourceText,
sourceLanguage,
sourceLanguage: effectiveSourceLanguage,
targetLanguage: effectiveTargetLanguage,
});
} else {
@@ -129,6 +179,66 @@ export default function TranslatorPage() {
}
};
const visibleSourceButtons = SOURCE_LANGUAGES.slice(0, sourceButtonCount);
const visibleTargetButtons = TARGET_LANGUAGES.slice(0, targetButtonCount);
const handleSaveCard = async () => {
if (!session) {
toast.error(t("pleaseLogin"));
return;
}
if (decks.length === 0) {
toast.error(t("pleaseCreateDeck"));
return;
}
if (!lastTranslation?.sourceText || !translationResult?.translatedText) {
toast.error(t("noTranslationToSave"));
return;
}
const deckSelect = document.getElementById("deck-select-translator") as HTMLSelectElement;
const deckId = deckSelect?.value ? Number(deckSelect.value) : decks[0]?.id;
if (!deckId) {
toast.error(t("noDeckSelected"));
return;
}
setIsSaving(true);
try {
const sourceText = lastTranslation.sourceText;
const hasSpaces = sourceText.includes(" ");
let cardType: CardType = "WORD";
if (!translationResult.sourceIpa) {
cardType = "SENTENCE";
} else if (hasSpaces) {
cardType = "PHRASE";
}
await actionCreateCard({
deckId,
word: sourceText,
ipa: translationResult.sourceIpa || null,
queryLang: lastTranslation.sourceLanguage,
cardType,
meanings: [{
partOfSpeech: null,
definition: translationResult.translatedText,
example: null,
}],
});
const deckName = decks.find((d) => d.id === deckId)?.name || "Unknown";
toast.success(t("savedToDeck", { deckName }));
setShowSaveModal(false);
} catch (error) {
toast.error(t("saveFailed"));
} finally {
setIsSaving(false);
}
};
return (
<div className="min-h-[calc(100vh-64px)] bg-white">
{/* TCard Component */}
@@ -161,49 +271,36 @@ export default function TranslatorPage() {
src={IMAGES.play_arrow}
alt="play"
onClick={() => {
const t = taref.current?.value;
if (!t) return;
tts(t, translationResult?.sourceLanguage || "");
const text = taref.current?.value;
if (!text) return;
tts(text, translationResult?.sourceLanguage || "");
}}
></IconClick>
</div>
</div>
<div className="option1 w-full flex gap-1 items-center overflow-x-auto">
<div ref={sourceContainerRef} className="option1 w-full flex gap-1 items-center overflow-x-auto">
<span className="shrink-0">{t("sourceLanguage")}</span>
<LightButton
selected={sourceLanguage === "Auto"}
onClick={() => setSourceLanguage("Auto")}
className="shrink-0 hidden lg:inline-flex"
>
{t("auto")}
</LightButton>
<LightButton
selected={sourceLanguage === "Chinese"}
onClick={() => setSourceLanguage("Chinese")}
className="shrink-0 hidden lg:inline-flex"
>
{t("chinese")}
</LightButton>
<LightButton
selected={sourceLanguage === "English"}
onClick={() => setSourceLanguage("English")}
className="shrink-0 hidden xl:inline-flex"
>
{t("english")}
</LightButton>
<Select
value={sourceLanguage}
onChange={(e) => setSourceLanguage(e.target.value)}
variant="light"
{visibleSourceButtons.map((lang) => (
<LightButton
key={lang.value}
selected={!customSourceLanguage && sourceLanguage === lang.value}
onClick={() => {
setSourceLanguage(lang.value);
setCustomSourceLanguage("");
}}
className="shrink-0"
>
{t(lang.labelKey)}
</LightButton>
))}
<Input
variant="bordered"
size="sm"
className="w-auto min-w-[100px] shrink-0"
>
{SOURCE_LANGUAGES.map((lang) => (
<option key={lang.value} value={lang.value}>
{t(lang.labelKey)}
</option>
))}
</Select>
value={customSourceLanguage}
onChange={(e) => setCustomSourceLanguage(e.target.value)}
placeholder={t("customLanguage")}
className="w-auto min-w-[120px] shrink-0"
/>
<div className="flex-1"></div>
<LightButton
selected={needIpa}
@@ -244,38 +341,21 @@ export default function TranslatorPage() {
></IconClick>
</div>
</div>
<div className="option2 w-full flex gap-1 items-center overflow-x-auto">
<div ref={targetContainerRef} className="option2 w-full flex gap-1 items-center overflow-x-auto">
<span className="shrink-0">{t("translateInto")}</span>
<LightButton
selected={!customTargetLanguage && targetLanguage === "Chinese"}
onClick={() => {
setTargetLanguage("Chinese");
setCustomTargetLanguage("");
}}
className="shrink-0 hidden lg:inline-flex"
>
{t("chinese")}
</LightButton>
<LightButton
selected={!customTargetLanguage && targetLanguage === "English"}
onClick={() => {
setTargetLanguage("English");
setCustomTargetLanguage("");
}}
className="shrink-0 hidden lg:inline-flex"
>
{t("english")}
</LightButton>
<LightButton
selected={!customTargetLanguage && targetLanguage === "Japanese"}
onClick={() => {
setTargetLanguage("Japanese");
setCustomTargetLanguage("");
}}
className="shrink-0 hidden xl:inline-flex"
>
{t("japanese")}
</LightButton>
{visibleTargetButtons.map((lang) => (
<LightButton
key={lang.value}
selected={!customTargetLanguage && targetLanguage === lang.value}
onClick={() => {
setTargetLanguage(lang.value);
setCustomTargetLanguage("");
}}
className="shrink-0"
>
{t(lang.labelKey)}
</LightButton>
))}
<Input
variant="bordered"
size="sm"
@@ -289,7 +369,7 @@ export default function TranslatorPage() {
</div>
{/* TranslateButton Component */}
<div className="w-screen flex justify-center items-center">
<div className="w-screen flex justify-center items-center gap-4">
<PrimaryButton
onClick={translate}
disabled={processing}
@@ -298,7 +378,49 @@ export default function TranslatorPage() {
>
{t("translate")}
</PrimaryButton>
{translationResult && session && decks.length > 0 && (
<CircleButton
onClick={() => setShowSaveModal(true)}
title={t("saveAsCard")}
>
<Plus size={20} />
</CircleButton>
)}
</div>
{showSaveModal && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-white rounded-lg p-6 w-full max-w-md mx-4">
<h2 className="text-xl font-semibold mb-4">{t("saveAsCard")}</h2>
<div className="mb-4">
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("selectDeck")}
</label>
<Select id="deck-select-translator" className="w-full">
{decks.map((deck) => (
<option key={deck.id} value={deck.id}>
{deck.name}
</option>
))}
</Select>
</div>
<div className="mb-4 p-3 bg-gray-50 rounded text-sm">
<div className="font-medium mb-1">{t("front")}:</div>
<div className="text-gray-700 mb-2">{lastTranslation?.sourceText}</div>
<div className="font-medium mb-1">{t("back")}:</div>
<div className="text-gray-700">{translationResult?.translatedText}</div>
</div>
<div className="flex justify-end gap-2">
<LightButton onClick={() => setShowSaveModal(false)}>
{t("cancel")}
</LightButton>
<PrimaryButton onClick={handleSaveCard} loading={isSaving}>
{t("save")}
</PrimaryButton>
</div>
</div>
</div>
)}
</div>
);
}

View File

@@ -27,7 +27,6 @@ import {
actionGetDeckById,
} from "@/modules/deck/deck-action";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
import { ImportButton, ExportButton } from "@/components/deck/ImportExport";
interface DeckCardProps {
deck: ActionOutputDeck;
@@ -199,7 +198,6 @@ export function DecksClient({ userId }: DecksClientProps) {
<Plus size={18} />
{t("newDeck")}
</LightButton>
<ImportButton onImportComplete={loadDecks} />
</div>
<CardList>

View File

@@ -1,15 +1,25 @@
"use client";
import { LightButton } from "@/design-system/base/button";
import { LightButton, PrimaryButton } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { X } from "lucide-react";
import { useRef, useState } from "react";
import { Select } from "@/design-system/base/select";
import { Textarea } from "@/design-system/base/textarea";
import { Modal } from "@/design-system/overlay/modal";
import { VStack, HStack } from "@/design-system/layout/stack";
import { Plus, Trash2 } from "lucide-react";
import { useState } from "react";
import { useTranslations } from "next-intl";
import { actionCreateNote } from "@/modules/note/note-action";
import { actionCreateCard } from "@/modules/card/card-action";
import { actionGetNoteTypesByUserId, actionCreateDefaultBasicNoteType } from "@/modules/note-type/note-type-action";
import type { CardType, CardMeaning } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
const QUERY_LANGUAGES = [
{ value: "en", labelKey: "english" },
{ value: "zh", labelKey: "chinese" },
{ value: "ja", labelKey: "japanese" },
{ value: "ko", labelKey: "korean" },
] as const;
interface AddCardModalProps {
isOpen: boolean;
onClose: () => void;
@@ -24,75 +34,89 @@ export function AddCardModal({
onAdded,
}: AddCardModalProps) {
const t = useTranslations("deck_id");
const wordRef = useRef<HTMLInputElement>(null);
const definitionRef = useRef<HTMLInputElement>(null);
const ipaRef = useRef<HTMLInputElement>(null);
const exampleRef = useRef<HTMLInputElement>(null);
const [cardType, setCardType] = useState<CardType>("WORD");
const [word, setWord] = useState("");
const [ipa, setIpa] = useState("");
const [queryLang, setQueryLang] = useState("en");
const [customQueryLang, setCustomQueryLang] = useState("");
const [meanings, setMeanings] = useState<CardMeaning[]>([
{ partOfSpeech: null, definition: "", example: null }
]);
const [isSubmitting, setIsSubmitting] = useState(false);
if (!isOpen) return null;
const showIpa = cardType === "WORD" || cardType === "PHRASE";
const addMeaning = () => {
setMeanings([...meanings, { partOfSpeech: null, definition: "", example: null }]);
};
const removeMeaning = (index: number) => {
if (meanings.length > 1) {
setMeanings(meanings.filter((_, i) => i !== index));
}
};
const updateMeaning = (
index: number,
field: "partOfSpeech" | "definition" | "example",
value: string
) => {
const updated = [...meanings];
updated[index] = {
...updated[index],
[field]: value || null
};
setMeanings(updated);
};
const resetForm = () => {
setCardType("WORD");
setWord("");
setIpa("");
setQueryLang("en");
setCustomQueryLang("");
setMeanings([{ partOfSpeech: null, definition: "", example: null }]);
};
const handleAdd = async () => {
const word = wordRef.current?.value?.trim();
const definition = definitionRef.current?.value?.trim();
if (!word.trim()) {
toast.error(t("wordRequired"));
return;
}
if (!word || !definition) {
toast.error(t("wordAndDefinitionRequired"));
const validMeanings = meanings.filter(m => m.definition?.trim());
if (validMeanings.length === 0) {
toast.error(t("definitionRequired"));
return;
}
setIsSubmitting(true);
const effectiveQueryLang = customQueryLang.trim() || queryLang;
try {
let noteTypesResult = await actionGetNoteTypesByUserId();
if (!noteTypesResult.success || !noteTypesResult.data || noteTypesResult.data.length === 0) {
const createResult = await actionCreateDefaultBasicNoteType();
if (!createResult.success || !createResult.data) {
throw new Error(createResult.message || "Failed to create note type");
}
noteTypesResult = await actionGetNoteTypesByUserId();
}
if (!noteTypesResult.success || !noteTypesResult.data || noteTypesResult.data.length === 0) {
throw new Error("No note type available");
}
const noteTypeId = noteTypesResult.data[0].id;
const fields = [
word,
definition,
ipaRef.current?.value?.trim() || "",
exampleRef.current?.value?.trim() || "",
];
const noteResult = await actionCreateNote({
noteTypeId,
fields,
tags: [],
});
if (!noteResult.success || !noteResult.data) {
throw new Error(noteResult.message || "Failed to create note");
}
const cardResult = await actionCreateCard({
noteId: BigInt(noteResult.data.id),
deckId,
word: word.trim(),
ipa: showIpa && ipa.trim() ? ipa.trim() : null,
queryLang: effectiveQueryLang,
cardType,
meanings: validMeanings.map(m => ({
partOfSpeech: cardType === "SENTENCE" ? null : (m.partOfSpeech?.trim() || null),
definition: m.definition!.trim(),
example: m.example?.trim() || null,
})),
});
if (!cardResult.success) {
throw new Error(cardResult.message || "Failed to create card");
}
if (wordRef.current) wordRef.current.value = "";
if (definitionRef.current) definitionRef.current.value = "";
if (ipaRef.current) ipaRef.current.value = "";
if (exampleRef.current) exampleRef.current.value = "";
resetForm();
onAdded();
onClose();
toast.success(t("cardAdded") || "Card added successfully");
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
} finally {
@@ -100,55 +124,155 @@ export function AddCardModal({
}
};
const handleClose = () => {
resetForm();
onClose();
};
return (
<div
className="fixed inset-0 bg-black/50 backdrop-blur flex items-center justify-center z-50"
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
handleAdd();
}
}}
>
<div className="bg-white rounded-md p-6 w-full max-w-md mx-4">
<div className="flex">
<h2 className="flex-1 text-xl font-light mb-4 text-center">
{t("addNewCard")}
</h2>
<X onClick={onClose} className="hover:cursor-pointer"></X>
<Modal open={isOpen} onClose={handleClose} size="md">
<Modal.Header>
<Modal.Title>{t("addNewCard")}</Modal.Title>
<Modal.CloseButton onClick={handleClose} />
</Modal.Header>
<Modal.Body className="space-y-4">
<HStack gap={3}>
<div className="flex-1">
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("cardType")}
</label>
<Select
value={cardType}
onChange={(e) => setCardType(e.target.value as CardType)}
className="w-full"
>
<option value="WORD">{t("wordCard")}</option>
<option value="PHRASE">{t("phraseCard")}</option>
<option value="SENTENCE">{t("sentenceCard")}</option>
</Select>
</div>
</HStack>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("queryLang")}
</label>
<HStack gap={2} className="flex-wrap">
{QUERY_LANGUAGES.map((lang) => (
<LightButton
key={lang.value}
selected={!customQueryLang && queryLang === lang.value}
onClick={() => {
setQueryLang(lang.value);
setCustomQueryLang("");
}}
size="sm"
>
{t(lang.labelKey)}
</LightButton>
))}
<Input
value={customQueryLang}
onChange={(e) => setCustomQueryLang(e.target.value)}
placeholder={t("enterLanguageName")}
className="w-auto min-w-[100px] flex-1"
size="sm"
/>
</HStack>
</div>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("word")} *
</label>
<Input ref={wordRef} className="w-full"></Input>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("definition")} *
</label>
<Input ref={definitionRef} className="w-full"></Input>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{cardType === "SENTENCE" ? t("sentence") : t("word")} *
</label>
<Input
value={word}
onChange={(e) => setWord(e.target.value)}
className="w-full"
placeholder={cardType === "SENTENCE" ? t("sentencePlaceholder") : t("wordPlaceholder")}
/>
</div>
{showIpa && (
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("ipa")}
</label>
<Input ref={ipaRef} className="w-full"></Input>
<Input
value={ipa}
onChange={(e) => setIpa(e.target.value)}
className="w-full"
placeholder={t("ipaPlaceholder")}
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("example")}
)}
<div>
<HStack justify="between" className="mb-2">
<label className="block text-sm font-medium text-gray-700">
{t("meanings")} *
</label>
<Input ref={exampleRef} className="w-full"></Input>
</div>
<button
onClick={addMeaning}
className="text-sm text-blue-600 hover:text-blue-700 flex items-center gap-1"
>
<Plus size={14} />
{t("addMeaning")}
</button>
</HStack>
<VStack gap={4}>
{meanings.map((meaning, index) => (
<div key={index} className="p-3 bg-gray-50 rounded-lg space-y-2">
<HStack gap={2}>
{cardType !== "SENTENCE" && (
<div className="w-28 shrink-0">
<Input
value={meaning.partOfSpeech || ""}
onChange={(e) => updateMeaning(index, "partOfSpeech", e.target.value)}
placeholder={t("partOfSpeech")}
className="w-full"
/>
</div>
)}
<div className="flex-1">
<Input
value={meaning.definition || ""}
onChange={(e) => updateMeaning(index, "definition", e.target.value)}
placeholder={t("definition")}
className="w-full"
/>
</div>
{meanings.length > 1 && (
<button
onClick={() => removeMeaning(index)}
className="p-2 text-gray-400 hover:text-red-500"
>
<Trash2 size={16} />
</button>
)}
</HStack>
<Textarea
value={meaning.example || ""}
onChange={(e) => updateMeaning(index, "example", e.target.value)}
placeholder={t("examplePlaceholder")}
className="w-full min-h-[40px] text-sm"
/>
</div>
))}
</VStack>
</div>
<div className="mt-4">
<LightButton onClick={handleAdd} disabled={isSubmitting}>
{isSubmitting ? t("adding") : t("add")}
</LightButton>
</div>
</div>
</div>
</Modal.Body>
<Modal.Footer>
<LightButton onClick={handleClose}>
{t("cancel")}
</LightButton>
<PrimaryButton onClick={handleAdd} loading={isSubmitting}>
{isSubmitting ? t("adding") : t("add")}
</PrimaryButton>
</Modal.Footer>
</Modal>
);
}

View File

@@ -1,84 +1,133 @@
import { Edit, Trash2 } from "lucide-react";
import { Trash2, Pencil } from "lucide-react";
import { useState } from "react";
import { CircleButton } from "@/design-system/base/button";
import { UpdateCardModal } from "./UpdateCardModal";
import { useTranslations } from "next-intl";
import type { ActionOutputCardWithNote } from "@/modules/card/card-action-dto";
import type { ActionOutputCard, CardType } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
import { actionDeleteCard } from "@/modules/card/card-action";
import { EditCardModal } from "./EditCardModal";
interface CardItemProps {
card: ActionOutputCardWithNote;
card: ActionOutputCard;
isReadOnly: boolean;
onDel: () => void;
refreshCards: () => void;
onUpdated: () => void;
}
const CARD_TYPE_LABELS: Record<CardType, string> = {
WORD: "Word",
PHRASE: "Phrase",
SENTENCE: "Sentence",
};
export function CardItem({
card,
isReadOnly,
onDel,
refreshCards,
onUpdated,
}: CardItemProps) {
const [openUpdateModal, setOpenUpdateModal] = useState(false);
const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
const [showEditModal, setShowEditModal] = useState(false);
const t = useTranslations("deck_id");
const fields = card.note.flds.split('\x1f');
const field1 = fields[0] || "";
const field2 = fields[1] || "";
const frontText = card.word;
const backText = card.meanings.map((m) =>
m.partOfSpeech ? `${m.partOfSpeech}: ${m.definition}` : m.definition
).join("; ");
const handleDelete = async () => {
try {
const result = await actionDeleteCard({ cardId: card.id });
if (result.success) {
toast.success(t("cardDeleted"));
onDel();
} else {
toast.error(result.message);
}
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
}
setShowDeleteConfirm(false);
};
return (
<div className="group border-b border-gray-100 hover:bg-gray-50 transition-colors">
<div className="p-4">
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-2 text-xs text-gray-500">
<span className="px-2 py-1 bg-gray-100 rounded-md">
{t("card")}
</span>
</div>
<>
<div className="group border-b border-gray-100 hover:bg-gray-50 transition-colors">
<div className="p-4">
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-2 text-xs text-gray-500">
<span className="px-2 py-1 bg-gray-100 rounded-md">
{t("card")}
</span>
<span className="px-2 py-1 bg-blue-50 text-blue-600 rounded-md">
{CARD_TYPE_LABELS[card.cardType]}
</span>
</div>
<div className="flex items-center gap-1 opacity-50 group-hover:opacity-100 transition-opacity">
{!isReadOnly && (
<>
<CircleButton
onClick={() => setOpenUpdateModal(true)}
title={t("edit")}
className="text-gray-400 hover:text-gray-600"
>
<Edit size={14} />
</CircleButton>
<CircleButton
onClick={onDel}
title={t("delete")}
className="text-gray-400 hover:text-red-500 hover:bg-red-50"
>
<Trash2 size={14} />
</CircleButton>
</>
)}
<div className="flex items-center gap-1 opacity-50 group-hover:opacity-100 transition-opacity">
{!isReadOnly && (
<>
<CircleButton
onClick={() => setShowEditModal(true)}
title={t("edit")}
className="text-gray-400 hover:text-blue-500 hover:bg-blue-50"
>
<Pencil size={14} />
</CircleButton>
<CircleButton
onClick={() => setShowDeleteConfirm(true)}
title={t("delete")}
className="text-gray-400 hover:text-red-500 hover:bg-red-50"
>
<Trash2 size={14} />
</CircleButton>
</>
)}
</div>
</div>
</div>
<div className="text-gray-900 grid grid-cols-2 gap-4 w-3/4">
<div>
{field1.length > 30
? field1.substring(0, 30) + "..."
: field1}
</div>
<div>
{field2.length > 30
? field2.substring(0, 30) + "..."
: field2}
<div className="text-gray-900 grid grid-cols-2 gap-4 w-3/4">
<div>
{frontText.length > 30
? frontText.substring(0, 30) + "..."
: frontText}
</div>
<div>
{backText.length > 30
? backText.substring(0, 30) + "..."
: backText}
</div>
</div>
</div>
</div>
<UpdateCardModal
isOpen={openUpdateModal}
onClose={() => setOpenUpdateModal(false)}
{showDeleteConfirm && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<div className="bg-white rounded-lg p-4 max-w-sm mx-4">
<p className="text-gray-700 mb-4">{t("deleteConfirm")}</p>
<div className="flex gap-2 justify-end">
<button
onClick={() => setShowDeleteConfirm(false)}
className="px-3 py-1 text-gray-600 hover:bg-gray-100 rounded"
>
{t("cancel")}
</button>
<button
onClick={handleDelete}
className="px-3 py-1 text-red-600 hover:bg-red-50 rounded"
>
{t("delete")}
</button>
</div>
</div>
</div>
)}
<EditCardModal
isOpen={showEditModal}
onClose={() => setShowEditModal(false)}
card={card}
onUpdated={() => {
setOpenUpdateModal(false);
refreshCards();
}}
onUpdated={onUpdated}
/>
</div>
</>
);
}

View File

@@ -0,0 +1,229 @@
"use client";
import { LightButton, PrimaryButton } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { Textarea } from "@/design-system/base/textarea";
import { Modal } from "@/design-system/overlay/modal";
import { VStack, HStack } from "@/design-system/layout/stack";
import { Plus, Trash2 } from "lucide-react";
import { useState, useEffect } from "react";
import { useTranslations } from "next-intl";
import { actionUpdateCard } from "@/modules/card/card-action";
import type { ActionOutputCard, CardMeaning } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
interface EditCardModalProps {
isOpen: boolean;
onClose: () => void;
card: ActionOutputCard | null;
onUpdated: () => void;
}
export function EditCardModal({
isOpen,
onClose,
card,
onUpdated,
}: EditCardModalProps) {
const t = useTranslations("deck_id");
const [word, setWord] = useState("");
const [ipa, setIpa] = useState("");
const [meanings, setMeanings] = useState<CardMeaning[]>([
{ partOfSpeech: null, definition: "", example: null }
]);
const [isSubmitting, setIsSubmitting] = useState(false);
const showIpa = card?.cardType === "WORD" || card?.cardType === "PHRASE";
useEffect(() => {
if (card) {
setWord(card.word);
setIpa(card.ipa || "");
setMeanings(
card.meanings.length > 0
? card.meanings
: [{ partOfSpeech: null, definition: "", example: null }]
);
}
}, [card]);
const addMeaning = () => {
setMeanings([...meanings, { partOfSpeech: null, definition: "", example: null }]);
};
const removeMeaning = (index: number) => {
if (meanings.length > 1) {
setMeanings(meanings.filter((_, i) => i !== index));
}
};
const updateMeaning = (index: number, field: keyof CardMeaning, value: string) => {
const updated = [...meanings];
updated[index] = { ...updated[index], [field]: value || null };
setMeanings(updated);
};
const handleUpdate = async () => {
if (!card) return;
if (!word.trim()) {
toast.error(t("wordRequired"));
return;
}
const validMeanings = meanings.filter(m => m.definition?.trim());
if (validMeanings.length === 0) {
toast.error(t("definitionRequired"));
return;
}
setIsSubmitting(true);
try {
const result = await actionUpdateCard({
cardId: card.id,
word: word.trim(),
ipa: showIpa && ipa.trim() ? ipa.trim() : null,
meanings: validMeanings.map(m => ({
partOfSpeech: card.cardType === "SENTENCE" ? null : (m.partOfSpeech?.trim() || null),
definition: m.definition!.trim(),
example: m.example?.trim() || null,
})),
});
if (!result.success) {
throw new Error(result.message || "Failed to update card");
}
onUpdated();
onClose();
toast.success(t("cardUpdated") || "Card updated successfully");
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
} finally {
setIsSubmitting(false);
}
};
if (!card) return null;
const cardTypeLabel = card.cardType === "WORD"
? t("wordCard")
: card.cardType === "PHRASE"
? t("phraseCard")
: t("sentenceCard");
return (
<Modal open={isOpen} onClose={onClose} size="md">
<Modal.Header>
<Modal.Title>{t("updateCard")}</Modal.Title>
<Modal.CloseButton onClick={onClose} />
</Modal.Header>
<Modal.Body className="space-y-4">
<HStack gap={2} className="text-sm text-gray-500">
<span className="px-2 py-1 bg-gray-100 rounded-md">
{t("card")}
</span>
<span className="px-2 py-1 bg-blue-50 text-blue-600 rounded-md">
{cardTypeLabel}
</span>
<span className="px-2 py-1 bg-gray-100 rounded-md">
{card.queryLang}
</span>
</HStack>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{card.cardType === "SENTENCE" ? t("sentence") : t("word")} *
</label>
<Input
value={word}
onChange={(e) => setWord(e.target.value)}
className="w-full"
/>
</div>
{showIpa && (
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("ipa")}
</label>
<Input
value={ipa}
onChange={(e) => setIpa(e.target.value)}
className="w-full"
placeholder={t("ipaPlaceholder")}
/>
</div>
)}
<div>
<HStack justify="between" className="mb-2">
<label className="block text-sm font-medium text-gray-700">
{t("meanings")} *
</label>
<button
onClick={addMeaning}
className="text-sm text-blue-600 hover:text-blue-700 flex items-center gap-1"
>
<Plus size={14} />
{t("addMeaning")}
</button>
</HStack>
<VStack gap={4}>
{meanings.map((meaning, index) => (
<div key={index} className="p-3 bg-gray-50 rounded-lg space-y-2">
<HStack gap={2}>
{card.cardType !== "SENTENCE" && (
<div className="w-28 shrink-0">
<Input
value={meaning.partOfSpeech || ""}
onChange={(e) => updateMeaning(index, "partOfSpeech", e.target.value)}
placeholder={t("partOfSpeech")}
className="w-full"
/>
</div>
)}
<div className="flex-1">
<Input
value={meaning.definition || ""}
onChange={(e) => updateMeaning(index, "definition", e.target.value)}
placeholder={t("definition")}
className="w-full"
/>
</div>
{meanings.length > 1 && (
<button
onClick={() => removeMeaning(index)}
className="p-2 text-gray-400 hover:text-red-500"
>
<Trash2 size={16} />
</button>
)}
</HStack>
<Textarea
value={meaning.example || ""}
onChange={(e) => updateMeaning(index, "example", e.target.value)}
placeholder={t("examplePlaceholder")}
className="w-full min-h-[40px] text-sm"
/>
</div>
))}
</VStack>
</div>
</Modal.Body>
<Modal.Footer>
<LightButton onClick={onClose}>
{t("cancel")}
</LightButton>
<PrimaryButton onClick={handleUpdate} loading={isSubmitting}>
{isSubmitting ? t("updating") : t("update")}
</PrimaryButton>
</Modal.Footer>
</Modal>
);
}

View File

@@ -1,38 +1,27 @@
"use client";
import { ArrowLeft, Plus, RotateCcw, Settings } from "lucide-react";
import { ArrowLeft, Plus } from "lucide-react";
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { AddCardModal } from "./AddCardModal";
import { CardItem } from "./CardItem";
import { useTranslations } from "next-intl";
import { PageLayout } from "@/components/ui/PageLayout";
import { PrimaryButton, CircleButton, LinkButton, LightButton } from "@/design-system/base/button";
import { PrimaryButton, CircleButton, LinkButton } from "@/design-system/base/button";
import { CardList } from "@/components/ui/CardList";
import { Modal } from "@/design-system/overlay/modal";
import { Input } from "@/design-system/base/input";
import { HStack, VStack } from "@/design-system/layout/stack";
import { VStack } from "@/design-system/layout/stack";
import { Skeleton } from "@/design-system/feedback/skeleton";
import { actionGetCardsByDeckIdWithNotes, actionDeleteCard, actionResetDeckCards, actionGetTodayStudyStats } from "@/modules/card/card-action";
import { actionGetDeckById, actionUpdateDeck } from "@/modules/deck/deck-action";
import type { ActionOutputCardWithNote } from "@/modules/card/card-action-dto";
import type { ActionOutputTodayStudyStats } from "@/modules/card/card-action-dto";
import { actionGetCardsByDeckId, actionDeleteCard } from "@/modules/card/card-action";
import { actionGetDeckById } from "@/modules/deck/deck-action";
import type { ActionOutputCard } from "@/modules/card/card-action-dto";
import type { ActionOutputDeck } from "@/modules/deck/deck-action-dto";
import { toast } from "sonner";
import { DEFAULT_NEW_PER_DAY, DEFAULT_REV_PER_DAY } from "@/shared/constant";
import { AddCardModal } from "./AddCardModal";
export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boolean; }) {
const [cards, setCards] = useState<ActionOutputCardWithNote[]>([]);
export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boolean }) {
const [cards, setCards] = useState<ActionOutputCard[]>([]);
const [loading, setLoading] = useState(true);
const [openAddModal, setAddModal] = useState(false);
const [openResetModal, setResetModal] = useState(false);
const [resetting, setResetting] = useState(false);
const [deckInfo, setDeckInfo] = useState<ActionOutputDeck | null>(null);
const [todayStats, setTodayStats] = useState<ActionOutputTodayStudyStats | null>(null);
const [openSettingsModal, setSettingsModal] = useState(false);
const [settingsForm, setSettingsForm] = useState({ newPerDay: DEFAULT_NEW_PER_DAY, revPerDay: DEFAULT_REV_PER_DAY });
const [savingSettings, setSavingSettings] = useState(false);
const router = useRouter();
const t = useTranslations("deck_id");
@@ -40,10 +29,9 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
const fetchCards = async () => {
setLoading(true);
try {
const [cardsResult, deckResult, statsResult] = await Promise.all([
actionGetCardsByDeckIdWithNotes({ deckId }),
const [cardsResult, deckResult] = await Promise.all([
actionGetCardsByDeckId({ deckId }),
actionGetDeckById({ deckId }),
actionGetTodayStudyStats({ deckId }),
]);
if (!cardsResult.success || !cardsResult.data) {
@@ -53,14 +41,6 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
if (deckResult.success && deckResult.data) {
setDeckInfo(deckResult.data);
setSettingsForm({
newPerDay: deckResult.data.newPerDay ?? 20,
revPerDay: deckResult.data.revPerDay ?? 200,
});
}
if (statsResult.success && statsResult.data) {
setTodayStats(statsResult.data);
}
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
@@ -72,55 +52,25 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
}, [deckId]);
const refreshCards = async () => {
await actionGetCardsByDeckIdWithNotes({ deckId })
.then(result => {
if (!result.success || !result.data) {
throw new Error(result.message || "Failed to refresh cards");
}
return result.data;
}).then(setCards)
.catch((error) => {
toast.error(error instanceof Error ? error.message : "Unknown error");
});
const result = await actionGetCardsByDeckId({ deckId });
if (result.success && result.data) {
setCards(result.data);
} else {
toast.error(result.message);
}
};
const handleResetDeck = async () => {
setResetting(true);
const handleDeleteCard = async (cardId: number) => {
try {
const result = await actionResetDeckCards({ deckId });
const result = await actionDeleteCard({ cardId });
if (result.success) {
toast.success(t("resetSuccess", { count: result.data?.count ?? 0 }));
setResetModal(false);
toast.success(t("cardDeleted"));
await refreshCards();
} else {
toast.error(result.message);
}
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
} finally {
setResetting(false);
}
};
const handleSaveSettings = async () => {
setSavingSettings(true);
try {
const result = await actionUpdateDeck({
deckId,
newPerDay: settingsForm.newPerDay,
revPerDay: settingsForm.revPerDay,
});
if (result.success) {
setDeckInfo(prev => prev ? { ...prev, newPerDay: settingsForm.newPerDay, revPerDay: settingsForm.revPerDay } : null);
setSettingsModal(false);
toast.success(t("settingsSaved"));
} else {
toast.error(result.message);
}
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
} finally {
setSavingSettings(false);
}
};
@@ -138,18 +88,11 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
<div className="flex items-center justify-between">
<div>
<h1 className="text-2xl md:text-3xl font-bold text-gray-800 mb-1">
{t("cards")}
{deckInfo?.name || t("cards")}
</h1>
<p className="text-sm text-gray-500">
{t("itemsCount", { count: cards.length })}
</p>
{todayStats && (
<HStack gap={3} className="mt-2 text-xs text-gray-600">
<span>{t("todayNew")}: {todayStats.newStudied}</span>
<span>{t("todayReview")}: {todayStats.reviewStudied}</span>
<span>{t("todayLearning")}: {todayStats.learningStudied}</span>
</HStack>
)}
</div>
<div className="flex items-center gap-2">
@@ -161,27 +104,13 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
{t("memorize")}
</PrimaryButton>
{!isReadOnly && (
<>
<CircleButton
onClick={() => setSettingsModal(true)}
title={t("settings")}
>
<Settings size={18} className="text-gray-700" />
</CircleButton>
<LightButton
onClick={() => setResetModal(true)}
leftIcon={<RotateCcw size={16} />}
>
{t("resetProgress")}
</LightButton>
<CircleButton
onClick={() => {
setAddModal(true);
}}
>
<Plus size={18} className="text-gray-700" />
</CircleButton>
</>
<CircleButton
onClick={() => {
setAddModal(true);
}}
>
<Plus size={18} className="text-gray-700" />
</CircleButton>
)}
</div>
</div>
@@ -199,100 +128,25 @@ export function InDeck({ deckId, isReadOnly }: { deckId: number; isReadOnly: boo
</div>
) : (
<div className="divide-y divide-gray-100">
{cards
.toSorted((a, b) => Number(BigInt(a.id) - BigInt(b.id)))
.map((card) => (
<CardItem
key={card.id}
card={card}
isReadOnly={isReadOnly}
onDel={() => {
actionDeleteCard({ cardId: BigInt(card.id) })
.then(result => {
if (!result.success) throw new Error(result.message || "Delete failed");
}).then(refreshCards)
.catch((error) => {
toast.error(error instanceof Error ? error.message : "Unknown error");
});
}}
refreshCards={refreshCards}
/>
))}
{cards.map((card) => (
<CardItem
key={card.id}
card={card}
isReadOnly={isReadOnly}
onDel={() => handleDeleteCard(card.id)}
onUpdated={refreshCards}
/>
))}
</div>
)}
</CardList>
<AddCardModal
<AddCardModal
isOpen={openAddModal}
onClose={() => setAddModal(false)}
deckId={deckId}
onAdded={refreshCards}
/>
{/* Reset Progress Confirmation Modal */}
<Modal open={openResetModal} onClose={() => setResetModal(false)} size="sm">
<Modal.Header>
<Modal.Title>{t("resetProgressTitle")}</Modal.Title>
</Modal.Header>
<Modal.Body>
<p className="text-gray-600">{t("resetProgressConfirm")}</p>
</Modal.Body>
<Modal.Footer>
<LightButton onClick={() => setResetModal(false)}>
{t("cancel")}
</LightButton>
<PrimaryButton onClick={handleResetDeck} loading={resetting}>
{resetting ? t("resetting") : t("resetProgress")}
</PrimaryButton>
</Modal.Footer>
</Modal>
{/* Settings Modal */}
<Modal open={openSettingsModal} onClose={() => setSettingsModal(false)} size="sm">
<Modal.Header>
<Modal.Title>{t("settingsTitle")}</Modal.Title>
</Modal.Header>
<Modal.Body>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("newPerDay")}
</label>
<Input
type="number"
variant="bordered"
value={settingsForm.newPerDay}
onChange={(e) => setSettingsForm(prev => ({ ...prev, newPerDay: parseInt(e.target.value) || 0 }))}
min={0}
max={999}
/>
<p className="text-xs text-gray-500 mt-1">{t("newPerDayHint")}</p>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("revPerDay")}
</label>
<Input
type="number"
variant="bordered"
value={settingsForm.revPerDay}
onChange={(e) => setSettingsForm(prev => ({ ...prev, revPerDay: parseInt(e.target.value) || 0 }))}
min={0}
max={9999}
/>
<p className="text-xs text-gray-500 mt-1">{t("revPerDayHint")}</p>
</div>
</div>
</Modal.Body>
<Modal.Footer>
<LightButton onClick={() => setSettingsModal(false)}>
{t("cancel")}
</LightButton>
<PrimaryButton onClick={handleSaveSettings} loading={savingSettings}>
{savingSettings ? t("saving") : t("save")}
</PrimaryButton>
</Modal.Footer>
</Modal>
</PageLayout>
);
};
}

View File

@@ -1,132 +0,0 @@
"use client";
import { LightButton } from "@/design-system/base/button";
import { Input } from "@/design-system/base/input";
import { X } from "lucide-react";
import { useRef, useState, useEffect } from "react";
import { useTranslations } from "next-intl";
import { actionUpdateNote } from "@/modules/note/note-action";
import type { ActionOutputCardWithNote } from "@/modules/card/card-action-dto";
import { toast } from "sonner";
interface UpdateCardModalProps {
isOpen: boolean;
onClose: () => void;
card: ActionOutputCardWithNote;
onUpdated: () => void;
}
export function UpdateCardModal({
isOpen,
onClose,
card,
onUpdated,
}: UpdateCardModalProps) {
const t = useTranslations("deck_id");
const wordRef = useRef<HTMLInputElement>(null);
const definitionRef = useRef<HTMLInputElement>(null);
const ipaRef = useRef<HTMLInputElement>(null);
const exampleRef = useRef<HTMLInputElement>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
useEffect(() => {
if (isOpen && card) {
const fields = card.note.flds.split('\x1f');
if (wordRef.current) wordRef.current.value = fields[0] || "";
if (definitionRef.current) definitionRef.current.value = fields[1] || "";
if (ipaRef.current) ipaRef.current.value = fields[2] || "";
if (exampleRef.current) exampleRef.current.value = fields[3] || "";
}
}, [isOpen, card]);
if (!isOpen) return null;
const handleUpdate = async () => {
const word = wordRef.current?.value?.trim();
const definition = definitionRef.current?.value?.trim();
if (!word || !definition) {
toast.error(t("wordAndDefinitionRequired"));
return;
}
setIsSubmitting(true);
try {
const fields = [
word,
definition,
ipaRef.current?.value?.trim() || "",
exampleRef.current?.value?.trim() || "",
];
const result = await actionUpdateNote({
noteId: BigInt(card.note.id),
fields,
});
if (!result.success) {
throw new Error(result.message || "Failed to update note");
}
toast.success(result.message);
onUpdated();
} catch (error) {
toast.error(error instanceof Error ? error.message : "Unknown error");
} finally {
setIsSubmitting(false);
}
};
return (
<div
className="fixed inset-0 bg-black/50 backdrop-blur flex items-center justify-center z-50"
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
handleUpdate();
}
}}
>
<div className="bg-white rounded-md p-6 w-full max-w-md mx-4">
<div className="flex">
<h2 className="flex-1 text-xl font-light mb-4 text-center">
{t("updateCard")}
</h2>
<X onClick={onClose} className="hover:cursor-pointer"></X>
</div>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("word")} *
</label>
<Input ref={wordRef} className="w-full"></Input>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("definition")} *
</label>
<Input ref={definitionRef} className="w-full"></Input>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("ipa")}
</label>
<Input ref={ipaRef} className="w-full"></Input>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("example")}
</label>
<Input ref={exampleRef} className="w-full"></Input>
</div>
</div>
<div className="mt-4">
<LightButton onClick={handleUpdate} disabled={isSubmitting}>
{isSubmitting ? t("updating") : t("update")}
</LightButton>
</div>
</div>
</div>
);
}

View File

@@ -4,17 +4,14 @@ import { useState, useEffect, useTransition, useCallback, useRef } from "react";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import localFont from "next/font/local";
import { Layers, Check, Clock, Sparkles, RotateCcw, Volume2, Headphones } from "lucide-react";
import type { ActionOutputCardWithNote, ActionOutputScheduledCard } from "@/modules/card/card-action-dto";
import { actionGetCardsForReview, actionAnswerCard } from "@/modules/card/card-action";
import { Layers, Check, RotateCcw, Volume2, Headphones, ChevronLeft, ChevronRight } from "lucide-react";
import { actionGetCardsByDeckId } from "@/modules/card/card-action";
import type { ActionOutputCard } from "@/modules/card/card-action-dto";
import { PageLayout } from "@/components/ui/PageLayout";
import { LightButton, CircleButton } from "@/design-system/base/button";
import { Badge } from "@/design-system/data-display/badge";
import { Progress } from "@/design-system/feedback/progress";
import { Skeleton } from "@/design-system/feedback/skeleton";
import { HStack, VStack } from "@/design-system/layout/stack";
import { CardType } from "../../../../../generated/prisma/enums";
import { calculatePreviewIntervals, formatPreviewInterval, type CardPreview } from "./interval-preview";
import { useAudioPlayer } from "@/hooks/useAudioPlayer";
import { getTTSUrl, type TTS_SUPPORTED_LANGUAGES } from "@/lib/bigmodel/tts";
@@ -27,17 +24,14 @@ interface MemorizeProps {
deckName: string;
}
type ReviewEase = 1 | 2 | 3 | 4;
const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
const t = useTranslations("memorize.review");
const router = useRouter();
const [isPending, startTransition] = useTransition();
const [cards, setCards] = useState<ActionOutputCardWithNote[]>([]);
const [cards, setCards] = useState<ActionOutputCard[]>([]);
const [currentIndex, setCurrentIndex] = useState(0);
const [showAnswer, setShowAnswer] = useState(false);
const [lastScheduled, setLastScheduled] = useState<ActionOutputScheduledCard | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isReversed, setIsReversed] = useState(false);
@@ -53,13 +47,12 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
setIsLoading(true);
setError(null);
startTransition(async () => {
const result = await actionGetCardsForReview({ deckId, limit: 50 });
const result = await actionGetCardsByDeckId({ deckId, limit: 100 });
if (!ignore) {
if (result.success && result.data) {
setCards(result.data);
setCurrentIndex(0);
setShowAnswer(false);
setLastScheduled(null);
setIsReversed(false);
setIsDictation(false);
} else {
@@ -77,54 +70,59 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
};
}, [deckId]);
const getCurrentCard = (): ActionOutputCardWithNote | null => {
const getCurrentCard = (): ActionOutputCard | null => {
return cards[currentIndex] ?? null;
};
const getNoteFields = (card: ActionOutputCardWithNote): string[] => {
return card.note.flds.split('\x1f');
const getFrontText = (card: ActionOutputCard): string => {
if (isReversed) {
return card.meanings.map((m) =>
m.partOfSpeech ? `${m.partOfSpeech}: ${m.definition}` : m.definition
).join("; ");
}
return card.word;
};
const getBackText = (card: ActionOutputCard): string => {
if (isReversed) {
return card.word;
}
return card.meanings.map((m) =>
m.partOfSpeech ? `${m.partOfSpeech}: ${m.definition}` : m.definition
).join("; ");
};
const handleShowAnswer = useCallback(() => {
setShowAnswer(true);
}, []);
const handleAnswer = useCallback((ease: ReviewEase) => {
const card = getCurrentCard();
if (!card) return;
const handleNextCard = useCallback(() => {
if (currentIndex < cards.length - 1) {
setCurrentIndex(currentIndex + 1);
setShowAnswer(false);
setIsReversed(false);
setIsDictation(false);
cleanupAudio();
}
}, [currentIndex, cards.length]);
startTransition(async () => {
const result = await actionAnswerCard({
cardId: BigInt(card.id),
ease,
});
const handlePrevCard = useCallback(() => {
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1);
setShowAnswer(false);
setIsReversed(false);
setIsDictation(false);
cleanupAudio();
}
}, [currentIndex]);
if (result.success && result.data) {
setLastScheduled(result.data.scheduled);
const remainingCards = cards.filter((_, idx) => idx !== currentIndex);
setCards(remainingCards);
if (remainingCards.length === 0) {
setCurrentIndex(0);
} else if (currentIndex >= remainingCards.length) {
setCurrentIndex(remainingCards.length - 1);
}
setShowAnswer(false);
setIsReversed(false);
setIsDictation(false);
if (audioUrlRef.current) {
URL.revokeObjectURL(audioUrlRef.current);
audioUrlRef.current = null;
}
stop();
} else {
setError(result.message);
}
});
}, [cards, currentIndex, stop]);
const cleanupAudio = useCallback(() => {
if (audioUrlRef.current) {
URL.revokeObjectURL(audioUrlRef.current);
audioUrlRef.current = null;
}
stop();
}, [stop]);
const playTTS = useCallback(async (text: string) => {
if (isAudioLoading) return;
@@ -159,8 +157,9 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
const currentCard = getCurrentCard();
if (!currentCard) return;
const fields = getNoteFields(currentCard);
const text = isReversed ? (fields[1] ?? "") : (fields[0] ?? "");
const text = isReversed
? currentCard.meanings.map((m) => m.definition).join("; ")
: currentCard.word;
if (text) {
playTTS(text);
@@ -179,79 +178,19 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
handleShowAnswer();
}
} else {
if (e.key === "1") {
if (e.key === "ArrowRight" || e.key === " " || e.key === "Enter") {
e.preventDefault();
handleAnswer(1);
} else if (e.key === "2") {
handleNextCard();
} else if (e.key === "ArrowLeft") {
e.preventDefault();
handleAnswer(2);
} else if (e.key === "3" || e.key === " " || e.key === "Enter") {
e.preventDefault();
handleAnswer(3);
} else if (e.key === "4") {
e.preventDefault();
handleAnswer(4);
handlePrevCard();
}
}
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [showAnswer, handleShowAnswer, handleAnswer]);
const formatNextReview = (scheduled: ActionOutputScheduledCard): string => {
const now = new Date();
const nextReview = new Date(scheduled.nextReviewDate);
const diffMs = nextReview.getTime() - now.getTime();
if (diffMs < 0) return t("now");
const diffMins = Math.floor(diffMs / 60000);
const diffHours = Math.floor(diffMs / 3600000);
const diffDays = Math.floor(diffMs / 86400000);
if (diffMins < 1) return t("lessThanMinute");
if (diffMins < 60) return t("inMinutes", { count: diffMins });
if (diffHours < 24) return t("inHours", { count: diffHours });
if (diffDays < 30) return t("inDays", { count: diffDays });
return t("inMonths", { count: Math.floor(diffDays / 30) });
};
const formatInterval = (ivl: number): string => {
if (ivl < 1) return t("minutes");
if (ivl < 30) return t("days", { count: ivl });
return t("months", { count: Math.floor(ivl / 30) });
};
const getCardTypeLabel = (type: CardType): string => {
switch (type) {
case CardType.NEW:
return t("cardTypeNew");
case CardType.LEARNING:
return t("cardTypeLearning");
case CardType.REVIEW:
return t("cardTypeReview");
case CardType.RELEARNING:
return t("cardTypeRelearning");
default:
return "";
}
};
const getCardTypeVariant = (type: CardType): "info" | "warning" | "success" | "primary" => {
switch (type) {
case CardType.NEW:
return "info";
case CardType.LEARNING:
return "warning";
case CardType.REVIEW:
return "success";
case CardType.RELEARNING:
return "primary";
default:
return "info";
}
};
}, [showAnswer, handleShowAnswer, handleNextCard, handlePrevCard]);
if (isLoading) {
return (
@@ -297,20 +236,8 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
}
const currentCard = getCurrentCard()!;
const fields = getNoteFields(currentCard);
const front = fields[0] ?? "";
const back = fields[1] ?? "";
const displayFront = isReversed ? back : front;
const displayBack = isReversed ? front : back;
const cardPreview: CardPreview = {
type: currentCard.type,
ivl: currentCard.ivl,
factor: currentCard.factor,
left: currentCard.left,
};
const previewIntervals = calculatePreviewIntervals(cardPreview);
const displayFront = getFrontText(currentCard);
const displayBack = getBackText(currentCard);
return (
<PageLayout>
@@ -319,38 +246,23 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
<Layers className="w-5 h-5" />
<span className="font-medium">{deckName}</span>
</HStack>
<HStack gap={3}>
<Badge variant={getCardTypeVariant(currentCard.type)} size="sm">
{getCardTypeLabel(currentCard.type)}
</Badge>
<span className="text-sm text-gray-500">
{t("progress", { current: currentIndex + 1, total: cards.length + currentIndex })}
</span>
</HStack>
<span className="text-sm text-gray-500">
{t("progress", { current: currentIndex + 1, total: cards.length })}
</span>
</HStack>
<Progress
value={Math.max(0, ((currentIndex) / (cards.length + currentIndex)) * 100)}
value={((currentIndex + 1) / cards.length) * 100}
showLabel={false}
animated={false}
className="mb-6"
/>
{lastScheduled && (
<div className="mb-4 p-3 bg-gray-50 rounded-lg text-sm text-gray-600">
<HStack gap={2}>
<Clock className="w-4 h-4" />
<span>
{t("nextReview")}: {formatNextReview(lastScheduled)}
</span>
</HStack>
</div>
)}
<HStack justify="center" gap={2} className="mb-4">
<LightButton
onClick={() => {
setIsReversed(!isReversed);
setShowAnswer(false);
}}
selected={isReversed}
leftIcon={<RotateCcw className="w-4 h-4" />}
@@ -388,7 +300,15 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
<>
<div className="border-t border-gray-200" />
<VStack align="center" justify="center" className="p-8 min-h-[20dvh] bg-gray-50 rounded-b-xl">
<div className="text-gray-900 text-xl md:text-2xl text-center">
<div className="text-gray-900 text-xl md:text-2xl text-center whitespace-pre-line">
{displayFront}
</div>
{currentCard.ipa && (
<div className="text-gray-500 text-sm mt-2">
{currentCard.ipa}
</div>
)}
<div className="text-gray-600 text-lg mt-4 text-center whitespace-pre-line">
{displayBack}
</div>
</VStack>
@@ -398,7 +318,7 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
) : (
<>
<HStack align="center" justify="center" className="p-8 min-h-[20dvh]">
<div className="text-gray-900 text-xl md:text-2xl text-center">
<div className="text-gray-900 text-xl md:text-2xl text-center whitespace-pre-line">
{displayFront}
</div>
</HStack>
@@ -407,7 +327,7 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
<>
<div className="border-t border-gray-200" />
<HStack align="center" justify="center" className="p-8 min-h-[20dvh] bg-gray-50 rounded-b-xl">
<div className="text-gray-900 text-xl md:text-2xl text-center">
<div className="text-gray-900 text-xl md:text-2xl text-center whitespace-pre-line">
{displayBack}
</div>
</HStack>
@@ -417,14 +337,6 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
)}
</div>
<HStack justify="center" gap={4} className="mb-6 text-sm text-gray-500">
<span>{t("interval")}: {formatInterval(currentCard.ivl)}</span>
<span></span>
<span>{t("ease")}: {currentCard.factor / 10}%</span>
<span></span>
<span>{t("lapses")}: {currentCard.lapses}</span>
</HStack>
<HStack justify="center">
{!showAnswer ? (
<LightButton
@@ -436,45 +348,25 @@ const Memorize: React.FC<MemorizeProps> = ({ deckId, deckName }) => {
<span className="ml-2 text-xs opacity-60">Space</span>
</LightButton>
) : (
<HStack wrap justify="center" gap={3}>
<button
onClick={() => handleAnswer(1)}
disabled={isPending}
className="flex flex-col items-center px-5 py-3 rounded-xl bg-red-100 hover:bg-red-200 text-red-700 transition-colors disabled:opacity-50 min-w-[80px]"
<HStack gap={4}>
<LightButton
onClick={handlePrevCard}
disabled={currentIndex === 0}
className="px-4 py-2"
>
<span className="font-medium">{t("again")}</span>
<span className="text-xs opacity-75">{formatPreviewInterval(previewIntervals.again)}</span>
</button>
<button
onClick={() => handleAnswer(2)}
disabled={isPending}
className="flex flex-col items-center px-5 py-3 rounded-xl bg-orange-100 hover:bg-orange-200 text-orange-700 transition-colors disabled:opacity-50 min-w-[80px]"
<ChevronLeft className="w-5 h-5" />
</LightButton>
<span className="text-gray-500 text-sm">
{t("nextCard", { default: "Next" })}
<span className="ml-2 text-xs opacity-60">Space</span>
</span>
<LightButton
onClick={handleNextCard}
disabled={currentIndex === cards.length - 1}
className="px-4 py-2"
>
<span className="font-medium">{t("hard")}</span>
<span className="text-xs opacity-75">{formatPreviewInterval(previewIntervals.hard)}</span>
</button>
<button
onClick={() => handleAnswer(3)}
disabled={isPending}
className="flex flex-col items-center px-5 py-3 rounded-xl bg-green-100 hover:bg-green-200 text-green-700 transition-colors disabled:opacity-50 min-w-[80px] ring-2 ring-green-300"
>
<div className="flex items-center gap-1">
<span className="font-medium">{t("good")}</span>
<Sparkles className="w-3 h-3 opacity-60" />
</div>
<span className="text-xs opacity-75">{formatPreviewInterval(previewIntervals.good)}</span>
</button>
<button
onClick={() => handleAnswer(4)}
disabled={isPending}
className="flex flex-col items-center px-5 py-3 rounded-xl bg-blue-100 hover:bg-blue-200 text-blue-700 transition-colors disabled:opacity-50 min-w-[80px]"
>
<span className="font-medium">{t("easy")}</span>
<span className="text-xs opacity-75">{formatPreviewInterval(previewIntervals.easy)}</span>
</button>
<ChevronRight className="w-5 h-5" />
</LightButton>
</HStack>
)}
</HStack>

View File

@@ -1,85 +0,0 @@
import { CardType } from "../../../../../generated/prisma/enums";
import { SM2_CONFIG } from "@/modules/card/card-service-dto";
export interface CardPreview {
type: CardType;
ivl: number;
factor: number;
left: number;
}
export interface PreviewIntervals {
again: number;
hard: number;
good: number;
easy: number;
}
function calculateReviewIntervals(ivl: number, factor: number): PreviewIntervals {
const MINUTES_PER_DAY = 1440;
return {
again: Math.max(1, Math.floor(ivl * SM2_CONFIG.NEW_INTERVAL)) * MINUTES_PER_DAY,
hard: Math.floor(ivl * SM2_CONFIG.HARD_INTERVAL * SM2_CONFIG.INTERVAL_MODIFIER) * MINUTES_PER_DAY,
good: Math.floor(ivl * (factor / 1000) * SM2_CONFIG.INTERVAL_MODIFIER) * MINUTES_PER_DAY,
easy: Math.floor(ivl * (factor / 1000) * SM2_CONFIG.EASY_BONUS * SM2_CONFIG.INTERVAL_MODIFIER) * MINUTES_PER_DAY,
};
}
function calculateNewCardIntervals(): PreviewIntervals {
const steps = SM2_CONFIG.LEARNING_STEPS;
return {
again: steps[0],
hard: steps.length >= 2 ? (steps[0] + steps[1]) / 2 : steps[0],
good: steps.length >= 2 ? steps[1] : SM2_CONFIG.GRADUATING_INTERVAL_GOOD * 1440,
easy: SM2_CONFIG.EASY_INTERVAL * 1440,
};
}
function calculateLearningIntervals(left: number, isRelearning: boolean): PreviewIntervals {
const steps = isRelearning ? SM2_CONFIG.RELEARNING_STEPS : SM2_CONFIG.LEARNING_STEPS;
const stepIndex = Math.floor(left % 1000);
const again = steps[0] ?? 1;
let hard: number;
if (stepIndex === 0 && steps.length >= 2) {
const step0 = steps[0] ?? 1;
const step1 = steps[1] ?? step0;
hard = (step0 + step1) / 2;
} else {
hard = steps[stepIndex] ?? steps[0] ?? 1;
}
let good: number;
if (stepIndex < steps.length - 1) {
good = steps[stepIndex + 1] ?? steps[0] ?? 1;
} else {
good = SM2_CONFIG.GRADUATING_INTERVAL_GOOD * 1440;
}
const easy = SM2_CONFIG.GRADUATING_INTERVAL_EASY * 1440;
return { again, hard, good, easy };
}
export function calculatePreviewIntervals(card: CardPreview): PreviewIntervals {
switch (card.type) {
case CardType.NEW:
return calculateNewCardIntervals();
case CardType.LEARNING:
return calculateLearningIntervals(card.left, false);
case CardType.RELEARNING:
return calculateLearningIntervals(card.left, true);
case CardType.REVIEW:
default:
return calculateReviewIntervals(card.ivl, card.factor);
}
}
export function formatPreviewInterval(minutes: number): string {
if (minutes < 1) return "<1";
if (minutes < 60) return `${Math.round(minutes)}`;
if (minutes < 1440) return `${Math.round(minutes / 60)}h`;
return `${Math.round(minutes / 1440)}d`;
}

View File

@@ -1,254 +0,0 @@
"use client";
import { useState, useRef } from "react";
import { Upload, Download, FileUp, X, Check, Loader2 } from "lucide-react";
import { LightButton, PrimaryButton } from "@/design-system/base/button";
import { Modal } from "@/design-system/overlay/modal";
import { actionPreviewApkg, actionImportApkg } from "@/modules/import/import-action";
import { actionExportApkg } from "@/modules/export/export-action";
import { toast } from "sonner";
import { useTranslations } from "next-intl";
interface ImportExportProps {
deckId?: number;
deckName?: string;
onImportComplete?: () => void;
}
interface PreviewDeck {
id: number;
name: string;
cardCount: number;
}
export function ImportButton({ onImportComplete }: ImportExportProps) {
const t = useTranslations("decks");
const [isModalOpen, setIsModalOpen] = useState(false);
const [step, setStep] = useState<"upload" | "select" | "importing">("upload");
const [file, setFile] = useState<File | null>(null);
const [decks, setDecks] = useState<PreviewDeck[]>([]);
const [selectedDeckId, setSelectedDeckId] = useState<number | null>(null);
const [deckName, setDeckName] = useState("");
const [loading, setLoading] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileSelect = async (e: React.ChangeEvent<HTMLInputElement>) => {
const selectedFile = e.target.files?.[0];
if (!selectedFile) return;
if (!selectedFile.name.endsWith(".apkg")) {
toast.error("Please select an .apkg file");
return;
}
setFile(selectedFile);
setLoading(true);
const formData = new FormData();
formData.append("file", selectedFile);
const result = await actionPreviewApkg(formData);
setLoading(false);
if (result.success && result.decks) {
setDecks(result.decks);
setStep("select");
if (result.decks.length === 1) {
setSelectedDeckId(result.decks[0].id);
setDeckName(result.decks[0].name);
}
} else {
toast.error(result.message);
}
};
const handleImport = async () => {
if (!file || selectedDeckId === null) {
toast.error("Please select a deck to import");
return;
}
setStep("importing");
const formData = new FormData();
formData.append("file", file);
formData.append("deckId", selectedDeckId.toString());
if (deckName) {
formData.append("deckName", deckName);
}
const result = await actionImportApkg(formData);
if (result.success) {
toast.success(result.message);
setIsModalOpen(false);
resetState();
onImportComplete?.();
} else {
toast.error(result.message);
setStep("select");
}
};
const resetState = () => {
setStep("upload");
setFile(null);
setDecks([]);
setSelectedDeckId(null);
setDeckName("");
setLoading(false);
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};
const handleClose = () => {
setIsModalOpen(false);
resetState();
};
return (
<>
<LightButton onClick={() => setIsModalOpen(true)}>
<Upload size={18} />
{t("importApkg")}
</LightButton>
<Modal open={isModalOpen} onClose={handleClose}>
<div className="p-6 w-full max-w-md">
<div className="flex justify-between items-center mb-4">
<h2 className="text-xl font-bold">{t("importApkg")}</h2>
<button onClick={handleClose} className="text-gray-400 hover:text-gray-600">
<X size={20} />
</button>
</div>
{step === "upload" && (
<div className="space-y-4">
<div
className="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center cursor-pointer hover:border-primary-500 transition-colors"
onClick={() => fileInputRef.current?.click()}
>
<FileUp size={40} className="mx-auto text-gray-400 mb-2" />
<p className="text-gray-600">{t("clickToUpload")}</p>
<p className="text-sm text-gray-400">{t("apkgFilesOnly")}</p>
</div>
<input
ref={fileInputRef}
type="file"
accept=".apkg"
className="hidden"
onChange={handleFileSelect}
/>
{loading && (
<div className="flex items-center justify-center gap-2 text-gray-500">
<Loader2 size={20} className="animate-spin" />
<span>{t("parsing")}</span>
</div>
)}
</div>
)}
{step === "select" && (
<div className="space-y-4">
<p className="text-sm text-gray-600">{t("foundDecks", { count: decks.length })}</p>
<div className="space-y-2 max-h-48 overflow-y-auto">
{decks.map((deck) => (
<div
key={deck.id}
className={`p-3 border rounded-lg cursor-pointer transition-colors ${
selectedDeckId === deck.id
? "border-primary-500 bg-primary-50"
: "border-gray-200 hover:border-gray-300"
}`}
onClick={() => {
setSelectedDeckId(deck.id);
setDeckName(deck.name);
}}
>
<div className="flex items-center justify-between">
<span className="font-medium">{deck.name}</span>
<span className="text-sm text-gray-500">{deck.cardCount} cards</span>
</div>
</div>
))}
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
{t("deckName")}
</label>
<input
type="text"
value={deckName}
onChange={(e) => setDeckName(e.target.value)}
className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500"
placeholder={t("enterDeckName")}
/>
</div>
<div className="flex gap-2">
<LightButton onClick={() => setStep("upload")} className="flex-1">
{t("back")}
</LightButton>
<PrimaryButton
onClick={handleImport}
disabled={selectedDeckId === null}
className="flex-1"
>
{t("import")}
</PrimaryButton>
</div>
</div>
)}
{step === "importing" && (
<div className="flex flex-col items-center justify-center py-8">
<Loader2 size={40} className="animate-spin text-primary-500 mb-4" />
<p className="text-gray-600">{t("importing")}</p>
</div>
)}
</div>
</Modal>
</>
);
}
export function ExportButton({ deckId, deckName }: ImportExportProps) {
const t = useTranslations("decks");
const [loading, setLoading] = useState(false);
const handleExport = async () => {
if (!deckId) return;
setLoading(true);
const result = await actionExportApkg(deckId);
setLoading(false);
if (result.success && result.data && result.filename) {
const blob = new Blob([result.data], { type: "application/octet-stream" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = result.filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
toast.success(t("exportSuccess"));
} else {
toast.error(result.message);
}
};
return (
<LightButton onClick={handleExport} disabled={loading}>
{loading ? <Loader2 size={18} className="animate-spin" /> : <Download size={18} />}
{t("exportApkg")}
</LightButton>
);
}

View File

@@ -1,414 +0,0 @@
import JSZip from "jszip";
import initSqlJs from "sql.js";
import type { Database } from "sql.js";
import { createHash } from "crypto";
import type {
AnkiDeck,
AnkiNoteType,
AnkiDeckConfig,
AnkiNoteRow,
AnkiCardRow,
AnkiRevlogRow,
} from "./types";
const BASE91_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~";
function generateGuid(): string {
let guid = "";
const bytes = new Uint8Array(10);
crypto.getRandomValues(bytes);
for (let i = 0; i < 10; i++) {
guid += BASE91_CHARS[bytes[i] % BASE91_CHARS.length];
}
return guid;
}
function checksum(text: string): number {
const hash = createHash("sha1").update(text.normalize("NFC")).digest("hex");
return parseInt(hash.substring(0, 8), 16);
}
function createCollectionSql(): string {
return `
CREATE TABLE col (
id INTEGER PRIMARY KEY,
crt INTEGER NOT NULL,
mod INTEGER NOT NULL,
scm INTEGER NOT NULL,
ver INTEGER NOT NULL DEFAULT 11,
dty INTEGER NOT NULL DEFAULT 0,
usn INTEGER NOT NULL DEFAULT 0,
ls INTEGER NOT NULL DEFAULT 0,
conf TEXT NOT NULL,
models TEXT NOT NULL,
decks TEXT NOT NULL,
dconf TEXT NOT NULL,
tags TEXT NOT NULL
);
CREATE TABLE notes (
id INTEGER PRIMARY KEY,
guid TEXT NOT NULL,
mid INTEGER NOT NULL,
mod INTEGER NOT NULL,
usn INTEGER NOT NULL,
tags TEXT NOT NULL,
flds TEXT NOT NULL,
sfld TEXT NOT NULL,
csum INTEGER NOT NULL,
flags INTEGER NOT NULL DEFAULT 0,
data TEXT NOT NULL DEFAULT ''
);
CREATE TABLE cards (
id INTEGER PRIMARY KEY,
nid INTEGER NOT NULL,
did INTEGER NOT NULL,
ord INTEGER NOT NULL,
mod INTEGER NOT NULL,
usn INTEGER NOT NULL,
type INTEGER NOT NULL,
queue INTEGER NOT NULL,
due INTEGER NOT NULL,
ivl INTEGER NOT NULL,
factor INTEGER NOT NULL,
reps INTEGER NOT NULL,
lapses INTEGER NOT NULL,
left INTEGER NOT NULL,
odue INTEGER NOT NULL DEFAULT 0,
odid INTEGER NOT NULL DEFAULT 0,
flags INTEGER NOT NULL DEFAULT 0,
data TEXT NOT NULL DEFAULT ''
);
CREATE TABLE revlog (
id INTEGER PRIMARY KEY,
cid INTEGER NOT NULL,
usn INTEGER NOT NULL,
ease INTEGER NOT NULL,
ivl INTEGER NOT NULL,
lastIvl INTEGER NOT NULL,
factor INTEGER NOT NULL,
time INTEGER NOT NULL,
type INTEGER NOT NULL
);
CREATE TABLE graves (
usn INTEGER NOT NULL,
oid INTEGER NOT NULL,
type INTEGER NOT NULL
);
CREATE INDEX ix_cards_nid ON cards (nid);
CREATE INDEX ix_cards_sched ON cards (did, queue, due);
CREATE INDEX ix_cards_usn ON cards (usn);
CREATE INDEX ix_notes_csum ON notes (csum);
CREATE INDEX ix_notes_usn ON notes (usn);
CREATE INDEX ix_revlog_cid ON revlog (cid);
CREATE INDEX ix_revlog_usn ON revlog (usn);
`;
}
function mapCardType(type: string): number {
switch (type) {
case "NEW": return 0;
case "LEARNING": return 1;
case "REVIEW": return 2;
case "RELEARNING": return 3;
default: return 0;
}
}
function mapCardQueue(queue: string): number {
switch (queue) {
case "USER_BURIED": return -3;
case "SCHED_BURIED": return -2;
case "SUSPENDED": return -1;
case "NEW": return 0;
case "LEARNING": return 1;
case "REVIEW": return 2;
case "IN_LEARNING": return 3;
case "PREVIEW": return 4;
default: return 0;
}
}
export interface ExportDeckData {
deck: {
id: number;
name: string;
desc: string;
collapsed: boolean;
conf: Record<string, unknown>;
};
noteType: {
id: number;
name: string;
kind: "STANDARD" | "CLOZE";
css: string;
fields: { name: string; ord: number }[];
templates: { name: string; ord: number; qfmt: string; afmt: string }[];
};
notes: {
id: bigint;
guid: string;
tags: string;
flds: string;
sfld: string;
csum: number;
}[];
cards: {
id: bigint;
noteId: bigint;
ord: number;
type: string;
queue: string;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
}[];
revlogs: {
id: bigint;
cardId: bigint;
ease: number;
ivl: number;
lastIvl: number;
factor: number;
time: number;
type: number;
}[];
media: Map<string, Buffer>;
}
async function createDatabase(data: ExportDeckData): Promise<Uint8Array> {
const SQL = await initSqlJs({
locateFile: (file: string) => `https://sql.js.org/dist/${file}`,
});
const db = new SQL.Database();
try {
db.run(createCollectionSql());
const now = Date.now();
const nowSeconds = Math.floor(now / 1000);
const defaultConfig = {
dueCounts: true,
estTimes: true,
newSpread: 0,
curDeck: data.deck.id,
curModel: data.noteType.id,
};
const deckJson: Record<string, AnkiDeck> = {
[data.deck.id.toString()]: {
id: data.deck.id,
mod: nowSeconds,
name: data.deck.name,
usn: -1,
lrnToday: [0, 0],
revToday: [0, 0],
newToday: [0, 0],
timeToday: [0, 0],
collapsed: data.deck.collapsed,
browserCollapsed: false,
desc: data.deck.desc,
dyn: 0,
conf: 1,
extendNew: 0,
extendRev: 0,
},
"1": {
id: 1,
mod: nowSeconds,
name: "Default",
usn: -1,
lrnToday: [0, 0],
revToday: [0, 0],
newToday: [0, 0],
timeToday: [0, 0],
collapsed: false,
browserCollapsed: false,
desc: "",
dyn: 0,
conf: 1,
extendNew: 0,
extendRev: 0,
},
};
const noteTypeJson: Record<string, AnkiNoteType> = {
[data.noteType.id.toString()]: {
id: data.noteType.id,
name: data.noteType.name,
type: data.noteType.kind === "CLOZE" ? 1 : 0,
mod: nowSeconds,
usn: -1,
sortf: 0,
did: data.deck.id,
flds: data.noteType.fields.map((f, i) => ({
id: now + i,
name: f.name,
ord: f.ord,
sticky: false,
rtl: false,
font: "Arial",
size: 20,
media: [],
})),
tmpls: data.noteType.templates.map((t, i) => ({
id: now + i + 100,
name: t.name,
ord: t.ord,
qfmt: t.qfmt,
afmt: t.afmt,
bqfmt: "",
bafmt: "",
did: null,
})),
css: data.noteType.css,
latexPre: "\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
latexPost: "\\end{document}",
latexsvg: false,
req: [[0, "any", [0]]],
},
};
const deckConfigJson: Record<string, AnkiDeckConfig> = {
"1": {
id: 1,
mod: nowSeconds,
name: "Default",
usn: -1,
maxTaken: 60,
autoplay: true,
timer: 0,
replayq: true,
new: {
bury: true,
delays: [1, 10],
initialFactor: 2500,
ints: [1, 4, 7],
order: 1,
perDay: 20,
},
rev: {
bury: true,
ease4: 1.3,
ivlFct: 1,
maxIvl: 36500,
perDay: 200,
hardFactor: 1.2,
},
lapse: {
delays: [10],
leechAction: 0,
leechFails: 8,
minInt: 1,
mult: 0,
},
dyn: false,
},
};
db.run(
`INSERT INTO col (id, crt, mod, scm, ver, dty, usn, ls, conf, models, decks, dconf, tags)
VALUES (1, ?, ?, ?, 11, 0, 0, 0, ?, ?, ?, ?, '{}')`,
[
nowSeconds,
now,
now,
JSON.stringify(defaultConfig),
JSON.stringify(noteTypeJson),
JSON.stringify(deckJson),
JSON.stringify(deckConfigJson),
]
);
for (const note of data.notes) {
db.run(
`INSERT INTO notes (id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, 0, '')`,
[
Number(note.id),
note.guid || generateGuid(),
data.noteType.id,
nowSeconds,
-1,
note.tags || " ",
note.flds,
note.sfld,
note.csum || checksum(note.sfld),
]
);
}
for (const card of data.cards) {
db.run(
`INSERT INTO cards (id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, 0, 0, '')`,
[
Number(card.id),
Number(card.noteId),
data.deck.id,
card.ord,
nowSeconds,
-1,
mapCardType(card.type),
mapCardQueue(card.queue),
card.due,
card.ivl,
card.factor,
card.reps,
card.lapses,
card.left,
]
);
}
for (const revlog of data.revlogs) {
db.run(
`INSERT INTO revlog (id, cid, usn, ease, ivl, lastIvl, factor, time, type)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
Number(revlog.id),
Number(revlog.cardId),
-1,
revlog.ease,
revlog.ivl,
revlog.lastIvl,
revlog.factor,
revlog.time,
revlog.type,
]
);
}
return db.export();
} finally {
db.close();
}
}
export async function exportApkg(data: ExportDeckData): Promise<Buffer> {
const zip = new JSZip();
const dbData = await createDatabase(data);
zip.file("collection.anki21", dbData);
const mediaMapping: Record<string, string> = {};
const mediaEntries = Array.from(data.media.entries());
mediaEntries.forEach(([filename, buffer], index) => {
mediaMapping[index.toString()] = filename;
zip.file(index.toString(), buffer);
});
zip.file("media", JSON.stringify(mediaMapping));
return zip.generateAsync({ type: "nodebuffer" });
}

View File

@@ -1,175 +0,0 @@
import JSZip from "jszip";
import initSqlJs from "sql.js";
import type { Database, SqlValue } from "sql.js";
import {
type AnkiDeck,
type AnkiNoteType,
type AnkiDeckConfig,
type AnkiNoteRow,
type AnkiCardRow,
type AnkiRevlogRow,
type ParsedApkg,
} from "./types";
async function openDatabase(zip: JSZip): Promise<Database | null> {
const SQL = await initSqlJs({
locateFile: (file: string) => `https://sql.js.org/dist/${file}`,
});
const anki21b = zip.file("collection.anki21b");
const anki21 = zip.file("collection.anki21");
const anki2 = zip.file("collection.anki2");
const dbFile = anki21b || anki21 || anki2;
if (!dbFile) return null;
const dbData = await dbFile.async("uint8array");
return new SQL.Database(dbData);
}
function parseJsonField<T>(jsonStr: string): T {
try {
return JSON.parse(jsonStr);
} catch {
return {} as T;
}
}
function queryAll<T>(db: Database, sql: string, params: SqlValue[] = []): T[] {
const stmt = db.prepare(sql);
try {
stmt.bind(params);
const results: T[] = [];
while (stmt.step()) {
results.push(stmt.getAsObject() as T);
}
return results;
} finally {
stmt.free();
}
}
function queryOne<T>(db: Database, sql: string, params: SqlValue[] = []): T | null {
const results = queryAll<T>(db, sql, params);
return results[0] ?? null;
}
export async function parseApkg(buffer: Buffer): Promise<ParsedApkg> {
const zip = await JSZip.loadAsync(buffer);
const db = await openDatabase(zip);
if (!db) {
throw new Error("No valid Anki database found in APKG file");
}
try {
const col = queryOne<{
crt: number;
mod: number;
ver: number;
conf: string;
models: string;
decks: string;
dconf: string;
tags: string;
}>(db, "SELECT crt, mod, ver, conf, models, decks, dconf, tags FROM col WHERE id = 1");
if (!col) {
throw new Error("Invalid APKG: no collection row found");
}
const decksMap = new Map<number, AnkiDeck>();
const decksJson = parseJsonField<Record<string, AnkiDeck>>(col.decks);
for (const [id, deck] of Object.entries(decksJson)) {
decksMap.set(parseInt(id, 10), deck);
}
const noteTypesMap = new Map<number, AnkiNoteType>();
const modelsJson = parseJsonField<Record<string, AnkiNoteType>>(col.models);
for (const [id, model] of Object.entries(modelsJson)) {
noteTypesMap.set(parseInt(id, 10), model);
}
const deckConfigsMap = new Map<number, AnkiDeckConfig>();
const dconfJson = parseJsonField<Record<string, AnkiDeckConfig>>(col.dconf);
for (const [id, config] of Object.entries(dconfJson)) {
deckConfigsMap.set(parseInt(id, 10), config);
}
const notes = queryAll<AnkiNoteRow>(
db,
"SELECT id, guid, mid, mod, usn, tags, flds, sfld, csum, flags, data FROM notes"
);
const cards = queryAll<AnkiCardRow>(
db,
"SELECT id, nid, did, ord, mod, usn, type, queue, due, ivl, factor, reps, lapses, left, odue, odid, flags, data FROM cards"
);
const revlogs = queryAll<AnkiRevlogRow>(
db,
"SELECT id, cid, usn, ease, ivl, lastIvl, factor, time, type FROM revlog"
);
const mediaMap = new Map<string, Buffer>();
const mediaFile = zip.file("media");
if (mediaFile) {
const mediaJson = parseJsonField<Record<string, string>>(await mediaFile.async("text"));
for (const [num, filename] of Object.entries(mediaJson)) {
const mediaData = zip.file(num);
if (mediaData) {
const data = await mediaData.async("nodebuffer");
mediaMap.set(filename, data);
}
}
}
return {
decks: decksMap,
noteTypes: noteTypesMap,
deckConfigs: deckConfigsMap,
notes,
cards,
revlogs,
media: mediaMap,
collectionMeta: {
crt: col.crt,
mod: col.mod,
ver: col.ver,
},
};
} finally {
db.close();
}
}
export function getDeckNotesAndCards(
parsed: ParsedApkg,
deckId: number
): { notes: AnkiNoteRow[]; cards: AnkiCardRow[] } {
const deckCards = parsed.cards.filter(c => c.did === deckId);
const noteIds = new Set(deckCards.map(c => c.nid));
const deckNotes = parsed.notes.filter(n => noteIds.has(n.id));
return { notes: deckNotes, cards: deckCards };
}
export function getDeckNames(parsed: ParsedApkg): { id: number; name: string; cardCount: number }[] {
const cardCounts = new Map<number, number>();
for (const card of parsed.cards) {
cardCounts.set(card.did, (cardCounts.get(card.did) ?? 0) + 1);
}
const result: { id: number; name: string; cardCount: number }[] = [];
for (const [id, deck] of parsed.decks) {
if (deck.dyn === 0) {
result.push({
id,
name: deck.name,
cardCount: cardCounts.get(id) ?? 0,
});
}
}
return result.sort((a, b) => a.name.localeCompare(b.name));
}

View File

@@ -1,193 +0,0 @@
/**
* Anki APKG format types
* Based on Anki's official database schema
*/
// ============================================
// APKG JSON Configuration Types
// ============================================
export interface AnkiField {
id: number;
name: string;
ord: number;
sticky: boolean;
rtl: boolean;
font: string;
size: number;
media: string[];
description?: string;
plainText?: boolean;
collapsed?: boolean;
excludeFromSearch?: boolean;
tag?: number;
preventDeletion?: boolean;
}
export interface AnkiTemplate {
id: number | null;
name: string;
ord: number;
qfmt: string;
afmt: string;
bqfmt?: string;
bafmt?: string;
did?: number | null;
bfont?: string;
bsize?: number;
}
export interface AnkiNoteType {
id: number;
name: string;
type: 0 | 1; // 0=standard, 1=cloze
mod: number;
usn: number;
sortf: number;
did: number | null;
tmpls: AnkiTemplate[];
flds: AnkiField[];
css: string;
latexPre: string;
latexPost: string;
latexsvg: boolean | null;
req: [number, string, number[]][];
originalStockKind?: number;
}
export interface AnkiDeckConfig {
id: number;
mod: number;
name: string;
usn: number;
maxTaken: number;
autoplay: boolean;
timer: 0 | 1;
replayq: boolean;
new: {
bury: boolean;
delays: number[];
initialFactor: number;
ints: [number, number, number];
order: number;
perDay: number;
};
rev: {
bury: boolean;
ease4: number;
ivlFct: number;
maxIvl: number;
perDay: number;
hardFactor: number;
};
lapse: {
delays: number[];
leechAction: 0 | 1;
leechFails: number;
minInt: number;
mult: number;
};
dyn: boolean;
}
export interface AnkiDeck {
id: number;
mod: number;
name: string;
usn: number;
lrnToday: [number, number];
revToday: [number, number];
newToday: [number, number];
timeToday: [number, number];
collapsed: boolean;
browserCollapsed: boolean;
desc: string;
dyn: 0 | 1;
conf: number;
extendNew: number;
extendRev: number;
reviewLimit?: number | null;
newLimit?: number | null;
reviewLimitToday?: number | null;
newLimitToday?: number | null;
md?: boolean;
}
// ============================================
// APKG Database Row Types
// ============================================
export interface AnkiNoteRow {
id: number;
guid: string;
mid: number;
mod: number;
usn: number;
tags: string;
flds: string;
sfld: string;
csum: number;
flags: number;
data: string;
}
export interface AnkiCardRow {
id: number;
nid: number;
did: number;
ord: number;
mod: number;
usn: number;
type: number; // 0=new, 1=learning, 2=review, 3=relearning
queue: number; // -3=buried(user), -2=buried(sched), -1=suspended, 0=new, 1=learning, 2=review, 3=day learning, 4=preview
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
}
export interface AnkiRevlogRow {
id: number;
cid: number;
usn: number;
ease: number;
ivl: number;
lastIvl: number;
factor: number;
time: number;
type: number;
}
// ============================================
// Parsed APKG Types
// ============================================
export interface ParsedApkg {
decks: Map<number, AnkiDeck>;
noteTypes: Map<number, AnkiNoteType>;
deckConfigs: Map<number, AnkiDeckConfig>;
notes: AnkiNoteRow[];
cards: AnkiCardRow[];
revlogs: AnkiRevlogRow[];
media: Map<string, Buffer>;
collectionMeta: {
crt: number;
mod: number;
ver: number;
};
}
export interface ApkgImportResult {
success: boolean;
deckName: string;
noteCount: number;
cardCount: number;
mediaCount: number;
errors: string[];
}

View File

@@ -14,17 +14,31 @@ export async function generateEntries(
const isWord = inputType === "word";
const prompt = `
生成词典条目。词语:"${standardForm}"${queryLang}。用${definitionLang}释义。
你是专业词典编纂专家。为词条"${standardForm}"${queryLang}生成${definitionLang}释义。
返回 JSON
${isWord ? `{"entries":[{"ipa":"音标","partOfSpeech":"词性","definition":"释义","example":"例句"}]}` : `{"entries":[{"definition":"释义","example":"例句"}]}`}
【核心要求】
生成尽可能完整、全面的词典条目,包括:
${isWord ? `- 所有常见词性(名词、动词、形容词、副词等)
- 每个词性下的所有常用义项
- 专业领域含义、口语含义、习语用法` : `- 所有常见含义和用法
- 字面义和引申义
- 不同语境下的解释`}
只返回 JSON
JSON格式】
${isWord ? `{"entries":[{"ipa":"国际音标","partOfSpeech":"词性","definition":"详细释义","example":"自然例句"}]}` : `{"entries":[{"definition":"详细释义","example":"自然例句"}]}`}
【质量标准】
- 条目数量:尽可能多,不要遗漏常用义项
- 释义:准确、完整、符合母语者习惯
- 例句:自然、地道、展示实际用法
- IPA使用标准国际音标单词/短语必填)
只返回JSON不要其他内容。
`.trim();
try {
const result = await getAnswer([
{ role: "system", content: "词典条目生成器,只返回 JSON。" },
{ role: "system", content: "专业词典编纂专家返回完整JSON词典数据。" },
{ role: "user", content: prompt },
]).then(parseAIGeneratedJSON<EntriesGenerationResult>);
@@ -47,6 +61,7 @@ ${isWord ? `{"entries":[{"ipa":"音标","partOfSpeech":"词性","definition":"
}
}
log.info("Generated dictionary entries", { count: result.entries.length });
return result;
} catch (error) {
log.error("Entries generation failed", { error: error instanceof Error ? error.message : String(error) });

View File

@@ -1,12 +1,5 @@
"use server";
import OpenAI from "openai";
const openai = new OpenAI({
apiKey: process.env.ZHIPU_API_KEY,
baseURL: "https://open.bigmodel.cn/api/paas/v4",
});
type Messages = Array<
| { role: "system"; content: string }
| { role: "user"; content: string }
@@ -20,13 +13,29 @@ async function getAnswer(prompt: string | Messages): Promise<string> {
? [{ role: "user", content: prompt }]
: prompt;
const response = await openai.chat.completions.create({
model: process.env.ZHIPU_MODEL_NAME || "glm-4",
messages: messages as OpenAI.Chat.Completions.ChatCompletionMessageParam[],
temperature: 0.2,
const response = await fetch("https://open.bigmodel.cn/api/paas/v4/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${process.env.ZHIPU_API_KEY}`,
},
body: JSON.stringify({
model: process.env.ZHIPU_MODEL_NAME || "glm-4.6",
messages,
temperature: 0.2,
thinking: {
type: "disabled"
}
}),
});
const content = response.choices[0]?.message?.content;
if (!response.ok) {
throw new Error(`AI API 请求失败: ${response.status}`);
}
const data = await response.json() as { choices?: Array<{ message?: { content?: string } }> };
const content = data.choices?.[0]?.message?.content;
if (!content) {
throw new Error("AI API 返回空响应");
}

View File

@@ -168,12 +168,12 @@ export async function executeTranslation(
let targetIpa: string | undefined;
if (needIpa) {
log.debug("[Stage 3] Generating IPA");
sourceIpa = await generateIPA(sourceText, detectedLanguage);
log.debug("[Stage 3] Source IPA", { sourceIpa });
targetIpa = await generateIPA(translatedText, targetLanguage);
log.debug("[Stage 3] Target IPA", { targetIpa });
log.debug("[Stage 3] Generating IPA in parallel");
[sourceIpa, targetIpa] = await Promise.all([
generateIPA(sourceText, detectedLanguage),
generateIPA(translatedText, targetLanguage),
]);
log.debug("[Stage 3] IPA complete", { sourceIpa, targetIpa });
}
// Assemble final result

View File

@@ -74,20 +74,8 @@ export async function repoDeleteUserCascade(dto: RepoInputDeleteUserCascade): Pr
log.info("Starting cascade delete for user", { userId });
await prisma.$transaction(async (tx) => {
await tx.revlog.deleteMany({
where: { card: { note: { userId } } }
});
await tx.card.deleteMany({
where: { note: { userId } }
});
await tx.note.deleteMany({
where: { userId }
});
await tx.noteType.deleteMany({
where: { userId }
where: { deck: { userId } }
});
await tx.deckFavorite.deleteMany({
@@ -107,14 +95,6 @@ export async function repoDeleteUserCascade(dto: RepoInputDeleteUserCascade): Pr
}
});
await tx.dictionaryLookUp.deleteMany({
where: { userId }
});
await tx.translationHistory.deleteMany({
where: { userId }
});
await tx.session.deleteMany({
where: { userId }
});

View File

@@ -1,196 +1,54 @@
import z from "zod";
import { generateValidator } from "@/utils/validate";
import type { RepoOutputCard, RepoOutputCardStats, CardMeaning, CardType } from "./card-repository-dto";
export type ActionOutputCard = RepoOutputCard;
export type ActionOutputCardStats = RepoOutputCardStats;
export type { CardMeaning, CardType };
const CardMeaningSchema = z.object({
partOfSpeech: z.string().nullable(),
definition: z.string(),
example: z.string().optional().nullable(),
});
export const schemaActionInputCreateCard = z.object({
noteId: z.bigint(),
deckId: z.number().int().positive(),
ord: z.number().int().min(0).optional(),
word: z.string().min(1),
ipa: z.string().optional().nullable(),
queryLang: z.string().min(1),
cardType: z.enum(["WORD", "PHRASE", "SENTENCE"]),
meanings: z.array(CardMeaningSchema).min(1),
});
export type ActionInputCreateCard = z.infer<typeof schemaActionInputCreateCard>;
export const validateActionInputCreateCard = generateValidator(schemaActionInputCreateCard);
export const schemaActionInputAnswerCard = z.object({
cardId: z.bigint(),
ease: z.union([
z.literal(1),
z.literal(2),
z.literal(3),
z.literal(4),
]),
export const schemaActionInputUpdateCard = z.object({
cardId: z.number().int().positive(),
word: z.string().min(1).optional(),
ipa: z.string().optional().nullable(),
meanings: z.array(CardMeaningSchema).min(1).optional(),
});
export type ActionInputAnswerCard = z.infer<typeof schemaActionInputAnswerCard>;
export const validateActionInputAnswerCard = generateValidator(schemaActionInputAnswerCard);
export type ActionInputUpdateCard = z.infer<typeof schemaActionInputUpdateCard>;
export const validateActionInputUpdateCard = generateValidator(schemaActionInputUpdateCard);
export const schemaActionInputGetCardsForReview = z.object({
deckId: z.number().int().positive(),
limit: z.number().int().min(1).max(100).optional(),
export const schemaActionInputDeleteCard = z.object({
cardId: z.number().int().positive(),
});
export type ActionInputGetCardsForReview = z.infer<typeof schemaActionInputGetCardsForReview>;
export const validateActionInputGetCardsForReview = generateValidator(schemaActionInputGetCardsForReview);
export const schemaActionInputGetNewCards = z.object({
deckId: z.number().int().positive(),
limit: z.number().int().min(1).max(100).optional(),
});
export type ActionInputGetNewCards = z.infer<typeof schemaActionInputGetNewCards>;
export const validateActionInputGetNewCards = generateValidator(schemaActionInputGetNewCards);
export type ActionInputDeleteCard = z.infer<typeof schemaActionInputDeleteCard>;
export const validateActionInputDeleteCard = generateValidator(schemaActionInputDeleteCard);
export const schemaActionInputGetCardsByDeckId = z.object({
deckId: z.number().int().positive(),
limit: z.number().int().min(1).max(100).optional(),
offset: z.number().int().min(0).optional(),
queue: z.union([
z.enum(["USER_BURIED", "SCHED_BURIED", "SUSPENDED", "NEW", "LEARNING", "REVIEW", "IN_LEARNING", "PREVIEW"]),
z.array(z.enum(["USER_BURIED", "SCHED_BURIED", "SUSPENDED", "NEW", "LEARNING", "REVIEW", "IN_LEARNING", "PREVIEW"])),
]).optional(),
});
export type ActionInputGetCardsByDeckId = z.infer<typeof schemaActionInputGetCardsByDeckId>;
export const validateActionInputGetCardsByDeckId = generateValidator(schemaActionInputGetCardsByDeckId);
export const schemaActionInputGetCardStats = z.object({
export const schemaActionInputGetRandomCard = z.object({
deckId: z.number().int().positive(),
excludeIds: z.array(z.number().int().positive()).optional(),
});
export type ActionInputGetCardStats = z.infer<typeof schemaActionInputGetCardStats>;
export const validateActionInputGetCardStats = generateValidator(schemaActionInputGetCardStats);
export const schemaActionInputDeleteCard = z.object({
cardId: z.bigint(),
});
export type ActionInputDeleteCard = z.infer<typeof schemaActionInputDeleteCard>;
export const validateActionInputDeleteCard = generateValidator(schemaActionInputDeleteCard);
export const schemaActionInputGetCardById = z.object({
cardId: z.bigint(),
});
export type ActionInputGetCardById = z.infer<typeof schemaActionInputGetCardById>;
export const validateActionInputGetCardById = generateValidator(schemaActionInputGetCardById);
export type ActionOutputCard = {
id: string;
noteId: string;
deckId: number;
ord: number;
mod: number;
usn: number;
type: "NEW" | "LEARNING" | "REVIEW" | "RELEARNING";
queue: "USER_BURIED" | "SCHED_BURIED" | "SUSPENDED" | "NEW" | "LEARNING" | "REVIEW" | "IN_LEARNING" | "PREVIEW";
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
createdAt: Date;
updatedAt: Date;
};
export type ActionOutputCardWithNote = ActionOutputCard & {
note: {
id: string;
flds: string;
sfld: string;
tags: string;
};
};
export type ActionOutputCardStats = {
total: number;
new: number;
learning: number;
review: number;
due: number;
};
export type ActionOutputScheduledCard = {
cardId: string;
newType: "NEW" | "LEARNING" | "REVIEW" | "RELEARNING";
newQueue: "USER_BURIED" | "SCHED_BURIED" | "SUSPENDED" | "NEW" | "LEARNING" | "REVIEW" | "IN_LEARNING" | "PREVIEW";
newDue: number;
newIvl: number;
newFactor: number;
newReps: number;
newLapses: number;
nextReviewDate: Date;
};
export type ActionOutputCreateCard = {
success: boolean;
message: string;
data?: {
cardId: string;
};
};
export type ActionOutputAnswerCard = {
success: boolean;
message: string;
data?: {
card: ActionOutputCard;
scheduled: ActionOutputScheduledCard;
};
};
export type ActionOutputGetCards = {
success: boolean;
message: string;
data?: ActionOutputCard[];
};
export type ActionOutputGetCardsWithNote = {
success: boolean;
message: string;
data?: ActionOutputCardWithNote[];
};
export type ActionOutputGetCardStats = {
success: boolean;
message: string;
data?: ActionOutputCardStats;
};
export type ActionOutputDeleteCard = {
success: boolean;
message: string;
};
export type ActionOutputGetCardById = {
success: boolean;
message: string;
data?: ActionOutputCardWithNote;
};
export const schemaActionInputResetDeckCards = z.object({
deckId: z.number().int().positive(),
});
export type ActionInputResetDeckCards = z.infer<typeof schemaActionInputResetDeckCards>;
export const validateActionInputResetDeckCards = generateValidator(schemaActionInputResetDeckCards);
export const schemaActionInputGetTodayStudyStats = z.object({
deckId: z.number().int().positive(),
});
export type ActionInputGetTodayStudyStats = z.infer<typeof schemaActionInputGetTodayStudyStats>;
export const validateActionInputGetTodayStudyStats = generateValidator(schemaActionInputGetTodayStudyStats);
export type ActionOutputGetTodayStudyStats = {
success: boolean;
message: string;
data?: ActionOutputTodayStudyStats;
};
export type ActionOutputTodayStudyStats = {
newStudied: number;
reviewStudied: number;
learningStudied: number;
totalStudied: number;
};
export type ActionOutputResetDeckCards = {
success: boolean;
message: string;
data?: {
count: number;
};
};
export type ActionInputGetRandomCard = z.infer<typeof schemaActionInputGetRandomCard>;
export const validateActionInputGetRandomCard = generateValidator(schemaActionInputGetRandomCard);

View File

@@ -4,308 +4,131 @@ import { auth } from "@/auth";
import { headers } from "next/headers";
import { createLogger } from "@/lib/logger";
import { ValidateError } from "@/lib/errors";
import {
ActionInputCreateCard,
ActionInputAnswerCard,
ActionInputGetCardsForReview,
ActionInputGetNewCards,
ActionInputGetCardsByDeckId,
ActionInputGetCardStats,
ActionInputDeleteCard,
ActionInputGetCardById,
ActionInputResetDeckCards,
ActionInputGetTodayStudyStats,
ActionOutputCreateCard,
ActionOutputAnswerCard,
ActionOutputGetCards,
ActionOutputGetCardsWithNote,
ActionOutputGetCardStats,
ActionOutputDeleteCard,
ActionOutputGetCardById,
ActionOutputResetDeckCards,
ActionOutputCard,
ActionOutputCardWithNote,
ActionOutputScheduledCard,
ActionOutputGetTodayStudyStats,
validateActionInputCreateCard,
validateActionInputAnswerCard,
validateActionInputGetCardsForReview,
validateActionInputGetNewCards,
validateActionInputGetCardsByDeckId,
validateActionInputGetCardStats,
validateActionInputDeleteCard,
validateActionInputGetCardById,
validateActionInputResetDeckCards,
validateActionInputGetTodayStudyStats,
} from "./card-action-dto";
import {
serviceCreateCard,
serviceAnswerCard,
serviceGetCardsForReview,
serviceGetNewCards,
serviceGetCardsByDeckId,
serviceGetCardsByDeckIdWithNotes,
serviceGetCardStats,
serviceUpdateCard,
serviceDeleteCard,
serviceGetCardByIdWithNote,
serviceCheckCardOwnership,
serviceResetDeckCards,
serviceGetTodayStudyStats,
serviceGetCardById,
serviceGetCardsByDeckId,
serviceGetRandomCard,
serviceGetCardStats,
serviceCheckDeckOwnership,
} from "./card-service";
import { CardQueue } from "../../../generated/prisma/enums";
import type { ActionOutputCard, ActionOutputCardStats } from "./card-action-dto";
import type { CardMeaning, CardType } from "./card-repository-dto";
import {
validateActionInputCreateCard,
validateActionInputUpdateCard,
validateActionInputDeleteCard,
validateActionInputGetCardsByDeckId,
validateActionInputGetRandomCard,
} from "./card-action-dto";
const log = createLogger("card-action");
function mapCardToOutput(card: {
id: bigint;
noteId: bigint;
deckId: number;
ord: number;
mod: number;
usn: number;
type: string;
queue: string;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
createdAt: Date;
updatedAt: Date;
}): ActionOutputCard {
function mapCardToOutput(card: any): ActionOutputCard {
return {
id: card.id.toString(),
noteId: card.noteId.toString(),
id: card.id,
deckId: card.deckId,
ord: card.ord,
mod: card.mod,
usn: card.usn,
type: card.type as ActionOutputCard["type"],
queue: card.queue as ActionOutputCard["queue"],
due: card.due,
ivl: card.ivl,
factor: card.factor,
reps: card.reps,
lapses: card.lapses,
left: card.left,
odue: card.odue,
odid: card.odid,
flags: card.flags,
data: card.data,
word: card.word,
ipa: card.ipa,
queryLang: card.queryLang,
cardType: card.cardType,
meanings: card.meanings,
createdAt: card.createdAt,
updatedAt: card.updatedAt,
};
}
function mapCardWithNoteToOutput(card: {
id: bigint;
noteId: bigint;
deckId: number;
ord: number;
mod: number;
usn: number;
type: string;
queue: string;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
createdAt: Date;
updatedAt: Date;
note: {
id: bigint;
flds: string;
sfld: string;
tags: string;
};
}): ActionOutputCardWithNote {
return {
...mapCardToOutput(card),
note: {
id: card.note.id.toString(),
flds: card.note.flds,
sfld: card.note.sfld,
tags: card.note.tags,
},
};
}
function mapScheduledToOutput(scheduled: {
cardId: bigint;
newType: string;
newQueue: string;
newDue: number;
newIvl: number;
newFactor: number;
newReps: number;
newLapses: number;
nextReviewDate: Date;
}): ActionOutputScheduledCard {
return {
cardId: scheduled.cardId.toString(),
newType: scheduled.newType as ActionOutputScheduledCard["newType"],
newQueue: scheduled.newQueue as ActionOutputScheduledCard["newQueue"],
newDue: scheduled.newDue,
newIvl: scheduled.newIvl,
newFactor: scheduled.newFactor,
newReps: scheduled.newReps,
newLapses: scheduled.newLapses,
nextReviewDate: scheduled.nextReviewDate,
};
}
async function checkCardOwnership(cardId: bigint): Promise<boolean> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) return false;
return serviceCheckCardOwnership({ cardId, userId: session.user.id });
}
async function getCurrentUserId(): Promise<string | null> {
const session = await auth.api.getSession({ headers: await headers() });
return session?.user?.id ?? null;
}
export async function actionCreateCard(
input: unknown,
): Promise<ActionOutputCreateCard> {
async function checkDeckOwnership(deckId: number): Promise<boolean> {
const userId = await getCurrentUserId();
if (!userId) return false;
return serviceCheckDeckOwnership({ deckId, userId });
}
export async function actionCreateCard(input: unknown) {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputCreateCard(input);
const cardId = await serviceCreateCard(validated);
return {
success: true,
message: "Card created successfully",
data: { cardId: cardId.toString() },
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to create card", { error: e });
return { success: false, message: "An error occurred while creating the card" };
}
}
export async function actionAnswerCard(
input: unknown,
): Promise<ActionOutputAnswerCard> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputAnswerCard(input);
const isOwner = await checkCardOwnership(validated.cardId);
const isOwner = await checkDeckOwnership(validated.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to answer this card" };
return { success: false, message: "You do not have permission to add cards to this deck" };
}
const result = await serviceAnswerCard(validated);
return {
success: true,
message: "Card answered successfully",
data: {
card: mapCardToOutput(result.card),
scheduled: mapScheduledToOutput(result.scheduled),
},
};
const result = await serviceCreateCard(validated);
return result;
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to answer card", { error: e });
return { success: false, message: "An error occurred while answering the card" };
log.error("Failed to create card", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to create card" };
}
}
export async function actionGetCardsForReview(
input: unknown,
): Promise<ActionOutputGetCardsWithNote> {
export async function actionUpdateCard(input: unknown) {
try {
const validated = validateActionInputUpdateCard(input);
const card = await serviceGetCardById(validated.cardId);
if (!card) {
return { success: false, message: "Card not found" };
}
const isOwner = await checkDeckOwnership(card.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to update this card" };
}
const result = await serviceUpdateCard(validated);
return result;
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to update card", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to update card" };
}
}
export async function actionDeleteCard(input: unknown) {
try {
const validated = validateActionInputDeleteCard(input);
const card = await serviceGetCardById(validated.cardId);
if (!card) {
return { success: false, message: "Card not found" };
}
const isOwner = await checkDeckOwnership(card.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to delete this card" };
}
const result = await serviceDeleteCard(validated);
return result;
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to delete card", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to delete card" };
}
}
export async function actionGetCardsByDeckId(input: unknown) {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetCardsForReview(input);
const cards = await serviceGetCardsForReview(validated);
return {
success: true,
message: "Cards fetched successfully",
data: cards.map(mapCardWithNoteToOutput),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get cards for review", { error: e });
return { success: false, message: "An error occurred while fetching cards" };
}
}
export async function actionGetNewCards(
input: unknown,
): Promise<ActionOutputGetCardsWithNote> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetNewCards(input);
const cards = await serviceGetNewCards(validated);
return {
success: true,
message: "New cards fetched successfully",
data: cards.map(mapCardWithNoteToOutput),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get new cards", { error: e });
return { success: false, message: "An error occurred while fetching new cards" };
}
}
export async function actionGetCardsByDeckId(
input: unknown,
): Promise<ActionOutputGetCards> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetCardsByDeckId(input);
const queue = validated.queue as CardQueue | CardQueue[] | undefined;
const cards = await serviceGetCardsByDeckId({
...validated,
queue,
});
const isOwner = await checkDeckOwnership(validated.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to view cards in this deck" };
}
const cards = await serviceGetCardsByDeckId(validated);
return {
success: true,
message: "Cards fetched successfully",
@@ -315,175 +138,83 @@ export async function actionGetCardsByDeckId(
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get cards by deck", { error: e });
return { success: false, message: "An error occurred while fetching cards" };
log.error("Failed to get cards", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to get cards" };
}
}
export async function actionGetCardsByDeckIdWithNotes(
input: unknown,
): Promise<ActionOutputGetCardsWithNote> {
export async function actionGetCardById(cardId: number) {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetCardsByDeckId(input);
const queue = validated.queue as CardQueue | CardQueue[] | undefined;
const cards = await serviceGetCardsByDeckIdWithNotes({
...validated,
queue,
});
const card = await serviceGetCardById(cardId);
if (!card) {
return { success: false, message: "Card not found" };
}
const isOwner = await checkDeckOwnership(card.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to view this card" };
}
return {
success: true,
message: "Cards fetched successfully",
data: cards.map(mapCardWithNoteToOutput),
message: "Card fetched successfully",
data: mapCardToOutput(card),
};
} catch (e) {
log.error("Failed to get card", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to get card" };
}
}
export async function actionGetRandomCard(input: unknown) {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetRandomCard(input);
const isOwner = await checkDeckOwnership(validated.deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to view cards in this deck" };
}
const card = await serviceGetRandomCard(validated);
if (!card) {
return { success: false, message: "No cards available" };
}
return {
success: true,
message: "Random card fetched successfully",
data: mapCardToOutput(card),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get cards by deck with notes", { error: e });
return { success: false, message: "An error occurred while fetching cards" };
log.error("Failed to get random card", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to get random card" };
}
}
export async function actionGetCardStats(
input: unknown,
): Promise<ActionOutputGetCardStats> {
export async function actionGetCardStats(deckId: number) {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetCardStats(input);
const stats = await serviceGetCardStats(validated);
const isOwner = await checkDeckOwnership(deckId);
if (!isOwner) {
return { success: false, message: "You do not have permission to view stats for this deck" };
}
const stats = await serviceGetCardStats(deckId);
return {
success: true,
message: "Card stats fetched successfully",
data: stats,
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get card stats", { error: e });
return { success: false, message: "An error occurred while fetching card stats" };
}
}
export async function actionDeleteCard(
input: unknown,
): Promise<ActionOutputDeleteCard> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputDeleteCard(input);
const isOwner = await checkCardOwnership(validated.cardId);
if (!isOwner) {
return { success: false, message: "You do not have permission to delete this card" };
}
await serviceDeleteCard(validated.cardId);
return { success: true, message: "Card deleted successfully" };
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to delete card", { error: e });
return { success: false, message: "An error occurred while deleting the card" };
}
}
export async function actionGetCardById(
input: unknown,
): Promise<ActionOutputGetCardById> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized" };
}
const validated = validateActionInputGetCardById(input);
const card = await serviceGetCardByIdWithNote(validated.cardId);
if (!card) {
return { success: false, message: "Card not found" };
}
return {
success: true,
message: "Card fetched successfully",
data: mapCardWithNoteToOutput(card),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get card by id", { error: e });
return { success: false, message: "An error occurred while fetching the card" };
}
}
export async function actionGetTodayStudyStats(
input: ActionInputGetTodayStudyStats,
): Promise<ActionOutputGetTodayStudyStats> {
try {
const validated = validateActionInputGetTodayStudyStats(input);
const stats = await serviceGetTodayStudyStats({ deckId: validated.deckId });
return {
success: true,
message: "Today's study stats fetched successfully",
data: stats,
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get today study stats", { error: e });
return { success: false, message: "An error occurred while fetching study stats" };
}
}
export async function actionResetDeckCards(
input: ActionInputResetDeckCards,
): Promise<ActionOutputResetDeckCards> {
try {
const userId = await getCurrentUserId();
if (!userId) {
return { success: false, message: "Unauthorized", data: { count: 0 } };
}
const validated = validateActionInputResetDeckCards(input);
const result = await serviceResetDeckCards({
deckId: validated.deckId,
userId,
});
if (!result.success) {
return { success: false, message: result.message, data: { count: 0 } };
}
return {
success: true,
message: result.message,
data: { count: result.count },
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message, data: { count: 0 } };
}
log.error("Failed to reset deck cards", { error: e });
return { success: false, message: "An error occurred while resetting deck cards", data: { count: 0 } };
log.error("Failed to get card stats", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Failed to get card stats" };
}
}

View File

@@ -1,127 +1,65 @@
import { CardType, CardQueue } from "../../../generated/prisma/enums";
export type CardMeaning = {
partOfSpeech: string | null;
definition: string;
example?: string | null;
}
export const CardTypeEnum = {
WORD: "WORD",
PHRASE: "PHRASE",
SENTENCE: "SENTENCE",
} as const;
export type CardType = keyof typeof CardTypeEnum;
export interface RepoInputCreateCard {
id: bigint;
noteId: bigint;
deckId: number;
ord: number;
due: number;
type?: CardType;
queue?: CardQueue;
ivl?: number;
factor?: number;
reps?: number;
lapses?: number;
left?: number;
odue?: number;
odid?: number;
flags?: number;
data?: string;
word: string;
ipa?: string | null;
queryLang: string;
cardType: CardType;
meanings: CardMeaning[];
}
export interface RepoInputUpdateCard {
ord?: number;
mod?: number;
usn?: number;
type?: CardType;
queue?: CardQueue;
due?: number;
ivl?: number;
factor?: number;
reps?: number;
lapses?: number;
left?: number;
odue?: number;
odid?: number;
flags?: number;
data?: string;
cardId: number;
word?: string;
ipa?: string | null;
meanings?: CardMeaning[];
}
export interface RepoInputDeleteCard {
cardId: number;
}
export interface RepoInputGetCardsByDeckId {
deckId: number;
limit?: number;
offset?: number;
queue?: CardQueue | CardQueue[];
}
export interface RepoInputGetCardsForReview {
export interface RepoInputGetRandomCard {
deckId: number;
limit?: number;
excludeIds?: number[];
}
export interface RepoInputGetNewCards {
deckId: number;
limit?: number;
}
export interface RepoInputBulkUpdateCard {
id: bigint;
data: RepoInputUpdateCard;
}
export interface RepoInputBulkUpdateCards {
cards: RepoInputBulkUpdateCard[];
export interface RepoInputCheckCardOwnership {
cardId: number;
userId: string;
}
export type RepoOutputCard = {
id: bigint;
noteId: bigint;
id: number;
deckId: number;
ord: number;
mod: number;
usn: number;
type: CardType;
queue: CardQueue;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
word: string;
ipa: string | null;
queryLang: string;
cardType: CardType;
meanings: CardMeaning[];
createdAt: Date;
updatedAt: Date;
};
export type RepoOutputCardWithNote = RepoOutputCard & {
note: {
id: bigint;
flds: string;
sfld: string;
tags: string;
};
};
}
export type RepoOutputCardStats = {
total: number;
new: number;
learning: number;
review: number;
due: number;
};
export type RepoOutputTodayStudyStats = {
newStudied: number;
reviewStudied: number;
learningStudied: number;
totalStudied: number;
};
export interface RepoInputGetTodayStudyStats {
deckId: number;
}
export interface RepoInputResetDeckCards {
deckId: number;
}
export type RepoOutputResetDeckCards = {
count: number;
};
export interface RepoInputGetTodayStudyStats {
deckId: number;
}

View File

@@ -1,273 +1,131 @@
import {
RepoInputCreateCard,
RepoInputUpdateCard,
RepoInputDeleteCard,
RepoInputGetCardsByDeckId,
RepoInputGetCardsForReview,
RepoInputGetNewCards,
RepoInputBulkUpdateCards,
RepoInputResetDeckCards,
RepoInputGetTodayStudyStats,
RepoInputGetRandomCard,
RepoInputCheckCardOwnership,
RepoOutputCard,
RepoOutputCardWithNote,
RepoOutputCardStats,
RepoOutputTodayStudyStats,
RepoOutputResetDeckCards,
CardMeaning,
} from "./card-repository-dto";
import { CardType, CardQueue } from "../../../generated/prisma/enums";
import { prisma } from "@/lib/db";
import { createLogger } from "@/lib/logger";
const log = createLogger("card-repository");
export async function repoCreateCard(
input: RepoInputCreateCard,
): Promise<bigint> {
log.debug("Creating card", { noteId: input.noteId.toString(), deckId: input.deckId });
export async function repoCreateCard(input: RepoInputCreateCard): Promise<number> {
log.debug("Creating card", { deckId: input.deckId, word: input.word });
const card = await prisma.card.create({
data: {
id: input.id,
noteId: input.noteId,
deckId: input.deckId,
ord: input.ord,
due: input.due,
mod: Math.floor(Date.now() / 1000),
type: input.type ?? CardType.NEW,
queue: input.queue ?? CardQueue.NEW,
ivl: input.ivl ?? 0,
factor: input.factor ?? 2500,
reps: input.reps ?? 0,
lapses: input.lapses ?? 0,
left: input.left ?? 0,
odue: input.odue ?? 0,
odid: input.odid ?? 0,
flags: input.flags ?? 0,
data: input.data ?? "",
word: input.word,
ipa: input.ipa,
queryLang: input.queryLang,
cardType: input.cardType,
meanings: {
create: input.meanings.map((m: CardMeaning) => ({
partOfSpeech: m.partOfSpeech,
definition: m.definition,
example: m.example,
})),
},
},
});
log.info("Card created", { cardId: card.id.toString() });
log.info("Card created", { cardId: card.id });
return card.id;
}
export async function repoUpdateCard(
id: bigint,
input: RepoInputUpdateCard,
): Promise<void> {
log.debug("Updating card", { cardId: id.toString() });
await prisma.card.update({
where: { id },
data: {
...input,
updatedAt: new Date(),
},
export async function repoUpdateCard(input: RepoInputUpdateCard): Promise<void> {
log.debug("Updating card", { cardId: input.cardId });
await prisma.$transaction(async (tx) => {
if (input.word !== undefined) {
await tx.card.update({
where: { id: input.cardId },
data: { word: input.word },
});
}
if (input.ipa !== undefined) {
await tx.card.update({
where: { id: input.cardId },
data: { ipa: input.ipa },
});
}
if (input.meanings !== undefined) {
await tx.cardMeaning.deleteMany({
where: { cardId: input.cardId },
});
await tx.cardMeaning.createMany({
data: input.meanings.map((m: CardMeaning) => ({
cardId: input.cardId,
partOfSpeech: m.partOfSpeech,
definition: m.definition,
example: m.example,
})),
});
}
await tx.card.update({
where: { id: input.cardId },
data: { updatedAt: new Date() },
});
});
log.info("Card updated", { cardId: id.toString() });
log.info("Card updated", { cardId: input.cardId });
}
export async function repoGetCardById(id: bigint): Promise<RepoOutputCard | null> {
const card = await prisma.card.findUnique({
where: { id },
});
return card;
}
export async function repoGetCardByIdWithNote(
id: bigint,
): Promise<RepoOutputCardWithNote | null> {
const card = await prisma.card.findUnique({
where: { id },
include: {
note: {
select: {
id: true,
flds: true,
sfld: true,
tags: true,
},
},
},
});
return card;
}
export async function repoGetCardsByDeckId(
input: RepoInputGetCardsByDeckId,
): Promise<RepoOutputCard[]> {
const { deckId, limit = 50, offset = 0, queue } = input;
const queueFilter = queue
? Array.isArray(queue)
? { in: queue }
: queue
: undefined;
const cards = await prisma.card.findMany({
where: {
deckId,
queue: queueFilter,
},
orderBy: { due: "asc" },
take: limit,
skip: offset,
});
log.debug("Fetched cards by deck", { deckId, count: cards.length });
return cards;
}
export async function repoGetCardsByDeckIdWithNotes(
input: RepoInputGetCardsByDeckId,
): Promise<RepoOutputCardWithNote[]> {
const { deckId, limit = 100, offset = 0, queue } = input;
const queueFilter = queue
? Array.isArray(queue)
? { in: queue }
: queue
: undefined;
const cards = await prisma.card.findMany({
where: {
deckId,
queue: queueFilter,
},
include: {
note: {
select: {
id: true,
flds: true,
sfld: true,
tags: true,
},
},
},
orderBy: { id: "asc" },
take: limit,
skip: offset,
});
log.debug("Fetched cards by deck with notes", { deckId, count: cards.length });
return cards;
}
export async function repoGetCardsForReview(
input: RepoInputGetCardsForReview,
): Promise<RepoOutputCardWithNote[]> {
const { deckId, limit = 20 } = input;
const now = Math.floor(Date.now() / 1000);
const todayDays = Math.floor(now / 86400);
const cards = await prisma.card.findMany({
where: {
deckId,
queue: { in: [CardQueue.NEW, CardQueue.LEARNING, CardQueue.REVIEW] },
OR: [
{ type: CardType.NEW },
{
type: { in: [CardType.LEARNING, CardType.REVIEW] },
due: { lte: todayDays },
},
],
},
include: {
note: {
select: {
id: true,
flds: true,
sfld: true,
tags: true,
},
},
},
orderBy: [
{ type: "asc" },
{ due: "asc" },
],
take: limit,
});
log.debug("Fetched cards for review", { deckId, count: cards.length });
return cards;
}
export async function repoGetNewCards(
input: RepoInputGetNewCards,
): Promise<RepoOutputCardWithNote[]> {
const { deckId, limit = 20 } = input;
const cards = await prisma.card.findMany({
where: {
deckId,
type: CardType.NEW,
queue: CardQueue.NEW,
},
include: {
note: {
select: {
id: true,
flds: true,
sfld: true,
tags: true,
},
},
},
orderBy: { due: "asc" },
take: limit,
});
log.debug("Fetched new cards", { deckId, count: cards.length });
return cards;
}
export async function repoDeleteCard(id: bigint): Promise<void> {
log.debug("Deleting card", { cardId: id.toString() });
export async function repoDeleteCard(input: RepoInputDeleteCard): Promise<void> {
log.debug("Deleting card", { cardId: input.cardId });
await prisma.card.delete({
where: { id },
where: { id: input.cardId },
});
log.info("Card deleted", { cardId: id.toString() });
log.info("Card deleted", { cardId: input.cardId });
}
export async function repoBulkUpdateCards(
input: RepoInputBulkUpdateCards,
): Promise<void> {
log.debug("Bulk updating cards", { count: input.cards.length });
await prisma.$transaction(
input.cards.map((item) =>
prisma.card.update({
where: { id: item.id },
data: {
...item.data,
updatedAt: new Date(),
},
}),
),
);
log.info("Bulk update completed", { count: input.cards.length });
export async function repoGetCardById(cardId: number): Promise<RepoOutputCard | null> {
const card = await prisma.card.findUnique({
where: { id: cardId },
include: { meanings: { orderBy: { createdAt: "asc" } } },
});
return card as RepoOutputCard | null;
}
export async function repoGetCardStats(deckId: number): Promise<RepoOutputCardStats> {
const now = Math.floor(Date.now() / 1000);
const todayDays = Math.floor(now / 86400);
const [total, newCards, learning, review, due] = await Promise.all([
prisma.card.count({ where: { deckId } }),
prisma.card.count({ where: { deckId, type: CardType.NEW } }),
prisma.card.count({ where: { deckId, type: CardType.LEARNING } }),
prisma.card.count({ where: { deckId, type: CardType.REVIEW } }),
prisma.card.count({
where: {
deckId,
type: { in: [CardType.LEARNING, CardType.REVIEW] },
due: { lte: todayDays },
},
}),
]);
return { total, new: newCards, learning, review, due };
export async function repoGetCardsByDeckId(input: RepoInputGetCardsByDeckId): Promise<RepoOutputCard[]> {
const { deckId, limit = 50, offset = 0 } = input;
const cards = await prisma.card.findMany({
where: { deckId },
include: { meanings: { orderBy: { createdAt: "asc" } } },
orderBy: { createdAt: "desc" },
take: limit,
skip: offset,
});
log.debug("Fetched cards by deck", { deckId, count: cards.length });
return cards as RepoOutputCard[];
}
export async function repoGetCardDeckOwnerId(cardId: bigint): Promise<string | null> {
export async function repoGetRandomCard(input: RepoInputGetRandomCard): Promise<RepoOutputCard | null> {
const { deckId, excludeIds = [] } = input;
const whereClause = excludeIds.length > 0
? { deckId, id: { notIn: excludeIds } }
: { deckId };
const count = await prisma.card.count({ where: whereClause });
if (count === 0) {
return null;
}
const skip = Math.floor(Math.random() * count);
const cards = await prisma.card.findMany({
where: whereClause,
include: { meanings: { orderBy: { createdAt: "asc" } } },
skip,
take: 1,
});
const card = cards[0];
if (!card) {
return null;
}
log.debug("Got random card", { cardId: card.id, deckId });
return card as RepoOutputCard;
}
export async function repoGetCardDeckOwnerId(cardId: number): Promise<string | null> {
const card = await prisma.card.findUnique({
where: { id: cardId },
include: {
@@ -279,106 +137,12 @@ export async function repoGetCardDeckOwnerId(cardId: bigint): Promise<string | n
return card?.deck.userId ?? null;
}
export async function repoGetNextDueCard(deckId: number): Promise<RepoOutputCard | null> {
const now = Math.floor(Date.now() / 1000);
const todayDays = Math.floor(now / 86400);
const card = await prisma.card.findFirst({
where: {
deckId,
queue: { in: [CardQueue.NEW, CardQueue.LEARNING, CardQueue.REVIEW] },
OR: [
{ type: CardType.NEW },
{
type: { in: [CardType.LEARNING, CardType.REVIEW] },
due: { lte: todayDays },
},
],
},
orderBy: [
{ type: "asc" },
{ due: "asc" },
],
});
return card;
export async function repoCheckCardOwnership(input: RepoInputCheckCardOwnership): Promise<boolean> {
const ownerId = await repoGetCardDeckOwnerId(input.cardId);
return ownerId === input.userId;
}
export async function repoGetCardsByNoteId(noteId: bigint): Promise<RepoOutputCard[]> {
const cards = await prisma.card.findMany({
where: { noteId },
orderBy: { ord: "asc" },
});
return cards;
}
export async function repoResetDeckCards(
input: RepoInputResetDeckCards,
): Promise<RepoOutputResetDeckCards> {
log.debug("Resetting deck cards", { deckId: input.deckId });
const result = await prisma.card.updateMany({
where: { deckId: input.deckId },
data: {
type: CardType.NEW,
queue: CardQueue.NEW,
due: 0,
ivl: 0,
factor: 2500,
reps: 0,
lapses: 1,
left: 1,
odue: 0,
odid: 0,
mod: Math.floor(Date.now() / 1000),
},
});
log.info("Deck cards reset", { deckId: input.deckId, count: result.count });
return { count: result.count };
}
export async function repoGetTodayStudyStats(
input: RepoInputGetTodayStudyStats,
): Promise<RepoOutputTodayStudyStats> {
const now = new Date();
const startOfToday = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
startOfToday.setUTCHours(0, 0, 0, 0);
const todayStart = startOfToday.getTime();
const stats = await prisma.revlog.groupBy({
by: ["type"],
where: {
card: {
deckId: input.deckId,
},
id: {
gte: todayStart,
},
},
_count: {
type: true,
},
});
let newStudied = 0;
let reviewStudied = 0;
let learningStudied = 0;
for (const stat of stats) {
if (stat.type === 0) {
newStudied = stat._count.type;
} else if (stat.type === 1) {
learningStudied = stat._count.type;
} else if (stat.type === 2 || stat.type === 3) {
reviewStudied += stat._count.type;
}
}
return {
newStudied,
reviewStudied,
learningStudied,
totalStudied: newStudied + reviewStudied + learningStudied,
};
export async function repoGetCardStats(deckId: number): Promise<RepoOutputCardStats> {
const total = await prisma.card.count({ where: { deckId } });
return { total };
}

View File

@@ -1,155 +1,4 @@
import { CardType, CardQueue } from "../../../generated/prisma/enums";
import type { RepoOutputCard, RepoOutputCardStats } from "./card-repository-dto";
export type ReviewEase = 1 | 2 | 3 | 4;
export interface ServiceInputCreateCard {
noteId: bigint;
deckId: number;
ord?: number;
}
export interface ServiceInputAnswerCard {
cardId: bigint;
ease: ReviewEase;
}
export interface ServiceInputGetCardsForReview {
deckId: number;
limit?: number;
}
export interface ServiceInputGetNewCards {
deckId: number;
limit?: number;
}
export interface ServiceInputGetCardsByDeckId {
deckId: number;
limit?: number;
offset?: number;
queue?: CardQueue | CardQueue[];
}
export interface ServiceInputGetCardStats {
deckId: number;
}
export interface ServiceInputCheckCardOwnership {
cardId: bigint;
userId: string;
}
export type ServiceOutputCheckCardOwnership = boolean;
export type ServiceOutputCard = {
id: bigint;
noteId: bigint;
deckId: number;
ord: number;
mod: number;
usn: number;
type: CardType;
queue: CardQueue;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
createdAt: Date;
updatedAt: Date;
};
export type ServiceOutputCardWithNote = ServiceOutputCard & {
note: {
id: bigint;
flds: string;
sfld: string;
tags: string;
};
};
export type ServiceOutputCardStats = {
total: number;
new: number;
learning: number;
review: number;
due: number;
};
export type ServiceOutputScheduledCard = {
cardId: bigint;
newType: CardType;
newQueue: CardQueue;
newDue: number;
newIvl: number;
newFactor: number;
newReps: number;
newLapses: number;
nextReviewDate: Date;
};
export type ServiceOutputReviewResult = {
success: boolean;
card: ServiceOutputCard;
scheduled: ServiceOutputScheduledCard;
};
export interface ServiceInputResetDeckCards {
deckId: number;
userId: string;
}
export interface ServiceInputCheckDeckOwnership {
deckId: number;
userId: string;
}
export type ServiceOutputCheckDeckOwnership = boolean;
export type ServiceOutputResetDeckCards = {
success: boolean;
count: number;
message: string;
};
export type ServiceInputGetTodayStudyStats = {
deckId: number;
};
export type ServiceOutputTodayStudyStats = {
newStudied: number;
reviewStudied: number;
learningStudied: number;
totalStudied: number;
};
export const SM2_CONFIG = {
LEARNING_STEPS: [1, 10],
RELEARNING_STEPS: [10],
GRADUATING_INTERVAL_GOOD: 1,
GRADUATING_INTERVAL_EASY: 4,
EASY_INTERVAL: 4,
MINIMUM_FACTOR: 1300,
DEFAULT_FACTOR: 2500,
MAXIMUM_INTERVAL: 36500,
EASY_BONUS: 1.3,
HARD_INTERVAL: 1.2,
NEW_INTERVAL: 0.0,
INTERVAL_MODIFIER: 1.0,
FACTOR_ADJUSTMENTS: {
1: -200,
2: -150,
3: 0,
4: 150,
},
INITIAL_INTERVALS: {
2: 1,
3: 3,
4: 4,
},
} as const;
export type ServiceOutputCard = RepoOutputCard;
export type ServiceOutputCardStats = RepoOutputCardStats;

View File

@@ -1,543 +1,104 @@
import type {
RepoOutputCard,
RepoOutputCardStats,
CardMeaning,
CardType,
} from "./card-repository-dto";
import type {
RepoInputCreateCard,
RepoInputUpdateCard,
RepoInputDeleteCard,
RepoInputGetCardsByDeckId,
RepoInputGetRandomCard,
RepoInputCheckCardOwnership,
} from "./card-repository-dto";
import { repoGetUserIdByDeckId } from "@/modules/deck/deck-repository";
import { createLogger } from "@/lib/logger";
import {
repoCreateCard,
repoUpdateCard,
repoGetCardById,
repoGetCardByIdWithNote,
repoGetCardsByDeckId,
repoGetCardsByDeckIdWithNotes,
repoGetCardsForReview,
repoGetNewCards,
repoGetCardStats,
repoDeleteCard,
repoGetCardsByNoteId,
repoGetCardDeckOwnerId,
repoResetDeckCards,
repoGetTodayStudyStats,
repoGetCardById,
repoGetCardsByDeckId,
repoGetRandomCard,
repoGetCardStats,
repoCheckCardOwnership,
} from "./card-repository";
import { repoGetUserIdByDeckId } from "@/modules/deck/deck-repository";
import {
RepoInputUpdateCard,
RepoOutputCard,
} from "./card-repository-dto";
import {
ServiceInputCreateCard,
ServiceInputAnswerCard,
ServiceInputGetCardsForReview,
ServiceInputGetNewCards,
ServiceInputGetCardsByDeckId,
ServiceInputGetCardStats,
ServiceInputCheckCardOwnership,
ServiceInputResetDeckCards,
ServiceInputCheckDeckOwnership,
ServiceInputGetTodayStudyStats,
ServiceOutputCard,
ServiceOutputCardWithNote,
ServiceOutputCardStats,
ServiceOutputScheduledCard,
ServiceOutputReviewResult,
ServiceOutputCheckCardOwnership,
ServiceOutputResetDeckCards,
ServiceOutputTodayStudyStats,
ReviewEase,
SM2_CONFIG,
} from "./card-service-dto";
import { CardType, CardQueue } from "../../../generated/prisma/enums";
const log = createLogger("card-service");
function generateCardId(): bigint {
return BigInt(Date.now());
export type { CardMeaning as ServiceCardMeaning, CardType as ServiceCardType };
export type ServiceInputCreateCard = RepoInputCreateCard;
export type ServiceInputUpdateCard = RepoInputUpdateCard;
export type ServiceInputDeleteCard = RepoInputDeleteCard;
export type ServiceInputGetCardsByDeckId = RepoInputGetCardsByDeckId;
export type ServiceInputGetRandomCard = RepoInputGetRandomCard;
export type ServiceInputCheckCardOwnership = RepoInputCheckCardOwnership;
export type ServiceInputCheckDeckOwnership = {
deckId: number;
userId: string;
};
export type ServiceOutputCard = RepoOutputCard;
export type ServiceOutputCardStats = RepoOutputCardStats;
export async function serviceCreateCard(input: ServiceInputCreateCard): Promise<{ success: boolean; cardId?: number; message: string }> {
log.info("Creating card", { deckId: input.deckId, word: input.word });
const cardId = await repoCreateCard(input);
log.info("Card created", { cardId });
return { success: true, cardId, message: "Card created successfully" };
}
function calculateDueDate(intervalDays: number): number {
const now = Math.floor(Date.now() / 1000);
const todayStart = Math.floor(now / 86400) * 86400;
return Math.floor(todayStart / 86400) + intervalDays;
}
function calculateNextReviewTime(intervalDays: number): Date {
const now = Date.now();
return new Date(now + intervalDays * 86400 * 1000);
}
function clampInterval(interval: number): number {
return Math.min(Math.max(1, interval), SM2_CONFIG.MAXIMUM_INTERVAL);
}
function scheduleNewCard(ease: ReviewEase, currentFactor: number): {
type: CardType;
queue: CardQueue;
ivl: number;
due: number;
newFactor: number;
} {
if (ease === 1) {
return {
type: CardType.LEARNING,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + SM2_CONFIG.LEARNING_STEPS[0] * 60,
newFactor: currentFactor,
};
}
if (ease === 2) {
if (SM2_CONFIG.LEARNING_STEPS.length >= 2) {
const avgStep = (SM2_CONFIG.LEARNING_STEPS[0] + SM2_CONFIG.LEARNING_STEPS[1]) / 2;
return {
type: CardType.LEARNING,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + avgStep * 60,
newFactor: currentFactor,
};
}
return {
type: CardType.LEARNING,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + SM2_CONFIG.LEARNING_STEPS[0] * 60,
newFactor: currentFactor,
};
}
if (ease === 3) {
if (SM2_CONFIG.LEARNING_STEPS.length >= 2) {
return {
type: CardType.LEARNING,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + SM2_CONFIG.LEARNING_STEPS[1] * 60,
newFactor: currentFactor,
};
}
const ivl = SM2_CONFIG.GRADUATING_INTERVAL_GOOD;
return {
type: CardType.REVIEW,
queue: CardQueue.REVIEW,
ivl,
due: calculateDueDate(ivl),
newFactor: SM2_CONFIG.DEFAULT_FACTOR,
};
}
const ivl = SM2_CONFIG.EASY_INTERVAL;
const newFactor = SM2_CONFIG.DEFAULT_FACTOR + SM2_CONFIG.FACTOR_ADJUSTMENTS[4];
return {
type: CardType.REVIEW,
queue: CardQueue.REVIEW,
ivl,
due: calculateDueDate(ivl),
newFactor: Math.max(SM2_CONFIG.MINIMUM_FACTOR, newFactor),
};
}
function scheduleLearningCard(ease: ReviewEase, currentFactor: number, left: number, isRelearning: boolean): {
type: CardType;
queue: CardQueue;
ivl: number;
due: number;
newFactor: number;
newLeft: number;
} {
const steps = isRelearning ? SM2_CONFIG.RELEARNING_STEPS : SM2_CONFIG.LEARNING_STEPS;
const totalSteps = steps.length;
const cardType = isRelearning ? CardType.RELEARNING : CardType.LEARNING;
if (ease === 1) {
return {
type: cardType,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + steps[0] * 60,
newFactor: currentFactor,
newLeft: totalSteps * 1000,
};
}
const stepIndex = Math.floor(left % 1000);
if (ease === 2) {
if (stepIndex === 0 && steps.length >= 2) {
const step0 = steps[0] ?? 1;
const step1 = steps[1] ?? step0;
const avgStep = (step0 + step1) / 2;
return {
type: cardType,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + avgStep * 60,
newFactor: currentFactor,
newLeft: left,
};
}
const currentStepDelay = steps[stepIndex] ?? steps[0] ?? 1;
return {
type: cardType,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + currentStepDelay * 60,
newFactor: currentFactor,
newLeft: left,
};
}
if (ease === 3) {
if (stepIndex < steps.length - 1) {
const nextStep = stepIndex + 1;
const nextStepDelay = steps[nextStep] ?? steps[0];
return {
type: cardType,
queue: CardQueue.LEARNING,
ivl: 0,
due: Math.floor(Date.now() / 1000) + nextStepDelay * 60,
newFactor: currentFactor,
newLeft: nextStep * 1000 + (totalSteps - nextStep),
};
}
const ivl = SM2_CONFIG.GRADUATING_INTERVAL_GOOD;
return {
type: CardType.REVIEW,
queue: CardQueue.REVIEW,
ivl,
due: calculateDueDate(ivl),
newFactor: SM2_CONFIG.DEFAULT_FACTOR,
newLeft: 0,
};
}
const ivl = SM2_CONFIG.GRADUATING_INTERVAL_EASY;
const newFactor = SM2_CONFIG.DEFAULT_FACTOR + SM2_CONFIG.FACTOR_ADJUSTMENTS[4];
return {
type: CardType.REVIEW,
queue: CardQueue.REVIEW,
ivl,
due: calculateDueDate(ivl),
newFactor: Math.max(SM2_CONFIG.MINIMUM_FACTOR, newFactor),
newLeft: 0,
};
}
function scheduleReviewCard(
ease: ReviewEase,
currentIvl: number,
currentFactor: number,
lapses: number,
): {
type: CardType;
queue: CardQueue;
ivl: number;
due: number;
newFactor: number;
newLapses: number;
} {
if (ease === 1) {
const newFactor = Math.max(SM2_CONFIG.MINIMUM_FACTOR, currentFactor + SM2_CONFIG.FACTOR_ADJUSTMENTS[1]);
const newIvl = Math.max(1, Math.floor(currentIvl * SM2_CONFIG.NEW_INTERVAL));
return {
type: CardType.RELEARNING,
queue: CardQueue.LEARNING,
ivl: newIvl,
due: Math.floor(Date.now() / 1000) + SM2_CONFIG.RELEARNING_STEPS[0] * 60,
newFactor,
newLapses: lapses + 1,
};
}
let newFactor: number;
let newIvl: number;
if (ease === 2) {
newFactor = Math.max(SM2_CONFIG.MINIMUM_FACTOR, currentFactor + SM2_CONFIG.FACTOR_ADJUSTMENTS[2]);
newIvl = Math.floor(currentIvl * SM2_CONFIG.HARD_INTERVAL * SM2_CONFIG.INTERVAL_MODIFIER);
} else if (ease === 3) {
newFactor = currentFactor;
newIvl = Math.floor(currentIvl * (currentFactor / 1000) * SM2_CONFIG.INTERVAL_MODIFIER);
} else {
newIvl = Math.floor(currentIvl * (currentFactor / 1000) * SM2_CONFIG.EASY_BONUS * SM2_CONFIG.INTERVAL_MODIFIER);
newFactor = Math.max(SM2_CONFIG.MINIMUM_FACTOR, currentFactor + SM2_CONFIG.FACTOR_ADJUSTMENTS[4]);
}
newIvl = clampInterval(newIvl);
newIvl = Math.max(currentIvl + 1, newIvl);
return {
type: CardType.REVIEW,
queue: CardQueue.REVIEW,
ivl: newIvl,
due: calculateDueDate(newIvl),
newFactor,
newLapses: lapses,
};
}
function mapToServiceOutput(card: RepoOutputCard): ServiceOutputCard {
return {
id: card.id,
noteId: card.noteId,
deckId: card.deckId,
ord: card.ord,
mod: card.mod,
usn: card.usn,
type: card.type,
queue: card.queue,
due: card.due,
ivl: card.ivl,
factor: card.factor,
reps: card.reps,
lapses: card.lapses,
left: card.left,
odue: card.odue,
odid: card.odid,
flags: card.flags,
data: card.data,
createdAt: card.createdAt,
updatedAt: card.updatedAt,
};
}
export async function serviceCreateCard(
input: ServiceInputCreateCard,
): Promise<bigint> {
log.info("Creating card from note", { noteId: input.noteId.toString(), deckId: input.deckId });
const existingCards = await repoGetCardsByNoteId(input.noteId);
const maxOrd = existingCards.reduce((max, c) => Math.max(max, c.ord), -1);
const ord = input.ord ?? maxOrd + 1;
const cardId = await repoCreateCard({
id: generateCardId(),
noteId: input.noteId,
deckId: input.deckId,
ord,
due: ord,
type: CardType.NEW,
queue: CardQueue.NEW,
});
log.info("Card created", { cardId: cardId.toString() });
return cardId;
}
export async function serviceAnswerCard(
input: ServiceInputAnswerCard,
): Promise<ServiceOutputReviewResult> {
log.info("Answering card", { cardId: input.cardId.toString(), ease: input.ease });
export async function serviceUpdateCard(input: ServiceInputUpdateCard): Promise<{ success: boolean; message: string }> {
log.info("Updating card", { cardId: input.cardId });
const card = await repoGetCardById(input.cardId);
if (!card) {
throw new Error(`Card not found: ${input.cardId.toString()}`);
return { success: false, message: "Card not found" };
}
await repoUpdateCard(input);
log.info("Card updated", { cardId: input.cardId });
return { success: true, message: "Card updated successfully" };
}
const { ease } = input;
let updateData: RepoInputUpdateCard;
let scheduled: ServiceOutputScheduledCard;
if (card.type === CardType.NEW) {
const result = scheduleNewCard(ease, card.factor);
updateData = {
type: result.type,
queue: result.queue,
ivl: result.ivl,
due: result.due,
factor: result.newFactor,
reps: card.reps + 1,
left: result.type === CardType.LEARNING
? SM2_CONFIG.LEARNING_STEPS.length * 1000
: 0,
mod: Math.floor(Date.now() / 1000),
};
scheduled = {
cardId: card.id,
newType: result.type,
newQueue: result.queue,
newDue: result.due,
newIvl: result.ivl,
newFactor: result.newFactor,
newReps: card.reps + 1,
newLapses: card.lapses,
nextReviewDate: calculateNextReviewTime(result.ivl),
};
} else if (card.type === CardType.LEARNING || card.type === CardType.RELEARNING) {
const result = scheduleLearningCard(ease, card.factor, card.left, card.type === CardType.RELEARNING);
updateData = {
type: result.type,
queue: result.queue,
ivl: result.ivl,
due: result.due,
factor: result.newFactor,
reps: card.reps + 1,
left: result.newLeft,
mod: Math.floor(Date.now() / 1000),
};
scheduled = {
cardId: card.id,
newType: result.type,
newQueue: result.queue,
newDue: result.due,
newIvl: result.ivl,
newFactor: result.newFactor,
newReps: card.reps + 1,
newLapses: card.lapses,
nextReviewDate: calculateNextReviewTime(result.ivl),
};
} else {
const result = scheduleReviewCard(ease, card.ivl, card.factor, card.lapses);
updateData = {
type: result.type,
queue: result.queue,
ivl: result.ivl,
due: result.due,
factor: result.newFactor,
reps: card.reps + 1,
lapses: result.newLapses,
left: result.type === CardType.RELEARNING
? SM2_CONFIG.RELEARNING_STEPS.length * 1000
: 0,
mod: Math.floor(Date.now() / 1000),
};
scheduled = {
cardId: card.id,
newType: result.type,
newQueue: result.queue,
newDue: result.due,
newIvl: result.ivl,
newFactor: result.newFactor,
newReps: card.reps + 1,
newLapses: result.newLapses,
nextReviewDate: calculateNextReviewTime(result.ivl),
};
export async function serviceDeleteCard(input: ServiceInputDeleteCard): Promise<{ success: boolean; message: string }> {
log.info("Deleting card", { cardId: input.cardId });
const card = await repoGetCardById(input.cardId);
if (!card) {
return { success: false, message: "Card not found" };
}
await repoUpdateCard(input.cardId, updateData);
const updatedCard = await repoGetCardById(input.cardId);
if (!updatedCard) {
throw new Error(`Card not found after update: ${input.cardId.toString()}`);
}
log.info("Card answered and scheduled", {
cardId: input.cardId.toString(),
newType: scheduled.newType,
newIvl: scheduled.newIvl,
nextReview: scheduled.nextReviewDate.toISOString(),
});
return {
success: true,
card: mapToServiceOutput(updatedCard),
scheduled,
};
await repoDeleteCard(input);
log.info("Card deleted", { cardId: input.cardId });
return { success: true, message: "Card deleted successfully" };
}
export async function serviceGetNextCardForReview(
deckId: number,
): Promise<ServiceOutputCardWithNote | null> {
log.debug("Getting next card for review", { deckId });
const cards = await repoGetCardsForReview({ deckId, limit: 1 });
return cards[0] ?? null;
export async function serviceGetCardById(cardId: number): Promise<ServiceOutputCard | null> {
return repoGetCardById(cardId);
}
export async function serviceGetCardsForReview(
input: ServiceInputGetCardsForReview,
): Promise<ServiceOutputCardWithNote[]> {
log.debug("Getting cards for review", { deckId: input.deckId });
return repoGetCardsForReview(input);
}
export async function serviceGetNewCards(
input: ServiceInputGetNewCards,
): Promise<ServiceOutputCardWithNote[]> {
log.debug("Getting new cards", { deckId: input.deckId });
return repoGetNewCards(input);
}
export async function serviceGetCardsByDeckId(
input: ServiceInputGetCardsByDeckId,
): Promise<ServiceOutputCard[]> {
export async function serviceGetCardsByDeckId(input: ServiceInputGetCardsByDeckId): Promise<ServiceOutputCard[]> {
log.debug("Getting cards by deck", { deckId: input.deckId });
const cards = await repoGetCardsByDeckId(input);
return cards.map(mapToServiceOutput);
return repoGetCardsByDeckId(input);
}
export async function serviceGetCardsByDeckIdWithNotes(
input: ServiceInputGetCardsByDeckId,
): Promise<ServiceOutputCardWithNote[]> {
log.debug("Getting cards by deck with notes", { deckId: input.deckId });
return repoGetCardsByDeckIdWithNotes(input);
export async function serviceGetRandomCard(input: ServiceInputGetRandomCard): Promise<ServiceOutputCard | null> {
log.debug("Getting random card", { deckId: input.deckId });
return repoGetRandomCard(input);
}
export async function serviceGetCardById(
cardId: bigint,
): Promise<ServiceOutputCard | null> {
const card = await repoGetCardById(cardId);
return card ? mapToServiceOutput(card) : null;
export async function serviceGetCardStats(deckId: number): Promise<ServiceOutputCardStats> {
log.debug("Getting card stats", { deckId });
return repoGetCardStats(deckId);
}
export async function serviceGetCardByIdWithNote(
cardId: bigint,
): Promise<ServiceOutputCardWithNote | null> {
return repoGetCardByIdWithNote(cardId);
export async function serviceCheckCardOwnership(input: ServiceInputCheckCardOwnership): Promise<boolean> {
log.debug("Checking card ownership", { cardId: input.cardId });
return repoCheckCardOwnership(input);
}
export async function serviceGetCardStats(
input: ServiceInputGetCardStats,
): Promise<ServiceOutputCardStats> {
log.debug("Getting card stats", { deckId: input.deckId });
return repoGetCardStats(input.deckId);
}
export async function serviceDeleteCard(cardId: bigint): Promise<void> {
log.info("Deleting card", { cardId: cardId.toString() });
await repoDeleteCard(cardId);
}
export async function serviceCheckCardOwnership(
input: ServiceInputCheckCardOwnership,
): Promise<ServiceOutputCheckCardOwnership> {
log.debug("Checking card ownership", { cardId: input.cardId.toString() });
const ownerId = await repoGetCardDeckOwnerId(input.cardId);
return ownerId === input.userId;
}
export async function serviceCheckDeckOwnership(
input: ServiceInputCheckDeckOwnership,
): Promise<ServiceOutputCheckCardOwnership> {
export async function serviceCheckDeckOwnership(input: ServiceInputCheckDeckOwnership): Promise<boolean> {
log.debug("Checking deck ownership", { deckId: input.deckId });
const ownerId = await repoGetUserIdByDeckId(input.deckId);
return ownerId === input.userId;
}
export async function serviceResetDeckCards(
input: ServiceInputResetDeckCards,
): Promise<ServiceOutputResetDeckCards> {
log.info("Resetting deck cards", { deckId: input.deckId, userId: input.userId });
const isOwner = await serviceCheckDeckOwnership({ deckId: input.deckId, userId: input.userId });
if (!isOwner) {
return { success: false, count: 0, message: "You do not have permission to reset this deck" };
}
const result = await repoResetDeckCards({ deckId: input.deckId });
log.info("Deck cards reset successfully", { deckId: input.deckId, count: result.count });
return { success: true, count: result.count, message: "Deck cards reset successfully" };
}
export async function serviceGetTodayStudyStats(
input: ServiceInputGetTodayStudyStats,
): Promise<ServiceOutputTodayStudyStats> {
log.debug("Getting today study stats", { deckId: input.deckId });
const repoStats = await repoGetTodayStudyStats({ deckId: input.deckId });
return {
newStudied: repoStats.newStudied,
reviewStudied: repoStats.reviewStudied,
learningStudied: repoStats.learningStudied,
totalStudied: repoStats.totalStudied,
};
}

View File

@@ -14,9 +14,6 @@ export const schemaActionInputUpdateDeck = z.object({
name: z.string().min(1).max(100).optional(),
desc: z.string().max(500).optional(),
visibility: z.enum(["PRIVATE", "PUBLIC"]).optional(),
collapsed: z.boolean().optional(),
newPerDay: z.number().int().min(0).max(999).optional(),
revPerDay: z.number().int().min(0).max(9999).optional(),
});
export type ActionInputUpdateDeck = z.infer<typeof schemaActionInputUpdateDeck>;
export const validateActionInputUpdateDeck = generateValidator(schemaActionInputUpdateDeck);
@@ -46,10 +43,6 @@ export type ActionOutputDeck = {
desc: string;
userId: string;
visibility: "PRIVATE" | "PUBLIC";
collapsed: boolean;
conf: unknown;
newPerDay: number;
revPerDay: number;
createdAt: Date;
updatedAt: Date;
cardCount?: number;

View File

@@ -100,9 +100,6 @@ export async function actionUpdateDeck(input: ActionInputUpdateDeck): Promise<Ac
name: validatedInput.name,
desc: validatedInput.desc,
visibility: validatedInput.visibility as Visibility | undefined,
collapsed: validatedInput.collapsed,
newPerDay: validatedInput.newPerDay,
revPerDay: validatedInput.revPerDay,
});
} catch (e) {
if (e instanceof ValidateError) {

View File

@@ -1,94 +1,74 @@
import { Visibility } from "../../../generated/prisma/enums";
import type { DictionaryItemWithEntries } from "@/shared/card-type";
export interface RepoInputCreateDeck {
name: string;
desc?: string;
userId: string;
visibility?: Visibility;
}
export interface RepoInputUpdateDeck {
id: number;
name?: string;
desc?: string;
visibility?: Visibility;
collapsed?: boolean;
newPerDay?: number;
revPerDay?: number;
}
export interface RepoInputGetDeckById {
id: number;
}
export interface RepoInputGetDecksByUserId {
userId: string;
}
export interface RepoInputGetPublicDecks {
limit?: number;
offset?: number;
orderBy?: "createdAt" | "name";
}
export interface RepoInputDeleteDeck {
id: number;
}
export type RepoOutputDeck = {
id: number;
name: string;
desc: string;
userId: string;
visibility: Visibility;
collapsed: boolean;
conf: unknown;
newPerDay: number;
revPerDay: number;
createdAt: Date;
updatedAt: Date;
cardCount?: number;
};
export type RepoOutputPublicDeck = RepoOutputDeck & {
userName: string | null;
userUsername: string | null;
favoriteCount: number;
};
export type RepoOutputDeckOwnership = {
userId: string;
};
export interface RepoInputToggleDeckFavorite {
deckId: number;
userId: string;
}
export interface RepoInputCheckDeckFavorite {
deckId: number;
userId: string;
}
export interface RepoInputSearchPublicDecks {
query: string;
limit?: number;
offset?: number;
}
export interface RepoInputGetPublicDeckById {
deckId: number;
}
export type RepoOutputDeckFavorite = {
isFavorited: boolean;
favoriteCount: number;
};
export interface RepoInputGetUserFavoriteDecks {
userId: string;
}
export type RepoOutputUserFavoriteDeck = RepoOutputPublicDeck & {
favoritedAt: Date;
};

View File

@@ -57,10 +57,6 @@ export async function repoGetDeckById(input: RepoInputGetDeckById): Promise<Repo
desc: deck.desc,
userId: deck.userId,
visibility: deck.visibility,
collapsed: deck.collapsed,
conf: deck.conf,
newPerDay: deck.newPerDay,
revPerDay: deck.revPerDay,
createdAt: deck.createdAt,
updatedAt: deck.updatedAt,
cardCount: deck._count?.cards ?? 0,
@@ -86,10 +82,6 @@ export async function repoGetDecksByUserId(input: RepoInputGetDecksByUserId): Pr
desc: deck.desc,
userId: deck.userId,
visibility: deck.visibility,
collapsed: deck.collapsed,
conf: deck.conf,
newPerDay: deck.newPerDay,
revPerDay: deck.revPerDay,
createdAt: deck.createdAt,
updatedAt: deck.updatedAt,
cardCount: deck._count?.cards ?? 0,
@@ -120,10 +112,6 @@ export async function repoGetPublicDecks(input: RepoInputGetPublicDecks = {}): P
desc: deck.desc,
userId: deck.userId,
visibility: deck.visibility,
collapsed: deck.collapsed,
conf: deck.conf,
newPerDay: deck.newPerDay,
revPerDay: deck.revPerDay,
createdAt: deck.createdAt,
updatedAt: deck.updatedAt,
cardCount: deck._count?.cards ?? 0,
@@ -179,10 +167,6 @@ export async function repoGetPublicDeckById(input: RepoInputGetPublicDeckById):
desc: deck.desc,
userId: deck.userId,
visibility: deck.visibility,
collapsed: deck.collapsed,
conf: deck.conf,
newPerDay: deck.newPerDay,
revPerDay: deck.revPerDay,
createdAt: deck.createdAt,
updatedAt: deck.updatedAt,
cardCount: deck._count?.cards ?? 0,
@@ -285,10 +269,6 @@ export async function repoSearchPublicDecks(input: RepoInputSearchPublicDecks):
desc: deck.desc,
userId: deck.userId,
visibility: deck.visibility,
collapsed: deck.collapsed,
conf: deck.conf,
newPerDay: deck.newPerDay,
revPerDay: deck.revPerDay,
createdAt: deck.createdAt,
updatedAt: deck.updatedAt,
cardCount: deck._count?.cards ?? 0,
@@ -324,10 +304,6 @@ export async function repoGetUserFavoriteDecks(
desc: fav.deck.desc,
userId: fav.deck.userId,
visibility: fav.deck.visibility,
collapsed: fav.deck.collapsed,
conf: fav.deck.conf,
newPerDay: fav.deck.newPerDay,
revPerDay: fav.deck.revPerDay,
createdAt: fav.deck.createdAt,
updatedAt: fav.deck.updatedAt,
cardCount: fav.deck._count?.cards ?? 0,

View File

@@ -12,9 +12,6 @@ export type ServiceInputUpdateDeck = {
name?: string;
desc?: string;
visibility?: Visibility;
collapsed?: boolean;
newPerDay?: number;
revPerDay?: number;
};
export type ServiceInputDeleteDeck = {
@@ -45,10 +42,6 @@ export type ServiceOutputDeck = {
desc: string;
userId: string;
visibility: Visibility;
collapsed: boolean;
conf: unknown;
newPerDay: number;
revPerDay: number;
createdAt: Date;
updatedAt: Date;
cardCount?: number;

View File

@@ -58,9 +58,6 @@ export async function serviceUpdateDeck(input: ServiceInputUpdateDeck): Promise<
name: input.name,
desc: input.desc,
visibility: input.visibility,
collapsed: input.collapsed,
newPerDay: input.newPerDay,
revPerDay: input.revPerDay,
});
log.info("Deck updated successfully", { deckId: input.deckId });
return { success: true, message: "Deck updated successfully" };

View File

@@ -1,22 +1,25 @@
import { TSharedItem } from "@/shared/dictionary-type";
import { LENGTH_MAX_DICTIONARY_TEXT, LENGTH_MAX_LANGUAGE, LENGTH_MIN_DICTIONARY_TEXT, LENGTH_MIN_LANGUAGE } from "@/shared/constant";
import { generateValidator } from "@/utils/validate";
import z from "zod";
import { generateValidator } from "@/utils/validate";
const schemaActionInputLookUpDictionary = z.object({
text: z.string().min(LENGTH_MIN_DICTIONARY_TEXT).max(LENGTH_MAX_DICTIONARY_TEXT),
queryLang: z.string().min(LENGTH_MIN_LANGUAGE).max(LENGTH_MAX_LANGUAGE),
forceRelook: z.boolean(),
definitionLang: z.string().min(LENGTH_MIN_LANGUAGE).max(LENGTH_MAX_LANGUAGE),
userId: z.string().optional()
export const schemaActionLookUpDictionary = z.object({
text: z.string().min(1),
queryLang: z.string().min(1),
definitionLang: z.string().min(1),
});
export type ActionInputLookUpDictionary = z.infer<typeof schemaActionInputLookUpDictionary>;
export const validateActionInputLookUpDictionary = generateValidator(schemaActionInputLookUpDictionary);
export type ActionInputLookUpDictionary = z.infer<typeof schemaActionLookUpDictionary>;
export const validateActionInputLookUpDictionary = generateValidator(schemaActionLookUpDictionary);
export type ActionOutputLookUpDictionary = {
message: string,
success: boolean;
data?: TSharedItem;
success: boolean;
message: string;
data?: {
standardForm: string;
entries: Array<{
ipa?: string;
definition: string;
partOfSpeech?: string;
example: string;
}>;
};
};

View File

@@ -1,30 +1,38 @@
"use server";
import { ActionInputLookUpDictionary, ActionOutputLookUpDictionary, validateActionInputLookUpDictionary } from "./dictionary-action-dto";
import { ValidateError } from "@/lib/errors";
import { executeDictionaryLookup } from "@/lib/bigmodel/dictionary/orchestrator";
import { createLogger } from "@/lib/logger";
import { serviceLookUp } from "./dictionary-service";
import { LookUpError } from "@/lib/errors";
import {
ActionInputLookUpDictionary,
ActionOutputLookUpDictionary,
validateActionInputLookUpDictionary,
} from "./dictionary-action-dto";
const log = createLogger("dictionary-action");
export const actionLookUpDictionary = async (dto: ActionInputLookUpDictionary): Promise<ActionOutputLookUpDictionary> => {
try {
return {
message: 'success',
success: true,
data: await serviceLookUp(validateActionInputLookUpDictionary(dto))
};
} catch (e) {
if (e instanceof ValidateError) {
return {
success: false,
message: e.message
};
}
log.error("Dictionary lookup failed", { error: e instanceof Error ? e.message : String(e) });
return {
success: false,
message: 'Unknown error occured.'
};
export async function actionLookUpDictionary(
input: unknown,
): Promise<ActionOutputLookUpDictionary> {
try {
const validated = validateActionInputLookUpDictionary(input);
const result = await executeDictionaryLookup(
validated.text,
validated.queryLang,
validated.definitionLang
);
return {
success: true,
message: "Lookup successful",
data: result,
};
} catch (e) {
if (e instanceof LookUpError) {
return { success: false, message: e.message };
}
};
log.error("Dictionary lookup failed", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Lookup failed" };
}
}

View File

@@ -1,58 +0,0 @@
export type RepoInputCreateDictionaryLookUp = {
userId?: string;
text: string;
queryLang: string;
definitionLang: string;
dictionaryItemId?: number;
};
export type RepoOutputSelectLastLookUpResultEntry = {
id: number;
itemId: number;
ipa: string | null;
definition: string;
partOfSpeech: string | null;
example: string;
createdAt: Date;
updatedAt: Date;
};
export type RepoOutputSelectLastLookUpResultItem = {
id: number;
frequency: number;
standardForm: string;
queryLang: string;
definitionLang: string;
createdAt: Date;
updatedAt: Date;
entries: RepoOutputSelectLastLookUpResultEntry[];
};
export type RepoOutputSelectLastLookUpResult = RepoOutputSelectLastLookUpResultItem | null;
export type RepoInputCreateDictionaryItem = {
standardForm: string;
queryLang: string;
definitionLang: string;
};
export type RepoInputCreateDictionaryEntry = {
itemId: number;
ipa?: string;
definition: string;
partOfSpeech?: string;
example: string;
};
export type RepoInputCreateDictionaryEntryWithoutItemId = {
ipa?: string;
definition: string;
partOfSpeech?: string;
example: string;
};
export type RepoInputSelectLastLookUpResult = {
text: string,
queryLang: string,
definitionLang: string;
};

View File

@@ -1,75 +0,0 @@
import { stringNormalize } from "@/utils/string";
import {
RepoInputCreateDictionaryEntryWithoutItemId,
RepoInputCreateDictionaryItem,
RepoInputCreateDictionaryLookUp,
RepoInputSelectLastLookUpResult,
RepoOutputSelectLastLookUpResult,
} from "./dictionary-repository-dto";
import { prisma } from "@/lib/db";
export async function repoSelectLastLookUpResult(dto: RepoInputSelectLastLookUpResult): Promise<RepoOutputSelectLastLookUpResult> {
const result = await prisma.dictionaryLookUp.findFirst({
where: {
normalizedText: stringNormalize(dto.text),
queryLang: dto.queryLang,
definitionLang: dto.definitionLang,
dictionaryItemId: {
not: null
}
},
include: {
dictionaryItem: {
include: {
entries: true
}
}
},
orderBy: {
createdAt: 'desc'
}
});
if (!result?.dictionaryItem) {
return null;
}
return result.dictionaryItem;
}
export async function repoCreateLookUp(content: RepoInputCreateDictionaryLookUp) {
return (await prisma.dictionaryLookUp.create({
data: { ...content, normalizedText: stringNormalize(content.text) }
})).id;
}
export async function repoCreateLookUpWithItemAndEntries(
itemData: RepoInputCreateDictionaryItem,
lookUpData: RepoInputCreateDictionaryLookUp,
entries: RepoInputCreateDictionaryEntryWithoutItemId[]
) {
return await prisma.$transaction(async (tx) => {
const item = await tx.dictionaryItem.create({
data: itemData
});
await tx.dictionaryLookUp.create({
data: {
...lookUpData,
normalizedText: stringNormalize(lookUpData.text),
dictionaryItemId: item.id
}
});
for (const entry of entries) {
await tx.dictionaryEntry.create({
data: {
...entry,
itemId: item.id
}
});
}
return item.id;
});
}

View File

@@ -1,11 +1,9 @@
import { TSharedItem } from "@/shared/dictionary-type";
export type ServiceInputLookUp = {
text: string,
queryLang: string,
definitionLang: string,
forceRelook: boolean,
userId?: string;
export type ServiceOutputLookUp = {
standardForm: string;
entries: Array<{
ipa?: string;
definition: string;
partOfSpeech?: string;
example: string;
}>;
};
export type ServiceOutputLookUp = TSharedItem;

View File

@@ -1,79 +0,0 @@
import { executeDictionaryLookup } from "@/lib/bigmodel/dictionary/orchestrator";
import { repoCreateLookUp, repoCreateLookUpWithItemAndEntries, repoSelectLastLookUpResult } from "./dictionary-repository";
import { ServiceInputLookUp } from "./dictionary-service-dto";
import { createLogger } from "@/lib/logger";
import { RepoOutputSelectLastLookUpResultItem } from "./dictionary-repository-dto";
const log = createLogger("dictionary-service");
function transformRawItemToSharedItem(rawItem: RepoOutputSelectLastLookUpResultItem) {
return {
id: rawItem.id,
standardForm: rawItem.standardForm,
entries: rawItem.entries.map(entry => ({
ipa: entry.ipa ?? undefined,
definition: entry.definition,
partOfSpeech: entry.partOfSpeech ?? undefined,
example: entry.example
}))
};
}
export const serviceLookUp = async (dto: ServiceInputLookUp) => {
const {
text,
queryLang,
userId,
definitionLang,
forceRelook
} = dto;
const lastLookUpResult = await repoSelectLastLookUpResult({
text,
queryLang,
definitionLang,
});
if (forceRelook || !lastLookUpResult) {
const response = await executeDictionaryLookup(
text,
queryLang,
definitionLang
);
repoCreateLookUpWithItemAndEntries(
{
standardForm: response.standardForm,
queryLang,
definitionLang
},
{
userId,
text,
queryLang,
definitionLang,
},
response.entries
).catch(error => {
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
});
return response;
} else {
const transformedResult = transformRawItemToSharedItem(lastLookUpResult);
repoCreateLookUp({
userId: userId,
text: text,
queryLang: queryLang,
definitionLang: definitionLang,
dictionaryItemId: transformedResult.id
}).catch(error => {
log.error("Failed to save dictionary data", { error: error instanceof Error ? error.message : String(error) });
});
return {
standardForm: transformedResult.standardForm,
entries: transformedResult.entries
};
}
};

View File

@@ -1,134 +0,0 @@
"use server";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { prisma } from "@/lib/db";
import { exportApkg, type ExportDeckData } from "@/lib/anki/apkg-exporter";
import { createLogger } from "@/lib/logger";
const log = createLogger("export-action");
export interface ActionOutputExportApkg {
success: boolean;
message: string;
data?: ArrayBuffer;
filename?: string;
}
export async function actionExportApkg(deckId: number): Promise<ActionOutputExportApkg> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return { success: false, message: "Unauthorized" };
}
try {
const deck = await prisma.deck.findFirst({
where: { id: deckId, userId: session.user.id },
include: {
cards: {
include: {
note: {
include: {
noteType: true,
},
},
},
},
},
});
if (!deck) {
return { success: false, message: "Deck not found or access denied" };
}
if (deck.cards.length === 0) {
return { success: false, message: "Deck has no cards to export" };
}
const firstCard = deck.cards[0];
if (!firstCard?.note?.noteType) {
return { success: false, message: "Deck has invalid card data" };
}
const noteType = firstCard.note.noteType;
const revlogs = await prisma.revlog.findMany({
where: {
cardId: { in: deck.cards.map(c => c.id) },
},
});
const exportData: ExportDeckData = {
deck: {
id: deck.id,
name: deck.name,
desc: deck.desc,
collapsed: deck.collapsed,
conf: deck.conf as Record<string, unknown>,
},
noteType: {
id: noteType.id,
name: noteType.name,
kind: noteType.kind,
css: noteType.css,
fields: (noteType.fields as { name: string; ord: number }[]) ?? [],
templates: (noteType.templates as { name: string; ord: number; qfmt: string; afmt: string }[]) ?? [],
},
notes: deck.cards.map((card) => ({
id: card.note.id,
guid: card.note.guid,
tags: card.note.tags,
flds: card.note.flds,
sfld: card.note.sfld,
csum: card.note.csum,
})),
cards: deck.cards.map((card) => ({
id: card.id,
noteId: card.noteId,
ord: card.ord,
type: card.type,
queue: card.queue,
due: card.due,
ivl: card.ivl,
factor: card.factor,
reps: card.reps,
lapses: card.lapses,
left: card.left,
})),
revlogs: revlogs.map((r) => ({
id: r.id,
cardId: r.cardId,
ease: r.ease,
ivl: r.ivl,
lastIvl: r.lastIvl,
factor: r.factor,
time: r.time,
type: r.type,
})),
media: new Map(),
};
const apkgBuffer = await exportApkg(exportData);
log.info("APKG exported successfully", {
userId: session.user.id,
deckId: deck.id,
cardCount: deck.cards.length,
});
const safeDeckName = deck.name.replace(/[^a-zA-Z0-9\u4e00-\u9fff_-]/g, "_");
return {
success: true,
message: "Deck exported successfully",
data: apkgBuffer.buffer.slice(apkgBuffer.byteOffset, apkgBuffer.byteOffset + apkgBuffer.byteLength) as ArrayBuffer,
filename: `${safeDeckName}.apkg`,
};
} catch (error) {
log.error("Failed to export APKG", { error, deckId });
return {
success: false,
message: error instanceof Error ? error.message : "Failed to export deck",
};
}
}

View File

@@ -1,308 +0,0 @@
"use server";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { parseApkg, getDeckNames, getDeckNotesAndCards } from "@/lib/anki/apkg-parser";
import { prisma } from "@/lib/db";
import { CardType, CardQueue, NoteKind } from "../../../generated/prisma/enums";
import { createLogger } from "@/lib/logger";
import { repoGenerateGuid, repoCalculateCsum } from "@/modules/note/note-repository";
import type { ParsedApkg } from "@/lib/anki/types";
import { randomBytes } from "crypto";
const log = createLogger("import-action");
const MAX_APKG_SIZE = 100 * 1024 * 1024;
export interface ActionOutputPreviewApkg {
success: boolean;
message: string;
decks?: { id: number; name: string; cardCount: number }[];
}
export interface ActionOutputImportApkg {
success: boolean;
message: string;
deckId?: number;
noteCount?: number;
cardCount?: number;
}
async function importNoteType(
parsed: ParsedApkg,
ankiNoteTypeId: number,
userId: string
): Promise<number> {
const ankiNoteType = parsed.noteTypes.get(ankiNoteTypeId);
if (!ankiNoteType) {
throw new Error(`Note type ${ankiNoteTypeId} not found in APKG`);
}
const existing = await prisma.noteType.findFirst({
where: { name: ankiNoteType.name, userId },
});
if (existing) {
return existing.id;
}
const fields = ankiNoteType.flds.map((f) => ({
name: f.name,
ord: f.ord,
sticky: f.sticky,
rtl: f.rtl,
font: f.font,
size: f.size,
media: f.media,
}));
const templates = ankiNoteType.tmpls.map((t) => ({
name: t.name,
ord: t.ord,
qfmt: t.qfmt,
afmt: t.afmt,
bqfmt: t.bqfmt,
bafmt: t.bafmt,
did: t.did,
}));
const noteType = await prisma.noteType.create({
data: {
name: ankiNoteType.name,
kind: ankiNoteType.type === 1 ? NoteKind.CLOZE : NoteKind.STANDARD,
css: ankiNoteType.css,
fields: JSON.parse(JSON.stringify(fields)),
templates: JSON.parse(JSON.stringify(templates)),
userId,
},
});
return noteType.id;
}
function mapAnkiCardType(type: number): CardType {
switch (type) {
case 0: return CardType.NEW;
case 1: return CardType.LEARNING;
case 2: return CardType.REVIEW;
case 3: return CardType.RELEARNING;
default: return CardType.NEW;
}
}
function mapAnkiCardQueue(queue: number): CardQueue {
switch (queue) {
case -3: return CardQueue.USER_BURIED;
case -2: return CardQueue.SCHED_BURIED;
case -1: return CardQueue.SUSPENDED;
case 0: return CardQueue.NEW;
case 1: return CardQueue.LEARNING;
case 2: return CardQueue.REVIEW;
case 3: return CardQueue.IN_LEARNING;
case 4: return CardQueue.PREVIEW;
default: return CardQueue.NEW;
}
}
function generateUniqueId(): bigint {
const bytes = randomBytes(8);
const timestamp = BigInt(Date.now());
const random = BigInt(`0x${bytes.toString("hex")}`);
return timestamp ^ random;
}
async function importDeck(
parsed: ParsedApkg,
ankiDeckId: number,
userId: string,
deckName?: string
): Promise<{ deckId: number; noteCount: number; cardCount: number }> {
const ankiDeck = parsed.decks.get(ankiDeckId);
if (!ankiDeck) {
throw new Error(`Deck ${ankiDeckId} not found in APKG`);
}
const { notes: ankiNotes, cards: ankiCards } = getDeckNotesAndCards(parsed, ankiDeckId);
const result = await prisma.$transaction(async (tx) => {
const deck = await tx.deck.create({
data: {
name: deckName || ankiDeck.name,
desc: ankiDeck.desc,
userId,
collapsed: ankiDeck.collapsed,
conf: {},
},
});
if (ankiNotes.length === 0) {
return { deckId: deck.id, noteCount: 0, cardCount: 0 };
}
const noteTypeIdMap = new Map<number, number>();
const noteIdMap = new Map<number, bigint>();
for (const ankiNote of ankiNotes) {
let noteTypeId = noteTypeIdMap.get(ankiNote.mid);
if (!noteTypeId) {
noteTypeId = await importNoteType(parsed, ankiNote.mid, userId);
noteTypeIdMap.set(ankiNote.mid, noteTypeId);
}
const noteId = generateUniqueId();
noteIdMap.set(ankiNote.id, noteId);
const guid = ankiNote.guid || repoGenerateGuid();
const csum = ankiNote.csum || repoCalculateCsum(ankiNote.sfld);
await tx.note.create({
data: {
id: noteId,
guid,
noteTypeId,
mod: ankiNote.mod,
usn: ankiNote.usn,
tags: ankiNote.tags,
flds: ankiNote.flds,
sfld: ankiNote.sfld,
csum,
flags: ankiNote.flags,
data: ankiNote.data,
userId,
},
});
}
for (const ankiCard of ankiCards) {
const noteId = noteIdMap.get(ankiCard.nid);
if (!noteId) {
log.warn("Card references non-existent note", { cardId: ankiCard.id, noteId: ankiCard.nid });
continue;
}
await tx.card.create({
data: {
id: generateUniqueId(),
noteId,
deckId: deck.id,
ord: ankiCard.ord,
mod: ankiCard.mod,
usn: ankiCard.usn,
type: mapAnkiCardType(ankiCard.type),
queue: mapAnkiCardQueue(ankiCard.queue),
due: ankiCard.due,
ivl: ankiCard.ivl,
factor: ankiCard.factor,
reps: ankiCard.reps,
lapses: ankiCard.lapses,
left: ankiCard.left,
odue: ankiCard.odue,
odid: ankiCard.odid,
flags: ankiCard.flags,
data: ankiCard.data,
},
});
}
return { deckId: deck.id, noteCount: ankiNotes.length, cardCount: ankiCards.length };
});
return result;
}
export async function actionPreviewApkg(formData: FormData): Promise<ActionOutputPreviewApkg> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return { success: false, message: "Unauthorized" };
}
const file = formData.get("file") as File | null;
if (!file) {
return { success: false, message: "No file provided" };
}
if (!file.name.endsWith(".apkg")) {
return { success: false, message: "Invalid file type. Please upload an .apkg file" };
}
if (file.size > MAX_APKG_SIZE) {
return { success: false, message: `File size exceeds ${MAX_APKG_SIZE / (1024 * 1024)}MB limit` };
}
try {
const buffer = Buffer.from(await file.arrayBuffer());
const parsed = await parseApkg(buffer);
const decks = getDeckNames(parsed);
return {
success: true,
message: `Found ${decks.length} deck(s)`,
decks: decks.filter(d => d.cardCount > 0),
};
} catch (error) {
log.error("Failed to preview APKG", { error });
return {
success: false,
message: error instanceof Error ? error.message : "Failed to parse APKG file",
};
}
}
export async function actionImportApkg(
formData: FormData
): Promise<ActionOutputImportApkg> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return { success: false, message: "Unauthorized" };
}
const file = formData.get("file") as File | null;
const deckIdStr = formData.get("deckId") as string | null;
const deckName = formData.get("deckName") as string | null;
if (!file) {
return { success: false, message: "No file provided" };
}
if (!deckIdStr) {
return { success: false, message: "No deck selected" };
}
const ankiDeckId = parseInt(deckIdStr, 10);
if (isNaN(ankiDeckId)) {
return { success: false, message: "Invalid deck ID" };
}
if (file.size > MAX_APKG_SIZE) {
return { success: false, message: `File size exceeds ${MAX_APKG_SIZE / (1024 * 1024)}MB limit` };
}
try {
const buffer = Buffer.from(await file.arrayBuffer());
const parsed = await parseApkg(buffer);
const result = await importDeck(parsed, ankiDeckId, session.user.id, deckName || undefined);
log.info("APKG imported successfully", {
userId: session.user.id,
deckId: result.deckId,
noteCount: result.noteCount,
cardCount: result.cardCount,
});
return {
success: true,
message: `Imported ${result.cardCount} cards from ${result.noteCount} notes`,
deckId: result.deckId,
noteCount: result.noteCount,
cardCount: result.cardCount,
};
} catch (error) {
log.error("Failed to import APKG", { error });
return {
success: false,
message: error instanceof Error ? error.message : "Failed to import APKG file",
};
}
}

View File

@@ -1,106 +0,0 @@
import z from "zod";
import { generateValidator } from "@/utils/validate";
import { NoteKind } from "../../../generated/prisma/enums";
import {
schemaNoteTypeField,
schemaNoteTypeTemplate,
NoteTypeField,
NoteTypeTemplate,
} from "./note-type-repository-dto";
export const LENGTH_MIN_NOTE_TYPE_NAME = 1;
export const LENGTH_MAX_NOTE_TYPE_NAME = 100;
export const LENGTH_MAX_CSS = 50000;
const schemaNoteTypeFieldAction = z.object({
name: z.string().min(1).max(schemaNoteTypeField.name.maxLength),
ord: z.number().int(),
sticky: z.boolean(),
rtl: z.boolean(),
font: z.string().max(schemaNoteTypeField.font.maxLength).optional(),
size: z.number().int().min(schemaNoteTypeField.size.min).max(schemaNoteTypeField.size.max).optional(),
media: z.array(z.string()).optional(),
});
const schemaNoteTypeTemplateAction = z.object({
name: z.string().min(1).max(schemaNoteTypeTemplate.name.maxLength),
ord: z.number().int(),
qfmt: z.string().min(1).max(schemaNoteTypeTemplate.qfmt.maxLength),
afmt: z.string().min(1).max(schemaNoteTypeTemplate.afmt.maxLength),
bqfmt: z.string().max(schemaNoteTypeTemplate.bqfmt.maxLength).optional(),
bafmt: z.string().max(schemaNoteTypeTemplate.bafmt.maxLength).optional(),
did: z.number().int().optional(),
});
export const schemaActionInputCreateNoteType = z.object({
name: z.string().min(LENGTH_MIN_NOTE_TYPE_NAME).max(LENGTH_MAX_NOTE_TYPE_NAME),
kind: z.enum(["STANDARD", "CLOZE"]).optional(),
css: z.string().max(LENGTH_MAX_CSS).optional(),
fields: z.array(schemaNoteTypeFieldAction).min(1),
templates: z.array(schemaNoteTypeTemplateAction).min(1),
});
export type ActionInputCreateNoteType = z.infer<typeof schemaActionInputCreateNoteType>;
export const validateActionInputCreateNoteType = generateValidator(schemaActionInputCreateNoteType);
export const schemaActionInputUpdateNoteType = z.object({
id: z.number().int().positive(),
name: z.string().min(LENGTH_MIN_NOTE_TYPE_NAME).max(LENGTH_MAX_NOTE_TYPE_NAME).optional(),
kind: z.enum(["STANDARD", "CLOZE"]).optional(),
css: z.string().max(LENGTH_MAX_CSS).optional(),
fields: z.array(schemaNoteTypeFieldAction).min(1).optional(),
templates: z.array(schemaNoteTypeTemplateAction).min(1).optional(),
});
export type ActionInputUpdateNoteType = z.infer<typeof schemaActionInputUpdateNoteType>;
export const validateActionInputUpdateNoteType = generateValidator(schemaActionInputUpdateNoteType);
export const schemaActionInputGetNoteTypeById = z.object({
id: z.number().int().positive(),
});
export type ActionInputGetNoteTypeById = z.infer<typeof schemaActionInputGetNoteTypeById>;
export const validateActionInputGetNoteTypeById = generateValidator(schemaActionInputGetNoteTypeById);
export const schemaActionInputDeleteNoteType = z.object({
id: z.number().int().positive(),
});
export type ActionInputDeleteNoteType = z.infer<typeof schemaActionInputDeleteNoteType>;
export const validateActionInputDeleteNoteType = generateValidator(schemaActionInputDeleteNoteType);
export type ActionOutputNoteType = {
id: number;
name: string;
kind: NoteKind;
css: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type ActionOutputCreateNoteType = {
success: boolean;
message: string;
data?: { id: number };
};
export type ActionOutputUpdateNoteType = {
success: boolean;
message: string;
};
export type ActionOutputGetNoteTypeById = {
success: boolean;
message: string;
data?: ActionOutputNoteType;
};
export type ActionOutputGetNoteTypesByUserId = {
success: boolean;
message: string;
data?: ActionOutputNoteType[];
};
export type ActionOutputDeleteNoteType = {
success: boolean;
message: string;
};

View File

@@ -1,255 +0,0 @@
"use server";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { createLogger } from "@/lib/logger";
import { ValidateError } from "@/lib/errors";
import {
ActionInputCreateNoteType,
ActionInputUpdateNoteType,
ActionInputDeleteNoteType,
ActionOutputCreateNoteType,
ActionOutputUpdateNoteType,
ActionOutputGetNoteTypeById,
ActionOutputGetNoteTypesByUserId,
ActionOutputDeleteNoteType,
validateActionInputCreateNoteType,
validateActionInputUpdateNoteType,
validateActionInputDeleteNoteType,
} from "./note-type-action-dto";
import {
serviceCreateNoteType,
serviceUpdateNoteType,
serviceGetNoteTypeById,
serviceGetNoteTypesByUserId,
serviceDeleteNoteType,
} from "./note-type-service";
import {
DEFAULT_BASIC_NOTE_TYPE_FIELDS,
DEFAULT_BASIC_NOTE_TYPE_TEMPLATES,
DEFAULT_BASIC_NOTE_TYPE_CSS,
DEFAULT_CLOZE_NOTE_TYPE_FIELDS,
DEFAULT_CLOZE_NOTE_TYPE_TEMPLATES,
DEFAULT_CLOZE_NOTE_TYPE_CSS,
} from "./note-type-repository-dto";
const log = createLogger("note-type-action");
export async function actionCreateNoteType(
input: ActionInputCreateNoteType,
): Promise<ActionOutputCreateNoteType> {
try {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return {
success: false,
message: "Unauthorized",
};
}
const validated = validateActionInputCreateNoteType(input);
const id = await serviceCreateNoteType({
name: validated.name,
kind: validated.kind,
css: validated.css,
fields: validated.fields,
templates: validated.templates,
userId: session.user.id,
});
return {
success: true,
message: "Note type created successfully",
data: { id },
};
} catch (e) {
if (e instanceof ValidateError) {
return {
success: false,
message: e.message,
};
}
log.error("Create note type failed", { error: e });
return {
success: false,
message: "Failed to create note type",
};
}
}
export async function actionUpdateNoteType(
input: ActionInputUpdateNoteType,
): Promise<ActionOutputUpdateNoteType> {
try {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return {
success: false,
message: "Unauthorized",
};
}
const validated = validateActionInputUpdateNoteType(input);
await serviceUpdateNoteType({
id: validated.id,
name: validated.name,
kind: validated.kind,
css: validated.css,
fields: validated.fields,
templates: validated.templates,
userId: session.user.id,
});
return {
success: true,
message: "Note type updated successfully",
};
} catch (e) {
if (e instanceof ValidateError) {
return {
success: false,
message: e.message,
};
}
log.error("Update note type failed", { error: e });
return {
success: false,
message: "Failed to update note type",
};
}
}
export async function actionGetNoteTypeById(
id: number,
): Promise<ActionOutputGetNoteTypeById> {
try {
const noteType = await serviceGetNoteTypeById({ id });
if (!noteType) {
return {
success: false,
message: "Note type not found",
};
}
return {
success: true,
message: "Note type retrieved successfully",
data: noteType,
};
} catch (e) {
log.error("Get note type failed", { error: e });
return {
success: false,
message: "Failed to retrieve note type",
};
}
}
export async function actionGetNoteTypesByUserId(): Promise<ActionOutputGetNoteTypesByUserId> {
try {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return {
success: false,
message: "Unauthorized",
};
}
const noteTypes = await serviceGetNoteTypesByUserId({
userId: session.user.id,
});
return {
success: true,
message: "Note types retrieved successfully",
data: noteTypes,
};
} catch (e) {
log.error("Get note types failed", { error: e });
return {
success: false,
message: "Failed to retrieve note types",
};
}
}
export async function actionDeleteNoteType(
input: ActionInputDeleteNoteType,
): Promise<ActionOutputDeleteNoteType> {
try {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
return {
success: false,
message: "Unauthorized",
};
}
const validated = validateActionInputDeleteNoteType(input);
await serviceDeleteNoteType({
id: validated.id,
userId: session.user.id,
});
return {
success: true,
message: "Note type deleted successfully",
};
} catch (e) {
if (e instanceof ValidateError) {
return {
success: false,
message: e.message,
};
}
log.error("Delete note type failed", { error: e });
return {
success: false,
message: "Failed to delete note type",
};
}
}
export async function actionCreateDefaultBasicNoteType(): Promise<ActionOutputCreateNoteType> {
return actionCreateNoteType({
name: "Basic Vocabulary",
kind: "STANDARD",
css: DEFAULT_BASIC_NOTE_TYPE_CSS,
fields: DEFAULT_BASIC_NOTE_TYPE_FIELDS,
templates: DEFAULT_BASIC_NOTE_TYPE_TEMPLATES,
});
}
export async function actionCreateDefaultClozeNoteType(): Promise<ActionOutputCreateNoteType> {
return actionCreateNoteType({
name: "Cloze",
kind: "CLOZE",
css: DEFAULT_CLOZE_NOTE_TYPE_CSS,
fields: DEFAULT_CLOZE_NOTE_TYPE_FIELDS,
templates: DEFAULT_CLOZE_NOTE_TYPE_TEMPLATES,
});
}
export async function actionGetDefaultBasicNoteTypeTemplate() {
return {
name: "Basic Vocabulary",
kind: "STANDARD" as const,
css: DEFAULT_BASIC_NOTE_TYPE_CSS,
fields: DEFAULT_BASIC_NOTE_TYPE_FIELDS,
templates: DEFAULT_BASIC_NOTE_TYPE_TEMPLATES,
};
}
export async function actionGetDefaultClozeNoteTypeTemplate() {
return {
name: "Cloze",
kind: "CLOZE" as const,
css: DEFAULT_CLOZE_NOTE_TYPE_CSS,
fields: DEFAULT_CLOZE_NOTE_TYPE_FIELDS,
templates: DEFAULT_CLOZE_NOTE_TYPE_TEMPLATES,
};
}

View File

@@ -1,181 +0,0 @@
import { NoteKind } from "../../../generated/prisma/enums";
// ============================================
// Field Schema (Anki flds structure)
// ============================================
export interface NoteTypeField {
name: string;
ord: number;
sticky: boolean;
rtl: boolean;
font?: string;
size?: number;
media?: string[];
}
export const schemaNoteTypeField = {
name: { minLength: 1, maxLength: 50 },
font: { maxLength: 100 },
size: { min: 8, max: 72 },
};
// ============================================
// Template Schema (Anki tmpls structure)
// ============================================
export interface NoteTypeTemplate {
name: string;
ord: number;
qfmt: string;
afmt: string;
bqfmt?: string;
bafmt?: string;
did?: number;
}
export const schemaNoteTypeTemplate = {
name: { minLength: 1, maxLength: 100 },
qfmt: { maxLength: 10000 },
afmt: { maxLength: 10000 },
bqfmt: { maxLength: 10000 },
bafmt: { maxLength: 10000 },
};
// ============================================
// Repository Input Types
// ============================================
export interface RepoInputCreateNoteType {
name: string;
kind?: NoteKind;
css?: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
}
export interface RepoInputUpdateNoteType {
id: number;
name?: string;
kind?: NoteKind;
css?: string;
fields?: NoteTypeField[];
templates?: NoteTypeTemplate[];
}
export interface RepoInputGetNoteTypeById {
id: number;
}
export interface RepoInputGetNoteTypesByUserId {
userId: string;
}
export interface RepoInputDeleteNoteType {
id: number;
}
export interface RepoInputCheckNotesExist {
noteTypeId: number;
}
// ============================================
// Repository Output Types
// ============================================
export type RepoOutputNoteType = {
id: number;
name: string;
kind: NoteKind;
css: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type RepoOutputNoteTypeOwnership = {
userId: string;
};
export type RepoOutputNotesExistCheck = {
exists: boolean;
count: number;
};
// ============================================
// Default Note Types
// ============================================
export const DEFAULT_BASIC_NOTE_TYPE_FIELDS: NoteTypeField[] = [
{ name: "Word", ord: 0, sticky: false, rtl: false, font: "Arial", size: 20 },
{ name: "Definition", ord: 1, sticky: false, rtl: false, font: "Arial", size: 20 },
{ name: "IPA", ord: 2, sticky: false, rtl: false, font: "Arial", size: 20 },
{ name: "Example", ord: 3, sticky: false, rtl: false, font: "Arial", size: 20 },
];
export const DEFAULT_BASIC_NOTE_TYPE_TEMPLATES: NoteTypeTemplate[] = [
{
name: "Word → Definition",
ord: 0,
qfmt: "{{Word}}<br>{{IPA}}",
afmt: "{{FrontSide}}<hr id=answer>{{Definition}}<br><br>{{Example}}",
},
{
name: "Definition → Word",
ord: 1,
qfmt: "{{Definition}}",
afmt: "{{FrontSide}}<hr id=answer>{{Word}}<br>{{IPA}}",
},
];
export const DEFAULT_BASIC_NOTE_TYPE_CSS = `.card {
font-family: Arial, sans-serif;
font-size: 20px;
text-align: center;
color: #333;
background-color: #fff;
}
.card1 {
background-color: #e8f4f8;
}
.card2 {
background-color: #f8f4e8;
}
hr {
border: none;
border-top: 1px solid #ccc;
margin: 20px 0;
}`;
export const DEFAULT_CLOZE_NOTE_TYPE_FIELDS: NoteTypeField[] = [
{ name: "Text", ord: 0, sticky: false, rtl: false, font: "Arial", size: 20 },
{ name: "Extra", ord: 1, sticky: false, rtl: false, font: "Arial", size: 20 },
];
export const DEFAULT_CLOZE_NOTE_TYPE_TEMPLATES: NoteTypeTemplate[] = [
{
name: "Cloze",
ord: 0,
qfmt: "{{cloze:Text}}",
afmt: "{{cloze:Text}}<br><br>{{Extra}}",
},
];
export const DEFAULT_CLOZE_NOTE_TYPE_CSS = `.card {
font-family: Arial, sans-serif;
font-size: 20px;
text-align: center;
color: #333;
background-color: #fff;
}
.cloze {
font-weight: bold;
color: #0066cc;
}`;

View File

@@ -1,151 +0,0 @@
import { prisma } from "@/lib/db";
import { createLogger } from "@/lib/logger";
import {
RepoInputCreateNoteType,
RepoInputUpdateNoteType,
RepoInputGetNoteTypeById,
RepoInputGetNoteTypesByUserId,
RepoInputDeleteNoteType,
RepoInputCheckNotesExist,
RepoOutputNoteType,
RepoOutputNoteTypeOwnership,
RepoOutputNotesExistCheck,
NoteTypeField,
NoteTypeTemplate,
} from "./note-type-repository-dto";
import { NoteKind } from "../../../generated/prisma/enums";
const log = createLogger("note-type-repository");
export async function repoCreateNoteType(
input: RepoInputCreateNoteType,
): Promise<number> {
const noteType = await prisma.noteType.create({
data: {
name: input.name,
kind: input.kind ?? NoteKind.STANDARD,
css: input.css ?? "",
fields: input.fields as unknown as object,
templates: input.templates as unknown as object,
userId: input.userId,
},
});
log.info("Created note type", { id: noteType.id, name: noteType.name });
return noteType.id;
}
export async function repoUpdateNoteType(
input: RepoInputUpdateNoteType,
): Promise<void> {
const updateData: {
name?: string;
kind?: NoteKind;
css?: string;
fields?: object;
templates?: object;
} = {};
if (input.name !== undefined) updateData.name = input.name;
if (input.kind !== undefined) updateData.kind = input.kind;
if (input.css !== undefined) updateData.css = input.css;
if (input.fields !== undefined)
updateData.fields = input.fields as unknown as object;
if (input.templates !== undefined)
updateData.templates = input.templates as unknown as object;
await prisma.noteType.update({
where: { id: input.id },
data: updateData,
});
log.info("Updated note type", { id: input.id });
}
export async function repoGetNoteTypeById(
input: RepoInputGetNoteTypeById,
): Promise<RepoOutputNoteType | null> {
const noteType = await prisma.noteType.findUnique({
where: { id: input.id },
});
if (!noteType) return null;
return {
id: noteType.id,
name: noteType.name,
kind: noteType.kind,
css: noteType.css,
fields: noteType.fields as unknown as NoteTypeField[],
templates: noteType.templates as unknown as NoteTypeTemplate[],
userId: noteType.userId,
createdAt: noteType.createdAt,
updatedAt: noteType.updatedAt,
};
}
export async function repoGetNoteTypesByUserId(
input: RepoInputGetNoteTypesByUserId,
): Promise<RepoOutputNoteType[]> {
const noteTypes = await prisma.noteType.findMany({
where: { userId: input.userId },
orderBy: { createdAt: "desc" },
});
return noteTypes.map((nt) => ({
id: nt.id,
name: nt.name,
kind: nt.kind,
css: nt.css,
fields: nt.fields as unknown as NoteTypeField[],
templates: nt.templates as unknown as NoteTypeTemplate[],
userId: nt.userId,
createdAt: nt.createdAt,
updatedAt: nt.updatedAt,
}));
}
export async function repoGetNoteTypeOwnership(
noteTypeId: number,
): Promise<RepoOutputNoteTypeOwnership | null> {
const noteType = await prisma.noteType.findUnique({
where: { id: noteTypeId },
select: { userId: true },
});
return noteType;
}
export async function repoDeleteNoteType(
input: RepoInputDeleteNoteType,
): Promise<void> {
await prisma.noteType.delete({
where: { id: input.id },
});
log.info("Deleted note type", { id: input.id });
}
export async function repoCheckNotesExist(
input: RepoInputCheckNotesExist,
): Promise<RepoOutputNotesExistCheck> {
const count = await prisma.note.count({
where: { noteTypeId: input.noteTypeId },
});
return {
exists: count > 0,
count,
};
}
export async function repoGetNoteTypeNameById(
noteTypeId: number,
): Promise<string | null> {
const noteType = await prisma.noteType.findUnique({
where: { id: noteTypeId },
select: { name: true },
});
return noteType?.name ?? null;
}

View File

@@ -1,60 +0,0 @@
import { NoteKind } from "../../../generated/prisma/enums";
import { NoteTypeField, NoteTypeTemplate } from "./note-type-repository-dto";
export type ServiceInputCreateNoteType = {
name: string;
kind?: NoteKind;
css?: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
};
export type ServiceInputUpdateNoteType = {
id: number;
name?: string;
kind?: NoteKind;
css?: string;
fields?: NoteTypeField[];
templates?: NoteTypeTemplate[];
userId: string;
};
export type ServiceInputGetNoteTypeById = {
id: number;
};
export type ServiceInputGetNoteTypesByUserId = {
userId: string;
};
export type ServiceInputDeleteNoteType = {
id: number;
userId: string;
};
export type ServiceInputValidateFields = {
fields: NoteTypeField[];
};
export type ServiceInputValidateTemplates = {
templates: NoteTypeTemplate[];
fields: NoteTypeField[];
};
export type ServiceOutputNoteType = {
id: number;
name: string;
kind: NoteKind;
css: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type ServiceOutputValidation = {
success: boolean;
errors: string[];
};

View File

@@ -1,272 +0,0 @@
import { createLogger } from "@/lib/logger";
import { ValidateError } from "@/lib/errors";
import {
repoCreateNoteType,
repoGetNoteTypeById,
repoGetNoteTypesByUserId,
repoUpdateNoteType,
repoDeleteNoteType,
repoGetNoteTypeOwnership,
repoCheckNotesExist,
} from "./note-type-repository";
import {
ServiceInputCreateNoteType,
ServiceInputUpdateNoteType,
ServiceInputGetNoteTypeById,
ServiceInputGetNoteTypesByUserId,
ServiceInputDeleteNoteType,
ServiceInputValidateFields,
ServiceInputValidateTemplates,
ServiceOutputNoteType,
ServiceOutputValidation,
} from "./note-type-service-dto";
import { schemaNoteTypeField, schemaNoteTypeTemplate } from "./note-type-repository-dto";
const log = createLogger("note-type-service");
export function serviceValidateFields(
input: ServiceInputValidateFields,
): ServiceOutputValidation {
const errors: string[] = [];
if (!Array.isArray(input.fields) || input.fields.length === 0) {
errors.push("Fields must be a non-empty array");
return { success: false, errors };
}
const seenNames = new Set<string>();
const seenOrds = new Set<number>();
for (let i = 0; i < input.fields.length; i++) {
const field = input.fields[i];
if (!field.name || field.name.trim().length === 0) {
errors.push(`Field ${i}: name is required`);
} else if (field.name.length > schemaNoteTypeField.name.maxLength) {
errors.push(`Field ${i}: name exceeds maximum length of ${schemaNoteTypeField.name.maxLength}`);
}
if (seenNames.has(field.name)) {
errors.push(`Field ${i}: duplicate field name "${field.name}"`);
}
seenNames.add(field.name);
if (typeof field.ord !== "number") {
errors.push(`Field ${i}: ord must be a number`);
} else if (seenOrds.has(field.ord)) {
errors.push(`Field ${i}: duplicate ordinal ${field.ord}`);
}
seenOrds.add(field.ord);
if (typeof field.sticky !== "boolean") {
errors.push(`Field ${i}: sticky must be a boolean`);
}
if (typeof field.rtl !== "boolean") {
errors.push(`Field ${i}: rtl must be a boolean`);
}
if (field.font && field.font.length > schemaNoteTypeField.font.maxLength) {
errors.push(`Field ${i}: font exceeds maximum length`);
}
if (field.size !== undefined && (field.size < schemaNoteTypeField.size.min || field.size > schemaNoteTypeField.size.max)) {
errors.push(`Field ${i}: size must be between ${schemaNoteTypeField.size.min} and ${schemaNoteTypeField.size.max}`);
}
}
return { success: errors.length === 0, errors };
}
export function serviceValidateTemplates(
input: ServiceInputValidateTemplates,
): ServiceOutputValidation {
const errors: string[] = [];
if (!Array.isArray(input.templates) || input.templates.length === 0) {
errors.push("Templates must be a non-empty array");
return { success: false, errors };
}
const fieldNames = new Set(input.fields.map((f) => f.name));
const seenNames = new Set<string>();
const seenOrds = new Set<number>();
const mustachePattern = /\{\{([^}]+)\}\}/g;
for (let i = 0; i < input.templates.length; i++) {
const template = input.templates[i];
if (!template.name || template.name.trim().length === 0) {
errors.push(`Template ${i}: name is required`);
} else if (template.name.length > schemaNoteTypeTemplate.name.maxLength) {
errors.push(`Template ${i}: name exceeds maximum length`);
}
if (seenNames.has(template.name)) {
errors.push(`Template ${i}: duplicate template name "${template.name}"`);
}
seenNames.add(template.name);
if (typeof template.ord !== "number") {
errors.push(`Template ${i}: ord must be a number`);
} else if (seenOrds.has(template.ord)) {
errors.push(`Template ${i}: duplicate ordinal ${template.ord}`);
}
seenOrds.add(template.ord);
if (!template.qfmt || template.qfmt.trim().length === 0) {
errors.push(`Template ${i}: qfmt (question format) is required`);
} else if (template.qfmt.length > schemaNoteTypeTemplate.qfmt.maxLength) {
errors.push(`Template ${i}: qfmt exceeds maximum length`);
}
if (!template.afmt || template.afmt.trim().length === 0) {
errors.push(`Template ${i}: afmt (answer format) is required`);
} else if (template.afmt.length > schemaNoteTypeTemplate.afmt.maxLength) {
errors.push(`Template ${i}: afmt exceeds maximum length`);
}
const qfmtMatches = template.qfmt.match(mustachePattern) || [];
const afmtMatches = template.afmt.match(mustachePattern) || [];
const allMatches = [...qfmtMatches, ...afmtMatches];
for (const match of allMatches) {
const content = match.slice(2, -2).trim();
if (content.startsWith("cloze:")) {
continue;
}
if (content === "FrontSide") {
continue;
}
if (content.startsWith("type:")) {
continue;
}
if (!fieldNames.has(content)) {
log.warn(`Template ${i}: unknown field reference "{{${content}}}"`);
}
}
}
return { success: errors.length === 0, errors };
}
export async function serviceCreateNoteType(
input: ServiceInputCreateNoteType,
): Promise<number> {
const fieldsValidation = serviceValidateFields({ fields: input.fields });
if (!fieldsValidation.success) {
throw new ValidateError(`Invalid fields: ${fieldsValidation.errors.join("; ")}`);
}
const templatesValidation = serviceValidateTemplates({
templates: input.templates,
fields: input.fields,
});
if (!templatesValidation.success) {
throw new ValidateError(`Invalid templates: ${templatesValidation.errors.join("; ")}`);
}
log.info("Creating note type", { name: input.name, userId: input.userId });
return repoCreateNoteType({
name: input.name,
kind: input.kind,
css: input.css,
fields: input.fields,
templates: input.templates,
userId: input.userId,
});
}
export async function serviceUpdateNoteType(
input: ServiceInputUpdateNoteType,
): Promise<void> {
const ownership = await repoGetNoteTypeOwnership(input.id);
if (!ownership) {
throw new ValidateError("Note type not found");
}
if (ownership.userId !== input.userId) {
throw new ValidateError("You do not have permission to update this note type");
}
if (input.fields) {
const fieldsValidation = serviceValidateFields({ fields: input.fields });
if (!fieldsValidation.success) {
throw new ValidateError(`Invalid fields: ${fieldsValidation.errors.join("; ")}`);
}
}
if (input.templates && input.fields) {
const templatesValidation = serviceValidateTemplates({
templates: input.templates,
fields: input.fields,
});
if (!templatesValidation.success) {
throw new ValidateError(`Invalid templates: ${templatesValidation.errors.join("; ")}`);
}
} else if (input.templates) {
const existing = await repoGetNoteTypeById({ id: input.id });
if (existing) {
const templatesValidation = serviceValidateTemplates({
templates: input.templates,
fields: existing.fields,
});
if (!templatesValidation.success) {
throw new ValidateError(`Invalid templates: ${templatesValidation.errors.join("; ")}`);
}
}
}
log.info("Updating note type", { id: input.id });
await repoUpdateNoteType({
id: input.id,
name: input.name,
kind: input.kind,
css: input.css,
fields: input.fields,
templates: input.templates,
});
}
export async function serviceGetNoteTypeById(
input: ServiceInputGetNoteTypeById,
): Promise<ServiceOutputNoteType | null> {
return repoGetNoteTypeById(input);
}
export async function serviceGetNoteTypesByUserId(
input: ServiceInputGetNoteTypesByUserId,
): Promise<ServiceOutputNoteType[]> {
return repoGetNoteTypesByUserId(input);
}
export async function serviceDeleteNoteType(
input: ServiceInputDeleteNoteType,
): Promise<void> {
const ownership = await repoGetNoteTypeOwnership(input.id);
if (!ownership) {
throw new ValidateError("Note type not found");
}
if (ownership.userId !== input.userId) {
throw new ValidateError("You do not have permission to delete this note type");
}
const notesCheck = await repoCheckNotesExist({ noteTypeId: input.id });
if (notesCheck.exists) {
throw new ValidateError(
`Cannot delete note type: ${notesCheck.count} notes are using this type`,
);
}
log.info("Deleting note type", { id: input.id });
await repoDeleteNoteType({ id: input.id });
}

View File

@@ -1,131 +0,0 @@
import { generateValidator } from "@/utils/validate";
import z from "zod";
export const LENGTH_MAX_NOTE_FIELD = 65535;
export const LENGTH_MIN_NOTE_FIELD = 0;
export const LENGTH_MAX_TAG = 100;
export const MAX_FIELDS = 100;
export const MAX_TAGS = 100;
export const schemaActionInputCreateNote = z.object({
noteTypeId: z.number().int().positive(),
fields: z
.array(z.string().max(LENGTH_MAX_NOTE_FIELD))
.min(1)
.max(MAX_FIELDS),
tags: z.array(z.string().max(LENGTH_MAX_TAG)).max(MAX_TAGS).optional(),
});
export type ActionInputCreateNote = z.infer<typeof schemaActionInputCreateNote>;
export const validateActionInputCreateNote = generateValidator(
schemaActionInputCreateNote,
);
export const schemaActionInputUpdateNote = z.object({
noteId: z.bigint(),
fields: z
.array(z.string().max(LENGTH_MAX_NOTE_FIELD))
.min(1)
.max(MAX_FIELDS)
.optional(),
tags: z.array(z.string().max(LENGTH_MAX_TAG)).max(MAX_TAGS).optional(),
});
export type ActionInputUpdateNote = z.infer<typeof schemaActionInputUpdateNote>;
export const validateActionInputUpdateNote = generateValidator(
schemaActionInputUpdateNote,
);
export const schemaActionInputDeleteNote = z.object({
noteId: z.bigint(),
});
export type ActionInputDeleteNote = z.infer<typeof schemaActionInputDeleteNote>;
export const validateActionInputDeleteNote = generateValidator(
schemaActionInputDeleteNote,
);
export const schemaActionInputGetNoteById = z.object({
noteId: z.bigint(),
});
export type ActionInputGetNoteById = z.infer<typeof schemaActionInputGetNoteById>;
export const validateActionInputGetNoteById = generateValidator(
schemaActionInputGetNoteById,
);
export const schemaActionInputGetNotesByNoteTypeId = z.object({
noteTypeId: z.number().int().positive(),
limit: z.number().int().positive().max(1000).optional(),
offset: z.number().int().nonnegative().optional(),
});
export type ActionInputGetNotesByNoteTypeId = z.infer<
typeof schemaActionInputGetNotesByNoteTypeId
>;
export const validateActionInputGetNotesByNoteTypeId = generateValidator(
schemaActionInputGetNotesByNoteTypeId,
);
export const schemaActionInputGetNotesByUserId = z.object({
userId: z.string().min(1),
limit: z.number().int().positive().max(1000).optional(),
offset: z.number().int().nonnegative().optional(),
});
export type ActionInputGetNotesByUserId = z.infer<
typeof schemaActionInputGetNotesByUserId
>;
export const validateActionInputGetNotesByUserId = generateValidator(
schemaActionInputGetNotesByUserId,
);
export type ActionOutputNote = {
id: string;
guid: string;
noteTypeId: number;
mod: number;
usn: number;
tags: string[];
fields: string[];
sfld: string;
csum: number;
flags: number;
data: string;
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type ActionOutputCreateNote = {
message: string;
success: boolean;
data?: {
id: string;
guid: string;
};
};
export type ActionOutputUpdateNote = {
message: string;
success: boolean;
};
export type ActionOutputDeleteNote = {
message: string;
success: boolean;
};
export type ActionOutputGetNoteById = {
message: string;
success: boolean;
data?: ActionOutputNote;
};
export type ActionOutputGetNotes = {
message: string;
success: boolean;
data?: ActionOutputNote[];
};
export type ActionOutputNoteCount = {
message: string;
success: boolean;
data?: {
count: number;
};
};

View File

@@ -1,344 +0,0 @@
"use server";
import { auth } from "@/auth";
import { headers } from "next/headers";
import { createLogger } from "@/lib/logger";
import { ValidateError } from "@/lib/errors";
import {
ActionInputCreateNote,
ActionInputUpdateNote,
ActionInputDeleteNote,
ActionInputGetNoteById,
ActionInputGetNotesByNoteTypeId,
ActionInputGetNotesByUserId,
ActionOutputCreateNote,
ActionOutputUpdateNote,
ActionOutputDeleteNote,
ActionOutputGetNoteById,
ActionOutputGetNotes,
ActionOutputNoteCount,
ActionOutputNote,
validateActionInputCreateNote,
validateActionInputUpdateNote,
validateActionInputDeleteNote,
validateActionInputGetNoteById,
validateActionInputGetNotesByNoteTypeId,
validateActionInputGetNotesByUserId,
} from "./note-action-dto";
import {
serviceCreateNote,
serviceUpdateNote,
serviceDeleteNote,
serviceGetNoteById,
serviceGetNotesByNoteTypeId,
serviceGetNotesByUserId,
serviceCountNotesByUserId,
serviceCountNotesByNoteTypeId,
NoteNotFoundError,
NoteOwnershipError,
} from "./note-service";
const log = createLogger("note-action");
function mapNoteToOutput(note: {
id: bigint;
guid: string;
noteTypeId: number;
mod: number;
usn: number;
tags: string[];
fields: string[];
sfld: string;
csum: number;
flags: number;
data: string;
userId: string;
createdAt: Date;
updatedAt: Date;
}): ActionOutputNote {
return {
id: note.id.toString(),
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tags,
fields: note.fields,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
};
}
async function requireAuth(): Promise<string> {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
throw new Error("Unauthorized");
}
return session.user.id;
}
export async function actionCreateNote(
input: unknown,
): Promise<ActionOutputCreateNote> {
try {
const userId = await requireAuth();
const validated = validateActionInputCreateNote(input);
log.debug("Creating note", { userId, noteTypeId: validated.noteTypeId });
const result = await serviceCreateNote({
...validated,
userId,
});
log.info("Note created", { id: result.id.toString(), guid: result.guid });
return {
success: true,
message: "Note created successfully",
data: {
id: result.id.toString(),
guid: result.guid,
},
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
if (e instanceof Error && e.message === "Unauthorized") {
return { success: false, message: "Unauthorized" };
}
log.error("Failed to create note", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionUpdateNote(
input: unknown,
): Promise<ActionOutputUpdateNote> {
try {
const userId = await requireAuth();
const validated = validateActionInputUpdateNote(input);
log.debug("Updating note", { noteId: validated.noteId.toString() });
await serviceUpdateNote({
...validated,
userId,
});
log.info("Note updated", { noteId: validated.noteId.toString() });
return {
success: true,
message: "Note updated successfully",
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
if (e instanceof NoteNotFoundError) {
return { success: false, message: "Note not found" };
}
if (e instanceof NoteOwnershipError) {
return { success: false, message: "You do not have permission to update this note" };
}
if (e instanceof Error && e.message === "Unauthorized") {
return { success: false, message: "Unauthorized" };
}
log.error("Failed to update note", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionDeleteNote(
input: unknown,
): Promise<ActionOutputDeleteNote> {
try {
const userId = await requireAuth();
const validated = validateActionInputDeleteNote(input);
log.debug("Deleting note", { noteId: validated.noteId.toString() });
await serviceDeleteNote({
...validated,
userId,
});
log.info("Note deleted", { noteId: validated.noteId.toString() });
return {
success: true,
message: "Note deleted successfully",
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
if (e instanceof NoteNotFoundError) {
return { success: false, message: "Note not found" };
}
if (e instanceof NoteOwnershipError) {
return { success: false, message: "You do not have permission to delete this note" };
}
if (e instanceof Error && e.message === "Unauthorized") {
return { success: false, message: "Unauthorized" };
}
log.error("Failed to delete note", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetNoteById(
input: unknown,
): Promise<ActionOutputGetNoteById> {
try {
const validated = validateActionInputGetNoteById(input);
log.debug("Fetching note", { noteId: validated.noteId.toString() });
const note = await serviceGetNoteById(validated);
if (!note) {
return {
success: false,
message: "Note not found",
};
}
return {
success: true,
message: "Note retrieved successfully",
data: mapNoteToOutput(note),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get note", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetNotesByNoteTypeId(
input: unknown,
): Promise<ActionOutputGetNotes> {
try {
const validated = validateActionInputGetNotesByNoteTypeId(input);
log.debug("Fetching notes by note type", { noteTypeId: validated.noteTypeId });
const notes = await serviceGetNotesByNoteTypeId(validated);
return {
success: true,
message: "Notes retrieved successfully",
data: notes.map(mapNoteToOutput),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get notes by note type", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetNotesByUserId(
input: unknown,
): Promise<ActionOutputGetNotes> {
try {
const validated = validateActionInputGetNotesByUserId(input);
log.debug("Fetching notes by user", { userId: validated.userId });
const notes = await serviceGetNotesByUserId(validated);
return {
success: true,
message: "Notes retrieved successfully",
data: notes.map(mapNoteToOutput),
};
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("Failed to get notes by user", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetMyNotes(
limit?: number,
offset?: number,
): Promise<ActionOutputGetNotes> {
try {
const userId = await requireAuth();
log.debug("Fetching current user's notes", { userId, limit, offset });
const notes = await serviceGetNotesByUserId({
userId,
limit,
offset,
});
return {
success: true,
message: "Notes retrieved successfully",
data: notes.map(mapNoteToOutput),
};
} catch (e) {
if (e instanceof Error && e.message === "Unauthorized") {
return { success: false, message: "Unauthorized" };
}
log.error("Failed to get user's notes", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetMyNoteCount(): Promise<ActionOutputNoteCount> {
try {
const userId = await requireAuth();
log.debug("Counting current user's notes", { userId });
const result = await serviceCountNotesByUserId(userId);
return {
success: true,
message: "Note count retrieved successfully",
data: { count: result.count },
};
} catch (e) {
if (e instanceof Error && e.message === "Unauthorized") {
return { success: false, message: "Unauthorized" };
}
log.error("Failed to count user's notes", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}
export async function actionGetNoteCountByNoteType(
noteTypeId: number,
): Promise<ActionOutputNoteCount> {
try {
log.debug("Counting notes by note type", { noteTypeId });
const result = await serviceCountNotesByNoteTypeId(noteTypeId);
return {
success: true,
message: "Note count retrieved successfully",
data: { count: result.count },
};
} catch (e) {
log.error("Failed to count notes by note type", { error: e });
return { success: false, message: "An unknown error occurred" };
}
}

View File

@@ -1,72 +0,0 @@
// Repository layer DTOs for Note module
// Follows Anki-compatible note structure with BigInt IDs
export interface RepoInputCreateNote {
noteTypeId: number;
fields: string[];
tags?: string[];
userId: string;
}
export interface RepoInputUpdateNote {
id: bigint;
fields?: string[];
tags?: string[];
}
export interface RepoInputGetNoteById {
id: bigint;
}
export interface RepoInputGetNotesByNoteTypeId {
noteTypeId: number;
limit?: number;
offset?: number;
}
export interface RepoInputGetNotesByUserId {
userId: string;
limit?: number;
offset?: number;
}
export interface RepoInputDeleteNote {
id: bigint;
}
export interface RepoInputCheckNoteOwnership {
noteId: bigint;
userId: string;
}
export type RepoOutputNote = {
id: bigint;
guid: string;
noteTypeId: number;
mod: number;
usn: number;
tags: string;
flds: string;
sfld: string;
csum: number;
flags: number;
data: string;
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type RepoOutputNoteWithFields = Omit<RepoOutputNote, "flds" | "tags"> & {
fields: string[];
tagsArray: string[];
};
export type RepoOutputNoteOwnership = {
userId: string;
};
// Helper function types
export type RepoHelperGenerateGuid = () => string;
export type RepoHelperCalculateCsum = (text: string) => number;
export type RepoHelperJoinFields = (fields: string[]) => string;
export type RepoHelperSplitFields = (flds: string) => string[];

View File

@@ -1,284 +0,0 @@
import { prisma } from "@/lib/db";
import { createLogger } from "@/lib/logger";
import { createHash } from "crypto";
import {
RepoInputCreateNote,
RepoInputUpdateNote,
RepoInputGetNoteById,
RepoInputGetNotesByNoteTypeId,
RepoInputGetNotesByUserId,
RepoInputDeleteNote,
RepoInputCheckNoteOwnership,
RepoOutputNote,
RepoOutputNoteWithFields,
RepoOutputNoteOwnership,
} from "./note-repository-dto";
const log = createLogger("note-repository");
const FIELD_SEPARATOR = "\x1f";
const BASE91_CHARS =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%&()*+,-./:;<=>?@[]^_`{|}~";
export function repoGenerateGuid(): string {
let guid = "";
const bytes = new Uint8Array(10);
crypto.getRandomValues(bytes);
for (let i = 0; i < 10; i++) {
guid += BASE91_CHARS[bytes[i] % BASE91_CHARS.length];
}
return guid;
}
export function repoCalculateCsum(text: string): number {
const hash = createHash("sha1").update(text.normalize("NFC")).digest("hex");
// Use 7 hex chars to stay within INTEGER range (max 268,435,455 < 2,147,483,647)
return parseInt(hash.substring(0, 7), 16);
}
export function repoJoinFields(fields: string[]): string {
return fields.join(FIELD_SEPARATOR);
}
export function repoSplitFields(flds: string): string[] {
return flds.split(FIELD_SEPARATOR);
}
export async function repoCreateNote(
input: RepoInputCreateNote,
): Promise<bigint> {
const now = Date.now();
const id = BigInt(now);
const guid = repoGenerateGuid();
const flds = repoJoinFields(input.fields);
const sfld = input.fields[0] ?? "";
const csum = repoCalculateCsum(sfld);
const tags = input.tags?.join(" ") ?? " ";
log.debug("Creating note", { id: id.toString(), guid, noteTypeId: input.noteTypeId });
await prisma.note.create({
data: {
id,
guid,
noteTypeId: input.noteTypeId,
mod: Math.floor(now / 1000),
usn: -1,
tags,
flds,
sfld,
csum,
flags: 0,
data: "",
userId: input.userId,
},
});
log.info("Note created", { id: id.toString(), guid });
return id;
}
export async function repoUpdateNote(input: RepoInputUpdateNote): Promise<void> {
const now = Date.now();
const updateData: {
mod?: number;
usn?: number;
flds?: string;
sfld?: string;
csum?: number;
tags?: string;
} = {
mod: Math.floor(now / 1000),
usn: -1,
};
if (input.fields) {
updateData.flds = repoJoinFields(input.fields);
updateData.sfld = input.fields[0] ?? "";
updateData.csum = repoCalculateCsum(updateData.sfld);
}
if (input.tags) {
updateData.tags = input.tags.join(" ");
}
log.debug("Updating note", { id: input.id.toString() });
await prisma.note.update({
where: { id: input.id },
data: updateData,
});
log.info("Note updated", { id: input.id.toString() });
}
export async function repoGetNoteById(
input: RepoInputGetNoteById,
): Promise<RepoOutputNote | null> {
const note = await prisma.note.findUnique({
where: { id: input.id },
});
if (!note) {
log.debug("Note not found", { id: input.id.toString() });
return null;
}
return {
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tags,
flds: note.flds,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
};
}
export async function repoGetNoteByIdWithFields(
input: RepoInputGetNoteById,
): Promise<RepoOutputNoteWithFields | null> {
const note = await repoGetNoteById(input);
if (!note) return null;
return {
...note,
fields: repoSplitFields(note.flds),
tagsArray: note.tags.trim() === "" ? [] : note.tags.trim().split(" "),
};
}
export async function repoGetNotesByNoteTypeId(
input: RepoInputGetNotesByNoteTypeId,
): Promise<RepoOutputNote[]> {
const { noteTypeId, limit = 50, offset = 0 } = input;
log.debug("Fetching notes by note type", { noteTypeId, limit, offset });
const notes = await prisma.note.findMany({
where: { noteTypeId },
orderBy: { id: "desc" },
take: limit,
skip: offset,
});
log.info("Fetched notes by note type", { noteTypeId, count: notes.length });
return notes.map((note) => ({
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tags,
flds: note.flds,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}));
}
export async function repoGetNotesByUserId(
input: RepoInputGetNotesByUserId,
): Promise<RepoOutputNote[]> {
const { userId, limit = 50, offset = 0 } = input;
log.debug("Fetching notes by user", { userId, limit, offset });
const notes = await prisma.note.findMany({
where: { userId },
orderBy: { id: "desc" },
take: limit,
skip: offset,
});
log.info("Fetched notes by user", { userId, count: notes.length });
return notes.map((note) => ({
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tags,
flds: note.flds,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}));
}
export async function repoGetNotesByUserIdWithFields(
input: RepoInputGetNotesByUserId,
): Promise<RepoOutputNoteWithFields[]> {
const notes = await repoGetNotesByUserId(input);
return notes.map((note) => ({
...note,
fields: repoSplitFields(note.flds),
tagsArray: note.tags.trim() === "" ? [] : note.tags.trim().split(" "),
}));
}
export async function repoDeleteNote(input: RepoInputDeleteNote): Promise<void> {
log.debug("Deleting note", { id: input.id.toString() });
await prisma.note.delete({
where: { id: input.id },
});
log.info("Note deleted", { id: input.id.toString() });
}
export async function repoCheckNoteOwnership(
input: RepoInputCheckNoteOwnership,
): Promise<boolean> {
const note = await prisma.note.findUnique({
where: { id: input.noteId },
select: { userId: true },
});
return note?.userId === input.userId;
}
export async function repoGetNoteOwnership(
input: RepoInputGetNoteById,
): Promise<RepoOutputNoteOwnership | null> {
const note = await prisma.note.findUnique({
where: { id: input.id },
select: { userId: true },
});
if (!note) return null;
return { userId: note.userId };
}
export async function repoCountNotesByUserId(userId: string): Promise<number> {
return prisma.note.count({
where: { userId },
});
}
export async function repoCountNotesByNoteTypeId(
noteTypeId: number,
): Promise<number> {
return prisma.note.count({
where: { noteTypeId },
});
}

View File

@@ -1,60 +0,0 @@
export type ServiceInputCreateNote = {
noteTypeId: number;
fields: string[];
tags?: string[];
userId: string;
};
export type ServiceInputUpdateNote = {
noteId: bigint;
fields?: string[];
tags?: string[];
userId: string;
};
export type ServiceInputDeleteNote = {
noteId: bigint;
userId: string;
};
export type ServiceInputGetNoteById = {
noteId: bigint;
};
export type ServiceInputGetNotesByNoteTypeId = {
noteTypeId: number;
limit?: number;
offset?: number;
};
export type ServiceInputGetNotesByUserId = {
userId: string;
limit?: number;
offset?: number;
};
export type ServiceOutputNote = {
id: bigint;
guid: string;
noteTypeId: number;
mod: number;
usn: number;
tags: string[];
fields: string[];
sfld: string;
csum: number;
flags: number;
data: string;
userId: string;
createdAt: Date;
updatedAt: Date;
};
export type ServiceOutputCreateNote = {
id: bigint;
guid: string;
};
export type ServiceOutputNoteCount = {
count: number;
};

View File

@@ -1,200 +0,0 @@
import { createLogger } from "@/lib/logger";
import {
repoCreateNote,
repoUpdateNote,
repoGetNoteByIdWithFields,
repoGetNotesByNoteTypeId,
repoGetNotesByUserIdWithFields,
repoDeleteNote,
repoCheckNoteOwnership,
repoCountNotesByUserId,
repoCountNotesByNoteTypeId,
} from "./note-repository";
import {
ServiceInputCreateNote,
ServiceInputUpdateNote,
ServiceInputDeleteNote,
ServiceInputGetNoteById,
ServiceInputGetNotesByNoteTypeId,
ServiceInputGetNotesByUserId,
ServiceOutputNote,
ServiceOutputCreateNote,
ServiceOutputNoteCount,
} from "./note-service-dto";
const log = createLogger("note-service");
export class NoteNotFoundError extends Error {
constructor(noteId: bigint) {
super(`Note not found: ${noteId.toString()}`);
this.name = "NoteNotFoundError";
}
}
export class NoteOwnershipError extends Error {
constructor() {
super("You do not have permission to access this note");
this.name = "NoteOwnershipError";
}
}
export async function serviceCreateNote(
input: ServiceInputCreateNote,
): Promise<ServiceOutputCreateNote> {
log.debug("Creating note", { userId: input.userId, noteTypeId: input.noteTypeId });
const id = await repoCreateNote({
noteTypeId: input.noteTypeId,
fields: input.fields,
tags: input.tags,
userId: input.userId,
});
const note = await repoGetNoteByIdWithFields({ id });
if (!note) {
throw new NoteNotFoundError(id);
}
log.info("Note created successfully", { id: id.toString(), guid: note.guid });
return {
id,
guid: note.guid,
};
}
export async function serviceUpdateNote(
input: ServiceInputUpdateNote,
): Promise<void> {
log.debug("Updating note", { noteId: input.noteId.toString() });
const isOwner = await repoCheckNoteOwnership({
noteId: input.noteId,
userId: input.userId,
});
if (!isOwner) {
throw new NoteOwnershipError();
}
await repoUpdateNote({
id: input.noteId,
fields: input.fields,
tags: input.tags,
});
log.info("Note updated successfully", { noteId: input.noteId.toString() });
}
export async function serviceDeleteNote(
input: ServiceInputDeleteNote,
): Promise<void> {
log.debug("Deleting note", { noteId: input.noteId.toString() });
const isOwner = await repoCheckNoteOwnership({
noteId: input.noteId,
userId: input.userId,
});
if (!isOwner) {
throw new NoteOwnershipError();
}
await repoDeleteNote({ id: input.noteId });
log.info("Note deleted successfully", { noteId: input.noteId.toString() });
}
export async function serviceGetNoteById(
input: ServiceInputGetNoteById,
): Promise<ServiceOutputNote | null> {
log.debug("Fetching note by id", { noteId: input.noteId.toString() });
const note = await repoGetNoteByIdWithFields({ id: input.noteId });
if (!note) {
log.debug("Note not found", { noteId: input.noteId.toString() });
return null;
}
return {
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tagsArray,
fields: note.fields,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
};
}
export async function serviceGetNotesByNoteTypeId(
input: ServiceInputGetNotesByNoteTypeId,
): Promise<ServiceOutputNote[]> {
log.debug("Fetching notes by note type", { noteTypeId: input.noteTypeId });
const notes = await repoGetNotesByNoteTypeId(input);
return notes.map((note) => ({
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tags.trim() === "" ? [] : note.tags.trim().split(" "),
fields: note.flds.split("\x1f"),
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}));
}
export async function serviceGetNotesByUserId(
input: ServiceInputGetNotesByUserId,
): Promise<ServiceOutputNote[]> {
log.debug("Fetching notes by user", { userId: input.userId });
const notes = await repoGetNotesByUserIdWithFields(input);
return notes.map((note) => ({
id: note.id,
guid: note.guid,
noteTypeId: note.noteTypeId,
mod: note.mod,
usn: note.usn,
tags: note.tagsArray,
fields: note.fields,
sfld: note.sfld,
csum: note.csum,
flags: note.flags,
data: note.data,
userId: note.userId,
createdAt: note.createdAt,
updatedAt: note.updatedAt,
}));
}
export async function serviceCountNotesByUserId(
userId: string,
): Promise<ServiceOutputNoteCount> {
const count = await repoCountNotesByUserId(userId);
return { count };
}
export async function serviceCountNotesByNoteTypeId(
noteTypeId: number,
): Promise<ServiceOutputNoteCount> {
const count = await repoCountNotesByNoteTypeId(noteTypeId);
return { count };
}

View File

@@ -1,20 +0,0 @@
import { z } from "zod";
export const schemaActionInputProcessOCR = z.object({
imageBase64: z.string().min(1, "Image is required"),
deckId: z.number().int().positive("Deck ID must be positive"),
sourceLanguage: z.string().optional(),
targetLanguage: z.string().optional(),
});
export type ActionInputProcessOCR = z.infer<typeof schemaActionInputProcessOCR>;
export interface ActionOutputProcessOCR {
success: boolean;
message: string;
data?: {
pairsCreated: number;
sourceLanguage?: string;
targetLanguage?: string;
};
}

View File

@@ -1,36 +0,0 @@
"use server";
import { headers } from "next/headers";
import { auth } from "@/auth";
import { validate } from "@/utils/validate";
import { ValidateError } from "@/lib/errors";
import { createLogger } from "@/lib/logger";
import { serviceProcessOCR } from "./ocr-service";
import { schemaActionInputProcessOCR } from "./ocr-action-dto";
import type { ActionOutputProcessOCR } from "./ocr-action-dto";
const log = createLogger("ocr-action");
export async function actionProcessOCR(
input: unknown
): Promise<ActionOutputProcessOCR> {
try {
const session = await auth.api.getSession({ headers: await headers() });
if (!session?.user?.id) {
log.warn("Unauthorized OCR attempt");
return { success: false, message: "Unauthorized" };
}
const validatedInput = validate(input, schemaActionInputProcessOCR);
return serviceProcessOCR({
...validatedInput,
userId: session.user.id,
});
} catch (e) {
if (e instanceof ValidateError) {
return { success: false, message: e.message };
}
log.error("OCR action failed", { error: e });
return { success: false, message: "Unknown error occurred." };
}
}

View File

@@ -1 +0,0 @@
export {};

View File

@@ -1 +0,0 @@
export {};

View File

@@ -1,21 +0,0 @@
import { z } from "zod";
export const schemaServiceInputProcessOCR = z.object({
imageBase64: z.string().min(1, "Image is required"),
deckId: z.number().int().positive("Deck ID must be positive"),
sourceLanguage: z.string().optional(),
targetLanguage: z.string().optional(),
userId: z.string().min(1, "User ID is required"),
});
export type ServiceInputProcessOCR = z.infer<typeof schemaServiceInputProcessOCR>;
export interface ServiceOutputProcessOCR {
success: boolean;
message: string;
data?: {
pairsCreated: number;
sourceLanguage?: string;
targetLanguage?: string;
};
}

View File

@@ -1,155 +0,0 @@
import { executeOCR } from "@/lib/bigmodel/ocr/orchestrator";
import { serviceCheckOwnership } from "@/modules/deck/deck-service";
import { serviceCreateNote } from "@/modules/note/note-service";
import { serviceCreateCard } from "@/modules/card/card-service";
import { serviceGetNoteTypesByUserId, serviceCreateNoteType } from "@/modules/note-type/note-type-service";
import { createLogger } from "@/lib/logger";
import type { ServiceInputProcessOCR, ServiceOutputProcessOCR } from "./ocr-service-dto";
import { NoteKind } from "../../../generated/prisma/enums";
const log = createLogger("ocr-service");
const VOCABULARY_NOTE_TYPE_NAME = "Vocabulary (OCR)";
async function getOrCreateVocabularyNoteType(userId: string): Promise<number> {
const existingTypes = await serviceGetNoteTypesByUserId({ userId });
const existing = existingTypes.find((nt) => nt.name === VOCABULARY_NOTE_TYPE_NAME);
if (existing) {
return existing.id;
}
const fields = [
{ name: "Word", ord: 0, sticky: false, rtl: false, font: "Arial", size: 20, media: [] },
{ name: "Definition", ord: 1, sticky: false, rtl: false, font: "Arial", size: 20, media: [] },
{ name: "Source Language", ord: 2, sticky: false, rtl: false, font: "Arial", size: 16, media: [] },
{ name: "Target Language", ord: 3, sticky: false, rtl: false, font: "Arial", size: 16, media: [] },
];
const templates = [
{
name: "Word → Definition",
ord: 0,
qfmt: "{{Word}}",
afmt: "{{FrontSide}}<hr id=answer>{{Definition}}",
},
{
name: "Definition → Word",
ord: 1,
qfmt: "{{Definition}}",
afmt: "{{FrontSide}}<hr id=answer>{{Word}}",
},
];
const css = ".card { font-family: Arial; font-size: 20px; text-align: center; color: black; background-color: white; }";
const noteTypeId = await serviceCreateNoteType({
name: VOCABULARY_NOTE_TYPE_NAME,
kind: NoteKind.STANDARD,
css,
fields,
templates,
userId,
});
log.info("Created vocabulary note type", { noteTypeId, userId });
return noteTypeId;
}
export async function serviceProcessOCR(
input: ServiceInputProcessOCR
): Promise<ServiceOutputProcessOCR> {
log.info("Processing OCR request", { deckId: input.deckId, userId: input.userId });
const isOwner = await serviceCheckOwnership({
deckId: input.deckId,
userId: input.userId
});
if (!isOwner) {
log.warn("Deck ownership mismatch", {
deckId: input.deckId,
userId: input.userId
});
return {
success: false,
message: "You don't have permission to modify this deck"
};
}
let ocrResult;
try {
log.debug("Calling OCR pipeline");
ocrResult = await executeOCR({
imageBase64: input.imageBase64,
sourceLanguage: input.sourceLanguage,
targetLanguage: input.targetLanguage,
});
} catch (error) {
log.error("OCR pipeline failed", { error });
return {
success: false,
message: "Failed to process image. Please try again."
};
}
if (!ocrResult.pairs || ocrResult.pairs.length === 0) {
log.info("No vocabulary pairs extracted from image");
return {
success: false,
message: "No vocabulary pairs could be extracted from the image"
};
}
const sourceLanguage = ocrResult.detectedSourceLanguage || input.sourceLanguage || "Unknown";
const targetLanguage = ocrResult.detectedTargetLanguage || input.targetLanguage || "Unknown";
const noteTypeId = await getOrCreateVocabularyNoteType(input.userId);
let pairsCreated = 0;
for (const pair of ocrResult.pairs) {
try {
const { id: noteId } = await serviceCreateNote({
noteTypeId,
userId: input.userId,
fields: [pair.word, pair.definition, sourceLanguage, targetLanguage],
tags: ["ocr"],
});
await serviceCreateCard({
noteId,
deckId: input.deckId,
ord: 0,
});
await serviceCreateCard({
noteId,
deckId: input.deckId,
ord: 1,
});
pairsCreated++;
} catch (error) {
log.error("Failed to create note/card", {
word: pair.word,
error
});
}
}
log.info("OCR processing complete", {
pairsCreated,
sourceLanguage,
targetLanguage
});
return {
success: true,
message: `Successfully created ${pairsCreated} vocabulary pairs`,
data: {
pairsCreated,
sourceLanguage,
targetLanguage,
},
};
}

View File

@@ -1,50 +1,40 @@
"use server";
import { serviceGenIPA, serviceGenLanguage, serviceTranslateText } from "./translator-service";
import {
ActionInputTranslateText,
ActionOutputTranslateText,
validateActionInputTranslateText,
} from "./translator-action-dto";
import { ValidateError } from "@/lib/errors";
import { createLogger } from "@/lib/logger";
import { serviceTranslateText, serviceGenIPA, serviceGenLanguage } from "./translator-service";
import { ValidateError } from "@/lib/errors";
const log = createLogger("translator-action");
export const actionTranslateText = async (
dto: ActionInputTranslateText
input: unknown,
): Promise<ActionOutputTranslateText> => {
try {
const validated = validateActionInputTranslateText(input);
const result = await serviceTranslateText(validated);
return {
message: "success",
success: true,
data: await serviceTranslateText(validateActionInputTranslateText(dto)),
message: "Translation completed",
data: result,
};
} catch (e) {
if (e instanceof ValidateError) {
return {
success: false,
message: e.message,
};
return { success: false, message: e.message };
}
log.error("Translation action failed", { error: e });
return {
success: false,
message: "Unknown error occurred.",
};
log.error("Translation failed", { error: e instanceof Error ? e.message : String(e) });
return { success: false, message: "Translation failed" };
}
};
/**
* @deprecated 保留此函数以支持旧代码text-speaker 功能)
*/
export const genIPA = async (text: string) => {
export const genIPA = async (text: string): Promise<string> => {
return serviceGenIPA({ text });
};
/**
* @deprecated 保留此函数以支持旧代码text-speaker 功能)
*/
export const genLanguage = async (text: string) => {
export const genLanguage = async (text: string): Promise<string> => {
return serviceGenLanguage({ text });
};

View File

@@ -1,23 +0,0 @@
export type RepoInputSelectLatestTranslation = {
sourceText: string;
targetLanguage: string;
};
export type RepoOutputSelectLatestTranslation = {
id: number;
translatedText: string;
sourceLanguage: string;
targetLanguage: string;
sourceIpa: string | null;
targetIpa: string | null;
} | null;
export type RepoInputCreateTranslationHistory = {
userId?: string;
sourceText: string;
sourceLanguage: string;
targetLanguage: string;
translatedText: string;
sourceIpa?: string;
targetIpa?: string;
};

View File

@@ -1,41 +0,0 @@
import {
RepoInputCreateTranslationHistory,
RepoInputSelectLatestTranslation,
RepoOutputSelectLatestTranslation,
} from "./translator-repository-dto";
import { prisma } from "@/lib/db";
export async function repoSelectLatestTranslation(
dto: RepoInputSelectLatestTranslation
): Promise<RepoOutputSelectLatestTranslation> {
const result = await prisma.translationHistory.findFirst({
where: {
sourceText: dto.sourceText,
targetLanguage: dto.targetLanguage,
},
orderBy: {
createdAt: "desc",
},
});
if (!result) {
return null;
}
return {
id: result.id,
translatedText: result.translatedText,
sourceLanguage: result.sourceLanguage,
targetLanguage: result.targetLanguage,
sourceIpa: result.sourceIpa,
targetIpa: result.targetIpa,
};
}
export async function repoCreateTranslationHistory(
data: RepoInputCreateTranslationHistory
) {
return await prisma.translationHistory.create({
data: data,
});
}

View File

@@ -1,6 +1,5 @@
import { executeTranslation } from "@/lib/bigmodel/translator/orchestrator";
import { getAnswer } from "@/lib/bigmodel/llm";
import { repoCreateTranslationHistory, repoSelectLatestTranslation } from "./translator-repository";
import {
ServiceInputTranslateText,
ServiceOutputTranslateText,
@@ -16,68 +15,23 @@ const log = createLogger("translator-service");
export const serviceTranslateText = async (
dto: ServiceInputTranslateText
): Promise<ServiceOutputTranslateText> => {
const { sourceText, targetLanguage, forceRetranslate, needIpa, userId, sourceLanguage } = dto;
const { sourceText, targetLanguage, sourceLanguage, needIpa } = dto;
// Check for existing translation
const lastTranslation = await repoSelectLatestTranslation({
const response = await executeTranslation(
sourceText,
targetLanguage,
});
needIpa,
sourceLanguage
);
if (forceRetranslate || !lastTranslation) {
// Call AI for translation
const response = await executeTranslation(
sourceText,
targetLanguage,
needIpa,
sourceLanguage
);
// Save translation history asynchronously (don't block response)
repoCreateTranslationHistory({
userId,
sourceText,
sourceLanguage: response.sourceLanguage,
targetLanguage: response.targetLanguage,
translatedText: response.translatedText,
sourceIpa: needIpa ? response.sourceIpa : undefined,
targetIpa: needIpa ? response.targetIpa : undefined,
}).catch((error) => {
log.error("Failed to save translation data", { error });
});
return {
sourceText: response.sourceText,
translatedText: response.translatedText,
sourceLanguage: response.sourceLanguage,
targetLanguage: response.targetLanguage,
sourceIpa: response.sourceIpa || "",
targetIpa: response.targetIpa || "",
};
} else {
// Return cached translation
// Still save a history record for analytics
repoCreateTranslationHistory({
userId,
sourceText,
sourceLanguage: lastTranslation.sourceLanguage,
targetLanguage: lastTranslation.targetLanguage,
translatedText: lastTranslation.translatedText,
sourceIpa: lastTranslation.sourceIpa || undefined,
targetIpa: lastTranslation.targetIpa || undefined,
}).catch((error) => {
log.error("Failed to save translation data", { error });
});
return {
sourceText,
translatedText: lastTranslation.translatedText,
sourceLanguage: lastTranslation.sourceLanguage,
targetLanguage: lastTranslation.targetLanguage,
sourceIpa: lastTranslation.sourceIpa || "",
targetIpa: lastTranslation.targetIpa || "",
};
}
return {
sourceText: response.sourceText,
translatedText: response.translatedText,
sourceLanguage: response.sourceLanguage,
targetLanguage: response.targetLanguage,
sourceIpa: response.sourceIpa || "",
targetIpa: response.targetIpa || "",
};
};
export const serviceGenIPA = async (
@@ -96,7 +50,7 @@ export const serviceGenIPA = async (
不要附带任何说明
不要擅自增减符号
不许用"/"或者"[]"包裹
`.trim(),
`.trim(),
)
)
.replaceAll("[", "")
@@ -139,7 +93,7 @@ export const serviceGenLanguage = async (
2. 首字母大写,其余小写
3. 不要附带任何说明
4. 不要擅自增减符号
`.trim()
`.trim()
},
{
role: "user",

View File

@@ -1,165 +0,0 @@
/**
* Shared types for Anki-compatible data structures
* Based on Anki's official database schema
*/
import type { CardType, CardQueue, NoteKind, Visibility } from "../../generated/prisma/enums";
// ============================================
// NoteType (Anki: models)
// ============================================
export interface NoteTypeField {
name: string;
ord: number;
sticky: boolean;
rtl: boolean;
font: string;
size: number;
media: string[];
}
export interface NoteTypeTemplate {
name: string;
ord: number;
qfmt: string; // Question format (Mustache template)
afmt: string; // Answer format (Mustache template)
bqfmt?: string; // Browser question format
bafmt?: string; // Browser answer format
did?: number; // Deck override
}
export interface TSharedNoteType {
id: number;
name: string;
kind: NoteKind;
css: string;
fields: NoteTypeField[];
templates: NoteTypeTemplate[];
userId: string;
createdAt: Date;
updatedAt: Date;
}
// ============================================
// Deck (Anki: decks) - replaces Folder
// ============================================
export interface TSharedDeck {
id: number;
name: string;
desc: string;
userId: string;
visibility: Visibility;
collapsed: boolean;
conf: Record<string, unknown>;
createdAt: Date;
updatedAt: Date;
cardCount?: number;
}
// ============================================
// Note (Anki: notes)
// ============================================
export interface TSharedNote {
id: bigint;
guid: string;
noteTypeId: number;
mod: number;
usn: number;
tags: string; // Space-separated
flds: string; // Field values separated by 0x1f
sfld: string; // Sort field
csum: number; // Checksum of first field
flags: number;
data: string;
userId: string;
createdAt: Date;
updatedAt: Date;
}
// Helper to get fields as array
export function getNoteFields(note: TSharedNote): string[] {
return note.flds.split('\x1f');
}
// Helper to set fields from array
export function setNoteFields(fields: string[]): string {
return fields.join('\x1f');
}
// Helper to get tags as array
export function getNoteTags(note: TSharedNote): string[] {
return note.tags.trim().split(' ').filter(Boolean);
}
// Helper to set tags from array
export function setNoteTags(tags: string[]): string {
return ` ${tags.join(' ')} `;
}
// ============================================
// Card (Anki: cards)
// ============================================
export interface TSharedCard {
id: bigint;
noteId: bigint;
deckId: number;
ord: number;
mod: number;
usn: number;
type: CardType;
queue: CardQueue;
due: number;
ivl: number;
factor: number;
reps: number;
lapses: number;
left: number;
odue: number;
odid: number;
flags: number;
data: string;
createdAt: Date;
updatedAt: Date;
}
// Card for review (with note data)
export interface TCardForReview extends TSharedCard {
note: TSharedNote & {
noteType: TSharedNoteType;
};
deck: TSharedDeck;
}
// ============================================
// Review
// ============================================
export type ReviewEase = 1 | 2 | 3 | 4; // Again, Hard, Good, Easy
export interface TSharedRevlog {
id: bigint;
cardId: bigint;
usn: number;
ease: number;
ivl: number;
lastIvl: number;
factor: number;
time: number; // Review time in ms
type: number;
}
// ============================================
// Deck Favorites
// ============================================
export interface TSharedDeckFavorite {
id: number;
userId: string;
deckId: number;
createdAt: Date;
deck?: TSharedDeck;
}

33
src/shared/card-type.ts Normal file
View File

@@ -0,0 +1,33 @@
export type DictionaryItemEntry = {
id: number;
itemId: number;
ipa: string | null;
definition: string;
partOfSpeech: string | null;
example: string;
createdAt: Date;
updatedAt: Date;
};
export type DictionaryItemWithEntries = {
id: number;
frequency: number;
standardForm: string;
queryLang: string;
definitionLang: string;
createdAt: Date;
updatedAt: Date;
entries: DictionaryItemEntry[];
};
export type CardSide = "A" | "B";
export type CardForStudy = {
id: number;
deckId: number;
showSideAFirst: boolean;
sideA: DictionaryItemWithEntries;
sideB: DictionaryItemWithEntries;
createdAt: Date;
updatedAt: Date;
};

View File

@@ -6,6 +6,7 @@ export type TSharedEntry = {
};
export type TSharedItem = {
id?: number;
standardForm: string,
entries: TSharedEntry[];
};

View File

@@ -280,7 +280,7 @@ export const THEME_PRESETS: ThemePreset[] = [
},
];
export const DEFAULT_THEME = "teal";
export const DEFAULT_THEME = "mist";
export function getThemePreset(id: string): ThemePreset | undefined {
return THEME_PRESETS.find((preset) => preset.id === id);