🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
'use client';
import {
Center,
createStaticStyles,
cssVar,
Flexbox,
Highlighter,
Icon,
Markdown,
Spin,
Tabs,
} from '@lobehub/ui';
import { CodeIcon, EyeIcon } from 'lucide-react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import AsyncError from '@/components/AsyncError';
import { useTextFileLoader } from '../../hooks/useTextFileLoader';
import NotSupport from '../../NotSupport';
const styles = createStaticStyles(({ css }) => ({
// Same floating-controls treatment as the LocalFile portal's text preview, so
// the render/raw toggle reads identically across both file-preview surfaces.
controls: css`
position: absolute;
z-index: 2;
inset-block-start: 8px;
inset-inline-end: 12px;
padding: 4px;
border: 1px solid ${cssVar.colorBorderSecondary};
border-radius: ${cssVar.borderRadiusLG};
opacity: 0.55;
background: ${cssVar.colorBgElevated};
backdrop-filter: blur(8px);
box-shadow: ${cssVar.boxShadowTertiary};
transition: opacity 150ms ease;
&:hover {
opacity: 1;
}
`,
page: css`
position: relative;
overflow: hidden auto;
width: 100%;
height: 100%;
`,
}));
type PreviewMode = 'render' | 'raw';
interface MarkdownViewerProps {
fileId: string;
url: string | null;
}
/**
* Rendered markdown preview for cloud files (with a raw-source toggle) — the
* FilePreview counterpart of the LocalFile portal's markdown pane. Plain code
* files keep going through `Renderer/Code`.
*/
const MarkdownViewer = memo<MarkdownViewerProps>(({ url }) => {
const { t } = useTranslation('file');
const { error, fileData, loading, tooLarge } = useTextFileLoader(url);
const [mode, setMode] = useState<PreviewMode>('render');
if (!loading && fileData === null)
return (
<Flexbox>
{error && <AsyncError error={error} variant={'block'} />}
<NotSupport tooLarge={tooLarge} url={url} />
</Flexbox>
);
if (loading || fileData === null)
return (
<Center height={'100%'} width={'100%'}>
<Spin size="large" />
</Center>
);
return (
<Flexbox className={styles.page}>
<Flexbox horizontal align={'center'} className={styles.controls} gap={4}>
<Tabs
activeKey={mode}
size={'small'}
items={[
{ icon: <Icon icon={EyeIcon} />, key: 'render', label: t('preview.render') },
{ icon: <Icon icon={CodeIcon} />, key: 'raw', label: t('preview.raw') },
]}
onChange={(key) => setMode(key as PreviewMode)}
/>
</Flexbox>
{mode === 'render' ? (
// `flex: none` keeps the article at its full height; as a shrinkable
// flex item it was squeezed to the page and its `overflow: hidden` cut
// the rest off, so the page had nothing to scroll.
<Markdown style={{ flex: 'none', paddingBlock: 16, paddingInline: 24 }}>
{fileData}
</Markdown>
) : (
// The source scrolls inside the highlighter, so its horizontal scrollbar
// stays in view; the inset matches `Renderer/Code`.
<Highlighter
language={'markdown'}
showLanguage={false}
style={{ paddingBlock: 16, paddingInline: '24px 4px' }}
variant={'borderless'}
>
{fileData}
</Highlighter>
)}
</Flexbox>
);
});
export default MarkdownViewer;