2020-03-18 18:11:37 +01:00
|
|
|
// @flow
|
|
|
|
import * as ICONS from 'constants/icons';
|
2021-02-09 17:05:56 +01:00
|
|
|
import * as PAGES from 'constants/pages';
|
2022-04-26 15:28:37 +02:00
|
|
|
import * as SETTINGS from 'constants/settings';
|
2020-03-18 18:11:37 +01:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import React from 'react';
|
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
|
|
|
import { Menu, MenuList, MenuButton, MenuItem } from '@reach/menu-button';
|
2020-03-18 22:14:11 +01:00
|
|
|
import ChannelTitle from 'component/channelTitle';
|
2020-03-18 18:11:37 +01:00
|
|
|
import Icon from 'component/common/icon';
|
2021-02-09 17:05:56 +01:00
|
|
|
import { useHistory } from 'react-router';
|
2022-03-09 19:05:37 +01:00
|
|
|
import useGetUserMemberships from 'effects/use-get-user-memberships';
|
|
|
|
import PremiumBadge from 'component/common/premium-badge';
|
2020-03-18 18:11:37 +01:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
selectedChannelUrl: string, // currently selected channel
|
|
|
|
channels: ?Array<ChannelClaim>,
|
|
|
|
onChannelSelect: (url: string) => void,
|
2021-02-09 17:05:56 +01:00
|
|
|
hideAnon?: boolean,
|
|
|
|
activeChannelClaim: ?ChannelClaim,
|
2022-05-04 15:07:51 +02:00
|
|
|
doSetActiveChannel: (claimId: ?string, override?: boolean) => void,
|
2021-02-09 17:05:56 +01:00
|
|
|
incognito: boolean,
|
2021-02-19 04:33:09 +01:00
|
|
|
doSetIncognito: (boolean) => void,
|
2022-03-09 19:05:37 +01:00
|
|
|
claimsByUri: { [string]: any },
|
|
|
|
doFetchUserMemberships: (claimIdCsv: string) => void,
|
|
|
|
odyseeMembershipByUri: (uri: string) => string,
|
2022-04-26 15:28:37 +02:00
|
|
|
storeSelection?: boolean,
|
|
|
|
doSetClientSetting: (key: string, value: string, pushPrefs: boolean) => void,
|
2022-05-05 12:58:11 +02:00
|
|
|
isHeaderMenu?: boolean,
|
2022-05-10 14:01:19 +02:00
|
|
|
autoSet?: boolean,
|
|
|
|
channelToSet?: string,
|
2020-03-18 18:11:37 +01:00
|
|
|
};
|
|
|
|
|
2022-05-04 14:34:31 +02:00
|
|
|
export default function ChannelSelector(props: Props) {
|
2022-03-09 19:05:37 +01:00
|
|
|
const {
|
|
|
|
channels,
|
|
|
|
activeChannelClaim,
|
|
|
|
doSetActiveChannel,
|
|
|
|
incognito,
|
|
|
|
doSetIncognito,
|
|
|
|
odyseeMembershipByUri,
|
|
|
|
claimsByUri,
|
|
|
|
doFetchUserMemberships,
|
2022-04-26 15:28:37 +02:00
|
|
|
storeSelection,
|
|
|
|
doSetClientSetting,
|
2022-05-04 14:34:31 +02:00
|
|
|
isHeaderMenu,
|
2022-05-10 14:01:19 +02:00
|
|
|
autoSet,
|
|
|
|
channelToSet,
|
2022-03-09 19:05:37 +01:00
|
|
|
} = props;
|
|
|
|
|
2022-05-04 15:07:51 +02:00
|
|
|
const hideAnon = Boolean(props.hideAnon || storeSelection);
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
const {
|
|
|
|
push,
|
|
|
|
location: { pathname },
|
|
|
|
} = useHistory();
|
2022-03-09 19:05:37 +01:00
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
const activeChannelUrl = activeChannelClaim && activeChannelClaim.permanent_url;
|
2020-03-18 18:11:37 +01:00
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
function handleChannelSelect(channelClaim) {
|
|
|
|
doSetIncognito(false);
|
|
|
|
doSetActiveChannel(channelClaim.claim_id);
|
2022-04-26 15:28:37 +02:00
|
|
|
|
|
|
|
if (storeSelection) {
|
|
|
|
doSetClientSetting(SETTINGS.ACTIVE_CHANNEL_CLAIM, channelClaim.claim_id, true);
|
|
|
|
}
|
2020-03-18 18:11:37 +01:00
|
|
|
}
|
|
|
|
|
2022-05-10 14:01:19 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!autoSet) return;
|
|
|
|
|
|
|
|
if (channelToSet) {
|
|
|
|
doSetActiveChannel(channelToSet);
|
|
|
|
doSetIncognito(false);
|
|
|
|
} else if (!channelToSet) {
|
|
|
|
doSetIncognito(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// on mount, if we get to autoSet a channel, set it.
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
2020-03-18 18:11:37 +01:00
|
|
|
return (
|
2021-02-09 17:05:56 +01:00
|
|
|
<div className="channel__selector">
|
|
|
|
<Menu>
|
2022-05-05 12:58:11 +02:00
|
|
|
{isHeaderMenu ? (
|
|
|
|
<MenuButton className="menu__link">
|
|
|
|
<ChannelThumbnail uri={activeChannelUrl} hideStakedIndicator xxsmall noLazyLoad />
|
|
|
|
{__('Change Default Channel')}
|
|
|
|
<Icon icon={ICONS.DOWN} />
|
|
|
|
</MenuButton>
|
|
|
|
) : (
|
|
|
|
<MenuButton>
|
|
|
|
{(incognito && !hideAnon) || !activeChannelUrl ? (
|
|
|
|
<IncognitoSelector isSelected />
|
|
|
|
) : (
|
|
|
|
<ChannelListItem
|
|
|
|
odyseeMembershipByUri={odyseeMembershipByUri}
|
|
|
|
uri={activeChannelUrl}
|
|
|
|
isSelected
|
|
|
|
claimsByUri={claimsByUri}
|
|
|
|
doFetchUserMemberships={doFetchUserMemberships}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</MenuButton>
|
|
|
|
)}
|
2022-03-09 19:05:37 +01:00
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
<MenuList className="menu__list channel__list">
|
|
|
|
{channels &&
|
2021-02-19 04:33:09 +01:00
|
|
|
channels.map((channel) => (
|
2021-02-09 17:05:56 +01:00
|
|
|
<MenuItem key={channel.permanent_url} onSelect={() => handleChannelSelect(channel)}>
|
2022-03-09 19:05:37 +01:00
|
|
|
<ChannelListItem
|
|
|
|
odyseeMembershipByUri={odyseeMembershipByUri}
|
|
|
|
uri={channel.permanent_url}
|
|
|
|
claimsByUri={claimsByUri}
|
|
|
|
doFetchUserMemberships={doFetchUserMemberships}
|
|
|
|
/>
|
2021-02-09 17:05:56 +01:00
|
|
|
</MenuItem>
|
|
|
|
))}
|
|
|
|
{!hideAnon && (
|
|
|
|
<MenuItem onSelect={() => doSetIncognito(true)}>
|
|
|
|
<IncognitoSelector />
|
2020-05-08 03:53:00 +02:00
|
|
|
</MenuItem>
|
2021-02-09 17:05:56 +01:00
|
|
|
)}
|
|
|
|
<MenuItem onSelect={() => push(`/$/${PAGES.CHANNEL_NEW}?redirect=${pathname}`)}>
|
|
|
|
<div className="channel__list-item">
|
|
|
|
<Icon sectionIcon icon={ICONS.CHANNEL} />
|
2021-02-19 04:33:09 +01:00
|
|
|
<h2 className="channel__list-text">{__('Create a new channel')}</h2>
|
2021-02-09 17:05:56 +01:00
|
|
|
</div>
|
|
|
|
</MenuItem>
|
|
|
|
</MenuList>
|
|
|
|
</Menu>
|
|
|
|
</div>
|
2020-03-18 18:11:37 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-05-04 14:34:31 +02:00
|
|
|
type ListItemProps = {
|
|
|
|
uri: string,
|
|
|
|
isSelected?: boolean,
|
|
|
|
claimsByUri: { [string]: any },
|
|
|
|
doFetchUserMemberships: (claimIdCsv: string) => void,
|
|
|
|
odyseeMembershipByUri: (uri: string) => string,
|
|
|
|
};
|
|
|
|
|
|
|
|
function ChannelListItem(props: ListItemProps) {
|
|
|
|
const { uri, isSelected = false, claimsByUri, doFetchUserMemberships, odyseeMembershipByUri } = props;
|
|
|
|
|
|
|
|
const membership = odyseeMembershipByUri(uri);
|
|
|
|
|
|
|
|
const shouldFetchUserMemberships = true;
|
|
|
|
useGetUserMemberships(shouldFetchUserMemberships, [uri], claimsByUri, doFetchUserMemberships, [uri]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={classnames('channel__list-item', { 'channel__list-item--selected': isSelected })}>
|
|
|
|
<ChannelThumbnail uri={uri} hideStakedIndicator xsmall noLazyLoad />
|
|
|
|
<ChannelTitle uri={uri} />
|
|
|
|
<PremiumBadge membership={membership} />
|
|
|
|
{isSelected && <Icon icon={ICONS.DOWN} />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncognitoSelectorProps = {
|
|
|
|
isSelected?: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
function IncognitoSelector(props: IncognitoSelectorProps) {
|
|
|
|
return (
|
|
|
|
<div className={classnames('channel__list-item', { 'channel__list-item--selected': props.isSelected })}>
|
|
|
|
<Icon sectionIcon icon={ICONS.ANONYMOUS} />
|
|
|
|
<h2 className="channel__list-text">{__('Anonymous')}</h2>
|
|
|
|
{props.isSelected && <Icon icon={ICONS.DOWN} />}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|