diff --git a/app/soapbox/reducers/index.ts b/app/soapbox/reducers/index.ts index 5f763ea8c..88f5467e6 100644 --- a/app/soapbox/reducers/index.ts +++ b/app/soapbox/reducers/index.ts @@ -75,29 +75,57 @@ import type { AnyAction, Reducer } from 'redux'; import type { EntityStore } from 'soapbox/entity-store/types'; import type { Account } from 'soapbox/schemas'; -interface LegacyImmutable { - get(key: any): (T & LegacyImmutable) | undefined +interface LegacyMap { + get(key: any): unknown getIn(keyPath: any[]): unknown - find(predicate: (value: T & LegacyImmutable, key: string) => boolean): T & LegacyImmutable | undefined toJS(): any } -function immutableize>(state: S): S & LegacyImmutable { +interface LegacyStore extends LegacyMap { + get(key: any): T & LegacyMap | undefined + getIn(keyPath: any[]): unknown + find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined + filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[] +} + +function immutableizeEntity>(entity: T): T & LegacyMap { + return { + ...entity, + + get(key: any): unknown { + return entity[key]; + }, + + getIn(keyPath: any[]): unknown { + return lodashGet(entity, keyPath); + }, + + toJS() { + return entity; + }, + }; +} + +function immutableizeStore>(state: S): S & LegacyStore { return { ...state, - get(id: any): T & LegacyImmutable | undefined { + get(id: any): T & LegacyMap | undefined { const entity = state[id]; - return entity ? immutableize(entity) : undefined; + return entity ? immutableizeEntity(entity) : undefined; }, getIn(keyPath: any[]): unknown { return lodashGet(state, keyPath); }, - find(predicate: (value: T & LegacyImmutable, key: string) => boolean): T & LegacyImmutable | undefined { - const result = Object.entries(state).find(([key, value]) => value && predicate(immutableize(value), key))?.[1]; - return result ? immutableize(result) : undefined; + find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined { + const result = Object.entries(state).find(([key, value]) => value && predicate(immutableizeEntity(value), key))?.[1]; + return result ? immutableizeEntity(result) : undefined; + }, + + filter(predicate: (value: T & LegacyMap, key: string) => boolean): (T & LegacyMap)[] { + return Object.entries(state).filter(([key, value]) => value && predicate(immutableizeEntity(value), key)).map(([key, value]) => immutableizeEntity(value!)); }, toJS() { @@ -211,7 +239,7 @@ type InferState = R extends Reducer ? S : never; const accountsSelector = createSelector( (state: InferState) => state.entities[Entities.ACCOUNTS]?.store as EntityStore || {}, - (accounts) => immutableize>(accounts), + (accounts) => immutableizeStore>(accounts), ); const extendedRootReducer = (state: InferState, action: AnyAction) => { diff --git a/app/soapbox/selectors/index.ts b/app/soapbox/selectors/index.ts index a936ffdf1..0caa3e935 100644 --- a/app/soapbox/selectors/index.ts +++ b/app/soapbox/selectors/index.ts @@ -45,17 +45,18 @@ export const makeGetAccount = () => { ], (base, counters, relationship, moved, meta, admin, patron) => { if (!base) return null; - return base.withMutations(map => { - if (counters) map.merge(counters); - if (meta) { - map.merge(meta); - map.set('pleroma', meta.pleroma.merge(base.get('pleroma', ImmutableMap()))); // Lol, thanks Pleroma - } - if (relationship) map.set('relationship', relationship); - map.set('moved', moved || null); - map.set('patron', patron || null); - map.setIn(['pleroma', 'admin'], admin); - }); + return base; + // return base.withMutations(map => { + // if (counters) map.merge(counters); + // if (meta) { + // map.merge(meta); + // map.set('pleroma', meta.pleroma.merge(base.get('pleroma') || ImmutableMap())); // Lol, thanks Pleroma + // } + // if (relationship) map.set('relationship', relationship); + // map.set('moved', moved || null); + // map.set('patron', patron || null); + // map.setIn(['pleroma', 'admin'], admin); + // }); }); }; @@ -70,7 +71,7 @@ const findAccountsByUsername = (state: RootState, username: string) => { export const findAccountByUsername = (state: RootState, username: string) => { const accounts = findAccountsByUsername(state, username); - if (accounts.size > 1) { + if (accounts.length > 1) { const me = state.me; const meURL = state.accounts.get(me)?.url || ''; @@ -85,7 +86,7 @@ export const findAccountByUsername = (state: RootState, username: string) => { } }); } else { - return accounts.first(); + return accounts[0]; } }; @@ -355,7 +356,7 @@ const getSimplePolicy = createSelector([ }); const getRemoteInstanceFavicon = (state: RootState, host: string) => ( - (state.accounts.find(account => getDomain(account) === host, null) || ImmutableMap()) + (state.accounts.find(account => getDomain(account) === host) || ImmutableMap()) .getIn(['pleroma', 'favicon']) );