Improve legacy store types

This commit is contained in:
Alex Gleason 2023-06-19 13:09:51 -05:00
parent 8a4239d153
commit e789b44792
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 53 additions and 24 deletions

View file

@ -75,29 +75,57 @@ import type { AnyAction, Reducer } from 'redux';
import type { EntityStore } from 'soapbox/entity-store/types'; import type { EntityStore } from 'soapbox/entity-store/types';
import type { Account } from 'soapbox/schemas'; import type { Account } from 'soapbox/schemas';
interface LegacyImmutable<T> { interface LegacyMap {
get(key: any): (T & LegacyImmutable<T>) | undefined get(key: any): unknown
getIn(keyPath: any[]): unknown getIn(keyPath: any[]): unknown
find(predicate: (value: T & LegacyImmutable<T>, key: string) => boolean): T & LegacyImmutable<T> | undefined
toJS(): any toJS(): any
} }
function immutableize<T, S extends Record<string, T | undefined>>(state: S): S & LegacyImmutable<T> { interface LegacyStore<T> 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<T extends Record<any, any>>(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<T, S extends Record<string, T | undefined>>(state: S): S & LegacyStore<T> {
return { return {
...state, ...state,
get(id: any): T & LegacyImmutable<T> | undefined { get(id: any): T & LegacyMap | undefined {
const entity = state[id]; const entity = state[id];
return entity ? immutableize(entity) : undefined; return entity ? immutableizeEntity(entity) : undefined;
}, },
getIn(keyPath: any[]): unknown { getIn(keyPath: any[]): unknown {
return lodashGet(state, keyPath); return lodashGet(state, keyPath);
}, },
find(predicate: (value: T & LegacyImmutable<T>, key: string) => boolean): T & LegacyImmutable<T> | undefined { find(predicate: (value: T & LegacyMap, key: string) => boolean): T & LegacyMap | undefined {
const result = Object.entries(state).find(([key, value]) => value && predicate(immutableize(value), key))?.[1]; const result = Object.entries(state).find(([key, value]) => value && predicate(immutableizeEntity(value), key))?.[1];
return result ? immutableize(result) : undefined; 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() { toJS() {
@ -211,7 +239,7 @@ type InferState<R> = R extends Reducer<infer S> ? S : never;
const accountsSelector = createSelector( const accountsSelector = createSelector(
(state: InferState<typeof appReducer>) => state.entities[Entities.ACCOUNTS]?.store as EntityStore<Account> || {}, (state: InferState<typeof appReducer>) => state.entities[Entities.ACCOUNTS]?.store as EntityStore<Account> || {},
(accounts) => immutableize<Account, EntityStore<Account>>(accounts), (accounts) => immutableizeStore<Account, EntityStore<Account>>(accounts),
); );
const extendedRootReducer = (state: InferState<typeof appReducer>, action: AnyAction) => { const extendedRootReducer = (state: InferState<typeof appReducer>, action: AnyAction) => {

View file

@ -45,17 +45,18 @@ export const makeGetAccount = () => {
], (base, counters, relationship, moved, meta, admin, patron) => { ], (base, counters, relationship, moved, meta, admin, patron) => {
if (!base) return null; if (!base) return null;
return base.withMutations(map => { return base;
if (counters) map.merge(counters); // return base.withMutations(map => {
if (meta) { // if (counters) map.merge(counters);
map.merge(meta); // if (meta) {
map.set('pleroma', meta.pleroma.merge(base.get('pleroma', ImmutableMap()))); // Lol, thanks Pleroma // 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); // if (relationship) map.set('relationship', relationship);
map.set('patron', patron || null); // map.set('moved', moved || null);
map.setIn(['pleroma', 'admin'], admin); // 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) => { export const findAccountByUsername = (state: RootState, username: string) => {
const accounts = findAccountsByUsername(state, username); const accounts = findAccountsByUsername(state, username);
if (accounts.size > 1) { if (accounts.length > 1) {
const me = state.me; const me = state.me;
const meURL = state.accounts.get(me)?.url || ''; const meURL = state.accounts.get(me)?.url || '';
@ -85,7 +86,7 @@ export const findAccountByUsername = (state: RootState, username: string) => {
} }
}); });
} else { } else {
return accounts.first(); return accounts[0];
} }
}; };
@ -355,7 +356,7 @@ const getSimplePolicy = createSelector([
}); });
const getRemoteInstanceFavicon = (state: RootState, host: string) => ( 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']) .getIn(['pleroma', 'favicon'])
); );