bigbuffet-rw/app/soapbox/reducers/suggestions.ts

100 lines
3 KiB
TypeScript
Raw Normal View History

import {
Map as ImmutableMap,
List as ImmutableList,
Record as ImmutableRecord,
} from 'immutable';
2022-01-10 14:01:24 -08:00
import { ACCOUNT_BLOCK_SUCCESS, ACCOUNT_MUTE_SUCCESS } from 'soapbox/actions/accounts';
import { DOMAIN_BLOCK_SUCCESS } from 'soapbox/actions/domain_blocks';
2020-03-27 13:59:38 -07:00
import {
SUGGESTIONS_FETCH_REQUEST,
SUGGESTIONS_FETCH_SUCCESS,
SUGGESTIONS_FETCH_FAIL,
SUGGESTIONS_DISMISS,
2021-09-17 13:21:27 -07:00
SUGGESTIONS_V2_FETCH_REQUEST,
SUGGESTIONS_V2_FETCH_SUCCESS,
SUGGESTIONS_V2_FETCH_FAIL,
2021-09-17 13:38:38 -07:00
} from '../actions/suggestions';
2020-03-27 13:59:38 -07:00
import type { AnyAction } from 'redux';
type SuggestionSource = 'past_interactions' | 'staff' | 'global';
type ReducerSuggestion = {
source: SuggestionSource,
account: string,
}
type SuggestionAccount = {
id: string,
}
type Suggestion = {
source: SuggestionSource,
account: SuggestionAccount,
}
const ReducerRecord = ImmutableRecord({
items: ImmutableList<ImmutableMap<string, any>>(),
2020-03-27 13:59:38 -07:00
isLoading: false,
});
type State = ReturnType<typeof ReducerRecord>;
/** Convert a v1 account into a v2 suggestion. */
const accountToSuggestion = (account: SuggestionAccount): ReducerSuggestion => {
2021-09-17 13:21:27 -07:00
return {
source: 'past_interactions',
account: account.id,
};
};
/** Import plain accounts into the reducer (legacy). */
const importAccounts = (state: State, accounts: SuggestionAccount[]): State => {
2021-09-17 13:21:27 -07:00
return state.withMutations(state => {
state.set('items', ImmutableList(accounts.map(account => ImmutableMap(accountToSuggestion(account)))));
2021-09-17 13:21:27 -07:00
state.set('isLoading', false);
});
};
/** Import full suggestion objects. */
const importSuggestions = (state: State, suggestions: Suggestion[]): State => {
2021-09-17 13:21:27 -07:00
return state.withMutations(state => {
state.set('items', ImmutableList(suggestions.map(x => ImmutableMap({ ...x, account: x.account.id }))));
2021-09-17 13:21:27 -07:00
state.set('isLoading', false);
});
};
const dismissAccount = (state: State, accountId: string): State => {
2021-10-02 13:18:27 -07:00
return state.update('items', items => items.filterNot(item => item.get('account') === accountId));
2021-09-17 13:21:27 -07:00
};
const dismissAccounts = (state: State, accountIds: string[]): State => {
2021-10-02 13:18:27 -07:00
return state.update('items', items => items.filterNot(item => accountIds.includes(item.get('account'))));
2021-09-17 13:21:27 -07:00
};
export default function suggestionsReducer(state = ReducerRecord(), action: AnyAction) {
switch (action.type) {
2022-05-11 14:06:35 -07:00
case SUGGESTIONS_FETCH_REQUEST:
case SUGGESTIONS_V2_FETCH_REQUEST:
return state.set('isLoading', true);
case SUGGESTIONS_FETCH_SUCCESS:
return importAccounts(state, action.accounts);
case SUGGESTIONS_V2_FETCH_SUCCESS:
return importSuggestions(state, action.suggestions);
case SUGGESTIONS_FETCH_FAIL:
case SUGGESTIONS_V2_FETCH_FAIL:
return state.set('isLoading', false);
case SUGGESTIONS_DISMISS:
return dismissAccount(state, action.id);
case ACCOUNT_BLOCK_SUCCESS:
case ACCOUNT_MUTE_SUCCESS:
return dismissAccount(state, action.relationship.id);
case DOMAIN_BLOCK_SUCCESS:
return dismissAccounts(state, action.accounts);
default:
return state;
2020-03-27 13:59:38 -07:00
}
2021-08-03 12:22:51 -07:00
}