2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2021-02-09 17:05:56 +01:00
|
|
|
import React from 'react';
|
2019-02-13 17:27:20 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2019-10-12 03:55:54 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
2021-02-09 17:05:56 +01:00
|
|
|
tiny?: boolean,
|
2022-04-28 09:48:48 +02:00
|
|
|
label?: string,
|
|
|
|
injected?: ?Array<string>,
|
|
|
|
channelIds?: Array<string>, // Specific channel IDs to show. Must be a subset of own channels.
|
|
|
|
// --- Redux ---
|
2021-02-09 17:05:56 +01:00
|
|
|
myChannelClaims: ?Array<ChannelClaim>,
|
2022-04-28 09:48:48 +02:00
|
|
|
fetchingChannels: boolean,
|
2021-12-02 13:59:34 +01:00
|
|
|
activeChannelId: ?string,
|
|
|
|
setActiveChannel: (string) => void,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
function SelectChannel(props: Props) {
|
|
|
|
const {
|
|
|
|
fetchingChannels,
|
2022-04-28 09:48:48 +02:00
|
|
|
channelIds,
|
2021-02-09 17:05:56 +01:00
|
|
|
myChannelClaims = [],
|
|
|
|
label,
|
|
|
|
injected = [],
|
|
|
|
tiny,
|
2021-12-02 13:59:34 +01:00
|
|
|
activeChannelId,
|
2021-02-09 17:05:56 +01:00
|
|
|
setActiveChannel,
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
function handleChannelChange(event: SyntheticInputEvent<*>) {
|
|
|
|
const channelClaimId = event.target.value;
|
|
|
|
setActiveChannel(channelClaimId);
|
2020-04-15 23:58:59 +02:00
|
|
|
}
|
|
|
|
|
2022-04-28 09:48:48 +02:00
|
|
|
let mine = myChannelClaims;
|
|
|
|
if (myChannelClaims && channelIds) {
|
|
|
|
mine = myChannelClaims.filter((x) => channelIds.includes(x.claim_id));
|
|
|
|
}
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<FormField
|
|
|
|
name="channel"
|
|
|
|
label={!tiny && (label || __('Channel'))}
|
|
|
|
labelOnLeft={tiny}
|
|
|
|
type={tiny ? 'select-tiny' : 'select'}
|
|
|
|
onChange={handleChannelChange}
|
2021-12-02 13:59:34 +01:00
|
|
|
value={activeChannelId}
|
2021-02-09 17:05:56 +01:00
|
|
|
disabled={fetchingChannels}
|
|
|
|
>
|
|
|
|
{fetchingChannels ? (
|
|
|
|
<option>{__('Loading your channels...')}</option>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-04-28 09:48:48 +02:00
|
|
|
{mine &&
|
|
|
|
mine.map(({ name, claim_id: claimId }) => (
|
2021-02-09 17:05:56 +01:00
|
|
|
<option key={claimId} value={claimId}>
|
|
|
|
{name}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
{injected &&
|
2021-12-02 13:59:34 +01:00
|
|
|
injected.map((item) => (
|
2021-02-09 17:05:56 +01:00
|
|
|
<option key={item} value={item}>
|
|
|
|
{item}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</FormField>
|
|
|
|
</>
|
|
|
|
);
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 17:05:56 +01:00
|
|
|
export default SelectChannel;
|