Scheduled post option bot time exclusion (#29330)

* Not displaying users timezone if user is a bot

* Added test

* Added the case of dm with self
Этот коммит содержится в:
Harshil Sharma
2024-11-21 13:30:34 +05:30
коммит произвёл GitHub
родитель 790103fae0
Коммит c98f2e75fb
3 изменённых файлов: 67 добавлений и 21 удалений

Просмотреть файл

@@ -64,6 +64,8 @@ describe('CoreMenuOptions Component', () => {
mockedUseTimePostBoxIndicator.mockReturnValue({ mockedUseTimePostBoxIndicator.mockReturnValue({
...defaultUseTimePostBoxIndicatorReturnValue, ...defaultUseTimePostBoxIndicatorReturnValue,
isDM: false, isDM: false,
isSelfDM: false,
isBot: false,
}); });
}); });
@@ -120,6 +122,8 @@ describe('CoreMenuOptions Component', () => {
mockedUseTimePostBoxIndicator.mockReturnValue({ mockedUseTimePostBoxIndicator.mockReturnValue({
...defaultUseTimePostBoxIndicatorReturnValue, ...defaultUseTimePostBoxIndicatorReturnValue,
isDM: true, isDM: true,
isSelfDM: false,
isBot: false,
}); });
renderComponent(); renderComponent();
@@ -152,4 +156,36 @@ describe('CoreMenuOptions Component', () => {
expect(handleOnSelect).toHaveBeenCalledWith(expect.anything(), expectedTimestamp); expect(handleOnSelect).toHaveBeenCalledWith(expect.anything(), expectedTimestamp);
}); });
it('should NOT include trailing element when isDM and isBot are true', () => {
setMockDate(2); // Tuesday
mockedUseTimePostBoxIndicator.mockReturnValue({
...defaultUseTimePostBoxIndicatorReturnValue,
isDM: true,
isSelfDM: false,
isBot: true,
});
renderComponent();
// Check the trailing element is NOT rendered in the component as this is a bot
expect(screen.queryByText(/John Doe/)).toBeNull();
});
it('should NOT include trailing element when the DM is with oneself', () => {
setMockDate(2); // Tuesday
mockedUseTimePostBoxIndicator.mockReturnValue({
...defaultUseTimePostBoxIndicatorReturnValue,
isDM: true,
isSelfDM: true,
isBot: false,
});
renderComponent();
// Check the trailing element is NOT rendered in the component as this is a bot
expect(screen.queryByText(/John Doe/)).toBeNull();
});
}); });

Просмотреть файл

@@ -46,6 +46,8 @@ function CoreMenuOptions({handleOnSelect, channelId}: Props) {
teammateTimezone, teammateTimezone,
teammateDisplayName, teammateDisplayName,
isDM, isDM,
isSelfDM,
isBot,
} = useTimePostBoxIndicator(channelId); } = useTimePostBoxIndicator(channelId);
const currentUserId = useSelector(getCurrentUserId); const currentUserId = useSelector(getCurrentUserId);
@@ -87,7 +89,7 @@ function CoreMenuOptions({handleOnSelect, channelId}: Props) {
const extraProps: Partial<MenuItemProps> = {}; const extraProps: Partial<MenuItemProps> = {};
if (isDM) { if (isDM && !isBot && !isSelfDM) {
const teammateTimezoneString = teammateTimezone.useAutomaticTimezone ? teammateTimezone.automaticTimezone : teammateTimezone.manualTimezone || 'UTC'; const teammateTimezoneString = teammateTimezone.useAutomaticTimezone ? teammateTimezone.automaticTimezone : teammateTimezone.manualTimezone || 'UTC';
const scheduledTimeInTeammateTimezone = getScheduledTimeInTeammateTimezone(tomorrow9amTime, teammateTimezoneString); const scheduledTimeInTeammateTimezone = getScheduledTimeInTeammateTimezone(tomorrow9amTime, teammateTimezoneString);
const teammateTimeDisplay = ( const teammateTimeDisplay = (

Просмотреть файл

@@ -5,10 +5,17 @@ import {DateTime} from 'luxon';
import {useState, useEffect, useMemo} from 'react'; import {useState, useEffect, useMemo} from 'react';
import {useSelector} from 'react-redux'; import {useSelector} from 'react-redux';
import type {UserProfile} from '@mattermost/types/users';
import {getDirectChannel} from 'mattermost-redux/selectors/entities/channels'; import {getDirectChannel} from 'mattermost-redux/selectors/entities/channels';
import {isScheduledPostsEnabled} from 'mattermost-redux/selectors/entities/scheduled_posts'; import {isScheduledPostsEnabled} from 'mattermost-redux/selectors/entities/scheduled_posts';
import {getTimezoneForUserProfile, getCurrentTimezone} from 'mattermost-redux/selectors/entities/timezone'; import {getTimezoneForUserProfile, getCurrentTimezone} from 'mattermost-redux/selectors/entities/timezone';
import {getStatusForUserId, getUser, makeGetDisplayName} from 'mattermost-redux/selectors/entities/users'; import {
getCurrentUserId,
getStatusForUserId,
getUser,
makeGetDisplayName,
} from 'mattermost-redux/selectors/entities/users';
import Constants, {UserStatuses} from 'utils/constants'; import Constants, {UserStatuses} from 'utils/constants';
@@ -43,27 +50,27 @@ function useTimePostBoxIndicator(channelId: string) {
const [timestamp, setTimestamp] = useState(0); const [timestamp, setTimestamp] = useState(0);
const [showIt, setShowIt] = useState(false); const [showIt, setShowIt] = useState(false);
// get teammate timezone information const teammate: UserProfile | undefined = useSelector((state: GlobalState) => getUser(state, teammateId));
const teammateTimezone = useSelector( const teammateTimezone = useMemo(() => {
(state: GlobalState) => { if (!teammate) {
if (!teammateId) { return DEFAULT_TIMEZONE;
return DEFAULT_TIMEZONE; }
}
const teammate = getUser(state, teammateId); return getTimezoneForUserProfile(teammate);
return getTimezoneForUserProfile(teammate); }, [teammate]);
},
(a, b) =>
a.automaticTimezone === b.automaticTimezone &&
a.manualTimezone === b.manualTimezone &&
a.useAutomaticTimezone === b.useAutomaticTimezone,
);
// current user timezone // current user timezone
const userCurrentTimezone = useSelector((state: GlobalState) => getCurrentTimezone(state)); const userCurrentTimezone = useSelector((state: GlobalState) => getCurrentTimezone(state));
// UseEffect to update the timestamp and the visibility for the time indicator // UseEffect to update the timestamp and the visibility for the time indicator
useEffect(() => { useEffect(() => {
if (isDM && teammate?.is_bot) {
// returning an empty cleanup function as we need to return a genuine cleanup
// function at if teammate is not a bot and useEffect functions need to
// have consistent return types. So, we have to return a () => void function everywhere.
return () => {};
}
function updateTime() { function updateTime() {
const timezone = const timezone =
teammateTimezone.useAutomaticTimezone ? teammateTimezone.automaticTimezone : teammateTimezone.manualTimezone || 'UTC'; teammateTimezone.useAutomaticTimezone ? teammateTimezone.automaticTimezone : teammateTimezone.manualTimezone || 'UTC';
@@ -85,16 +92,15 @@ function useTimePostBoxIndicator(channelId: string) {
const interval = setInterval(updateTime, MINUTE); const interval = setInterval(updateTime, MINUTE);
return () => clearInterval(interval); return () => clearInterval(interval);
}, [ }, [teammate, teammateTimezone.useAutomaticTimezone, teammateTimezone.automaticTimezone, teammateTimezone.manualTimezone, isDM]);
teammateTimezone.useAutomaticTimezone,
teammateTimezone.automaticTimezone,
teammateTimezone.manualTimezone,
]);
const isScheduledPostEnabledValue = useSelector(isScheduledPostsEnabled); const isScheduledPostEnabledValue = useSelector(isScheduledPostsEnabled);
const showRemoteUserHour = isDM && showIt && timestamp !== 0; const showRemoteUserHour = isDM && showIt && timestamp !== 0;
const currentUserId = useSelector(getCurrentUserId);
const isSelfDM = isDM && teammateId === currentUserId;
return { return {
showRemoteUserHour, showRemoteUserHour,
isDM, isDM,
@@ -105,6 +111,8 @@ function useTimePostBoxIndicator(channelId: string) {
showDndWarning, showDndWarning,
teammateId, teammateId,
teammateDisplayName, teammateDisplayName,
isSelfDM,
isBot: isDM && teammate.is_bot,
}; };
} }