mattermost-mobile-upstream/app/components/user_list_row/index.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

200 lines
5.8 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useMemo, useRef, useState} from 'react';
import {defineMessages, useIntl} from 'react-intl';
import {
Platform,
View,
} from 'react-native';
import {storeProfileLongPressTutorial} from '@actions/app/global';
import CompassIcon from '@components/compass_icon';
import FormattedText from '@components/formatted_text';
import TutorialHighlight from '@components/tutorial_highlight';
import TutorialLongPress from '@components/tutorial_highlight/long_press';
import UserItem from '@components/user_item';
import {useTheme} from '@context/theme';
import {useIsTablet} from '@hooks/device';
import useDidMount from '@hooks/did_mount';
import {makeStyleSheetFromTheme, changeOpacity} from '@utils/theme';
import {typography} from '@utils/typography';
import type UserModel from '@typings/database/models/servers/user';
type Props = {
highlight?: boolean;
id: string;
isChannelAdmin: boolean;
manageMode: boolean;
onLongPress: (user: UserProfile | UserModel) => void;
onPress?: (user: UserProfile | UserModel) => void;
selectable: boolean;
disabled?: boolean;
selected: boolean;
showManageMode: boolean;
testID: string;
tutorialWatched?: boolean;
user: UserProfile;
}
const getStyleFromTheme = makeStyleSheetFromTheme((theme) => {
return {
selector: {
alignItems: 'center',
justifyContent: 'center',
marginLeft: 12,
},
selectorManage: {
alignItems: 'center',
flexDirection: 'row',
justifyContent: 'center',
marginLeft: 12,
},
manageText: {
color: changeOpacity(theme.centerChannelColor, 0.64),
...typography('Body', 100, 'Regular'),
},
tutorial: {
top: Platform.select({ios: -74, default: -94}),
},
tutorialTablet: {
top: -84,
},
};
});
const DEFAULT_ICON_OPACITY = 0.32;
const messages = defineMessages({
admin: {
id: 'mobile.manage_members.admin',
defaultMessage: 'Admin',
},
member: {
id: 'mobile.manage_members.member',
defaultMessage: 'Member',
},
});
function UserListRow({
id,
highlight,
isChannelAdmin,
onPress,
onLongPress,
manageMode = false,
selectable,
disabled,
selected,
showManageMode = false,
testID,
tutorialWatched = false,
user,
}: Props) {
const theme = useTheme();
const isTablet = useIsTablet();
const [showTutorial, setShowTutorial] = useState(false);
const viewRef = useRef<View>(null);
const tutorialTimerRef = useRef<NodeJS.Timeout | null>(null);
const style = getStyleFromTheme(theme);
const {formatMessage} = useIntl();
const handleDismissTutorial = useCallback(() => {
setShowTutorial(false);
storeProfileLongPressTutorial();
}, []);
const handlePress = useCallback((u: UserModel | UserProfile) => {
onPress?.(u);
}, [onPress]);
const manageModeIcon = useMemo(() => {
if (!showManageMode) {
return null;
}
const color = changeOpacity(theme.centerChannelColor, 0.64);
const message = isChannelAdmin ? messages.admin : messages.member;
return (
<View style={style.selectorManage}>
<FormattedText
{...message}
style={style.manageText}
/>
<CompassIcon
name={'chevron-down'}
size={18}
color={color}
/>
</View>
);
}, [isChannelAdmin, showManageMode, style.manageText, style.selectorManage, theme.centerChannelColor]);
const onLayout = useCallback(() => {
if (highlight && !tutorialWatched) {
tutorialTimerRef.current = setTimeout(() => {
setShowTutorial(true);
}, 300);
}
}, [highlight, tutorialWatched]);
useDidMount(() => {
return () => {
if (tutorialTimerRef.current) {
clearTimeout(tutorialTimerRef.current);
}
};
});
const icon = useMemo(() => {
if (!selectable && !selected) {
return null;
}
const color = selected ? theme.buttonBg : changeOpacity(theme.centerChannelColor, DEFAULT_ICON_OPACITY);
return (
<View style={style.selector}>
<CompassIcon
name={selected ? 'check-circle' : 'circle-outline'}
size={28}
color={color}
/>
</View>
);
}, [selectable, selected, theme.buttonBg, theme.centerChannelColor, style.selector]);
const userItemTestID = `${testID}.${id}`;
return (
<>
<UserItem
user={user}
onUserLongPress={onLongPress}
onUserPress={handlePress}
showBadges={true}
testID={userItemTestID}
rightDecorator={manageMode ? manageModeIcon : icon}
disabled={!(selectable || selected || !disabled)}
viewRef={viewRef}
onLayout={onLayout}
/>
{showTutorial &&
<TutorialHighlight
itemRef={viewRef}
onDismiss={handleDismissTutorial}
inModal={true}
>
<TutorialLongPress
message={formatMessage({id: 'user.tutorial.long_press', defaultMessage: "Long-press on an item to view a user's profile"})}
style={isTablet ? style.tutorialTablet : style.tutorial}
/>
</TutorialHighlight>
}
</>
);
}
export default React.memo(UserListRow);