import React, { FC, memo, useMemo, useCallback, useState, useEffect, } from '../../../../lib/teact/teact'; import { withGlobal } from '../../../../lib/teact/teactn'; import { GlobalActions } from '../../../../global/types'; import { ApiChatFolder, ApiChat, ApiUser } from '../../../../api/types'; import { NotifyException, NotifySettings, SettingsScreens } from '../../../../types'; import { STICKER_SIZE_FOLDER_SETTINGS } from '../../../../config'; import { pick } from '../../../../util/iteratees'; import { selectNotifyExceptions, selectNotifySettings } from '../../../../modules/selectors'; import { throttle } from '../../../../util/schedulers'; import getAnimationData from '../../../common/helpers/animatedAssets'; import { getFolderDescriptionText } from '../../../../modules/helpers'; import useLang from '../../../../hooks/useLang'; import useHistoryBack from '../../../../hooks/useHistoryBack'; import ListItem from '../../../ui/ListItem'; import Button from '../../../ui/Button'; import Loading from '../../../ui/Loading'; import AnimatedSticker from '../../../common/AnimatedSticker'; type OwnProps = { onCreateFolder: () => void; onEditFolder: (folder: ApiChatFolder) => void; isActive?: boolean; onScreenSelect: (screen: SettingsScreens) => void; onReset: () => void; }; type StateProps = { chatsById: Record; usersById: Record; orderedFolderIds?: number[]; foldersById: Record; recommendedChatFolders?: ApiChatFolder[]; notifySettings: NotifySettings; notifyExceptions?: Record; }; type DispatchProps = Pick; const runThrottledForLoadRecommended = throttle((cb) => cb(), 60000, true); const MAX_ALLOWED_FOLDERS = 10; const SettingsFoldersMain: FC = ({ onCreateFolder, onEditFolder, isActive, onScreenSelect, onReset, chatsById, usersById, orderedFolderIds, foldersById, recommendedChatFolders, notifySettings, notifyExceptions, loadRecommendedChatFolders, addChatFolder, showDialog, }) => { const [animationData, setAnimationData] = useState>(); const [isAnimationLoaded, setIsAnimationLoaded] = useState(false); const handleAnimationLoad = useCallback(() => setIsAnimationLoaded(true), []); useEffect(() => { if (!animationData) { getAnimationData('FoldersAll').then(setAnimationData); } }, [animationData]); // Due to the parent Transition, this component never gets unmounted, // that's why we use throttled API call on every update. useEffect(() => { runThrottledForLoadRecommended(() => { loadRecommendedChatFolders(); }); }, [loadRecommendedChatFolders]); const handleCreateFolder = useCallback(() => { if (Object.keys(foldersById).length >= MAX_ALLOWED_FOLDERS) { showDialog({ data: { message: 'DIALOG_FILTERS_TOO_MUCH', hasErrorKey: true, }, }); return; } onCreateFolder(); }, [foldersById, showDialog, onCreateFolder]); const lang = useLang(); useHistoryBack(isActive, onReset, onScreenSelect, SettingsScreens.Folders); const userFolders = useMemo(() => { if (!orderedFolderIds) { return undefined; } const chatIds = Object.keys(chatsById).map(Number); return orderedFolderIds.map((id) => { const folder = foldersById[id]; return { id: folder.id, title: folder.title, subtitle: getFolderDescriptionText( lang, chatsById, usersById, folder, chatIds, notifySettings, notifyExceptions, ), }; }); }, [orderedFolderIds, chatsById, foldersById, usersById, notifySettings, notifyExceptions, lang]); const handleCreateFolderFromRecommended = useCallback((folder: ApiChatFolder) => { if (Object.keys(foldersById).length >= MAX_ALLOWED_FOLDERS) { showDialog({ data: { message: 'DIALOG_FILTERS_TOO_MUCH', hasErrorKey: true, }, }); return; } addChatFolder({ folder }); }, [foldersById, addChatFolder, showDialog]); return (
{animationData && ( )}

{lang('CreateNewFilterInfo')}

{lang('Filters')}

{userFolders && userFolders.length ? userFolders.map((folder) => ( onEditFolder(foldersById[folder.id])} > {folder.title} {folder.subtitle} )) : userFolders && !userFolders.length ? (

You have no folders yet.

) : }
{(recommendedChatFolders && !!recommendedChatFolders.length) && (

{lang('FilterRecommended')}

{recommendedChatFolders.map((folder) => ( handleCreateFolderFromRecommended(folder)} >
{folder.title} {folder.description}
))}
)}
); }; export default memo(withGlobal( (global): StateProps => { const { chats: { byId: chatsById }, users: { byId: usersById }, } = global; const { orderedIds: orderedFolderIds, byId: foldersById, recommended: recommendedChatFolders, } = global.chatFolders; return { chatsById, usersById, orderedFolderIds, foldersById, recommendedChatFolders, notifySettings: selectNotifySettings(global), notifyExceptions: selectNotifyExceptions(global), }; }, (setGlobal, actions): DispatchProps => pick(actions, ['loadRecommendedChatFolders', 'addChatFolder', 'showDialog']), )(SettingsFoldersMain));