accounts.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { createSelector } from '@reduxjs/toolkit';
  2. import { Record as ImmutableRecord } from 'immutable';
  3. import { accountDefaultValues } from 'mastodon/models/account';
  4. import type { Account, AccountShape } from 'mastodon/models/account';
  5. import type { Relationship } from 'mastodon/models/relationship';
  6. import type { RootState } from 'mastodon/store';
  7. const getAccountBase = (state: RootState, id: string) =>
  8. state.accounts.get(id, null);
  9. const getAccountRelationship = (state: RootState, id: string) =>
  10. state.relationships.get(id, null);
  11. const getAccountMoved = (state: RootState, id: string) => {
  12. const movedToId = state.accounts.get(id)?.moved;
  13. if (!movedToId) return undefined;
  14. return state.accounts.get(movedToId);
  15. };
  16. interface FullAccountShape extends Omit<AccountShape, 'moved'> {
  17. relationship: Relationship | null;
  18. moved: Account | null;
  19. }
  20. const FullAccountFactory = ImmutableRecord<FullAccountShape>({
  21. ...accountDefaultValues,
  22. moved: null,
  23. relationship: null,
  24. });
  25. export function makeGetAccount() {
  26. return createSelector(
  27. [getAccountBase, getAccountRelationship, getAccountMoved],
  28. (base, relationship, moved) => {
  29. if (base === null) {
  30. return null;
  31. }
  32. return FullAccountFactory(base)
  33. .set('relationship', relationship)
  34. .set('moved', moved ?? null);
  35. },
  36. );
  37. }