From 4e43602146d0e56498ca5bd5edbfadaf6b2fb248 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 5 Jul 2023 13:13:41 +0200 Subject: [PATCH] Cloud Folders: Save folder info on link share or copy (#3316) --- src/components/common/InviteLink.tsx | 18 +++++++------ .../folders/SettingsShareChatlist.tsx | 25 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/components/common/InviteLink.tsx b/src/components/common/InviteLink.tsx index 50e92166f..e324a8111 100644 --- a/src/components/common/InviteLink.tsx +++ b/src/components/common/InviteLink.tsx @@ -1,4 +1,4 @@ -import React, { memo, useCallback, useMemo } from '../../lib/teact/teact'; +import React, { memo, useMemo } from '../../lib/teact/teact'; import { getActions } from '../../global'; import type { FC } from '../../lib/teact/teact'; @@ -7,6 +7,7 @@ import { copyTextToClipboard } from '../../util/clipboard'; import buildClassName from '../../util/buildClassName'; import useLang from '../../hooks/useLang'; import useAppLayout from '../../hooks/useAppLayout'; +import useLastCallback from '../../hooks/useLastCallback'; import DropdownMenu from '../ui/DropdownMenu'; import MenuItem from '../ui/MenuItem'; @@ -32,20 +33,21 @@ const InviteLink: FC = ({ const { isMobile } = useAppLayout(); - const copyLink = useCallback((link: string) => { + const copyLink = useLastCallback((link: string) => { copyTextToClipboard(link); showNotification({ message: lang('LinkCopied'), }); - }, [lang]); + }); - const handleCopyPrimaryClicked = useCallback(() => { + const handleCopyPrimaryClicked = useLastCallback(() => { + if (isDisabled) return; copyLink(inviteLink); - }, [copyLink, inviteLink]); + }); - const handleShare = useCallback(() => { + const handleShare = useLastCallback(() => { openChatWithDraft({ text: inviteLink }); - }, [inviteLink]); + }); const PrimaryLinkMenuButton: FC<{ onTrigger: () => void; isOpen?: boolean }> = useMemo(() => { return ({ onTrigger, isOpen }) => ( @@ -80,7 +82,7 @@ const InviteLink: FC = ({ trigger={PrimaryLinkMenuButton} positionX="right" > - {lang('Copy')} + {lang('Copy')} {onRevoke && ( {lang('RevokeButton')} )} diff --git a/src/components/left/settings/folders/SettingsShareChatlist.tsx b/src/components/left/settings/folders/SettingsShareChatlist.tsx index c65bcf73c..540f7ac26 100644 --- a/src/components/left/settings/folders/SettingsShareChatlist.tsx +++ b/src/components/left/settings/folders/SettingsShareChatlist.tsx @@ -1,5 +1,5 @@ import React, { - memo, useCallback, useEffect, useMemo, useRef, useState, + memo, useEffect, useMemo, useRef, useState, } from '../../../../lib/teact/teact'; import { getActions, getGlobal, withGlobal } from '../../../../global'; @@ -18,6 +18,7 @@ import { isChatChannel, isUserBot } from '../../../../global/helpers'; import useLang from '../../../../hooks/useLang'; import useHistoryBack from '../../../../hooks/useHistoryBack'; import useEffectWithPrevDeps from '../../../../hooks/useEffectWithPrevDeps'; +import useLastCallback from '../../../../hooks/useLastCallback'; import AnimatedIcon from '../../../common/AnimatedIcon'; import InviteLink from '../../../common/InviteLink'; @@ -75,12 +76,12 @@ const SettingsShareChatlist: FC = ({ } }, [folderId, isActive, url]); - const handleRevoke = useCallback(() => { + const handleRevoke = useLastCallback(() => { if (!url || !folderId) return; deleteChatlistInvite({ folderId, url }); onReset(); - }, [folderId, onReset, url]); + }); const itemIds = useMemo(() => { return (includedChatIds || []).concat(pinnedChatIds || []); @@ -107,7 +108,7 @@ const SettingsShareChatlist: FC = ({ } }, [url, unlockedIds, peerIds]); - const handleClickDisabled = useCallback((id: string) => { + const handleClickDisabled = useLastCallback((id: string) => { const global = getGlobal(); const user = selectUser(global, id); const chat = selectChat(global, id); @@ -128,17 +129,17 @@ const SettingsShareChatlist: FC = ({ message: lang('FolderLinkScreen.AlertTextUnavailablePublicGroup'), }); } - }, [lang]); + }); - const handleSelectedIdsChange = useCallback((ids: string[]) => { + const handleSelectedIdsChange = useLastCallback((ids: string[]) => { setSelectedIds(ids); setIsTouched(true); - }, []); + }); - const handleSubmit = useCallback(() => { - if (!folderId || !url) return; + const handleSubmit = useLastCallback(() => { + if (!folderId || !url || !isTouched) return; editChatlistInvite({ folderId, peerIds: selectedIds, url }); - }, [folderId, selectedIds, url]); + }); const chatsCount = selectedIds.length; const isDisabled = !chatsCount || isLoading; @@ -159,9 +160,9 @@ const SettingsShareChatlist: FC = ({