Use Immutable Record
for accounts in Redux state (#26559)
This commit is contained in:
parent
9d799d40ba
commit
3bf2a7296e
65 changed files with 765 additions and 662 deletions
82
app/javascript/mastodon/reducers/accounts.ts
Normal file
82
app/javascript/mastodon/reducers/accounts.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { Map as ImmutableMap } from 'immutable';
|
||||
|
||||
import type { Reducer } from 'redux';
|
||||
|
||||
import {
|
||||
followAccountSuccess,
|
||||
unfollowAccountSuccess,
|
||||
importAccounts,
|
||||
revealAccount,
|
||||
} from 'mastodon/actions/accounts_typed';
|
||||
import type { ApiAccountJSON } from 'mastodon/api_types/accounts';
|
||||
import { me } from 'mastodon/initial_state';
|
||||
import type { Account } from 'mastodon/models/account';
|
||||
import { createAccountFromServerJSON } from 'mastodon/models/account';
|
||||
|
||||
const initialState = ImmutableMap<string, Account>();
|
||||
|
||||
const normalizeAccount = (
|
||||
state: typeof initialState,
|
||||
account: ApiAccountJSON,
|
||||
) => {
|
||||
return state.set(
|
||||
account.id,
|
||||
createAccountFromServerJSON(account).set(
|
||||
'hidden',
|
||||
state.get(account.id)?.hidden === false
|
||||
? false
|
||||
: account.limited || false,
|
||||
),
|
||||
);
|
||||
};
|
||||
|
||||
const normalizeAccounts = (
|
||||
state: typeof initialState,
|
||||
accounts: ApiAccountJSON[],
|
||||
) => {
|
||||
accounts.forEach((account) => {
|
||||
state = normalizeAccount(state, account);
|
||||
});
|
||||
|
||||
return state;
|
||||
};
|
||||
|
||||
export const accountsReducer: Reducer<typeof initialState> = (
|
||||
state = initialState,
|
||||
action,
|
||||
) => {
|
||||
const currentUserId = me;
|
||||
|
||||
if (!currentUserId)
|
||||
throw new Error(
|
||||
'No current user (me) defined when calling `accountsReducer`',
|
||||
);
|
||||
|
||||
if (revealAccount.match(action))
|
||||
return state.setIn([action.payload.id, 'hidden'], false);
|
||||
else if (importAccounts.match(action))
|
||||
return normalizeAccounts(state, action.payload.accounts);
|
||||
else if (followAccountSuccess.match(action))
|
||||
return state
|
||||
.update(
|
||||
action.payload.relationship.id,
|
||||
(account) => account?.update('followers_count', (n) => n + 1),
|
||||
)
|
||||
.update(
|
||||
currentUserId,
|
||||
(account) => account?.update('following_count', (n) => n + 1),
|
||||
);
|
||||
else if (unfollowAccountSuccess.match(action))
|
||||
return state
|
||||
.update(
|
||||
action.payload.relationship.id,
|
||||
(account) =>
|
||||
account?.update('followers_count', (n) => Math.max(0, n - 1)),
|
||||
)
|
||||
.update(
|
||||
currentUserId,
|
||||
(account) =>
|
||||
account?.update('following_count', (n) => Math.max(0, n - 1)),
|
||||
);
|
||||
else return state;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue