Composer: Show translated ban due date (#1286)

This commit is contained in:
Alexander Zinchuk 2021-07-16 17:44:21 +03:00
parent 08a27a98b8
commit b44792afab
7 changed files with 43 additions and 17 deletions

View File

@ -135,6 +135,7 @@ export interface ApiChatBannedRights {
changeInfo?: true;
inviteUsers?: true;
pinMessages?: true;
untilDate?: number;
}
export interface ApiRestrictionReason {

View File

@ -3,7 +3,7 @@ import React, {
} from '../../lib/teact/teact';
import { withGlobal } from '../../lib/teact/teactn';
import { MAIN_THREAD_ID } from '../../api/types';
import { ApiChatBannedRights, MAIN_THREAD_ID } from '../../api/types';
import { GlobalActions, MessageListType } from '../../global/types';
import { ThemeKey } from '../../types';
@ -69,7 +69,8 @@ type StateProps = {
isPinnedMessageList?: boolean;
isScheduledMessageList?: boolean;
canPost?: boolean;
messageSendingRestrictionReason?: string;
currentUserBannedRights?: ApiChatBannedRights;
defaultBannedRights?: ApiChatBannedRights;
hasPinnedOrAudioMessage?: boolean;
pinnedMessagesCount?: number;
theme: ThemeKey;
@ -104,7 +105,8 @@ const MiddleColumn: FC<StateProps & DispatchProps> = ({
isPinnedMessageList,
isScheduledMessageList,
canPost,
messageSendingRestrictionReason,
currentUserBannedRights,
defaultBannedRights,
hasPinnedOrAudioMessage,
pinnedMessagesCount,
customBackground,
@ -127,6 +129,7 @@ const MiddleColumn: FC<StateProps & DispatchProps> = ({
}) => {
const { width: windowWidth } = useWindowSize();
const lang = useLang();
const [dropAreaState, setDropAreaState] = useState(DropAreaState.None);
const [isFabShown, setIsFabShown] = useState<boolean | undefined>();
const [isNotchShown, setIsNotchShown] = useState<boolean | undefined>();
@ -235,6 +238,11 @@ const MiddleColumn: FC<StateProps & DispatchProps> = ({
!isSelectModeActive && 'shown',
);
const messageSendingRestrictionReason = getMessageSendingRestrictionReason(
lang, currentUserBannedRights, defaultBannedRights,
);
// CSS Variables calculation doesn't work properly with transforms, so we calculate transform values in JS
const {
composerHiddenScale, toolbarHiddenScale,
@ -245,8 +253,6 @@ const MiddleColumn: FC<StateProps & DispatchProps> = ({
[renderingCanPost, windowWidth],
);
const lang = useLang();
const footerClassName = buildClassName(
'middle-column-footer',
!renderingCanPost && 'no-composer',
@ -443,7 +449,8 @@ export default memo(withGlobal(
canPost: !isPinnedMessageList && (!chat || canPost) && (!isBotNotStarted || IS_SINGLE_COLUMN_LAYOUT),
isPinnedMessageList,
isScheduledMessageList,
messageSendingRestrictionReason: chat && getMessageSendingRestrictionReason(chat),
currentUserBannedRights: chat && chat.currentUserBannedRights,
defaultBannedRights: chat && chat.defaultBannedRights,
hasPinnedOrAudioMessage: (
threadId !== MAIN_THREAD_ID
|| Boolean(pinnedIds && pinnedIds.length)

View File

@ -104,7 +104,7 @@ const ManageGroupPermissions: FC<OwnProps & StateProps & DispatchProps> = ({
setPermissions((p) => ({
...p,
[name]: getUpdatedPermissionValue(p[name as keyof ApiChatBannedRights]),
[name]: getUpdatedPermissionValue(p[name as Exclude<keyof ApiChatBannedRights, 'untilDate'>]),
...(name === 'sendStickers' && {
sendGifs: getUpdatedPermissionValue(p[name]),
}),

View File

@ -82,7 +82,7 @@ const ManageGroupUserPermissions: FC<OwnProps & StateProps & DispatchProps> = ({
setPermissions((p) => ({
...p,
[name]: getUpdatedPermissionValue(p[name as keyof ApiChatBannedRights]),
[name]: getUpdatedPermissionValue(p[name as Exclude<keyof ApiChatBannedRights, 'untilDate'>]),
...(name === 'sendStickers' && {
sendGifs: getUpdatedPermissionValue(p[name]),
}),
@ -117,7 +117,7 @@ const ManageGroupUserPermissions: FC<OwnProps & StateProps & DispatchProps> = ({
});
}, [chat, selectedChatMemberId, updateChatMemberBannedRights]);
const getControlIsDisabled = useCallback((key: keyof ApiChatBannedRights) => {
const getControlIsDisabled = useCallback((key: Exclude<keyof ApiChatBannedRights, 'untilDate'>) => {
if (isFormFullyDisabled) {
return true;
}

View File

@ -6,12 +6,16 @@ import {
ApiChatFolder,
MAIN_THREAD_ID,
} from '../../api/types';
import { NotifyException, NotifySettings } from '../../types';
import { LangFn } from '../../hooks/useLang';
import { ARCHIVED_FOLDER_ID } from '../../config';
import { orderBy } from '../../util/iteratees';
import { getUserFirstOrLastName } from './users';
import { LangFn } from '../../hooks/useLang';
import { formatDateToString, formatTime } from '../../util/dateFormat';
const FOREVER_BANNED_DATE = Date.now() / 1000 + 31622400; // 366 days
const VEIFIED_PRIORITY_BASE = 3e9;
const PINNED_PRIORITY_BASE = 3e8;
@ -176,12 +180,24 @@ export function getAllowedAttachmentOptions(chat?: ApiChat, isChatWithBot = fals
};
}
export function getMessageSendingRestrictionReason(chat: ApiChat) {
if (chat.currentUserBannedRights && chat.currentUserBannedRights.sendMessages) {
return 'You are not allowed to send messages in this chat.';
export function getMessageSendingRestrictionReason(
lang: LangFn, currentUserBannedRights?: ApiChatBannedRights, defaultBannedRights?: ApiChatBannedRights,
) {
if (currentUserBannedRights && currentUserBannedRights.sendMessages) {
const { untilDate } = currentUserBannedRights;
return untilDate && untilDate < FOREVER_BANNED_DATE
? lang(
'Channel.Persmission.Denied.SendMessages.Until',
lang(
'formatDateAtTime',
[formatDateToString(new Date(untilDate * 1000), lang.code), formatTime(untilDate * 1000)],
),
)
: lang('Channel.Persmission.Denied.SendMessages.Forever');
}
if (chat.defaultBannedRights && chat.defaultBannedRights.sendMessages) {
return 'Sending messages is not allowed in this chat.';
if (defaultBannedRights && defaultBannedRights.sendMessages) {
return lang('Channel.Persmission.Denied.SendMessages.DefaultRestrictedText');
}
return undefined;

View File

@ -176,9 +176,9 @@ export function formatVoiceRecordDuration(durationInMs: number) {
return `${parts.join(':')},${String(milliseconds).padStart(2, '0')}`;
}
export function formatDateToString(date: Date) {
export function formatDateToString(date: Date, locale = 'en-US') {
return date.toLocaleString(
'en-US',
locale,
{
year: 'numeric',
month: 'short',

View File

@ -11,6 +11,7 @@ interface LangFn {
(key: string, value?: any, format?: 'i'): any;
isRtl?: boolean;
code?: string;
}
const FALLBACK_LANG_CODE = 'en';
@ -124,6 +125,7 @@ export async function setLanguage(langCode: string, callback?: NoneToVoidFunctio
const { languages } = getGlobal().settings.byKey;
const langInfo = languages ? languages.find((l) => l.langCode === langCode) : undefined;
getTranslation.isRtl = Boolean(langInfo && langInfo.rtl);
getTranslation.code = langCode;
if (callback) {
callback();