-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
Copy pathindex.tsx
89 lines (76 loc) · 2.43 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Highlighter } from '@lobehub/ui';
import { memo, useEffect, useMemo } from 'react';
import { Flexbox } from 'react-layout-kit';
import { useChatStore } from '@/store/chat';
import { chatPortalSelectors, chatSelectors } from '@/store/chat/selectors';
import { ArtifactType } from '@/types/artifact';
import Renderer from './Renderer';
const ArtifactsUI = memo(() => {
const [
messageId,
displayMode,
isMessageGenerating,
artifactType,
artifactContent,
artifactCodeLanguage,
isArtifactTagClosed,
] = useChatStore((s) => {
const messageId = chatPortalSelectors.artifactMessageId(s) || '';
return [
messageId,
s.portalArtifactDisplayMode,
chatSelectors.isMessageGenerating(messageId)(s),
chatPortalSelectors.artifactType(s),
chatPortalSelectors.artifactCode(messageId)(s),
chatPortalSelectors.artifactCodeLanguage(s),
chatPortalSelectors.isArtifactTagClosed(messageId)(s),
];
});
useEffect(() => {
// when message generating , check whether the artifact is closed
// if close, move the display mode to preview
if (isMessageGenerating && isArtifactTagClosed && displayMode === 'code') {
useChatStore.setState({ portalArtifactDisplayMode: 'preview' });
}
}, [isMessageGenerating, displayMode, isArtifactTagClosed]);
const language = useMemo(() => {
switch (artifactType) {
case ArtifactType.React: {
return 'tsx';
}
case ArtifactType.Code: {
return artifactCodeLanguage;
}
case ArtifactType.Python: {
return 'python';
}
default: {
return 'html';
}
}
}, [artifactType, artifactCodeLanguage]);
// make sure the message and id is valid
if (!messageId) return;
// show code when the artifact is not closed or the display mode is code or the artifact type is code
const showCode =
!isArtifactTagClosed || displayMode === 'code' || artifactType === ArtifactType.Code;
return (
<Flexbox
className={'portal-artifact'}
flex={1}
gap={8}
height={'100%'}
paddingInline={12}
style={{ overflow: 'hidden' }}
>
{showCode ? (
<Highlighter language={language || 'txt'} style={{ maxHeight: '100%', overflow: 'hidden' }}>
{artifactContent}
</Highlighter>
) : (
<Renderer content={artifactContent} type={artifactType} />
)}
</Flexbox>
);
});
export default ArtifactsUI;