From 079736e19928ef87b77e28a4641fd01b17c9ee08 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 7 Oct 2021 14:35:07 -0500 Subject: [PATCH] Profile: start refactoring findAccountByUsername logic --- app/soapbox/features/account_gallery/index.js | 5 ++--- app/soapbox/features/account_timeline/index.js | 5 ++--- app/soapbox/features/favourited_statuses/index.js | 4 ++-- app/soapbox/features/followers/index.js | 4 ++-- app/soapbox/features/following/index.js | 4 ++-- app/soapbox/pages/profile_page.js | 3 ++- app/soapbox/selectors/index.js | 8 ++++++++ 7 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/soapbox/features/account_gallery/index.js b/app/soapbox/features/account_gallery/index.js index d09182d09..843f83e4d 100644 --- a/app/soapbox/features/account_gallery/index.js +++ b/app/soapbox/features/account_gallery/index.js @@ -10,7 +10,7 @@ import { expandAccountMediaTimeline } from '../../actions/timelines'; import LoadingIndicator from 'soapbox/components/loading_indicator'; import Column from '../ui/components/column'; import ImmutablePureComponent from 'react-immutable-pure-component'; -import { getAccountGallery } from 'soapbox/selectors'; +import { getAccountGallery, findAccountByUsername } from 'soapbox/selectors'; import MediaItem from './components/media_item'; import LoadMore from 'soapbox/components/load_more'; import MissingIndicator from 'soapbox/components/missing_indicator'; @@ -21,7 +21,6 @@ import { FormattedMessage } from 'react-intl'; const mapStateToProps = (state, { params, withReplies = false }) => { const username = params.username || ''; const me = state.get('me'); - const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase()); let accountId = -1; @@ -29,7 +28,7 @@ const mapStateToProps = (state, { params, withReplies = false }) => { if (accountFetchError) { accountId = null; } else { - const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + const account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; accountUsername = account ? account.getIn(['acct'], '') : ''; } diff --git a/app/soapbox/features/account_timeline/index.js b/app/soapbox/features/account_timeline/index.js index 2e49ed9db..003ab8dda 100644 --- a/app/soapbox/features/account_timeline/index.js +++ b/app/soapbox/features/account_timeline/index.js @@ -18,7 +18,7 @@ import { NavLink } from 'react-router-dom'; import { fetchPatronAccount } from '../../actions/patron'; import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSettings } from 'soapbox/actions/settings'; -import { makeGetStatusIds } from 'soapbox/selectors'; +import { makeGetStatusIds, findAccountByUsername } from 'soapbox/selectors'; import classNames from 'classnames'; const makeMapStateToProps = () => { @@ -27,7 +27,6 @@ const makeMapStateToProps = () => { const mapStateToProps = (state, { params, withReplies = false }) => { const username = params.username || ''; const me = state.get('me'); - const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase()); const soapboxConfig = getSoapboxConfig(state); @@ -37,7 +36,7 @@ const makeMapStateToProps = () => { if (accountFetchError) { accountId = null; } else { - const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + const account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; accountUsername = account ? account.getIn(['acct'], '') : ''; accountApId = account ? account.get('url') : ''; diff --git a/app/soapbox/features/favourited_statuses/index.js b/app/soapbox/features/favourited_statuses/index.js index be87190fa..e10455f21 100644 --- a/app/soapbox/features/favourited_statuses/index.js +++ b/app/soapbox/features/favourited_statuses/index.js @@ -11,6 +11,7 @@ import { debounce } from 'lodash'; import MissingIndicator from 'soapbox/components/missing_indicator'; import { fetchAccount, fetchAccountByUsername } from '../../actions/accounts'; import LoadingIndicator from '../../components/loading_indicator'; +import { findAccountByUsername } from 'soapbox/selectors'; const mapStateToProps = (state, { params }) => { const username = params.username || ''; @@ -28,14 +29,13 @@ const mapStateToProps = (state, { params }) => { }; } - const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase()); let accountId = -1; if (accountFetchError) { accountId = null; } else { - const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + const account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; } diff --git a/app/soapbox/features/followers/index.js b/app/soapbox/features/followers/index.js index 25d91880e..a4d48e0d0 100644 --- a/app/soapbox/features/followers/index.js +++ b/app/soapbox/features/followers/index.js @@ -17,18 +17,18 @@ import Column from '../ui/components/column'; import ScrollableList from '../../components/scrollable_list'; import MissingIndicator from 'soapbox/components/missing_indicator'; import { getFollowDifference } from 'soapbox/utils/accounts'; +import { findAccountByUsername } from 'soapbox/selectors'; const mapStateToProps = (state, { params, withReplies = false }) => { const username = params.username || ''; const me = state.get('me'); - const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase()); let accountId = -1; if (accountFetchError) { accountId = null; } else { - const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + const account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; } diff --git a/app/soapbox/features/following/index.js b/app/soapbox/features/following/index.js index 7e480e1a9..c09a742b8 100644 --- a/app/soapbox/features/following/index.js +++ b/app/soapbox/features/following/index.js @@ -17,18 +17,18 @@ import Column from '../ui/components/column'; import ScrollableList from '../../components/scrollable_list'; import MissingIndicator from 'soapbox/components/missing_indicator'; import { getFollowDifference } from 'soapbox/utils/accounts'; +import { findAccountByUsername } from 'soapbox/selectors'; const mapStateToProps = (state, { params, withReplies = false }) => { const username = params.username || ''; const me = state.get('me'); - const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() === username.toLowerCase()); let accountId = -1; if (accountFetchError) { accountId = null; } else { - const account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + const account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; } diff --git a/app/soapbox/pages/profile_page.js b/app/soapbox/pages/profile_page.js index 576ac0e75..ae09b4c2c 100644 --- a/app/soapbox/pages/profile_page.js +++ b/app/soapbox/pages/profile_page.js @@ -19,6 +19,7 @@ import { displayFqn } from 'soapbox/utils/state'; import { getFeatures } from 'soapbox/utils/features'; import { makeGetAccount } from '../selectors'; import { Redirect } from 'react-router-dom'; +import { findAccountByUsername } from 'soapbox/selectors'; const mapStateToProps = (state, { params, withReplies = false }) => { const username = params.username || ''; @@ -32,7 +33,7 @@ const mapStateToProps = (state, { params, withReplies = false }) => { if (accountFetchError) { accountId = null; } else { - account = accounts.find(acct => username.toLowerCase() === acct.getIn(['acct'], '').toLowerCase()); + account = findAccountByUsername(state, username); accountId = account ? account.getIn(['id'], null) : -1; accountUsername = account ? account.getIn(['acct'], '') : ''; } diff --git a/app/soapbox/selectors/index.js b/app/soapbox/selectors/index.js index 60afa8bd8..4520ee40e 100644 --- a/app/soapbox/selectors/index.js +++ b/app/soapbox/selectors/index.js @@ -47,6 +47,14 @@ export const makeGetAccount = () => { }); }; +export const findAccountByUsername = (state, username) => { + const accounts = state.get('accounts'); + + return accounts.find(account => { + return username.toLowerCase() === account.getIn(['acct'], '').toLowerCase(); + }); +}; + const toServerSideType = columnType => { switch (columnType) { case 'home':