...
This commit is contained in:
@@ -2,16 +2,15 @@
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import Container from "@/components/ui/Container";
|
||||
import { lookUp } from "@/lib/server/bigmodel/dictionaryActions";
|
||||
import { toast } from "sonner";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { Folder } from "../../../../generated/prisma/browser";
|
||||
import { getFoldersByUserId } from "@/lib/server/services/folderService";
|
||||
import { DictLookUpResponse, isDictErrorResponse } from "./types";
|
||||
import { DictLookUpResponse } from "./types";
|
||||
import { SearchForm } from "./SearchForm";
|
||||
import { SearchResult } from "./SearchResult";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { POPULAR_LANGUAGES } from "./constants";
|
||||
import { performDictionaryLookup } from "./utils";
|
||||
|
||||
export default function Dictionary() {
|
||||
const t = useTranslations("dictionary");
|
||||
@@ -52,28 +51,22 @@ export default function Dictionary() {
|
||||
setHasSearched(true);
|
||||
setSearchResult(null);
|
||||
|
||||
try {
|
||||
// 使用查询语言和释义语言的 nativeName
|
||||
const result = await lookUp({
|
||||
const result = await performDictionaryLookup(
|
||||
{
|
||||
text: searchQuery,
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
queryLang: getNativeName(queryLang)
|
||||
})
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang)
|
||||
},
|
||||
t
|
||||
);
|
||||
|
||||
// 检查是否为错误响应
|
||||
if (isDictErrorResponse(result)) {
|
||||
toast.error(result.error);
|
||||
setSearchResult(null);
|
||||
} else {
|
||||
setSearchResult(result);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("词典查询失败:", error);
|
||||
toast.error(t("lookupFailed"));
|
||||
if (result.success && result.data) {
|
||||
setSearchResult(result.data);
|
||||
} else {
|
||||
setSearchResult(null);
|
||||
} finally {
|
||||
setIsSearching(false);
|
||||
}
|
||||
|
||||
setIsSearching(false);
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -111,7 +104,7 @@ export default function Dictionary() {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isSearching && searchResult && !isDictErrorResponse(searchResult) && (
|
||||
{!isSearching && searchResult && (
|
||||
<SearchResult
|
||||
searchResult={searchResult}
|
||||
searchQuery={searchQuery}
|
||||
|
||||
@@ -3,17 +3,15 @@ import { toast } from "sonner";
|
||||
import { authClient } from "@/lib/auth-client";
|
||||
import { Folder } from "../../../../generated/prisma/browser";
|
||||
import { createPair } from "@/lib/server/services/pairService";
|
||||
import { lookUp } from "@/lib/server/bigmodel/dictionaryActions";
|
||||
import {
|
||||
DictWordResponse,
|
||||
DictPhraseResponse,
|
||||
isDictWordResponse,
|
||||
DictWordEntry,
|
||||
isDictErrorResponse,
|
||||
} from "./types";
|
||||
import { DictionaryEntry } from "./DictionaryEntry";
|
||||
import { POPULAR_LANGUAGES } from "./constants";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { performDictionaryLookup } from "./utils";
|
||||
|
||||
interface SearchResultProps {
|
||||
searchResult: DictWordResponse | DictPhraseResponse;
|
||||
@@ -46,26 +44,21 @@ export function SearchResult({
|
||||
const handleRelookup = async () => {
|
||||
onSearchingChange(true);
|
||||
|
||||
try {
|
||||
const result = await lookUp({
|
||||
const result = await performDictionaryLookup(
|
||||
{
|
||||
text: searchQuery,
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
queryLang: getNativeName(queryLang),
|
||||
definitionLang: getNativeName(definitionLang),
|
||||
forceRelook: true
|
||||
});
|
||||
},
|
||||
t
|
||||
);
|
||||
|
||||
if (isDictErrorResponse(result)) {
|
||||
toast.error(result.error);
|
||||
} else {
|
||||
onResultUpdate(result);
|
||||
toast.success(t("relookupSuccess"));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("词典重新查询失败:", error);
|
||||
toast.error(t("lookupFailed"));
|
||||
} finally {
|
||||
onSearchingChange(false);
|
||||
if (result.success && result.data) {
|
||||
onResultUpdate(result.data);
|
||||
}
|
||||
|
||||
onSearchingChange(false);
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
|
||||
51
src/app/(features)/dictionary/utils.ts
Normal file
51
src/app/(features)/dictionary/utils.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { toast } from "sonner";
|
||||
import { lookUp } from "@/lib/server/bigmodel/dictionaryActions";
|
||||
import {
|
||||
DictWordResponse,
|
||||
DictPhraseResponse,
|
||||
} from "./types";
|
||||
|
||||
interface LookupOptions {
|
||||
text: string;
|
||||
queryLang: string;
|
||||
definitionLang: string;
|
||||
forceRelook?: boolean;
|
||||
}
|
||||
|
||||
interface LookupResult {
|
||||
success: boolean;
|
||||
data?: DictWordResponse | DictPhraseResponse;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行词典查询的通用函数
|
||||
* @param options - 查询选项
|
||||
* @param t - 翻译函数
|
||||
* @returns 查询结果
|
||||
*/
|
||||
export async function performDictionaryLookup(
|
||||
options: LookupOptions,
|
||||
t?: (key: string) => string
|
||||
): Promise<LookupResult> {
|
||||
const { text, queryLang, definitionLang, forceRelook = false } = options;
|
||||
|
||||
try {
|
||||
const result = await lookUp({
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang,
|
||||
forceRelook
|
||||
});
|
||||
|
||||
// 成功时显示提示(仅强制重新查询时)
|
||||
if (forceRelook && t) {
|
||||
toast.success(t("relookupSuccess"));
|
||||
}
|
||||
|
||||
return { success: true, data: result };
|
||||
} catch (error) {
|
||||
toast.error(String(error));
|
||||
return { success: false, error: String(error) };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user