@sendbird/uikit-react
Version:
Sendbird UIKit for React: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
48 lines (45 loc) • 2.34 kB
JavaScript
import { useRef, useCallback, useEffect } from 'react';
/**
* Tracks typing lifecycle on a Sendbird channel and ensures `endTyping` is
* sent when the channel changes, the `active` flag toggles to false, or the
* component unmounts. Returns memoized `startTyping`/`stopTyping` helpers
* that the caller wires to input events.
*
* @param channel - the channel currently being typed in
* @param active - when set to false, the cleanup runs and clears typing
* state. Useful for edit-mode wrappers that pass `showEdit`
* so closing the edit panel emits `endTyping`.
*/
function useTypingLifecycle(channel, active) {
if (active === void 0) { active = true; }
// Holds the channel that startTyping was last invoked on. Storing the
// actual reference (rather than a boolean flag) ensures endTyping is sent
// on the same channel instance that received startTyping, even if the
// channel object is re-instantiated under the same URL.
var typingChannelRef = useRef(null);
var startTyping = useCallback(function () {
var _a;
(_a = channel === null || channel === void 0 ? void 0 : channel.startTyping) === null || _a === void 0 ? void 0 : _a.call(channel);
typingChannelRef.current = channel !== null && channel !== void 0 ? channel : null;
}, [channel]);
var stopTyping = useCallback(function () {
var _a, _b;
(_b = (_a = typingChannelRef.current) === null || _a === void 0 ? void 0 : _a.endTyping) === null || _b === void 0 ? void 0 : _b.call(_a);
typingChannelRef.current = null;
}, []);
// Run cleanup on channel URL change, active toggle, or unmount.
// Reads typingChannelRef so endTyping is sent on the channel where
// startTyping was actually invoked.
useEffect(function () {
return function () {
var _a, _b;
if (typingChannelRef.current) {
(_b = (_a = typingChannelRef.current).endTyping) === null || _b === void 0 ? void 0 : _b.call(_a);
typingChannelRef.current = null;
}
};
}, [channel === null || channel === void 0 ? void 0 : channel.url, active]);
return { startTyping: startTyping, stopTyping: stopTyping };
}
export { useTypingLifecycle as u };
//# sourceMappingURL=bundle-AsQ1wnFm.js.map