Fix reducer and selector, import accounts into entity store

This commit is contained in:
Alex Gleason 2023-06-20 15:24:35 -05:00
parent 412fe84d13
commit 4258d4b27f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 33 additions and 23 deletions

View file

@ -1,6 +1,6 @@
import { importEntities } from 'soapbox/entity-store/actions';
import { Entities } from 'soapbox/entity-store/entities';
import { Group, groupSchema } from 'soapbox/schemas';
import { Group, accountSchema, groupSchema } from 'soapbox/schemas';
import { filteredArray } from 'soapbox/schemas/utils';
import { getSettings } from '../settings';
@ -17,11 +17,27 @@ const STATUSES_IMPORT = 'STATUSES_IMPORT';
const POLLS_IMPORT = 'POLLS_IMPORT';
const ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP = 'ACCOUNT_FETCH_FAIL_FOR_USERNAME_LOOKUP';
const importAccount = (account: APIEntity) =>
({ type: ACCOUNT_IMPORT, account });
const importAccount = (data: APIEntity) =>
(dispatch: AppDispatch, _getState: () => RootState) => {
dispatch({ type: ACCOUNT_IMPORT, account: data });
try {
const account = accountSchema.parse(data);
dispatch(importEntities([account], Entities.ACCOUNTS));
} catch (e) {
//
}
};
const importAccounts = (accounts: APIEntity[]) =>
({ type: ACCOUNTS_IMPORT, accounts });
const importAccounts = (data: APIEntity[]) =>
(dispatch: AppDispatch, _getState: () => RootState) => {
dispatch({ type: ACCOUNTS_IMPORT, accounts: data });
try {
const accounts = filteredArray(accountSchema).parse(data);
dispatch(importEntities(accounts, Entities.ACCOUNTS));
} catch (e) {
//
}
};
const importGroup = (group: Group) =>
importEntities([group], Entities.GROUPS);

View file

@ -183,12 +183,15 @@ const accountsSelector = createSelector(
(accounts) => immutableizeStore<Account, EntityStore<Account>>(accounts),
);
const extendedRootReducer = (state: InferState<typeof appReducer>, action: AnyAction) => {
const extendedRootReducer = (
state: InferState<typeof appReducer>,
action: AnyAction,
): ReturnType<typeof rootReducer> & { accounts: ReturnType<typeof accountsSelector> } => {
const extendedState = rootReducer(state, action);
return {
...extendedState,
accounts: accountsSelector(extendedState),
};
// @ts-ignore
extendedState.accounts = accountsSelector(extendedState);
// @ts-ignore
return extendedState;
};
export default extendedRootReducer as Reducer<ReturnType<typeof extendedRootReducer>>;

View file

@ -15,7 +15,6 @@ import { getFeatures } from 'soapbox/utils/features';
import { shouldFilter } from 'soapbox/utils/timelines';
import type { ContextType } from 'soapbox/normalizers/filter';
import type { ReducerAccount } from 'soapbox/reducers/accounts';
import type { ReducerChat } from 'soapbox/reducers/chats';
import type { RootState } from 'soapbox/store';
import type { Filter as FilterEntity, Notification, Status, Group } from 'soapbox/types/entities';
@ -168,8 +167,6 @@ export const makeGetStatus = () => {
[
(state: RootState, { id }: APIStatus) => state.statuses.get(id) as Status | undefined,
(state: RootState, { id }: APIStatus) => state.statuses.get(state.statuses.get(id)?.reblog || '') as Status | undefined,
(state: RootState, { id }: APIStatus) => state.accounts.get(state.statuses.get(id)?.account || '') as ReducerAccount | undefined,
(state: RootState, { id }: APIStatus) => state.accounts.get(state.statuses.get(state.statuses.get(id)?.reblog || '')?.account || '') as ReducerAccount | undefined,
(state: RootState, { id }: APIStatus) => state.entities[Entities.GROUPS]?.store[state.statuses.get(id)?.group || ''] as Group | undefined,
(_state: RootState, { username }: APIStatus) => username,
getFilters,
@ -177,8 +174,9 @@ export const makeGetStatus = () => {
(state: RootState) => getFeatures(state.instance),
],
(statusBase, statusReblog, accountBase, accountReblog, group, username, filters, me, features) => {
if (!statusBase || !accountBase) return null;
(statusBase, statusReblog, group, username, filters, me, features) => {
if (!statusBase) return null;
const accountBase = statusBase.account;
const accountUsername = accountBase.acct;
//Must be owner of status if username exists
@ -186,13 +184,6 @@ export const makeGetStatus = () => {
return null;
}
if (statusReblog && accountReblog) {
// @ts-ignore AAHHHHH
statusReblog = statusReblog.set('account', accountReblog);
} else {
statusReblog = undefined;
}
return statusBase.withMutations((map: Status) => {
map.set('reblog', statusReblog || null);
// @ts-ignore :(
@ -200,7 +191,7 @@ export const makeGetStatus = () => {
// @ts-ignore
map.set('group', group || null);
if ((features.filters) && (accountReblog || accountBase).id !== me) {
if ((features.filters) && accountBase.id !== me) {
const filtered = checkFiltered(statusReblog?.search_index || statusBase.search_index, filters);
map.set('filtered', filtered);