Skip to content

Commit 6ae7385

Browse files
committed
fix TS errors + chatbox context
1 parent c26d9b1 commit 6ae7385

5 files changed

Lines changed: 227 additions & 331 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { createContext, useContext } from "react";
2+
import type { ChatRoom, PendingRoomInvite } from "./store";
3+
4+
type ChatEventName =
5+
| "messageSent"
6+
| "startTyping"
7+
| "stopTyping"
8+
| "roomSwitch"
9+
| "roomJoin"
10+
| "roomLeave"
11+
| "roomCreate"
12+
| "inviteSend"
13+
| "inviteAccept"
14+
| "inviteDecline";
15+
16+
interface ExposedState {
17+
value: string;
18+
onChange: (v: string) => void;
19+
}
20+
21+
export interface ChatBoxContextValue {
22+
// Data
23+
messages: any[];
24+
rooms: ChatRoom[];
25+
currentRoomId: string;
26+
currentRoom: ChatRoom | null;
27+
currentUserId: string;
28+
currentUserName: string;
29+
typingUsers: any[];
30+
pendingInvites: PendingRoomInvite[];
31+
32+
// Exposed state
33+
chatTitle: ExposedState;
34+
messageText: ExposedState;
35+
lastSentMessageText: ExposedState;
36+
37+
// UI config
38+
showHeader: boolean;
39+
showRoomsPanel: boolean;
40+
roomsPanelWidth: string;
41+
allowRoomCreation: boolean;
42+
allowRoomSearch: boolean;
43+
style: any;
44+
animationStyle: any;
45+
46+
// Events
47+
onEvent: (event: ChatEventName) => any;
48+
49+
// Room actions
50+
onRoomSwitch: (roomId: string) => void;
51+
onRoomJoin: (roomId: string) => void;
52+
onRoomLeave: (roomId: string) => void;
53+
onRoomCreate: (
54+
name: string,
55+
type: "public" | "private" | "llm",
56+
description?: string,
57+
llmQueryName?: string,
58+
) => void;
59+
onInviteSend: (toUserId: string) => void;
60+
onInviteAccept: (inviteId: string) => void;
61+
onInviteDecline: (inviteId: string) => void;
62+
}
63+
64+
export const ChatBoxContext = createContext<ChatBoxContextValue | null>(null);
65+
66+
export function useChatBox(): ChatBoxContextValue {
67+
const ctx = useContext(ChatBoxContext);
68+
if (!ctx) {
69+
throw new Error("useChatBox must be used within a ChatBoxProvider");
70+
}
71+
return ctx;
72+
}

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/chatBoxComp.tsx

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import { hiddenPropertyView } from "comps/utils/propertyUtils";
2020
import { EditorContext } from "comps/editorState";
2121

2222
import { ChatBoxView } from "./components/ChatBoxView";
23+
import { ChatBoxContext } from "./ChatBoxContext";
24+
import type { ChatRoom, PendingRoomInvite } from "./store";
2325

2426
// ─── Events ──────────────────────────────────────────────────────────────────
2527

@@ -220,60 +222,80 @@ ChatBoxPropertyView.displayName = "ChatBoxV2PropertyView";
220222

221223
let ChatBoxV2Tmp = (function () {
222224
return new UICompBuilder(childrenMap, (props) => {
225+
const messages = Array.isArray(props.messages) ? props.messages : [];
226+
const rooms = (Array.isArray(props.rooms) ? props.rooms : []) as unknown as ChatRoom[];
227+
const typingUsers = Array.isArray(props.typingUsers) ? props.typingUsers : [];
228+
const pendingInvites = (Array.isArray(props.pendingInvites)
229+
? props.pendingInvites
230+
: []) as unknown as PendingRoomInvite[];
231+
const currentRoom = rooms.find((r) => r.id === props.currentRoomId) ?? null;
232+
233+
const contextValue = {
234+
messages,
235+
rooms,
236+
currentRoomId: props.currentRoomId,
237+
currentRoom,
238+
currentUserId: props.currentUserId,
239+
currentUserName: props.currentUserName,
240+
typingUsers,
241+
pendingInvites,
242+
243+
chatTitle: props.chatTitle,
244+
messageText: props.messageText,
245+
lastSentMessageText: props.lastSentMessageText,
246+
247+
showHeader: props.showHeader,
248+
showRoomsPanel: props.showRoomsPanel,
249+
roomsPanelWidth: props.roomsPanelWidth,
250+
allowRoomCreation: props.allowRoomCreation,
251+
allowRoomSearch: props.allowRoomSearch,
252+
style: props.style,
253+
animationStyle: props.animationStyle,
254+
255+
onEvent: props.onEvent,
256+
257+
onRoomSwitch: (roomId: string) => {
258+
props.pendingRoomId.onChange(roomId);
259+
props.onEvent("roomSwitch");
260+
},
261+
onRoomJoin: (roomId: string) => {
262+
props.pendingRoomId.onChange(roomId);
263+
props.onEvent("roomJoin");
264+
},
265+
onRoomLeave: (roomId: string) => {
266+
props.pendingRoomId.onChange(roomId);
267+
props.onEvent("roomLeave");
268+
},
269+
onRoomCreate: (
270+
name: string,
271+
type: "public" | "private" | "llm",
272+
description?: string,
273+
llmQueryName?: string,
274+
) => {
275+
props.newRoomName.onChange(name);
276+
props.newRoomType.onChange(type);
277+
props.newRoomDescription.onChange(description || "");
278+
props.newRoomLlmQuery.onChange(llmQueryName || "");
279+
props.onEvent("roomCreate");
280+
},
281+
onInviteSend: (toUserId: string) => {
282+
props.inviteTargetUserId.onChange(toUserId);
283+
props.onEvent("inviteSend");
284+
},
285+
onInviteAccept: (inviteId: string) => {
286+
props.pendingInviteId.onChange(inviteId);
287+
props.onEvent("inviteAccept");
288+
},
289+
onInviteDecline: (inviteId: string) => {
290+
props.pendingInviteId.onChange(inviteId);
291+
props.onEvent("inviteDecline");
292+
},
293+
};
294+
223295
return (
224-
<ChatBoxView
225-
chatTitle={props.chatTitle}
226-
showHeader={props.showHeader}
227-
messages={props.messages}
228-
currentUserId={props.currentUserId}
229-
currentUserName={props.currentUserName}
230-
typingUsers={props.typingUsers}
231-
lastSentMessageText={props.lastSentMessageText}
232-
messageText={props.messageText}
233-
style={props.style}
234-
animationStyle={props.animationStyle}
235-
onEvent={props.onEvent}
236-
// Rooms panel
237-
rooms={props.rooms}
238-
currentRoomId={props.currentRoomId}
239-
pendingInvites={props.pendingInvites}
240-
showRoomsPanel={props.showRoomsPanel}
241-
roomsPanelWidth={props.roomsPanelWidth}
242-
allowRoomCreation={props.allowRoomCreation}
243-
allowRoomSearch={props.allowRoomSearch}
244-
// Callbacks that set state then fire events
245-
onRoomSwitch={(roomId) => {
246-
props.pendingRoomId.onChange(roomId);
247-
props.onEvent("roomSwitch");
248-
}}
249-
onRoomJoin={(roomId) => {
250-
props.pendingRoomId.onChange(roomId);
251-
props.onEvent("roomJoin");
252-
}}
253-
onRoomLeave={(roomId) => {
254-
props.pendingRoomId.onChange(roomId);
255-
props.onEvent("roomLeave");
256-
}}
257-
onRoomCreate={(name, type, description, llmQueryName) => {
258-
props.newRoomName.onChange(name);
259-
props.newRoomType.onChange(type);
260-
props.newRoomDescription.onChange(description || "");
261-
props.newRoomLlmQuery.onChange(llmQueryName || "");
262-
props.onEvent("roomCreate");
263-
}}
264-
onInviteSend={(toUserId) => {
265-
props.inviteTargetUserId.onChange(toUserId);
266-
props.onEvent("inviteSend");
267-
}}
268-
onInviteAccept={(inviteId) => {
269-
props.pendingInviteId.onChange(inviteId);
270-
props.onEvent("inviteAccept");
271-
}}
272-
onInviteDecline={(inviteId) => {
273-
props.pendingInviteId.onChange(inviteId);
274-
props.onEvent("inviteDecline");
275-
}}
276-
/>
296+
<ChatBoxContext.Provider value={contextValue}>
297+
<ChatBoxView />
298+
</ChatBoxContext.Provider>
277299
);
278300
})
279301
.setPropertyViewFn((children) => (

0 commit comments

Comments
 (0)