diff --git a/app/soapbox/reducers/index.ts b/app/soapbox/reducers/index.ts index 88f5467e6..4caaca3ba 100644 --- a/app/soapbox/reducers/index.ts +++ b/app/soapbox/reducers/index.ts @@ -1,5 +1,4 @@ import { Record as ImmutableRecord } from 'immutable'; -import { default as lodashGet } from 'lodash/get'; import { combineReducers } from 'redux-immutable'; import { createSelector } from 'reselect'; @@ -7,6 +6,7 @@ import { AUTH_LOGGED_OUT } from 'soapbox/actions/auth'; import * as BuildConfig from 'soapbox/build-config'; import { Entities } from 'soapbox/entity-store/entities'; import entities from 'soapbox/entity-store/reducer'; +import { immutableizeStore } from 'soapbox/utils/legacy'; import account_notes from './account-notes'; import accounts_counters from './accounts-counters'; @@ -75,65 +75,6 @@ import type { AnyAction, Reducer } from 'redux'; import type { EntityStore } from 'soapbox/entity-store/types'; import type { Account } from 'soapbox/schemas'; -interface LegacyMap { - get(key: any): unknown - getIn(keyPath: any[]): unknown - toJS(): any -} - -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 & LegacyMap | undefined { - const entity = state[id]; - return entity ? immutableizeEntity(entity) : undefined; - }, - - getIn(keyPath: any[]): unknown { - return lodashGet(state, keyPath); - }, - - 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() { - return state; - }, - }; -} - const reducers = { account_notes, accounts_counters, diff --git a/app/soapbox/utils/legacy.ts b/app/soapbox/utils/legacy.ts new file mode 100644 index 000000000..269031aa7 --- /dev/null +++ b/app/soapbox/utils/legacy.ts @@ -0,0 +1,68 @@ +import { default as lodashGet } from 'lodash/get'; + +interface LegacyMap { + get(key: any): unknown + getIn(keyPath: any[]): unknown + toJS(): any +} + +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 & LegacyMap | undefined { + const entity = state[id]; + return entity ? immutableizeEntity(entity) : undefined; + }, + + getIn(keyPath: any[]): unknown { + return lodashGet(state, keyPath); + }, + + 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() { + return state; + }, + }; +} + + +export { + immutableizeStore, + immutableizeEntity, + type LegacyMap, + type LegacyStore, +}; \ No newline at end of file