mirror of https://github.com/msgbyte/tailchat
feat: 增加插件文档的按钮
parent
9328c9b8de
commit
e29c22f4dc
@ -0,0 +1,30 @@
|
||||
import { LoadingSpinner } from '@/components/LoadingSpinner';
|
||||
import { Markdown } from '@/components/Markdown';
|
||||
import { Problem } from '@/components/Problem';
|
||||
import React from 'react';
|
||||
import { useAsync } from 'tailchat-shared';
|
||||
|
||||
export const DocumentMarkdownRender: React.FC<{ url: string }> = React.memo(
|
||||
({ url }) => {
|
||||
const { loading, value, error } = useAsync(async () => {
|
||||
const data = await fetch(url);
|
||||
if (data.status >= 400) {
|
||||
throw new Error('请求异常');
|
||||
}
|
||||
const raw = data.text();
|
||||
|
||||
return raw;
|
||||
}, [url]);
|
||||
|
||||
if (loading) {
|
||||
return <LoadingSpinner />;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return <Problem text={String(error)} />;
|
||||
}
|
||||
|
||||
return <Markdown raw={String(value)} />;
|
||||
}
|
||||
);
|
||||
DocumentMarkdownRender.displayName = 'DocumentMarkdownRender';
|
@ -0,0 +1,24 @@
|
||||
import { Problem } from '@/components/Problem';
|
||||
import React from 'react';
|
||||
import { isValidStr, t } from 'tailchat-shared';
|
||||
import { DocumentMarkdownRender } from './DocumentMarkdownRender';
|
||||
|
||||
interface DocumentViewProps {
|
||||
documentUrl?: string;
|
||||
}
|
||||
export const DocumentView: React.FC<DocumentViewProps> = React.memo((props) => {
|
||||
const { documentUrl } = props;
|
||||
|
||||
if (!isValidStr(documentUrl)) {
|
||||
return <Problem text={t('该插件没有更多描述')} />;
|
||||
}
|
||||
|
||||
if (documentUrl.endsWith('.md')) {
|
||||
return <DocumentMarkdownRender url={documentUrl} />;
|
||||
} else if (documentUrl.endsWith('.html')) {
|
||||
return <iframe src={documentUrl} />;
|
||||
} else {
|
||||
return <Problem text={t('不支持渲染的文档链接')} />;
|
||||
}
|
||||
});
|
||||
DocumentView.displayName = 'DocumentView';
|
Loading…
Reference in New Issue