import React, { PropsWithChildren, useCallback, useEffect, useState, } from 'react'; import { Icon, IconTypes } from '@tencentcloud/chat-uikit-react'; import { useTUILiveContext } from '..'; interface ativeClickReturn { value?: boolean, suffixValue?: boolean, } interface useLiveAtiveBasicParams { className?: string, icon?: IconTypes, activeIcon?: IconTypes, iconWidth?: number, iconHeight?: number, name?: string, key?: string, value?: boolean, onClick?: () => Promise; } export interface useLiveAtiveElementsParams extends useLiveAtiveBasicParams { suffix?: useLiveAtiveBasicParams; } export function useLiveActiveElements( props:PropsWithChildren, ) { const { name, key = '', icon, activeIcon, value: propsValue, className, suffix, iconWidth, iconHeight, } = props; const { increaseGroupCounter, decreaseGroupCounter, callback, } = useTUILiveContext('TUILiveHeader'); const [value, setValue] = useState(false); const [suffixValue, setSuffixValue] = useState(false); const hadnleGroupCounter = useCallback(async (data:boolean) => { if (data && increaseGroupCounter) { await increaseGroupCounter({ key, value: 1, }); } if (!data && decreaseGroupCounter) { await decreaseGroupCounter({ key, value: 1, }); } callback && callback(key); }, [increaseGroupCounter, decreaseGroupCounter, callback, key]); const handleClick = async () => { await hadnleGroupCounter(!value); setValue(!value); setSuffixValue(false); }; const handleSuffixClick = async () => { if (value) { await hadnleGroupCounter(suffixValue); } setSuffixValue(!suffixValue); setValue(false); }; useEffect(() => { if (propsValue) { setValue(propsValue); } }, [propsValue]); useEffect(() => { if (suffix?.value) { setSuffixValue(suffix?.value); } }, [suffix?.value]); return (
  • {name}
    { suffix && (
    ) }
  • ); }