Skip to content

Commit 42bebed

Browse files
committed
AccountItem [nfc]: Stop caring about account's index in state.accounts
1 parent f127c17 commit 42bebed

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/account/AccountItem.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import ZulipText from '../common/ZulipText';
88
import Touchable from '../common/Touchable';
99
import ZulipTextIntl from '../common/ZulipTextIntl';
1010
import { IconDone, IconTrash } from '../common/Icons';
11+
import { useGlobalSelector } from '../react-redux';
12+
import { getIsActiveAccount } from './accountsSelectors';
1113
import type { AccountStatus } from './accountsSelectors';
1214

1315
const styles = createStyleSheet({
@@ -44,26 +46,27 @@ const styles = createStyleSheet({
4446
});
4547

4648
type Props = $ReadOnly<{|
47-
index: number,
4849
account: AccountStatus,
49-
onSelect: (index: number) => Promise<void> | void,
50-
onRemove: (index: number) => Promise<void> | void,
50+
onSelect: AccountStatus => Promise<void> | void,
51+
onRemove: AccountStatus => Promise<void> | void,
5152
|}>;
5253

5354
export default function AccountItem(props: Props): Node {
5455
const { email, realm, isLoggedIn } = props.account;
5556

57+
const isActiveAccount = useGlobalSelector(state => getIsActiveAccount(state, { email, realm }));
58+
5659
// Don't show the "remove account" button (the "trash" icon) for the
5760
// active account when it's logged in. This prevents removing it when the
5861
// main app UI, relying on that account's data, may be on the nav stack.
5962
// See `getHaveServerData`.
60-
const showDoneIcon = props.index === 0 && isLoggedIn;
63+
const showDoneIcon = isActiveAccount && isLoggedIn;
6164

6265
const backgroundItemColor = isLoggedIn ? 'hsla(177, 70%, 47%, 0.1)' : 'hsla(0,0%,50%,0.1)';
6366
const textColor = isLoggedIn ? BRAND_COLOR : 'gray';
6467

6568
return (
66-
<Touchable style={styles.wrapper} onPress={() => props.onSelect(props.index)}>
69+
<Touchable style={styles.wrapper} onPress={() => props.onSelect(props.account)}>
6770
<View
6871
style={[
6972
styles.accountItem,
@@ -87,7 +90,7 @@ export default function AccountItem(props: Props): Node {
8790
style={styles.icon}
8891
size={24}
8992
color="crimson"
90-
onPress={() => props.onRemove(props.index)}
93+
onPress={() => props.onRemove(props.account)}
9194
/>
9295
) : (
9396
<IconDone style={styles.icon} size={24} color={BRAND_COLOR} />

src/account/AccountList.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import React from 'react';
33
import type { Node } from 'react';
44
import { View, FlatList } from 'react-native';
55

6-
import type { AccountStatus } from './accountsSelectors';
76
import ViewPlaceholder from '../common/ViewPlaceholder';
87
import AccountItem from './AccountItem';
8+
import type { AccountStatus } from './accountsSelectors';
99

1010
type Props = $ReadOnly<{|
1111
accountStatuses: $ReadOnlyArray<AccountStatus>,
12-
onAccountSelect: number => Promise<void> | void,
13-
onAccountRemove: number => Promise<void> | void,
12+
onAccountSelect: AccountStatus => Promise<void> | void,
13+
onAccountRemove: AccountStatus => Promise<void> | void,
1414
|}>;
1515

1616
export default function AccountList(props: Props): Node {
@@ -23,12 +23,7 @@ export default function AccountList(props: Props): Node {
2323
keyExtractor={item => `${item.email}${item.realm.toString()}`}
2424
ItemSeparatorComponent={() => <ViewPlaceholder height={8} />}
2525
renderItem={({ item, index }) => (
26-
<AccountItem
27-
index={index}
28-
account={item}
29-
onSelect={onAccountSelect}
30-
onRemove={onAccountRemove}
31-
/>
26+
<AccountItem account={item} onSelect={onAccountSelect} onRemove={onAccountRemove} />
3227
)}
3328
/>
3429
</View>

src/account/AccountPickScreen.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export default function AccountPickScreen(props: Props): Node {
3939
const _ = useContext(TranslationContext);
4040

4141
const handleAccountSelect = useCallback(
42-
async (index: number) => {
43-
const { realm, email, isLoggedIn } = accountStatuses[index];
42+
async accountStatus => {
43+
const { realm, email, isLoggedIn } = accountStatus;
4444
if (isLoggedIn) {
4545
dispatch(accountSwitch({ realm, email }));
4646
} else {
@@ -62,12 +62,12 @@ export default function AccountPickScreen(props: Props): Node {
6262
navigation.push('auth', { serverSettings });
6363
}
6464
},
65-
[accountStatuses, globalSettings, dispatch, navigation, _],
65+
[globalSettings, dispatch, navigation, _],
6666
);
6767

6868
const handleAccountRemove = useCallback(
69-
(index: number) => {
70-
const { realm, email, isLoggedIn } = accountStatuses[index];
69+
accountStatus => {
70+
const { realm, email, isLoggedIn } = accountStatus;
7171
const account = accountsByIdentity({ realm, email });
7272
invariant(account, 'AccountPickScreen: should have account');
7373

@@ -91,7 +91,7 @@ export default function AccountPickScreen(props: Props): Node {
9191
_,
9292
});
9393
},
94-
[accountStatuses, accountsByIdentity, _, dispatch],
94+
[accountsByIdentity, _, dispatch],
9595
);
9696

9797
return (

0 commit comments

Comments
 (0)