Skip to content

Commit eb97781

Browse files
Merge branch 'main' into docs/add-example-links
2 parents 1325775 + 2e08ebd commit eb97781

File tree

9 files changed

+69
-36
lines changed

9 files changed

+69
-36
lines changed

.changeset/breezy-files-divide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/chatkit-react': patch
3+
---
4+
5+
Removed usage of React 19 only ref callback cleanup function

.changeset/config.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66
"linked": [],
77
"access": "public",
88
"baseBranch": "main",
9-
"updateInternalDependencies": "patch",
10-
"ignore": ["react-explorer"]
9+
"updateInternalDependencies": "patch"
1110
}

.changeset/thick-eggs-know.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/chatkit-react': minor
3+
---
4+
5+
Exposed a ref to the underlying `OpenAIChatKit` DOM element in the return value of `useChatKit`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# @openai/chatkit-react
2+
3+
## 1.0.0
4+
5+
### Major Changes
6+
7+
- 5e4488e: Initial release
8+
9+
### Patch Changes
10+
11+
- Updated dependencies [5e4488e]
12+
- @openai/chatkit@1.0.0

packages/chatkit-react/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openai/chatkit-react",
3-
"version": "0.0.0",
3+
"version": "1.0.0",
44
"description": "React bindings for the ChatKit Web Component.",
55
"type": "module",
66
"sideEffects": false,
@@ -43,5 +43,9 @@
4343
"access": "public",
4444
"provenance": false
4545
},
46+
"repository": {
47+
"type": "git",
48+
"url": "https://github.com/openai/chatkit-js"
49+
},
4650
"license": "MIT"
4751
}

packages/chatkit-react/src/ChatKit.tsx

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,46 +45,39 @@ export const ChatKit = React.forwardRef<OpenAIChatKit, ChatKitProps>(
4545
return (
4646
<openai-chatkit
4747
ref={(chatKit) => {
48-
ref.current = chatKit;
4948
control.setInstance(chatKit);
49+
5050
if (typeof forwardedRef === 'function') {
5151
forwardedRef(chatKit);
5252
} else if (forwardedRef) {
5353
forwardedRef.current = chatKit;
5454
}
5555

56-
if (ref.current) {
57-
const abortController = new AbortController();
58-
const events = {
59-
'chatkit.error': 'onError',
60-
'chatkit.response.end': 'onResponseEnd',
61-
'chatkit.response.start': 'onResponseStart',
62-
'chatkit.log': 'onLog',
63-
'chatkit.thread.change': 'onThreadChange',
64-
'chatkit.thread.load.start': 'onThreadLoadStart',
65-
'chatkit.thread.load.end': 'onThreadLoadEnd',
66-
} satisfies {
67-
[K in keyof ChatKitEvents]: ToEventHandlerKey<K>;
68-
};
56+
if (!ref.current) {
57+
return;
58+
}
59+
60+
const events: {
61+
[K in keyof ChatKitEvents]: ToEventHandlerKey<K>;
62+
} = {
63+
'chatkit.error': 'onError',
64+
'chatkit.response.end': 'onResponseEnd',
65+
'chatkit.response.start': 'onResponseStart',
66+
'chatkit.log': 'onLog',
67+
'chatkit.thread.change': 'onThreadChange',
68+
'chatkit.thread.load.start': 'onThreadLoadStart',
69+
'chatkit.thread.load.end': 'onThreadLoadEnd',
70+
};
6971

70-
for (const eventName of Object.keys(
71-
events,
72-
) as (keyof ChatKitEvents)[]) {
73-
ref.current.addEventListener(
74-
eventName,
75-
(e) => {
76-
const handler = control.handlers[events[eventName]];
77-
if (typeof handler === 'function') {
78-
handler(e.detail as any);
79-
}
80-
},
81-
{ signal: abortController.signal },
82-
);
83-
}
72+
const eventNames = Object.keys(events) as (keyof ChatKitEvents)[];
8473

85-
return () => {
86-
abortController.abort();
87-
};
74+
for (const event of eventNames) {
75+
ref.current.addEventListener(event, (e) => {
76+
const handler = control.handlers[events[event]];
77+
if (typeof handler === 'function') {
78+
handler(e.detail as any);
79+
}
80+
});
8881
}
8982
}}
9083
{...htmlProps}

packages/chatkit-react/src/useChatKit.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type ChatKitControl = {
5050

5151
export type UseChatKitReturn = ChatKitMethods & {
5252
control: ChatKitControl;
53+
ref: React.RefObject<OpenAIChatKit | null>;
5354
};
5455

5556
export function useChatKit(options: UseChatKitOptions): UseChatKitReturn {
@@ -98,5 +99,8 @@ export function useChatKit(options: UseChatKitOptions): UseChatKitReturn {
9899
};
99100
}, [stableOptions, setInstance]);
100101

101-
return React.useMemo(() => ({ ...methods, control }), [methods, control]);
102+
return React.useMemo(
103+
() => ({ ...methods, control, ref }),
104+
[methods, control],
105+
);
102106
}

packages/chatkit/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# @openai/chatkit
2+
3+
## 1.0.0
4+
5+
### Major Changes
6+
7+
- 5e4488e: Initial release

packages/chatkit/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openai/chatkit",
3-
"version": "0.0.0",
3+
"version": "1.0.0",
44
"type": "module",
55
"description": "Type definitions for the ChatKit Web Component.",
66
"sideEffects": false,
@@ -22,5 +22,9 @@
2222
"access": "public",
2323
"provenance": false
2424
},
25+
"repository": {
26+
"type": "git",
27+
"url": "https://github.com/openai/chatkit-js"
28+
},
2529
"license": "MIT"
2630
}

0 commit comments

Comments
 (0)