From dff2a18b20e5d1f5ffa24a70fa6c825170795b87 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Sun, 27 Nov 2022 19:16:49 +0100 Subject: [PATCH] Profile / Notifications: Better animation and optimistic behavior (#2153) --- src/components/common/ChatExtra.tsx | 22 ++++++++++++++++++---- src/components/ui/Switcher.scss | 12 +++--------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/components/common/ChatExtra.tsx b/src/components/common/ChatExtra.tsx index c7a25e08a..f94e5df9d 100644 --- a/src/components/common/ChatExtra.tsx +++ b/src/components/common/ChatExtra.tsx @@ -1,5 +1,7 @@ import type { FC } from '../../lib/teact/teact'; -import React, { memo, useCallback, useEffect } from '../../lib/teact/teact'; +import React, { + memo, useCallback, useEffect, useState, +} from '../../lib/teact/teact'; import { getActions, withGlobal } from '../../global'; import type { GlobalState } from '../../global/types'; @@ -14,6 +16,7 @@ import { import renderText from './helpers/renderText'; import { copyTextToClipboard } from '../../util/clipboard'; import { formatPhoneNumberWithCode } from '../../util/phoneNumber'; +import { debounce } from '../../util/schedulers'; import useLang from '../../hooks/useLang'; import ListItem from '../ui/ListItem'; @@ -34,6 +37,8 @@ type StateProps = } & Pick; +const runDebounced = debounce((cb) => cb(), 500, false); + const ChatExtra: FC = ({ lastSyncTime, user, @@ -59,6 +64,7 @@ const ChatExtra: FC = ({ const { id: chatId } = chat || {}; const lang = useLang(); + const [areNotificationsEnabled, setAreNotificationsEnabled] = useState(!isMuted); useEffect(() => { if (lastSyncTime && userId) { loadFullUser({ userId }); @@ -66,8 +72,16 @@ const ChatExtra: FC = ({ }, [loadFullUser, userId, lastSyncTime]); const handleNotificationChange = useCallback(() => { - updateChatMutedState({ chatId, isMuted: !isMuted }); - }, [chatId, isMuted, updateChatMutedState]); + setAreNotificationsEnabled((current) => { + const newAreNotificationsEnabled = !current; + + runDebounced(() => { + updateChatMutedState({ chatId, isMuted: !newAreNotificationsEnabled }); + }); + + return newAreNotificationsEnabled; + }); + }, [chatId, updateChatMutedState]); if (!chat || chat.isRestricted || (isSelf && !forceShowSelf)) { return undefined; @@ -136,7 +150,7 @@ const ChatExtra: FC = ({ diff --git a/src/components/ui/Switcher.scss b/src/components/ui/Switcher.scss index 1a2e132a3..c8c917e40 100644 --- a/src/components/ui/Switcher.scss +++ b/src/components/ui/Switcher.scss @@ -43,7 +43,7 @@ display: inline-block; border-radius: 0.5rem; position: relative; - transition: background-color 0.2s ease-in; + transition: background-color 150ms; } .widget:after { @@ -55,8 +55,7 @@ height: 1.125rem; background-color: var(--color-background); border-radius: 0.75rem; - /* stylelint-disable-next-line plugin/no-low-performance-animation-properties */ - transition: border-color 0.2s ease-out; + transition: transform 200ms; border: 0.125rem solid var(--color-gray); } @@ -65,12 +64,7 @@ } input:checked + .widget:after { - left: calc(100% - 1.125rem); - transform: translateX(calc(-100% + 1.125rem)); + transform: translateX(100%); border-color: var(--color-primary); } - - .widget:active:after { - width: 1.25rem; - } }