* add mutation observer hok * add hook to read speaking member by observing iframe content * display speaking member name in call status bar and improve layout * fix shrining
37 lines
1 KiB
TypeScript
37 lines
1 KiB
TypeScript
import { useEffect, useMemo } from 'react';
|
|
|
|
export type OnMutationCallback = (mutations: MutationRecord[]) => void;
|
|
|
|
export const getMutationRecord = (
|
|
target: Node,
|
|
mutations: MutationRecord[]
|
|
): MutationRecord | undefined => mutations.find((mutation) => mutation.target === target);
|
|
|
|
export const useMutationObserver = (
|
|
onMutationCallback: OnMutationCallback,
|
|
observeElement?: Node | null | (() => Node | null),
|
|
options?: MutationObserverInit
|
|
): MutationObserver => {
|
|
const mutationObserver = useMemo(
|
|
() => new MutationObserver(onMutationCallback),
|
|
[onMutationCallback]
|
|
);
|
|
|
|
useEffect(() => () => mutationObserver?.disconnect(), [mutationObserver]);
|
|
|
|
useEffect(() => {
|
|
const element = typeof observeElement === 'function' ? observeElement() : observeElement;
|
|
|
|
if (element) {
|
|
mutationObserver.observe(element, options);
|
|
}
|
|
|
|
return () => {
|
|
if (element) {
|
|
mutationObserver.disconnect();
|
|
}
|
|
};
|
|
}, [mutationObserver, observeElement, options]);
|
|
|
|
return mutationObserver;
|
|
};
|