The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/client/UIKit/hooks/useUIKitStateManager.tsx

35 lines
1.0 KiB

import type { UIKitUserInteractionResult, UiKitPayload } from '@rocket.chat/core-typings';
import { isErrorType } from '@rocket.chat/core-typings';
import { useSafely } from '@rocket.chat/fuselage-hooks';
import { useEffect, useState } from 'react';
import * as ActionManager from '../../../app/ui-message/client/ActionManager';
const useUIKitStateManager = <S extends UiKitPayload>(initialState: S): S => {
const [state, setState] = useSafely(useState(initialState));
const { viewId } = state;
useEffect(() => {
const handleUpdate = ({ ...data }: UIKitUserInteractionResult): void => {
if (isErrorType(data)) {
const { errors } = data;
setState((state) => ({ ...state, errors }));
return;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { type, ...rest } = data;
setState(rest as any);
};
ActionManager.on(viewId, handleUpdate);
return (): void => {
ActionManager.off(viewId, handleUpdate);
};
}, [setState, viewId]);
return state;
};
export { useUIKitStateManager };