mattermost-mobile-upstream/app/components/block_renderer/button_element.tsx
Daniel Espino García 84ea188366
Mattermost Blocks (#9764)
* Mattermost Blocks

* Move block renderer to its own components folder

* Sync translation layer

* UX fixes

* Refactor button to use the button component

* Code review

* Fix CI

* Add animations to collapse blocks

* Add unit tests

* Add e2e tests and related fixes

* Address coderabbit comments

* Address UX comments

* Address feedback

* Address coderabbit comments

* Fix snapshots

* Sync with webapp translation

* Add node limit to button markdown node

* Address feedback

* i18n-extract

* Fix test

* Move image utils to the utils folder

* Remove unneeded style

* Revert build script change

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
2026-06-29 18:42:16 +02:00

78 lines
2.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useMemo, useState} from 'react';
import {type StyleProp, type TextStyle, type ViewStyle} from 'react-native';
import Button from '@components/button';
import {usePreventDoubleTap} from '@hooks/utils';
import {resolveMmButtonColors} from './utils/button';
import type {ActionHandler} from './types';
type ButtonElementProps = {
element: MmButtonBlock;
onAction: ActionHandler;
theme: Theme;
};
export const ButtonElement = ({element, onAction, theme}: ButtonElementProps) => {
const [isExecuting, setIsExecuting] = useState(false);
const isPrimary = element.style === 'primary';
const isDisabled = element.disabled === true || isExecuting;
const useStyledTertiary = !isPrimary && !isDisabled;
const buttonColors = useMemo(
() => resolveMmButtonColors(element.style, theme),
[element.style, theme],
);
const backgroundStyle = useMemo((): StyleProp<ViewStyle> => {
if (!useStyledTertiary) {
return {};
}
return [
{backgroundColor: buttonColors.backgroundColor},
];
}, [buttonColors.backgroundColor, useStyledTertiary]);
const textStyle = useMemo((): StyleProp<TextStyle> => {
if (!useStyledTertiary) {
return undefined;
}
return {color: buttonColors.color};
}, [buttonColors.color, useStyledTertiary]);
const handlePress = usePreventDoubleTap(useCallback(async () => {
try {
setIsExecuting(true);
await onAction(element.action_id, undefined, element.query, element.cookie);
} finally {
setIsExecuting(false);
}
}, [element.action_id, element.cookie, element.query, onAction]));
if (!element.text || !element.action_id) {
return null;
}
return (
<Button
theme={theme}
text={element.text}
testID={`mm_blocks.button.${element.action_id}`}
onPress={handlePress}
disabled={isDisabled}
showLoader={isExecuting}
size='m'
emphasis={isPrimary || isDisabled ? 'primary' : 'tertiary'}
backgroundStyle={backgroundStyle}
textStyle={textStyle}
renderLabelAsMarkdown={true}
/>
);
};