nexo/apps/mattermost/app/hooks/channel_switch.ts

35 lines
1,006 B
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {useState} from 'react';
import {DeviceEventEmitter} from 'react-native';
import {Events} from '@constants';
import useDidMount from './did_mount';
export const useChannelSwitch = () => {
const [loading, setLoading] = useState(false);
useDidMount(() => {
let time: NodeJS.Timeout | undefined;
const l = DeviceEventEmitter.addListener(Events.CHANNEL_SWITCH, (switching: boolean) => {
if (time) {
clearTimeout(time);
}
if (switching) {
setLoading(true);
} else {
// eslint-disable-next-line max-nested-callbacks
time = setTimeout(() => setLoading(false), 0);
}
});
return () => {
l.remove();
if (time) {
clearTimeout(time);
}
};
});
return loading;
};