Skip to content

Commit

Permalink
feat: check source language before request translation api
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jun 13, 2024
1 parent 12ddfc5 commit abe96c5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
39 changes: 37 additions & 2 deletions src/renderer/src/components/entry-column/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { views } from "@renderer/lib/constants"
import { FeedViewType } from "@renderer/lib/enum"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import type { SupportedLanguages } from "@renderer/models"
import { Queries } from "@renderer/queries"
import { feedActions, useFeedStore } from "@renderer/store"
import { useEntry } from "@renderer/store/entry/hooks"
import { franc } from "franc-min"
import type { FC } from "react"
import { memo } from "react"

Expand All @@ -20,6 +22,23 @@ import { SocialMediaItem } from "./social-media-item"
import type { UniversalItemProps } from "./types"
import { VideoItem } from "./video-item"

const LanguageMap: Record<SupportedLanguages, {
code: string
}> = {
"en": {
code: "eng",
},
"ja": {
code: "jpn",
},
"zh-CN": {
code: "cmn",
},
"zh-TW": {
code: "cmn",
},
}

function EntryItemImpl({
entryId,
view,
Expand All @@ -33,12 +52,28 @@ function EntryItemImpl({
entry,
})

let fields = (entry.settings?.translation && view !== undefined) ? (views[view!].translation).split(",") : []

fields = fields.filter((field) => {
if (entry.settings?.translation && entry.entries[field]) {
const sourceLanguage = franc(entry.entries[field])

if (sourceLanguage === LanguageMap[entry.settings?.translation].code) {
return false
} else {
return true
}
} else {
return false
}
})

const translation = useBizQuery(Queries.ai.translation({
id: entry.entries.id,
language: entry.settings?.translation,
fields: view ? views[view].translation : "",
fields: fields?.join(",") || "title",
}), {
enabled: !!entry.settings?.translation,
enabled: !!entry.settings?.translation && !!fields?.length,
})

const activeEntry = useFeedStore((state) => state.activeEntry)
Expand Down
42 changes: 21 additions & 21 deletions src/renderer/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
name: string;
condition: {
value: string;
field: "title" | "view" | "site_url" | "feed_url";
field: "title" | "site_url" | "view" | "feed_url";
operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex";
}[];
result: {
Expand All @@ -47,7 +47,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
}[] | undefined;
blockRules?: {
value: string | number;
field: "title" | "content" | "link" | "all" | "author" | "order";
field: "title" | "content" | "author" | "link" | "all" | "order";
operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex";
}[] | undefined;
};
Expand All @@ -64,7 +64,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
name: string;
condition: {
value: string;
field: "title" | "view" | "site_url" | "feed_url";
field: "title" | "site_url" | "view" | "feed_url";
operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex";
}[];
result: {
Expand All @@ -76,7 +76,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
}[] | undefined;
blockRules?: {
value: string | number;
field: "title" | "content" | "link" | "all" | "author" | "order";
field: "title" | "content" | "author" | "link" | "all" | "order";
operator: "contains" | "not_contains" | "eq" | "not_eq" | "gt" | "lt" | "regex";
}[] | undefined;
};
Expand Down Expand Up @@ -134,8 +134,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
$post: {
input: {
json: {
view?: number | undefined;
feedId?: string | undefined;
view?: number | undefined;
feedIdList?: string[] | undefined;
};
};
Expand Down Expand Up @@ -258,9 +258,9 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
readCount: number;
subscription?: {
title: string | null;
view: number;
userId: string;
feedId: string;
view: number;
category: string | null;
isPrivate: boolean | null;
} | undefined;
Expand Down Expand Up @@ -314,11 +314,11 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
description: string | null;
title: string | null;
content: string | null;
author: string | null;
id: string;
url: string | null;
feedId: string;
url: string | null;
guid: string;
author: string | null;
authorUrl: string | null;
authorAvatar: string | null;
changedAt: string;
Expand Down Expand Up @@ -387,8 +387,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
$post: {
input: {
json: {
view?: number | undefined;
feedId?: string | undefined;
view?: number | undefined;
read?: boolean | undefined;
limit?: number | undefined;
feedIdList?: string[] | undefined;
Expand All @@ -403,10 +403,10 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
entries: {
description: string | null;
title: string | null;
author: string | null;
id: string;
url: string | null;
guid: string;
author: string | null;
authorUrl: string | null;
authorAvatar: string | null;
changedAt: string;
Expand Down Expand Up @@ -465,10 +465,10 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
description: string | null;
title: string | null;
content: string | null;
author: string | null;
id: string;
url: string | null;
guid: string;
author: string | null;
authorUrl: string | null;
authorAvatar: string | null;
changedAt: string;
Expand All @@ -482,6 +482,9 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
title?: string | undefined;
}[] | null | undefined;
};
collections: {
createdAt: string;
};
feeds: {
description: string | null;
title: string | null;
Expand All @@ -497,9 +500,6 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
errorMessage: string | null;
errorAt: string | null;
};
collections: {
createdAt: string;
};
read: boolean | null;
settings?: {
translation?: "en" | "ja" | "zh-CN" | "zh-TW" | undefined;
Expand Down Expand Up @@ -528,11 +528,11 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
description: string | null;
title: string | null;
content: string | null;
author: string | null;
id: string;
url: string | null;
feedId: string;
url: string | null;
guid: string;
author: string | null;
authorUrl: string | null;
authorAvatar: string | null;
changedAt: string;
Expand Down Expand Up @@ -563,8 +563,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
code: 0;
data: {
title: string | null;
view: number;
userId: string;
feedId: string;
feeds: {
description: string | null;
title: string | null;
Expand All @@ -580,7 +580,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
errorMessage: string | null;
errorAt: string | null;
};
feedId: string;
view: number;
category: string | null;
isPrivate: boolean | null;
}[];
Expand All @@ -591,8 +591,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
$post: {
input: {
json: {
view: number;
url: string;
view: number;
category?: string | null | undefined;
isPrivate?: boolean | null | undefined;
};
Expand All @@ -606,8 +606,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
$delete: {
input: {
json: {
url?: string | undefined;
feedId?: string | undefined;
url?: string | undefined;
};
};
output: {
Expand All @@ -619,8 +619,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
$patch: {
input: {
json: {
view: number;
feedId: string;
view: number;
category?: string | null | undefined;
isPrivate?: boolean | null | undefined;
};
Expand Down

0 comments on commit abe96c5

Please sign in to comment.