2021-09-25 22:46:13 -05:00
|
|
|
import { Map as ImmutableMap } from 'immutable';
|
|
|
|
|
2023-05-28 09:38:10 -05:00
|
|
|
import { ACCOUNT_LOOKUP_FAIL } from '../actions/accounts';
|
2023-11-03 10:00:03 -05:00
|
|
|
import { importAccounts } from '../actions/accounts_typed';
|
2024-01-02 09:02:25 -06:00
|
|
|
import { domain } from '../initial_state';
|
2023-05-28 09:38:10 -05:00
|
|
|
|
2024-01-02 09:02:25 -06:00
|
|
|
export const normalizeForLookup = str => {
|
|
|
|
str = str.toLowerCase();
|
|
|
|
const trailingIndex = str.indexOf(`@${domain.toLowerCase()}`);
|
|
|
|
return (trailingIndex > 0) ? str.slice(0, trailingIndex) : str;
|
|
|
|
};
|
2022-10-21 03:06:03 -05:00
|
|
|
|
2021-09-25 22:46:13 -05:00
|
|
|
const initialState = ImmutableMap();
|
|
|
|
|
|
|
|
export default function accountsMap(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
2022-11-27 13:48:12 -06:00
|
|
|
case ACCOUNT_LOOKUP_FAIL:
|
|
|
|
return action.error?.response?.status === 404 ? state.set(normalizeForLookup(action.acct), null) : state;
|
2023-11-03 10:00:03 -05:00
|
|
|
case importAccounts.type:
|
|
|
|
return state.withMutations(map => action.payload.accounts.forEach(account => map.set(normalizeForLookup(account.acct), account.id)));
|
2021-09-25 22:46:13 -05:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2023-02-03 13:52:07 -06:00
|
|
|
}
|