diff --git a/packages/pl-fe/src/actions/history.ts b/packages/pl-fe/src/actions/history.ts index 059a92487..5bb0bec59 100644 --- a/packages/pl-fe/src/actions/history.ts +++ b/packages/pl-fe/src/actions/history.ts @@ -11,7 +11,7 @@ const HISTORY_FETCH_FAIL = 'HISTORY_FETCH_FAIL' as const; const fetchHistory = (statusId: string) => (dispatch: AppDispatch, getState: () => RootState) => { - const loading = getState().history.getIn([statusId, 'loading']); + const loading = getState().history[statusId]?.loading; if (loading) return; diff --git a/packages/pl-fe/src/features/ui/components/modals/compare-history-modal.tsx b/packages/pl-fe/src/features/ui/components/modals/compare-history-modal.tsx index a24991112..8bb84762c 100644 --- a/packages/pl-fe/src/features/ui/components/modals/compare-history-modal.tsx +++ b/packages/pl-fe/src/features/ui/components/modals/compare-history-modal.tsx @@ -22,8 +22,8 @@ interface CompareHistoryModalProps { const CompareHistoryModal: React.FC = ({ onClose, statusId }) => { const dispatch = useAppDispatch(); - const loading = useAppSelector(state => state.history.getIn([statusId, 'loading'])); - const versions = useAppSelector(state => state.history.get(statusId)?.items); + const loading = useAppSelector(state => state.history[statusId]?.loading); + const versions = useAppSelector(state => state.history[statusId]?.items); const status = useAppSelector(state => state.statuses[statusId]); diff --git a/packages/pl-fe/src/reducers/domain-lists.ts b/packages/pl-fe/src/reducers/domain-lists.ts index d607a4fbc..9aea3238e 100644 --- a/packages/pl-fe/src/reducers/domain-lists.ts +++ b/packages/pl-fe/src/reducers/domain-lists.ts @@ -1,4 +1,4 @@ -import { OrderedSet as ImmutableOrderedSet, Record as ImmutableRecord } from 'immutable'; +import { create } from 'mutative'; import { DOMAIN_BLOCKS_FETCH_SUCCESS, @@ -9,25 +9,36 @@ import { import type { PaginatedResponse } from 'pl-api'; -const BlocksRecord = ImmutableRecord({ - items: ImmutableOrderedSet(), - next: null as (() => Promise>) | null, -}); +interface State { + blocks: { + items: Array; + next: (() => Promise>) | null; + }; +} -const ReducerRecord = ImmutableRecord({ - blocks: BlocksRecord(), -}); +const initialState: State = { + blocks: { + items: [], + next: null, + }, +}; -type State = ReturnType; - -const domainLists = (state: State = ReducerRecord(), action: DomainBlocksAction) => { +const domainLists = (state: State = initialState, action: DomainBlocksAction): State => { switch (action.type) { case DOMAIN_BLOCKS_FETCH_SUCCESS: - return state.setIn(['blocks', 'items'], ImmutableOrderedSet(action.domains)).setIn(['blocks', 'next'], action.next); + return create(state, (draft) => { + draft.blocks.items = action.domains; + draft.blocks.next = action.next; + }); case DOMAIN_BLOCKS_EXPAND_SUCCESS: - return state.updateIn(['blocks', 'items'], set => (set as ImmutableOrderedSet).union(action.domains)).setIn(['blocks', 'next'], action.next); + return create(state, (draft) => { + draft.blocks.items = [...new Set([...draft.blocks.items, ...action.domains])]; + draft.blocks.next = action.next; + }); case DOMAIN_UNBLOCK_SUCCESS: - return state.updateIn(['blocks', 'items'], set => (set as ImmutableOrderedSet).delete(action.domain)); + return create(state, (draft) => { + draft.blocks.items = draft.blocks.items.filter(item => item !== action.domain); + }); default: return state; } diff --git a/packages/pl-fe/src/reducers/followed-tags.ts b/packages/pl-fe/src/reducers/followed-tags.ts index 7aa673f2a..a99496a7b 100644 --- a/packages/pl-fe/src/reducers/followed-tags.ts +++ b/packages/pl-fe/src/reducers/followed-tags.ts @@ -1,4 +1,4 @@ -import { List as ImmutableList, Record as ImmutableRecord } from 'immutable'; +import { create } from 'mutative'; import { FOLLOWED_HASHTAGS_FETCH_REQUEST, @@ -12,34 +12,42 @@ import { import type { PaginatedResponse, Tag } from 'pl-api'; -const ReducerRecord = ImmutableRecord({ - items: ImmutableList(), - isLoading: false, - next: null as (() => Promise>) | null, -}); +interface State { + items: Array; + isLoading: boolean; + next: (() => Promise>) | null; +} -const followed_tags = (state = ReducerRecord(), action: TagsAction) => { +const initalState: State = { + items: [], + isLoading: false, + next: null, +}; + +const followed_tags = (state = initalState, action: TagsAction): State => { switch (action.type) { case FOLLOWED_HASHTAGS_FETCH_REQUEST: - return state.set('isLoading', true); + case FOLLOWED_HASHTAGS_EXPAND_REQUEST: + return create(state, draft => { + draft.isLoading = true; + }); case FOLLOWED_HASHTAGS_FETCH_SUCCESS: - return state.withMutations(map => { - map.set('items', ImmutableList(action.followed_tags)); - map.set('isLoading', false); - map.set('next', action.next); + return create(state, draft => { + draft.items = action.followed_tags; + draft.isLoading = true; + draft.next = action.next; }); case FOLLOWED_HASHTAGS_FETCH_FAIL: - return state.set('isLoading', false); - case FOLLOWED_HASHTAGS_EXPAND_REQUEST: - return state.set('isLoading', true); - case FOLLOWED_HASHTAGS_EXPAND_SUCCESS: - return state.withMutations(map => { - map.update('items', list => list.concat(action.followed_tags)); - map.set('isLoading', false); - map.set('next', action.next); - }); case FOLLOWED_HASHTAGS_EXPAND_FAIL: - return state.set('isLoading', false); + return create(state, draft => { + draft.isLoading = false; + }); + case FOLLOWED_HASHTAGS_EXPAND_SUCCESS: + return create(state, draft => { + draft.items = [...draft.items, ...action.followed_tags]; + draft.isLoading = true; + draft.next = action.next; + }); default: return state; } diff --git a/packages/pl-fe/src/reducers/history.ts b/packages/pl-fe/src/reducers/history.ts index 48784fb11..33fac0b9f 100644 --- a/packages/pl-fe/src/reducers/history.ts +++ b/packages/pl-fe/src/reducers/history.ts @@ -1,17 +1,17 @@ -import { Map as ImmutableMap, Record as ImmutableRecord } from 'immutable'; +import { create } from 'mutative'; import { HISTORY_FETCH_REQUEST, HISTORY_FETCH_SUCCESS, HISTORY_FETCH_FAIL, type HistoryAction } from 'pl-fe/actions/history'; import type { StatusEdit } from 'pl-api'; -const HistoryRecord = ImmutableRecord({ - loading: false, - items: [] as Array>, -}); +interface History { + loading: boolean; + items: Array>; +} -type State = ImmutableMap>; +type State = Record; -const initialState: State = ImmutableMap(); +const initialState: State = {}; const minifyStatusEdit = ({ account, ...statusEdit }: StatusEdit, i: number) => ({ ...statusEdit, account_id: account.id, original: i === 0, @@ -20,17 +20,23 @@ const minifyStatusEdit = ({ account, ...statusEdit }: StatusEdit, i: number) => const history = (state: State = initialState, action: HistoryAction) => { switch (action.type) { case HISTORY_FETCH_REQUEST: - return state.update(action.statusId, HistoryRecord(), history => history!.withMutations(map => { - map.set('loading', true); - map.set('items', []); - })); + return create(state, (draft) => { + draft[action.statusId] = { + loading: true, + items: [], + }; + }); case HISTORY_FETCH_SUCCESS: - return state.update(action.statusId, HistoryRecord(), history => history!.withMutations(map => { - map.set('loading', false); - map.set('items', action.history.map(minifyStatusEdit).toReversed()); - })); + return create(state, (draft) => { + draft[action.statusId] = { + loading: false, + items: action.history.map(minifyStatusEdit).toReversed(), + }; + }); case HISTORY_FETCH_FAIL: - return state.update(action.statusId, HistoryRecord(), history => history!.set('loading', false)); + return create(state, (draft) => { + if (draft[action.statusId]) draft[action.statusId].loading = false; + }); default: return state; } diff --git a/packages/pl-fe/src/reducers/meta.ts b/packages/pl-fe/src/reducers/meta.ts index 5c7238c4a..6a47da2b5 100644 --- a/packages/pl-fe/src/reducers/meta.ts +++ b/packages/pl-fe/src/reducers/meta.ts @@ -1,17 +1,15 @@ -import { Record as ImmutableRecord } from 'immutable'; - import { INSTANCE_FETCH_FAIL, type InstanceAction } from 'pl-fe/actions/instance'; -const ReducerRecord = ImmutableRecord({ +const initialState = { /** Whether /api/v1/instance 404'd (and we should display the external auth form). */ instance_fetch_failed: false, -}); +}; -const meta = (state = ReducerRecord(), action: InstanceAction) => { +const meta = (state = initialState, action: InstanceAction): typeof initialState => { switch (action.type) { case INSTANCE_FETCH_FAIL: if (action.error?.response?.status === 404) { - return state.set('instance_fetch_failed', true); + return { instance_fetch_failed: true }; } return state; default: