-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathChatSubComponents.tsx
More file actions
198 lines (179 loc) · 6.13 KB
/
ChatSubComponents.tsx
File metadata and controls
198 lines (179 loc) · 6.13 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* Helper sub-components extracted from ChatPanel
*
* StageIndicator, ActionsTaken, CharacterAvatar, renderMessageContent
*/
import React, { useState, useEffect, useCallback, memo, useRef } from 'react';
import { ChevronDown, ChevronRight } from 'lucide-react';
import type { CharacterConfig } from '@/lib/characterManager';
import { resolveEmotionMedia } from '@/lib/characterManager';
import type { ModManager } from '@/lib/modManager';
import styles from './index.module.scss';
// ---------------------------------------------------------------------------
// Render message content — formats (action text) as styled spans
// ---------------------------------------------------------------------------
export function renderMessageContent(content: string): React.ReactNode {
const parts = content.split(/(\([^)]+\))/g);
return parts.map((part, i) => {
if (/^\([^)]+\)$/.test(part)) {
return (
<span key={i} className={styles.emotion}>
{part}
</span>
);
}
return part;
});
}
// ---------------------------------------------------------------------------
// Stage Indicator
// ---------------------------------------------------------------------------
export const StageIndicator: React.FC<{ modManager: ModManager | null }> = ({ modManager }) => {
if (!modManager) return null;
const total = modManager.stageCount;
const current = modManager.currentStageIndex;
const finished = modManager.isFinished;
return (
<div className={styles.stageIndicator}>
<span className={styles.stageText}>
Stage {finished ? total : current + 1}/{total}
</span>
<div className={styles.stageDots}>
{Array.from({ length: total }, (_, i) => (
<div
key={i}
className={`${styles.stageDot} ${
i < current || finished
? styles.stageDotCompleted
: i === current
? styles.stageDotCurrent
: ''
}`}
/>
))}
</div>
</div>
);
};
// ---------------------------------------------------------------------------
// Actions Taken (collapsible)
// ---------------------------------------------------------------------------
export const ActionsTaken: React.FC<{ calls: string[] }> = ({ calls }) => {
const [open, setOpen] = useState(false);
if (calls.length === 0) return null;
return (
<div className={styles.actionsTaken}>
<button className={styles.actionsTakenToggle} onClick={() => setOpen(!open)}>
Actions taken
{open ? <ChevronDown size={12} /> : <ChevronRight size={12} />}
</button>
{open && (
<div className={styles.actionsTakenList}>
{calls.map((c, i) => (
<div key={i}>{c}</div>
))}
</div>
)}
</div>
);
};
// ---------------------------------------------------------------------------
// CharacterAvatar – crossfade between emotion media without flashing
// ---------------------------------------------------------------------------
interface AvatarLayer {
url: string;
type: 'video' | 'image';
active: boolean;
}
export const CharacterAvatar: React.FC<{
character: CharacterConfig;
emotion?: string;
onEmotionEnd: () => void;
}> = memo(({ character, emotion, onEmotionEnd }) => {
const isIdle = !emotion;
const media = resolveEmotionMedia(character, emotion || 'default');
const [layers, setLayers] = useState<AvatarLayer[]>(() =>
media ? [{ url: media.url, type: media.type, active: true }] : [],
);
const activeUrl = layers.find((l) => l.active)?.url;
const cleanupRef = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(() => {
return () => {
if (cleanupRef.current) clearTimeout(cleanupRef.current);
};
}, []);
useEffect(() => {
if (!media) {
setLayers([]);
return;
}
if (media.url === activeUrl) return;
setLayers((prev) => {
// If the URL already exists (possibly inactive), reactivate it
const existing = prev.find((l) => l.url === media.url);
if (existing) {
// Cancel any pending cleanup that might remove this layer
if (cleanupRef.current) {
clearTimeout(cleanupRef.current);
cleanupRef.current = null;
}
return prev.map((l) => ({
...l,
active: l.url === media.url,
}));
}
return [...prev, { url: media.url, type: media.type, active: false }];
});
}, [media?.url, activeUrl]);
const handleMediaReady = useCallback((readyUrl: string) => {
setLayers((prev) => {
const staleUrls = prev.filter((l) => l.url !== readyUrl).map((l) => l.url);
if (cleanupRef.current) clearTimeout(cleanupRef.current);
cleanupRef.current = setTimeout(() => {
setLayers((curr) => curr.filter((l) => !staleUrls.includes(l.url)));
}, 300);
return prev.map((l) => ({ ...l, active: l.url === readyUrl }));
});
}, []);
if (layers.length === 0) {
return <div className={styles.avatarPlaceholder}>{character.character_name.charAt(0)}</div>;
}
return (
<>
{layers.map((layer) => {
const layerStyle: React.CSSProperties = {
position: 'absolute',
inset: 0,
opacity: layer.active ? 1 : 0,
transition: 'opacity 0.25s ease-out',
};
if (layer.type === 'video') {
return (
<video
key={layer.url}
className={styles.avatarImage}
style={layerStyle}
src={layer.url}
autoPlay
loop={layer.active ? isIdle : false}
muted
playsInline
onCanPlay={!layer.active ? () => handleMediaReady(layer.url) : undefined}
onEnded={layer.active && !isIdle ? onEmotionEnd : undefined}
/>
);
}
return (
<img
key={layer.url}
className={styles.avatarImage}
style={layerStyle}
src={layer.url}
alt={character.character_name}
onLoad={!layer.active ? () => handleMediaReady(layer.url) : undefined}
/>
);
})}
</>
);
});