From b2d5bbf53751cc950685e9578701f354c8b8bce2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 10 Nov 2024 12:11:09 +0100 Subject: [PATCH] pl-fe: migrate status lists to mutative MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-fe/src/actions/bookmarks.ts | 6 +- packages/pl-fe/src/actions/events.ts | 4 +- packages/pl-fe/src/actions/favourites.ts | 12 +- .../pl-fe/src/actions/scheduled-statuses.ts | 6 +- packages/pl-fe/src/actions/status-quotes.ts | 6 +- packages/pl-fe/src/components/status-list.tsx | 29 ++- .../pl-fe/src/features/bookmarks/index.tsx | 7 +- .../events/components/event-carousel.tsx | 10 +- packages/pl-fe/src/features/events/index.tsx | 8 +- .../features/favourited-statuses/index.tsx | 7 +- .../src/features/pinned-statuses/index.tsx | 6 +- packages/pl-fe/src/features/quotes/index.tsx | 9 +- .../src/features/scheduled-statuses/index.tsx | 6 +- .../src/features/ui/components/timeline.tsx | 2 +- packages/pl-fe/src/reducers/status-lists.ts | 168 +++++++++--------- packages/pl-fe/src/selectors/index.ts | 2 +- 16 files changed, 142 insertions(+), 146 deletions(-) diff --git a/packages/pl-fe/src/actions/bookmarks.ts b/packages/pl-fe/src/actions/bookmarks.ts index c1ff1b064..2e1f66d37 100644 --- a/packages/pl-fe/src/actions/bookmarks.ts +++ b/packages/pl-fe/src/actions/bookmarks.ts @@ -17,7 +17,7 @@ const noOp = () => new Promise(f => f(undefined)); const fetchBookmarkedStatuses = (folderId?: string) => (dispatch: AppDispatch, getState: () => RootState) => { - if (getState().status_lists.get(folderId ? `bookmarks:${folderId}` : 'bookmarks')?.isLoading) { + if (getState().status_lists[folderId ? `bookmarks:${folderId}` : 'bookmarks']?.isLoading) { return dispatch(noOp); } @@ -52,9 +52,9 @@ const fetchBookmarkedStatusesFail = (error: unknown, folderId?: string) => ({ const expandBookmarkedStatuses = (folderId?: string) => (dispatch: AppDispatch, getState: () => RootState) => { const list = folderId ? `bookmarks:${folderId}` : 'bookmarks'; - const next = getState().status_lists.get(list)?.next || null; + const next = getState().status_lists[list]?.next || null; - if (next === null || getState().status_lists.get(list)?.isLoading) { + if (next === null || getState().status_lists[list]?.isLoading) { return dispatch(noOp); } diff --git a/packages/pl-fe/src/actions/events.ts b/packages/pl-fe/src/actions/events.ts index 466cc90ec..4fb518f12 100644 --- a/packages/pl-fe/src/actions/events.ts +++ b/packages/pl-fe/src/actions/events.ts @@ -448,7 +448,7 @@ const initEventEdit = (statusId: string) => (dispatch: AppDispatch, getState: () const fetchRecentEvents = () => (dispatch: AppDispatch, getState: () => RootState) => { - if (getState().status_lists.get('recent_events')?.isLoading) { + if (getState().status_lists.recent_events?.isLoading) { return; } @@ -470,7 +470,7 @@ const fetchRecentEvents = () => const fetchJoinedEvents = () => (dispatch: AppDispatch, getState: () => RootState) => { - if (getState().status_lists.get('joined_events')?.isLoading) { + if (getState().status_lists.joined_events?.isLoading) { return; } diff --git a/packages/pl-fe/src/actions/favourites.ts b/packages/pl-fe/src/actions/favourites.ts index 736a3b88a..4cf82a1aa 100644 --- a/packages/pl-fe/src/actions/favourites.ts +++ b/packages/pl-fe/src/actions/favourites.ts @@ -27,7 +27,7 @@ const fetchFavouritedStatuses = () => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - if (getState().status_lists.get('favourites')?.isLoading) { + if (getState().status_lists.favourites?.isLoading) { return; } @@ -60,9 +60,9 @@ const expandFavouritedStatuses = () => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - const next = getState().status_lists.get('favourites')?.next || null; + const next = getState().status_lists.favourites?.next || null; - if (next === null || getState().status_lists.get('favourites')?.isLoading) { + if (next === null || getState().status_lists.favourites?.isLoading) { return; } @@ -95,7 +95,7 @@ const fetchAccountFavouritedStatuses = (accountId: string) => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - if (getState().status_lists.get(`favourites:${accountId}`)?.isLoading) { + if (getState().status_lists[`favourites:${accountId}`]?.isLoading) { return; } @@ -131,9 +131,9 @@ const expandAccountFavouritedStatuses = (accountId: string) => (dispatch: AppDispatch, getState: () => RootState) => { if (!isLoggedIn(getState)) return; - const next = getState().status_lists.get(`favourites:${accountId}`)?.next || null; + const next = getState().status_lists[`favourites:${accountId}`]?.next || null; - if (next === null || getState().status_lists.get(`favourites:${accountId}`)?.isLoading) { + if (next === null || getState().status_lists[`favourites:${accountId}`]?.isLoading) { return; } diff --git a/packages/pl-fe/src/actions/scheduled-statuses.ts b/packages/pl-fe/src/actions/scheduled-statuses.ts index 75da24ddf..0df595913 100644 --- a/packages/pl-fe/src/actions/scheduled-statuses.ts +++ b/packages/pl-fe/src/actions/scheduled-statuses.ts @@ -19,7 +19,7 @@ const fetchScheduledStatuses = () => (dispatch: AppDispatch, getState: () => RootState) => { const state = getState(); - if (state.status_lists.get('scheduled_statuses')?.isLoading) { + if (state.status_lists.scheduled_statuses?.isLoading) { return; } @@ -63,9 +63,9 @@ const fetchScheduledStatusesFail = (error: unknown) => ({ const expandScheduledStatuses = () => (dispatch: AppDispatch, getState: () => RootState) => { - const next = getState().status_lists.get('scheduled_statuses')?.next as any as () => Promise> || null; + const next = getState().status_lists.scheduled_statuses?.next as any as () => Promise> || null; - if (next === null || getState().status_lists.get('scheduled_statuses')?.isLoading) { + if (next === null || getState().status_lists.scheduled_statuses?.isLoading) { return; } diff --git a/packages/pl-fe/src/actions/status-quotes.ts b/packages/pl-fe/src/actions/status-quotes.ts index cdfe8ed6e..d5f0251b0 100644 --- a/packages/pl-fe/src/actions/status-quotes.ts +++ b/packages/pl-fe/src/actions/status-quotes.ts @@ -35,7 +35,7 @@ interface FetchStatusQuotesFailAction { const fetchStatusQuotes = (statusId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - if (getState().status_lists.getIn([`quotes:${statusId}`, 'isLoading'])) { + if (getState().status_lists[`quotes:${statusId}`]?.isLoading) { return dispatch(noOp); } @@ -78,9 +78,9 @@ interface ExpandStatusQuotesFailAction { const expandStatusQuotes = (statusId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - const next = getState().status_lists.get(`quotes:${statusId}`)?.next || null; + const next = getState().status_lists[`quotes:${statusId}`]?.next || null; - if (next === null || getState().status_lists.getIn([`quotes:${statusId}`, 'isLoading'])) { + if (next === null || getState().status_lists[`quotes:${statusId}`]?.isLoading) { return dispatch(noOp); } diff --git a/packages/pl-fe/src/components/status-list.tsx b/packages/pl-fe/src/components/status-list.tsx index 182883598..2f7829d0a 100644 --- a/packages/pl-fe/src/components/status-list.tsx +++ b/packages/pl-fe/src/components/status-list.tsx @@ -1,5 +1,4 @@ import clsx from 'clsx'; -import { OrderedSet as ImmutableOrderedSet } from 'immutable'; import debounce from 'lodash/debounce'; import React, { useCallback } from 'react'; import { FormattedMessage } from 'react-intl'; @@ -18,11 +17,11 @@ interface IStatusList extends Omit; + statusIds: Array; /** Last _unfiltered_ status ID (maxId) for pagination. */ lastStatusId?: string; /** Pinned statuses to show at the top of the feed. */ - featuredStatusIds?: ImmutableOrderedSet; + featuredStatusIds?: Array; /** Pagination callback when the end of the list is reached. */ onLoadMore?: (lastStatusId: string) => void; /** Whether the data is currently being fetched. */ @@ -57,13 +56,13 @@ const StatusList: React.FC = ({ }) => { const plFeConfig = usePlFeConfig(); - const getFeaturedStatusCount = () => featuredStatusIds?.size || 0; + const getFeaturedStatusCount = () => featuredStatusIds?.length || 0; const getCurrentStatusIndex = (id: string, featured: boolean): number => { if (featured) { - return featuredStatusIds?.keySeq().findIndex(key => key === id) || 0; + return featuredStatusIds?.findIndex(key => key === id) || 0; } else { - return statusIds.keySeq().findIndex(key => key === id) + getFeaturedStatusCount(); + return statusIds.findIndex(key => key === id) + getFeaturedStatusCount(); } }; @@ -78,11 +77,11 @@ const StatusList: React.FC = ({ }; const handleLoadOlder = useCallback(debounce(() => { - const maxId = lastStatusId || statusIds.last(); + const maxId = lastStatusId || statusIds.at(-1); if (onLoadMore && maxId) { onLoadMore(maxId.replace('末suggestions-', '')); } - }, 300, { leading: true }), [onLoadMore, lastStatusId, statusIds.last()]); + }, 300, { leading: true }), [onLoadMore, lastStatusId, statusIds.at(-1)]); const selectChild = (index: number) => { const selector = `#status-list [data-index="${index}"] .focusable`; @@ -92,9 +91,9 @@ const StatusList: React.FC = ({ }; const renderLoadGap = (index: number) => { - const ids = statusIds.toList(); - const nextId = ids.get(index + 1); - const prevId = ids.get(index - 1); + const ids = statusIds; + const nextId = ids[index + 1]; + const prevId = ids[index - 1]; if (index < 1 || !nextId || !prevId || !onLoadMore) return null; @@ -136,7 +135,7 @@ const StatusList: React.FC = ({ const renderFeaturedStatuses = (): React.ReactNode[] => { if (!featuredStatusIds) return []; - return featuredStatusIds.toArray().map(statusId => ( + return featuredStatusIds.map(statusId => ( = ({ ); const renderStatuses = (): React.ReactNode[] => { - if (isLoading || statusIds.size > 0) { - return statusIds.toList().reduce((acc, statusId, index) => { + if (isLoading || statusIds.length > 0) { + return statusIds.reduce((acc, statusId, index) => { if (statusId === null) { const gap = renderLoadGap(index); if (gap) { @@ -214,7 +213,7 @@ const StatusList: React.FC = ({ id='status-list' key='scrollable-list' isLoading={isLoading} - showLoading={isLoading && statusIds.size === 0} + showLoading={isLoading && statusIds.length === 0} onLoadMore={handleLoadOlder} placeholderComponent={() => } placeholderCount={20} diff --git a/packages/pl-fe/src/features/bookmarks/index.tsx b/packages/pl-fe/src/features/bookmarks/index.tsx index cf7648dd7..d0a27bf4e 100644 --- a/packages/pl-fe/src/features/bookmarks/index.tsx +++ b/packages/pl-fe/src/features/bookmarks/index.tsx @@ -1,4 +1,3 @@ -import { OrderedSet as ImmutableOrderedSet } from 'immutable'; import debounce from 'lodash/debounce'; import React from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; @@ -54,9 +53,9 @@ const Bookmarks: React.FC = ({ params }) => { const bookmarksKey = folderId ? `bookmarks:${folderId}` : 'bookmarks'; - const statusIds = useAppSelector((state) => state.status_lists.get(bookmarksKey)?.items || ImmutableOrderedSet()); - const isLoading = useAppSelector((state) => state.status_lists.get(bookmarksKey)?.isLoading === true); - const hasMore = useAppSelector((state) => !!state.status_lists.get(bookmarksKey)?.next); + const statusIds = useAppSelector((state) => state.status_lists[bookmarksKey]?.items || []); + const isLoading = useAppSelector((state) => state.status_lists[bookmarksKey]?.isLoading === true); + const hasMore = useAppSelector((state) => !!state.status_lists[bookmarksKey]?.next); React.useEffect(() => { dispatch(fetchBookmarkedStatuses(folderId)); diff --git a/packages/pl-fe/src/features/events/components/event-carousel.tsx b/packages/pl-fe/src/features/events/components/event-carousel.tsx index 726a1ee13..1ba8a4527 100644 --- a/packages/pl-fe/src/features/events/components/event-carousel.tsx +++ b/packages/pl-fe/src/features/events/components/event-carousel.tsx @@ -10,8 +10,6 @@ import { makeGetStatus } from 'pl-fe/selectors'; import PlaceholderEventPreview from '../../placeholder/components/placeholder-event-preview'; -import type { OrderedSet as ImmutableOrderedSet } from 'immutable'; - const Event = ({ id }: { id: string }) => { const getStatus = useCallback(makeGetStatus(), []); const status = useAppSelector(state => getStatus(state, { id })); @@ -29,7 +27,7 @@ const Event = ({ id }: { id: string }) => { }; interface IEventCarousel { - statusIds: ImmutableOrderedSet; + statusIds: Array; isLoading?: boolean | null; emptyMessage: React.ReactNode; } @@ -38,10 +36,10 @@ const EventCarousel: React.FC = ({ statusIds, isLoading, emptyMe const [index, setIndex] = useState(0); const handleChangeIndex = (index: number) => { - setIndex(index % statusIds.size); + setIndex(index % statusIds.length); }; - if (statusIds.size === 0) { + if (statusIds.length === 0) { if (isLoading) { return ; } @@ -67,7 +65,7 @@ const EventCarousel: React.FC = ({ statusIds, isLoading, emptyMe {statusIds.map(statusId => )} - {index !== statusIds.size - 1 && ( + {index !== statusIds.length - 1 && (