|
3 | 3 | import React, { useCallback } from 'react'; |
4 | 4 | import type { Node } from 'react'; |
5 | 5 |
|
| 6 | +import { createSelector } from 'reselect'; |
6 | 7 | import type { RouteProp } from '../react-navigation'; |
7 | 8 | import type { AppNavigationProp } from '../nav/AppNavigator'; |
8 | | -import { useGlobalSelector, useSelector } from '../react-redux'; |
| 9 | +import { useGlobalSelector } from '../react-redux'; |
9 | 10 | import Screen from '../common/Screen'; |
10 | 11 | import NavRow from '../common/NavRow'; |
11 | 12 | import ZulipText from '../common/ZulipText'; |
12 | 13 | import { openLinkWithUserPreference } from '../utils/openLink'; |
13 | | -import { getRealmUrl, getRealmName, getGlobalSettings } from '../selectors'; |
| 14 | +import { getRealmName, getGlobalSettings } from '../selectors'; |
| 15 | +import { getAccounts } from '../directSelectors'; |
| 16 | +import type { GlobalSelector } from '../reduxTypes'; |
| 17 | +import { getAccount, tryGetActiveAccountState } from '../account/accountsSelectors'; |
| 18 | +import { identityOfAccount, keyOfIdentity } from '../account/accountMisc'; |
| 19 | +import { getHaveServerData } from '../haveServerDataSelectors'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Data for all realms represented in `state.accounts`, logged-in or not, |
| 23 | + * unique by URL. |
| 24 | + * |
| 25 | + * The realm name will be missing when we don't have server data for any |
| 26 | + * account on the realm. |
| 27 | + */ |
| 28 | +type ViewModel = $ReadOnlyArray<{| |
| 29 | + +realm: URL, |
| 30 | + +name: string | null, |
| 31 | + +policiesUrl: URL, |
| 32 | +|}>; |
| 33 | + |
| 34 | +const getViewModel: GlobalSelector<ViewModel> = createSelector( |
| 35 | + getAccounts, |
| 36 | + tryGetActiveAccountState, |
| 37 | + (accounts, activeAccountState) => { |
| 38 | + const result = new Map(accounts.map(a => [a.realm.toString(), null])); |
| 39 | + |
| 40 | + accounts.forEach(account => { |
| 41 | + const realmStr = account.realm.toString(); |
| 42 | + |
| 43 | + if (result.get(realmStr) != null) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // TODO(#5006): Add realm name for any account we have server data for, |
| 48 | + // not just the active account. |
| 49 | + if ( |
| 50 | + activeAccountState |
| 51 | + && keyOfIdentity(identityOfAccount(getAccount(activeAccountState))) |
| 52 | + === keyOfIdentity(identityOfAccount(account)) |
| 53 | + && getHaveServerData(activeAccountState) |
| 54 | + ) { |
| 55 | + result.set(realmStr, getRealmName(activeAccountState)); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + return [...result.entries()].map(([realmStr, name]) => { |
| 60 | + const realm = new URL(realmStr); |
| 61 | + return { |
| 62 | + realm, |
| 63 | + name, |
| 64 | + policiesUrl: new URL('/policies/?nav=no', realm), |
| 65 | + }; |
| 66 | + }); |
| 67 | + }, |
| 68 | +); |
14 | 69 |
|
15 | 70 | type Props = $ReadOnly<{| |
16 | 71 | navigation: AppNavigationProp<'legal'>, |
17 | 72 | route: RouteProp<'legal', void>, |
18 | 73 | |}>; |
19 | 74 |
|
20 | | -/** (NB this is a per-account screen: it leads to this realm's policies.) */ |
| 75 | +/** |
| 76 | + * A global, all-accounts screen linking to terms for all realms we know about. |
| 77 | + */ |
21 | 78 | export default function LegalScreen(props: Props): Node { |
22 | | - const realm = useSelector(getRealmUrl); |
23 | | - const realmName = useSelector(getRealmName); |
| 79 | + const viewModel = useGlobalSelector(getViewModel); |
24 | 80 |
|
25 | 81 | const globalSettings = useGlobalSelector(getGlobalSettings); |
26 | 82 |
|
27 | 83 | const openZulipPolicies = useCallback(() => { |
28 | 84 | openLinkWithUserPreference(new URL('https://zulip.com/policies/?nav=no'), globalSettings); |
29 | 85 | }, [globalSettings]); |
30 | 86 |
|
31 | | - const openRealmPolicies = useCallback(() => { |
32 | | - openLinkWithUserPreference(new URL('/policies/?nav=no', realm), globalSettings); |
33 | | - }, [realm, globalSettings]); |
34 | | - |
35 | 87 | return ( |
36 | 88 | <Screen title="Legal"> |
37 | 89 | <NavRow title="Zulip terms" onPress={openZulipPolicies} type="external" /> |
38 | | - <NavRow |
39 | | - // These are really terms set by the server admin responsible for |
40 | | - // hosting the org, and that server admin may or may not represent |
41 | | - // the org itself, as this text might be read to imply. (E.g., |
42 | | - // on Zulip Cloud they don't.) But: |
43 | | - // - We don't want to complicate the wording. Not everyone knows |
44 | | - // what a server is. |
45 | | - // - These terms will often differ from Zulip's own terms (the ones |
46 | | - // at the other link). |
47 | | - // - These terms will apply to all users in the org, in all cases. |
48 | | - // We should link to them. |
49 | | - title={{ |
50 | | - text: 'Terms for {realmName}', |
51 | | - values: { realmName: <ZulipText style={{ fontWeight: 'bold' }} text={realmName} /> }, |
52 | | - }} |
53 | | - onPress={openRealmPolicies} |
54 | | - type="external" |
55 | | - /> |
| 90 | + {viewModel.map(({ realm, name, policiesUrl }) => ( |
| 91 | + <NavRow |
| 92 | + key={realm.toString()} |
| 93 | + // These are really terms set by the server admin responsible for |
| 94 | + // hosting the org, and that server admin may or may not represent |
| 95 | + // the org itself, as this text might be read to imply. (E.g., |
| 96 | + // on Zulip Cloud they don't.) But: |
| 97 | + // - We don't want to complicate the wording. Not everyone knows |
| 98 | + // what a server is. |
| 99 | + // - These terms will often differ from Zulip's own terms (the ones |
| 100 | + // at the "Zulip terms" link). |
| 101 | + // - These terms will apply to all users in the org, in all cases. |
| 102 | + // We should link to them. |
| 103 | + title={{ |
| 104 | + text: 'Terms for {realmName}', |
| 105 | + values: { |
| 106 | + realmName: ( |
| 107 | + // The realm name comes from server data. If we don't |
| 108 | + // have server data, fall back on the realm URL. |
| 109 | + <ZulipText style={{ fontWeight: 'bold' }} text={name ?? realm.toString()} /> |
| 110 | + ), |
| 111 | + }, |
| 112 | + }} |
| 113 | + subtitle={ |
| 114 | + // It's nice to be explicit about where the policies live, |
| 115 | + // though the "?nav=no" is a bit annoying. But also, this line |
| 116 | + // disambiguates multiple realms with the same name; the name is |
| 117 | + // shown (when we have it) in `title`. |
| 118 | + { text: '{_}', values: { _: policiesUrl.toString() } } |
| 119 | + } |
| 120 | + onPress={() => { |
| 121 | + openLinkWithUserPreference(policiesUrl, globalSettings); |
| 122 | + }} |
| 123 | + type="external" |
| 124 | + /> |
| 125 | + ))} |
56 | 126 | </Screen> |
57 | 127 | ); |
58 | 128 | } |
0 commit comments