From 659b65c7f9324ad73601a2b280c7f89f5bf284fd Mon Sep 17 00:00:00 2001 From: Innei Date: Sat, 19 Aug 2023 00:26:05 +0800 Subject: [PATCH] fix: summary lang Signed-off-by: Innei --- src/app/notes/[id]/pageImpl.tsx | 2 +- .../[category]/[slug]/pageImpl.tsx | 2 +- src/components/widgets/ai/Summary.tsx | 6 +- src/components/widgets/peek/NotePreview.tsx | 2 +- src/components/widgets/peek/PostPreview.tsx | 2 +- .../{ai => shared}/SummarySwitcher.tsx | 8 +- .../widgets/xlog/XLogSummaryAsync.tsx | 77 +++++++++++++++++++ .../widgets/xlog/XLogSummaryRSC.tsx | 17 ++-- 8 files changed, 99 insertions(+), 17 deletions(-) rename src/components/widgets/{ai => shared}/SummarySwitcher.tsx (81%) create mode 100644 src/components/widgets/xlog/XLogSummaryAsync.tsx diff --git a/src/app/notes/[id]/pageImpl.tsx b/src/app/notes/[id]/pageImpl.tsx index db4173657a..7fba050b4d 100644 --- a/src/app/notes/[id]/pageImpl.tsx +++ b/src/app/notes/[id]/pageImpl.tsx @@ -4,7 +4,6 @@ import type { NoteModel } from '@mx-space/api-client' import { ClientOnly } from '~/components/common/ClientOnly' -import { SummarySwitcher } from '~/components/widgets/ai/SummarySwitcher' import { NoteActionAside, NoteFooterNavigationBarForMobile, @@ -14,6 +13,7 @@ import { NoteRootBanner } from '~/components/widgets/note/NoteBanner' import { ArticleRightAside } from '~/components/widgets/shared/ArticleRightAside' import { BanCopyWrapper } from '~/components/widgets/shared/BanCopyWrapper' import { ReadIndicatorForMobile } from '~/components/widgets/shared/ReadIndicator' +import { SummarySwitcher } from '~/components/widgets/shared/SummarySwitcher' import { SubscribeBell } from '~/components/widgets/subscribe' import { XLogInfoForNote } from '~/components/widgets/xlog' import { LayoutRightSidePortal } from '~/providers/shared/LayoutRightSideProvider' diff --git a/src/app/posts/(post-detail)/[category]/[slug]/pageImpl.tsx b/src/app/posts/(post-detail)/[category]/[slug]/pageImpl.tsx index f6e465645c..87b80c1346 100644 --- a/src/app/posts/(post-detail)/[category]/[slug]/pageImpl.tsx +++ b/src/app/posts/(post-detail)/[category]/[slug]/pageImpl.tsx @@ -1,7 +1,6 @@ import type { PostModel } from '@mx-space/api-client' import { ClientOnly } from '~/components/common/ClientOnly' -import { SummarySwitcher } from '~/components/widgets/ai/SummarySwitcher' import { PostActionAside, PostCopyright, @@ -11,6 +10,7 @@ import { import { ArticleRightAside } from '~/components/widgets/shared/ArticleRightAside' import { GoToAdminEditingButton } from '~/components/widgets/shared/GoToAdminEditingButton' import { ReadIndicatorForMobile } from '~/components/widgets/shared/ReadIndicator' +import { SummarySwitcher } from '~/components/widgets/shared/SummarySwitcher' import { SubscribeBell } from '~/components/widgets/subscribe' import { XLogInfoForPost } from '~/components/widgets/xlog' import { LayoutRightSidePortal } from '~/providers/shared/LayoutRightSideProvider' diff --git a/src/components/widgets/ai/Summary.tsx b/src/components/widgets/ai/Summary.tsx index bcc041da2e..4d14283fd2 100644 --- a/src/components/widgets/ai/Summary.tsx +++ b/src/components/widgets/ai/Summary.tsx @@ -50,10 +50,12 @@ export const AISummary: FC = (props) => { summary: string source: string }>( - [`ai-summary`, data.id, API_URL, data.modified], + ['ai-summary', data.id, API_URL, data.modified], async () => { const data = await fetch( - `/api/ai/summary?data=${encodeURIComponent(JSON.stringify(payload))}`, + `/api/ai/summary?data=${encodeURIComponent( + JSON.stringify(payload), + )}&lang=${navigator.language}`, ).then((res) => res.json()) if (!data) throw new Error('请求错误') return data diff --git a/src/components/widgets/peek/NotePreview.tsx b/src/components/widgets/peek/NotePreview.tsx index 085384a551..1bb846a38f 100644 --- a/src/components/widgets/peek/NotePreview.tsx +++ b/src/components/widgets/peek/NotePreview.tsx @@ -21,9 +21,9 @@ import { import { WrappedElementProvider } from '~/providers/shared/WrappedElementProvider' import { queries } from '~/queries/definition' -import { SummarySwitcher } from '../ai/SummarySwitcher' import { NoteHideIfSecret, NoteMetaBar, NoteRootBanner } from '../note' import { BanCopyWrapper } from '../shared/BanCopyWrapper' +import { SummarySwitcher } from '../shared/SummarySwitcher' interface NotePreviewProps { noteId: number diff --git a/src/components/widgets/peek/PostPreview.tsx b/src/components/widgets/peek/PostPreview.tsx index 354566f54c..56912daf5b 100644 --- a/src/components/widgets/peek/PostPreview.tsx +++ b/src/components/widgets/peek/PostPreview.tsx @@ -19,8 +19,8 @@ import { import { WrappedElementProvider } from '~/providers/shared/WrappedElementProvider' import { queries } from '~/queries/definition' -import { SummarySwitcher } from '../ai/SummarySwitcher' import { PostOutdate } from '../post' +import { SummarySwitcher } from '../shared/SummarySwitcher' interface PostPreviewProps { category: string diff --git a/src/components/widgets/ai/SummarySwitcher.tsx b/src/components/widgets/shared/SummarySwitcher.tsx similarity index 81% rename from src/components/widgets/ai/SummarySwitcher.tsx rename to src/components/widgets/shared/SummarySwitcher.tsx index 5ab676b3c6..45902e5b60 100644 --- a/src/components/widgets/ai/SummarySwitcher.tsx +++ b/src/components/widgets/shared/SummarySwitcher.tsx @@ -1,13 +1,13 @@ import { memo } from 'react' import type { FC } from 'react' -import type { AiSummaryProps } from './Summary' +import type { AiSummaryProps } from '../ai/Summary' import { appStaticConfig } from '~/app.static.config' import { ErrorBoundary } from '~/components/common/ErrorBoundary' +import { AISummary } from '../ai/Summary' import { getCidForBaseModel } from '../xlog/utils' -import { XLogSummary } from '../xlog/XLogSummaryRSC' -import { AISummary } from './Summary' +import { XLogSummaryAsync } from '../xlog/XLogSummaryAsync' export const SummarySwitcher: FC = memo((props) => { const { enabled, providers } = appStaticConfig.ai.summary @@ -22,7 +22,7 @@ export const SummarySwitcher: FC = memo((props) => { if (comp) break switch (provider) { case 'xlog': - if (cid) comp = + if (cid) comp = break case 'openai': if (!process.env.OPENAI_API_KEY) return diff --git a/src/components/widgets/xlog/XLogSummaryAsync.tsx b/src/components/widgets/xlog/XLogSummaryAsync.tsx new file mode 100644 index 0000000000..a6ffa2037a --- /dev/null +++ b/src/components/widgets/xlog/XLogSummaryAsync.tsx @@ -0,0 +1,77 @@ +'use client' + +import { Suspense } from 'react' + +import { clsxm } from '~/lib/helper' + +import { AutoResizeHeight } from '../shared/AutoResizeHeight' + +const fetchData = async (cid: string) => { + if (!cid) { + return null + } + + return fetch(`/api/xlog/summary?cid=${cid}&lang=${navigator.language}`, { + next: { + revalidate: 60 * 10, + }, + }) + .then((res) => res.json()) + .catch(() => null) +} + +export const XLogSummaryAsync = async ( + props: ComponentType<{ + cid: string + }>, +) => { + const { cid } = props + if (!cid) return null + + return ( +
+
+ + AI 生成的摘要 +
+ + + + + + +

+ (此服务由{' '} + + xLog + {' '} + 驱动) +

+
+ ) +} + +const RealDataRender = async ({ cid }: { cid: string }) => { + const data = await fetchData(cid) + if (!data) return null + + return ( +

+ {data?.data} +

+ ) +} + +const LoadingSkeleton = ( +
+ + + +
+) diff --git a/src/components/widgets/xlog/XLogSummaryRSC.tsx b/src/components/widgets/xlog/XLogSummaryRSC.tsx index 20cd7eb23e..adea2c3d11 100644 --- a/src/components/widgets/xlog/XLogSummaryRSC.tsx +++ b/src/components/widgets/xlog/XLogSummaryRSC.tsx @@ -5,7 +5,7 @@ const headers = { 'User-Agent': `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Shiro`, } -const fetchData = async (cid: string, lang = 'zh') => { +const fetchData = async (cid: string) => { if (!cid) { return null } @@ -15,13 +15,16 @@ const fetchData = async (cid: string, lang = 'zh') => { abortController.abort() }, 3000) - return fetch(`https://xlog.app/api/summary?cid=${cid}&lang=${lang}`, { - headers: new Headers(headers), - signal: abortController.signal, - next: { - revalidate: 60 * 10, + return fetch( + `https://xlog.app/api/summary?cid=${cid}&lang=${navigator.language}`, + { + headers: new Headers(headers), + signal: abortController.signal, + next: { + revalidate: 60 * 10, + }, }, - }) + ) .then((res) => res.json()) .catch(() => null) }