Skip to content

Commit

Permalink
Split hooks from module
Browse files Browse the repository at this point in the history
  • Loading branch information
tassoevan committed Dec 10, 2024
1 parent 5615553 commit 9f350d6
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { GenericMenuItemProps } from '@rocket.chat/ui-client';
import { useTranslation, usePermission, useRouter } from '@rocket.chat/ui-contexts';
import React from 'react';

import { useUserDropdownAppsActionButtons } from '../../../hooks/useAppActionButtons';
import { useUserDropdownAppsActionButtons } from '../../../hooks/useUserDropdownAppsActionButtons';
import { useAppRequestStats } from '../../../views/marketplace/hooks/useAppRequestStats';

export const useMarketPlaceMenu = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ import { useViewOriginalTranslationAction } from './useViewOriginalTranslationAc
import { useWebDAVMessageAction } from './useWebDAVMessageAction';
import type { MessageActionContext } from '../../../../app/ui-utils/client/lib/MessageAction';
import { MessageAction } from '../../../../app/ui-utils/client/lib/MessageAction';
import { useMessageActionAppsActionButtons } from '../../../hooks/useAppActionButtons';
import { useEmbeddedLayout } from '../../../hooks/useEmbeddedLayout';
import { useMessageActionAppsActionButtons } from '../../../hooks/useMessageActionAppsActionButtons';
import { roomsQueryKeys } from '../../../lib/queryKeys';

const getMessageContext = (message: IMessage, room: IRoom, context?: MessageActionContext): MessageActionContext => {
Expand Down
186 changes: 3 additions & 183 deletions apps/meteor/client/hooks/useAppActionButtons.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import { type IUIActionButton, type UIActionButtonContext } from '@rocket.chat/apps-engine/definition/ui';
import type { IMessage } from '@rocket.chat/core-typings';
import { useDebouncedCallback } from '@rocket.chat/fuselage-hooks';
import type { GenericMenuItemProps } from '@rocket.chat/ui-client';
import { useEndpoint, useStream, useToastMessageDispatch, useUserId } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useEndpoint, useStream, useUserId } from '@rocket.chat/ui-contexts';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useEffect } from 'react';

import { useApplyButtonFilters, useApplyButtonAuthFilter } from './useApplyButtonFilters';
import { useFilterActionsByContext } from './useFilterActions';
import { UiKitTriggerTimeoutError } from '../../app/ui-message/client/UiKitTriggerTimeoutError';
import type { MessageActionConfig, MessageActionContext } from '../../app/ui-utils/client/lib/MessageAction';
import type { MessageBoxAction } from '../../app/ui-utils/client/lib/messageBox';
import { Utilities } from '../../ee/lib/misc/Utilities';
import { useUiKitActionManager } from '../uikit/hooks/useUiKitActionManager';

const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${appId}/${actionId}`;
export const getIdForActionButton = ({ appId, actionId }: IUIActionButton): string => `${appId}/${actionId}`;

export const useAppActionButtons = <TContext extends `${UIActionButtonContext}`>(context?: TContext) => {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -62,171 +50,3 @@ export const useAppActionButtons = <TContext extends `${UIActionButtonContext}`>

return result;
};

export const useMessageboxAppsActionButtons = () => {
const result = useAppActionButtons('messageBoxAction');
const actionManager = useUiKitActionManager();
const dispatchToastMessage = useToastMessageDispatch();
const { t } = useTranslation();

const applyButtonFilters = useApplyButtonFilters();

const data = useMemo(
() =>
result.data
?.filter((action) => {
return applyButtonFilters(action);
})
.map((action) => {
const item: Omit<MessageBoxAction, 'icon'> = {
id: getIdForActionButton(action),
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId),
action: (params) => {
void actionManager
.emitInteraction(action.appId, {
type: 'actionButton',
rid: params.rid,
tmid: params.tmid,
actionId: action.actionId,
payload: { context: action.context, message: params.chat.composer?.text ?? '' },
})
.catch(async (reason) => {
if (reason instanceof UiKitTriggerTimeoutError) {
dispatchToastMessage({
type: 'error',
message: t('UIKit_Interaction_Timeout'),
});
return;
}

return reason;
});
},
};

return item;
}),
[actionManager, applyButtonFilters, dispatchToastMessage, result.data, t],
);
return {
...result,
data,
} as UseQueryResult<MessageBoxAction[]>;
};

export const useUserDropdownAppsActionButtons = () => {
const result = useAppActionButtons('userDropdownAction');
const actionManager = useUiKitActionManager();
const dispatchToastMessage = useToastMessageDispatch();
const { t } = useTranslation();

const applyButtonFilters = useApplyButtonAuthFilter();

const data = useMemo(
() =>
result.data
?.filter((action) => {
return applyButtonFilters(action);
})
.map((action) => {
return {
id: `${action.appId}_${action.actionId}`,
// icon: action.icon as GenericMenuItemProps['icon'],
content: action.labelI18n,
onClick: () => {
void actionManager
.emitInteraction(action.appId, {
type: 'actionButton',
actionId: action.actionId,
payload: { context: action.context },
})
.catch(async (reason) => {
if (reason instanceof UiKitTriggerTimeoutError) {
dispatchToastMessage({
type: 'error',
message: t('UIKit_Interaction_Timeout'),
});
return;
}

return reason;
});
},
};
}),
[actionManager, applyButtonFilters, dispatchToastMessage, result.data, t],
);
return {
...result,
data,
} as UseQueryResult<GenericMenuItemProps[]>;
};

export const useMessageActionAppsActionButtons = (message: IMessage, context?: MessageActionContext, category?: string) => {
const result = useAppActionButtons('messageAction');
const actionManager = useUiKitActionManager();
const applyButtonFilters = useApplyButtonFilters(category);
const dispatchToastMessage = useToastMessageDispatch();
const { t } = useTranslation();
const filterActionsByContext = useFilterActionsByContext(context);
const data = useMemo(
() =>
result.data
?.filter((action) => {
if (!filterActionsByContext(action)) {
return false;
}
return applyButtonFilters(action);
})
.map((action) => {
const item: MessageActionConfig = {
icon: undefined as any,
id: getIdForActionButton(action),
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId),
order: 7,
type: 'apps',
variant: action.variant,
group: 'menu',
action: () => {
void actionManager
.emitInteraction(action.appId, {
type: 'actionButton',
rid: message.rid,
tmid: message.tmid,
mid: message._id,
actionId: action.actionId,
payload: { context: action.context },
})
.catch(async (reason) => {
if (reason instanceof UiKitTriggerTimeoutError) {
dispatchToastMessage({
type: 'error',
message: t('UIKit_Interaction_Timeout'),
});
return;
}

return reason;
});
},
};

return item;
}),
[
actionManager,
applyButtonFilters,
dispatchToastMessage,
filterActionsByContext,
message._id,
message.rid,
message.tmid,
result.data,
t,
],
);
return {
...result,
data,
} as UseQueryResult<MessageActionConfig[]>;
};
19 changes: 0 additions & 19 deletions apps/meteor/client/hooks/useFilterActions.ts

This file was deleted.

78 changes: 78 additions & 0 deletions apps/meteor/client/hooks/useMessageActionAppsActionButtons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { type IUIActionButton, MessageActionContext as AppsEngineMessageActionContext } from '@rocket.chat/apps-engine/definition/ui';
import type { IMessage } from '@rocket.chat/core-typings';
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useAppActionButtons, getIdForActionButton } from './useAppActionButtons';
import { useApplyButtonFilters } from './useApplyButtonFilters';
import { UiKitTriggerTimeoutError } from '../../app/ui-message/client/UiKitTriggerTimeoutError';
import type { MessageActionContext, MessageActionConfig } from '../../app/ui-utils/client/lib/MessageAction';
import { Utilities } from '../../ee/lib/misc/Utilities';
import { useUiKitActionManager } from '../uikit/hooks/useUiKitActionManager';

const filterActionsByContext = (context: string | undefined, action: IUIActionButton) => {
if (!context) {
return true;
}

const messageActionContext = action.when?.messageActionContext || Object.values(AppsEngineMessageActionContext);
const isContextMatch = messageActionContext.includes(context as AppsEngineMessageActionContext);

return isContextMatch;
};

export const useMessageActionAppsActionButtons = (message: IMessage, context?: MessageActionContext, category?: string) => {
const result = useAppActionButtons('messageAction');
const actionManager = useUiKitActionManager();
const applyButtonFilters = useApplyButtonFilters(category);
const dispatchToastMessage = useToastMessageDispatch();
const { t } = useTranslation();
const data = useMemo(
() =>
result.data
?.filter((action) => filterActionsByContext(context, action))
.filter((action) => applyButtonFilters(action))
.map((action) => {
const item: MessageActionConfig = {
icon: undefined as any,
id: getIdForActionButton(action),
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId),
order: 7,
type: 'apps',
variant: action.variant,
group: 'menu',
action: () => {
void actionManager
.emitInteraction(action.appId, {
type: 'actionButton',
rid: message.rid,
tmid: message.tmid,
mid: message._id,
actionId: action.actionId,
payload: { context: action.context },
})
.catch(async (reason) => {
if (reason instanceof UiKitTriggerTimeoutError) {
dispatchToastMessage({
type: 'error',
message: t('UIKit_Interaction_Timeout'),
});
return;
}

return reason;
});
},
};

return item;
}),
[actionManager, applyButtonFilters, context, dispatchToastMessage, message._id, message.rid, message.tmid, result.data, t],
);
return {
...result,
data,
} as UseQueryResult<MessageActionConfig[]>;
};
62 changes: 62 additions & 0 deletions apps/meteor/client/hooks/useMessageboxAppsActionButtons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import { useAppActionButtons, getIdForActionButton } from './useAppActionButtons';
import { useApplyButtonFilters } from './useApplyButtonFilters';
import { UiKitTriggerTimeoutError } from '../../app/ui-message/client/UiKitTriggerTimeoutError';
import type { MessageBoxAction } from '../../app/ui-utils/client/lib/messageBox';
import { Utilities } from '../../ee/lib/misc/Utilities';
import { useUiKitActionManager } from '../uikit/hooks/useUiKitActionManager';

export const useMessageboxAppsActionButtons = (): UseQueryResult<MessageBoxAction[]> => {
const result = useAppActionButtons('messageBoxAction');
const actionManager = useUiKitActionManager();
const dispatchToastMessage = useToastMessageDispatch();
const { t } = useTranslation();

const applyButtonFilters = useApplyButtonFilters();

const data = useMemo(
() =>
result.data
?.filter((action) => {
return applyButtonFilters(action);
})
.map((action) => {
const item: Omit<MessageBoxAction, 'icon'> = {
id: getIdForActionButton(action),
label: Utilities.getI18nKeyForApp(action.labelI18n, action.appId),
action: (params) => {
void actionManager
.emitInteraction(action.appId, {
type: 'actionButton',
rid: params.rid,
tmid: params.tmid,
actionId: action.actionId,
payload: { context: action.context, message: params.chat.composer?.text ?? '' },
})
.catch(async (reason) => {
if (reason instanceof UiKitTriggerTimeoutError) {
dispatchToastMessage({
type: 'error',
message: t('UIKit_Interaction_Timeout'),
});
return;
}

return reason;
});
},
};

return item;
}),
[actionManager, applyButtonFilters, dispatchToastMessage, result.data, t],
);
return {
...result,
data,
} as UseQueryResult<MessageBoxAction[]>;
};
Loading

0 comments on commit 9f350d6

Please sign in to comment.