-
-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use client' | ||
|
||
import { useQuery } from '@tanstack/react-query' | ||
import { useMemo } from 'react' | ||
import type { NoteModel, PageModel, PostModel } from '@mx-space/api-client' | ||
import type { ArticleDataType } from '~/types/api' | ||
import type { FC, ReactNode } from 'react' | ||
|
||
import { AutoResizeHeight } from '~/components/widgets/shared/AutoResizeHeight' | ||
import { API_URL } from '~/constants/env' | ||
import { clsxm } from '~/lib/helper' | ||
import { isNoteModel, isPageModel, isPostModel } from '~/lib/url-builder' | ||
|
||
export const AISummary: FC<{ | ||
data: PostModel | NoteModel | PageModel | ||
className?: string | ||
}> = (props) => { | ||
const { data } = props | ||
|
||
const payload = useMemo(() => { | ||
let payload: ArticleDataType | ||
|
||
if (isPostModel(data)) { | ||
payload = { | ||
category: data.category.slug, | ||
slug: data.slug, | ||
type: 'post', | ||
} | ||
} else if (isNoteModel(data)) { | ||
payload = { | ||
nid: data.nid, | ||
type: 'note', | ||
} | ||
} else if (isPageModel(data)) { | ||
payload = { | ||
slug: data.slug, | ||
type: 'page', | ||
} | ||
} else { | ||
throw new Error('未知类型') | ||
} | ||
|
||
return payload | ||
}, [data]) | ||
const { data: response, isLoading } = useQuery<{ | ||
summary: string | ||
source: string | ||
}>( | ||
[`ai-summary`, data.id, API_URL, data.modified], | ||
async () => { | ||
const data = await fetch( | ||
`/api/ai/summary?data=${encodeURIComponent(JSON.stringify(payload))}`, | ||
{ | ||
next: { | ||
revalidate: 60 * 10, | ||
}, | ||
}, | ||
).then((res) => res.json()) | ||
if (!data) throw new Error('请求错误') | ||
return data | ||
}, | ||
{ | ||
staleTime: 1000 * 60 * 60 * 24 * 7, | ||
retryDelay: 5000, | ||
}, | ||
) | ||
|
||
const Inner: ReactNode = ( | ||
<div | ||
data-hide-print | ||
className={clsxm( | ||
`space-y-2 rounded-xl border border-slate-200 p-4 dark:border-neutral-800`, | ||
props.className, | ||
)} | ||
> | ||
<div className="flex items-center"> | ||
<i className="icon-[mingcute--sparkles-line] mr-2 text-lg" /> | ||
AI 生成的摘要 | ||
</div> | ||
|
||
<AutoResizeHeight duration={0.3}> | ||
<p className="text-base-content/85 !m-0 text-sm leading-loose"> | ||
{isLoading ? ( | ||
<div className="space-y-2"> | ||
<span className="block h-5 w-full animate-pulse rounded-xl bg-zinc-200 dark:bg-neutral-800" /> | ||
<span className="block h-5 w-full animate-pulse rounded-xl bg-zinc-200 dark:bg-neutral-800" /> | ||
<span className="block h-5 w-full animate-pulse rounded-xl bg-zinc-200 dark:bg-neutral-800" /> | ||
</div> | ||
) : ( | ||
response?.summary | ||
)} | ||
</p> | ||
</AutoResizeHeight> | ||
</div> | ||
) | ||
|
||
return ( | ||
<AutoResizeHeight duration={0.2} className="mt-4"> | ||
{Inner} | ||
</AutoResizeHeight> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type ArticleDataType = | ||
| { | ||
type: 'post' | ||
category: string | ||
slug: string | ||
} | ||
| { | ||
type: 'note' | ||
nid: number | ||
} | ||
| { | ||
type: 'page' | ||
slug: string | ||
} |
877f04f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
shiro – ./
shiro-innei.vercel.app
springtide.vercel.app
innei.in
shiro-git-main-innei.vercel.app