2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-06-28 19:00:29 +02:00
|
|
|
import React, { useState } from 'react';
|
2019-05-07 04:35:04 +02:00
|
|
|
import { parseURI } from 'lbry-redux';
|
2018-03-26 23:32:43 +02:00
|
|
|
import Page from 'component/page';
|
2019-05-07 04:35:04 +02:00
|
|
|
import SubscribeButton from 'component/subscribeButton';
|
2019-07-08 22:54:58 +02:00
|
|
|
import BlockButton from 'component/blockButton';
|
2019-05-07 04:35:04 +02:00
|
|
|
import ShareButton from 'component/shareButton';
|
|
|
|
import { Tabs, TabList, Tab, TabPanels, TabPanel } from 'component/common/tabs';
|
|
|
|
import { withRouter } from 'react-router';
|
2019-06-28 19:00:29 +02:00
|
|
|
import Button from 'component/button';
|
2019-05-07 04:35:04 +02:00
|
|
|
import { formatLbryUriForWeb } from 'util/uri';
|
|
|
|
import ChannelContent from 'component/channelContent';
|
|
|
|
import ChannelAbout from 'component/channelAbout';
|
2019-10-15 00:21:40 +02:00
|
|
|
import ChannelDiscussion from 'component/channelDiscussion';
|
2019-05-07 04:35:04 +02:00
|
|
|
import ChannelThumbnail from 'component/channelThumbnail';
|
2019-07-02 19:54:42 +02:00
|
|
|
import ChannelEdit from 'component/channelEdit';
|
2019-07-08 20:58:33 +02:00
|
|
|
import ClaimUri from 'component/claimUri';
|
2019-06-28 19:00:29 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2019-08-02 02:56:25 +02:00
|
|
|
import classnames from 'classnames';
|
2019-08-30 18:56:35 +02:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
2019-09-16 07:32:02 +02:00
|
|
|
import { Form, FormField } from 'component/common/form';
|
|
|
|
import ClaimPreview from 'component/claimPreview';
|
|
|
|
import Icon from 'component/common/icon';
|
2019-10-03 23:40:54 +02:00
|
|
|
import HelpLink from 'component/common/help-link';
|
2019-10-19 17:24:42 +02:00
|
|
|
import debounce from 'util/debounce';
|
2019-10-19 15:43:16 +02:00
|
|
|
import { DEBOUNCE_WAIT_DURATION_MS } from 'constants/search';
|
2019-05-07 04:35:04 +02:00
|
|
|
|
|
|
|
const PAGE_VIEW_QUERY = `view`;
|
|
|
|
const ABOUT_PAGE = `about`;
|
2019-10-15 00:21:40 +02:00
|
|
|
const DISCUSSION_PAGE = `discussion`;
|
2019-09-16 07:32:02 +02:00
|
|
|
const LIGHTHOUSE_URL = 'https://lighthouse.lbry.com/search';
|
2017-05-04 05:44:08 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
uri: string,
|
2019-07-17 22:49:06 +02:00
|
|
|
claim: ChannelClaim,
|
2019-05-07 04:35:04 +02:00
|
|
|
title: ?string,
|
|
|
|
cover: ?string,
|
|
|
|
thumbnail: ?string,
|
2019-05-14 07:12:24 +02:00
|
|
|
page: number,
|
2019-05-07 04:35:04 +02:00
|
|
|
location: { search: string },
|
2019-04-04 23:05:23 +02:00
|
|
|
history: { push: string => void },
|
2019-05-07 04:35:04 +02:00
|
|
|
match: { params: { attribute: ?string } },
|
2019-06-28 19:00:29 +02:00
|
|
|
channelIsMine: boolean,
|
2019-07-08 22:54:58 +02:00
|
|
|
isSubscribed: boolean,
|
|
|
|
channelIsBlocked: boolean,
|
2019-08-29 03:39:21 +02:00
|
|
|
blackListedOutpoints: Array<{
|
|
|
|
txid: string,
|
|
|
|
nout: number,
|
|
|
|
}>,
|
2019-08-30 18:56:35 +02:00
|
|
|
openModal: (id: string, { uri: string, claimIsMine?: boolean, isSupport?: boolean }) => void,
|
|
|
|
supportOption: boolean,
|
2019-09-25 05:42:51 +02:00
|
|
|
fetchSubCount: string => void,
|
|
|
|
subCount: number,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
function ChannelPage(props: Props) {
|
2019-07-08 22:54:58 +02:00
|
|
|
const {
|
|
|
|
uri,
|
|
|
|
title,
|
|
|
|
cover,
|
|
|
|
history,
|
|
|
|
location,
|
|
|
|
page,
|
|
|
|
channelIsMine,
|
|
|
|
thumbnail,
|
|
|
|
claim,
|
|
|
|
isSubscribed,
|
|
|
|
channelIsBlocked,
|
2019-08-29 03:39:21 +02:00
|
|
|
blackListedOutpoints,
|
2019-08-30 18:56:35 +02:00
|
|
|
openModal,
|
|
|
|
supportOption,
|
2019-09-25 05:42:51 +02:00
|
|
|
fetchSubCount,
|
|
|
|
subCount,
|
2019-07-08 22:54:58 +02:00
|
|
|
} = props;
|
2019-09-19 22:32:45 +02:00
|
|
|
|
2019-07-08 20:58:33 +02:00
|
|
|
const { channelName } = parseURI(uri);
|
2019-03-28 17:53:13 +01:00
|
|
|
const { search } = location;
|
|
|
|
const urlParams = new URLSearchParams(search);
|
2019-05-07 04:35:04 +02:00
|
|
|
const currentView = urlParams.get(PAGE_VIEW_QUERY) || undefined;
|
2019-07-17 22:49:06 +02:00
|
|
|
const { permanent_url: permanentUrl } = claim;
|
2019-06-28 19:00:29 +02:00
|
|
|
const [editing, setEditing] = useState(false);
|
|
|
|
const [thumbPreview, setThumbPreview] = useState(thumbnail);
|
|
|
|
const [coverPreview, setCoverPreview] = useState(cover);
|
2019-09-16 07:32:02 +02:00
|
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
|
|
const [searchResults, setSearchResults] = useState(undefined);
|
2019-10-19 15:43:16 +02:00
|
|
|
// passing callback to useState ensures it's only set on initial render. Without this, the debouncing wont work.
|
2019-10-19 17:24:42 +02:00
|
|
|
const [performSearch] = useState(() => debounce(updateResults, DEBOUNCE_WAIT_DURATION_MS));
|
2019-09-27 05:52:04 +02:00
|
|
|
const claimId = claim.claim_id;
|
2019-06-28 19:00:29 +02:00
|
|
|
|
2019-05-07 04:35:04 +02:00
|
|
|
// If a user changes tabs, update the url so it stays on the same page if they refresh.
|
|
|
|
// We don't want to use links here because we can't animate the tab change and using links
|
|
|
|
// would alter the Tab label's role attribute, which should stay role="tab" to work with keyboards/screen readers.
|
2019-10-15 00:21:40 +02:00
|
|
|
const tabIndex = currentView === ABOUT_PAGE || editing ? 1 : currentView === DISCUSSION_PAGE ? 2 : 0;
|
|
|
|
|
2019-09-16 07:32:02 +02:00
|
|
|
function onTabChange(newTabIndex) {
|
2019-05-07 04:35:04 +02:00
|
|
|
let url = formatLbryUriForWeb(uri);
|
2019-05-14 07:12:24 +02:00
|
|
|
let search = '?';
|
2019-10-15 00:21:40 +02:00
|
|
|
|
|
|
|
if (newTabIndex === 0) {
|
2019-09-16 07:32:02 +02:00
|
|
|
setSearchResults(null);
|
2019-05-14 07:12:24 +02:00
|
|
|
search += `page=${page}`;
|
2019-10-15 00:21:40 +02:00
|
|
|
} else if (newTabIndex === 1) {
|
|
|
|
search += `${PAGE_VIEW_QUERY}=${ABOUT_PAGE}`;
|
|
|
|
} else {
|
|
|
|
search += `${PAGE_VIEW_QUERY}=${DISCUSSION_PAGE}`;
|
2017-08-24 23:12:23 +02:00
|
|
|
}
|
2019-05-14 07:12:24 +02:00
|
|
|
history.push(`${url}${search}`);
|
2019-09-16 07:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleSearch() {
|
2019-10-19 17:24:42 +02:00
|
|
|
performSearch(searchQuery);
|
2019-10-19 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
2019-10-19 17:24:42 +02:00
|
|
|
function updateResults(query) {
|
|
|
|
// In order to display original search results, search results must be set to null. A query of '' should display original results.
|
|
|
|
if (query === '') return setSearchResults(null);
|
|
|
|
|
|
|
|
const url = generateFetchUrl(query);
|
|
|
|
getResults(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateFetchUrl(query) {
|
|
|
|
return `${LIGHTHOUSE_URL}?s=${encodeURIComponent(query)}&channel_id=${encodeURIComponent(claim.claim_id)}`;
|
2019-10-19 15:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getResults(fetchUrl) {
|
2019-09-16 07:32:02 +02:00
|
|
|
fetch(fetchUrl)
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(results => {
|
|
|
|
const urls = results.map(({ name, claimId }) => {
|
|
|
|
return `lbry://${name}#${claimId}`;
|
|
|
|
});
|
|
|
|
setSearchResults(urls);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setSearchResults(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-19 17:24:42 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
handleSearch();
|
|
|
|
}, [searchQuery]);
|
|
|
|
|
2019-09-16 07:32:02 +02:00
|
|
|
function handleInputChange(e) {
|
|
|
|
const { value } = e.target;
|
|
|
|
setSearchQuery(value);
|
|
|
|
}
|
2019-03-28 17:53:13 +01:00
|
|
|
|
2019-08-29 03:39:21 +02:00
|
|
|
let channelIsBlackListed = false;
|
|
|
|
|
|
|
|
if (claim && blackListedOutpoints) {
|
|
|
|
channelIsBlackListed = blackListedOutpoints.some(
|
|
|
|
outpoint => outpoint.txid === claim.txid && outpoint.nout === claim.nout
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-23 15:57:03 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
setSearchResults(null);
|
|
|
|
setSearchQuery('');
|
2019-09-27 05:52:04 +02:00
|
|
|
|
|
|
|
fetchSubCount(claimId);
|
|
|
|
}, [uri, fetchSubCount, claimId]);
|
2019-09-23 15:57:03 +02:00
|
|
|
|
2019-03-28 17:53:13 +01:00
|
|
|
return (
|
2019-06-11 20:10:58 +02:00
|
|
|
<Page>
|
|
|
|
<div className="card">
|
|
|
|
<header className="channel-cover">
|
2019-09-16 07:32:02 +02:00
|
|
|
<div className="channel__quick-actions">
|
|
|
|
{!channelIsBlocked && !channelIsBlackListed && <ShareButton uri={uri} />}
|
|
|
|
{!channelIsMine && (
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
icon={ICONS.TIP}
|
|
|
|
label={__('Tip')}
|
|
|
|
title={__('Send a tip to this creator')}
|
|
|
|
onClick={() => openModal(MODALS.SEND_TIP, { uri, channelIsMine, isSupport: false })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{(channelIsMine || (!channelIsMine && supportOption)) && (
|
|
|
|
<Button
|
|
|
|
button="alt"
|
|
|
|
icon={ICONS.SUPPORT}
|
|
|
|
label={__('Support')}
|
|
|
|
title={__('Support this creator')}
|
|
|
|
onClick={() => openModal(MODALS.SEND_TIP, { uri, channelIsMine, isSupport: true })}
|
|
|
|
/>
|
|
|
|
)}
|
2019-09-30 23:48:30 +02:00
|
|
|
{!channelIsBlocked && (!channelIsBlackListed || isSubscribed) && <SubscribeButton uri={permanentUrl} />}
|
2019-09-16 07:32:02 +02:00
|
|
|
{!isSubscribed && <BlockButton uri={permanentUrl} />}
|
|
|
|
</div>
|
2019-08-02 02:56:25 +02:00
|
|
|
{!editing && cover && (
|
|
|
|
<img
|
|
|
|
className={classnames('channel-cover__custom', { 'channel__image--blurred': channelIsBlocked })}
|
|
|
|
src={cover}
|
|
|
|
/>
|
|
|
|
)}
|
2019-06-28 19:00:29 +02:00
|
|
|
{editing && <img className="channel-cover__custom" src={coverPreview} />}
|
|
|
|
{/* component that offers select/upload */}
|
|
|
|
<div className="channel__primary-info ">
|
2019-08-02 02:56:25 +02:00
|
|
|
{!editing && (
|
|
|
|
<ChannelThumbnail className="channel__thumbnail--channel-page" uri={uri} obscure={channelIsBlocked} />
|
|
|
|
)}
|
2019-06-28 19:00:29 +02:00
|
|
|
{editing && (
|
|
|
|
<ChannelThumbnail
|
|
|
|
className="channel__thumbnail--channel-page"
|
|
|
|
uri={uri}
|
|
|
|
thumbnailPreview={thumbPreview}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<h1 className="channel__title">
|
2019-08-30 01:18:06 +02:00
|
|
|
{title || '@' + channelName}
|
2019-06-28 19:00:29 +02:00
|
|
|
{channelIsMine && !editing && (
|
2019-07-02 19:54:42 +02:00
|
|
|
<Button title={__('Edit')} onClick={() => setEditing(!editing)} icon={ICONS.EDIT} iconSize={49} />
|
2019-06-28 19:00:29 +02:00
|
|
|
)}
|
|
|
|
</h1>
|
2019-09-27 05:52:04 +02:00
|
|
|
<div className="channel__meta">
|
2019-07-08 20:58:33 +02:00
|
|
|
<ClaimUri uri={uri} />
|
2019-09-25 05:42:51 +02:00
|
|
|
<span>
|
|
|
|
{subCount} {subCount !== 1 ? __('Subscribers') : __('Subscriber')}
|
2019-10-03 23:40:54 +02:00
|
|
|
<HelpLink href="https://lbry.com/faq/views" />
|
2019-09-25 05:42:51 +02:00
|
|
|
</span>
|
2019-09-27 05:52:04 +02:00
|
|
|
</div>
|
2019-05-07 04:35:04 +02:00
|
|
|
</div>
|
2019-06-11 20:10:58 +02:00
|
|
|
</header>
|
2019-07-02 19:54:42 +02:00
|
|
|
<Tabs onChange={onTabChange} index={tabIndex}>
|
|
|
|
<TabList className="tabs__list--channel-page">
|
|
|
|
<Tab disabled={editing}>{__('Content')}</Tab>
|
|
|
|
<Tab>{editing ? __('Editing Your Channel') : __('About')}</Tab>
|
2019-10-15 00:21:40 +02:00
|
|
|
<Tab disabled={editing}>{__('Discussion')}</Tab>
|
2019-10-18 19:28:43 +02:00
|
|
|
{/* only render searchbar on content page (tab index 0 === content page) */}
|
|
|
|
{tabIndex === 0 ? (
|
2019-10-03 23:20:55 +02:00
|
|
|
<Form onSubmit={handleSearch} className="wunderbar--inline">
|
2019-10-18 19:28:43 +02:00
|
|
|
<Icon icon={ICONS.SEARCH} />
|
|
|
|
<FormField
|
|
|
|
className="wunderbar__input"
|
|
|
|
value={searchQuery}
|
|
|
|
onChange={handleInputChange}
|
|
|
|
type="text"
|
|
|
|
placeholder={__('Search')}
|
|
|
|
/>
|
|
|
|
</Form>
|
|
|
|
) : (
|
|
|
|
<div />
|
|
|
|
)}
|
2019-07-02 19:54:42 +02:00
|
|
|
</TabList>
|
2019-03-28 17:53:13 +01:00
|
|
|
|
2019-07-02 19:54:42 +02:00
|
|
|
<TabPanels>
|
|
|
|
<TabPanel>
|
2019-09-16 07:32:02 +02:00
|
|
|
{searchResults ? (
|
|
|
|
searchResults.map(url => <ClaimPreview key={url} uri={url} />)
|
|
|
|
) : (
|
|
|
|
<ChannelContent uri={uri} channelIsBlackListed={channelIsBlackListed} />
|
|
|
|
)}
|
2019-07-02 19:54:42 +02:00
|
|
|
</TabPanel>
|
|
|
|
<TabPanel>
|
|
|
|
{editing ? (
|
|
|
|
<ChannelEdit
|
|
|
|
uri={uri}
|
|
|
|
setEditing={setEditing}
|
|
|
|
updateThumb={v => setThumbPreview(v)}
|
|
|
|
updateCover={v => setCoverPreview(v)}
|
|
|
|
/>
|
|
|
|
) : (
|
2019-06-28 19:00:29 +02:00
|
|
|
<ChannelAbout uri={uri} />
|
2019-07-02 19:54:42 +02:00
|
|
|
)}
|
|
|
|
</TabPanel>
|
2019-10-15 00:21:40 +02:00
|
|
|
<TabPanel>
|
|
|
|
<ChannelDiscussion uri={uri} />
|
|
|
|
</TabPanel>
|
2019-07-02 19:54:42 +02:00
|
|
|
</TabPanels>
|
|
|
|
</Tabs>
|
2019-06-11 20:10:58 +02:00
|
|
|
</div>
|
2019-03-28 17:53:13 +01:00
|
|
|
</Page>
|
|
|
|
);
|
2017-05-04 05:44:08 +02:00
|
|
|
}
|
|
|
|
|
2019-04-04 23:05:23 +02:00
|
|
|
export default withRouter(ChannelPage);
|