feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { type PropsWithChildren, createContext } from 'react';
|
||||
|
||||
import mitt from 'mitt';
|
||||
import { useCreation } from 'ahooks';
|
||||
|
||||
import {
|
||||
type UIKitEventMap,
|
||||
type UIKitEventCenter,
|
||||
type UIKitEventProviderProps,
|
||||
} from './type';
|
||||
import { useObserveChatContainer } from './hooks';
|
||||
|
||||
export const UIKitEventContext = createContext<UIKitEventCenter | null>(null);
|
||||
|
||||
export const UIKitEventProvider: React.FC<
|
||||
PropsWithChildren<UIKitEventProviderProps>
|
||||
> = ({ chatContainerRef, children }) => {
|
||||
const eventCenter = useCreation(() => mitt<UIKitEventMap>(), []);
|
||||
|
||||
useObserveChatContainer({ eventCenter, chatContainerRef });
|
||||
|
||||
return (
|
||||
<UIKitEventContext.Provider value={eventCenter}>
|
||||
{children}
|
||||
</UIKitEventContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useEffect, type RefObject } from 'react';
|
||||
|
||||
import { type Emitter } from 'mitt';
|
||||
|
||||
import { UIKitEvents, type UIKitEventMap } from './type';
|
||||
|
||||
export const useObserveChatContainer = ({
|
||||
eventCenter,
|
||||
chatContainerRef,
|
||||
}: {
|
||||
eventCenter: Emitter<UIKitEventMap>;
|
||||
chatContainerRef: RefObject<HTMLDivElement>;
|
||||
}) => {
|
||||
useEffect(() => {
|
||||
if (!chatContainerRef.current) {
|
||||
return;
|
||||
}
|
||||
const resizeObserver = new ResizeObserver(() => {
|
||||
eventCenter.emit(UIKitEvents.WINDOW_RESIZE);
|
||||
});
|
||||
|
||||
resizeObserver.observe(chatContainerRef.current);
|
||||
|
||||
return () => {
|
||||
resizeObserver.disconnect();
|
||||
};
|
||||
}, []);
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useContext } from 'react';
|
||||
|
||||
import { UIKitEventContext } from './context';
|
||||
|
||||
export const useUiKitEventCenter = () => useContext(UIKitEventContext);
|
||||
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { type RefObject } from 'react';
|
||||
|
||||
import { type Emitter } from 'mitt';
|
||||
|
||||
export enum UIKitEvents {
|
||||
WINDOW_RESIZE,
|
||||
AFTER_CARD_RENDER,
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -- mitt 的类型不认 interface
|
||||
export type UIKitEventMap = {
|
||||
[UIKitEvents.WINDOW_RESIZE]: undefined;
|
||||
[UIKitEvents.AFTER_CARD_RENDER]: { messageId: string };
|
||||
};
|
||||
|
||||
export type UIKitEventCenter = Emitter<UIKitEventMap>;
|
||||
|
||||
export interface UIKitEventProviderProps {
|
||||
chatContainerRef: RefObject<HTMLDivElement>;
|
||||
}
|
||||
Reference in New Issue
Block a user