// @flow import * as ICONS from 'constants/icons'; import * as PAGES from 'constants/pages'; import React from 'react'; import { Lbryio } from 'lbryinc'; import ChannelSelector from 'component/channelSelector'; import ClaimPreview from 'component/claimPreview'; import Card from 'component/common/card'; import Spinner from 'component/spinner'; import Icon from 'component/common/icon'; import usePersistedState from 'effects/use-persisted-state'; import Button from 'component/button'; import Yrbl from 'component/yrbl'; import { useHistory } from 'react-router-dom'; import analytics from 'analytics'; type Props = { channels: Array, fetchingChannels: boolean, prepareEdit: string => void, }; export default function CreatorAnalytics(props: Props) { const { channels, prepareEdit } = props; const history = useHistory(); const [stats, setStats] = React.useState(); const [selectedChannelUrl, setSelectedChannelUrl] = usePersistedState('analytics-selected-channel'); const [fetchingStats, setFetchingStats] = React.useState(false); const hasChannels = channels && channels.length > 0; const firstChannel = hasChannels && channels[0]; const firstChannelUrl = firstChannel && (firstChannel.canonical_url || firstChannel.permanent_url); // permanent_url is needed for pending publishes const selectedChannelClaim = channels && channels.find(claim => claim.canonical_url === selectedChannelUrl || claim.permanent_url === selectedChannelUrl); const selectedChannelClaimId = selectedChannelClaim && selectedChannelClaim.claim_id; const channelFoundForSelectedChannelUrl = channels && channels.find(channel => { return selectedChannelUrl === channel.canonical_url || selectedChannelUrl === channel.permanent_url; }); React.useEffect(() => { // set default channel if ((!selectedChannelUrl || !channelFoundForSelectedChannelUrl) && firstChannelUrl) { setSelectedChannelUrl(firstChannelUrl); } }, [selectedChannelUrl, firstChannelUrl, channelFoundForSelectedChannelUrl]); const channelForEffect = JSON.stringify(selectedChannelClaim); React.useEffect(() => { if (selectedChannelClaimId && channelForEffect) { setFetchingStats(true); Lbryio.call('reports', 'content', { claim_id: selectedChannelClaimId }) .then(res => { setFetchingStats(false); setStats(res); }) .catch(() => { const channelToSend = JSON.parse(channelForEffect); analytics.apiLogPublish(channelToSend); setFetchingStats(false); }); } }, [selectedChannelClaimId, channelForEffect, setFetchingStats, setStats]); return (
{ setStats(null); setSelectedChannelUrl(newChannelUrl); }} />
{fetchingStats && !stats && (
)} {!fetchingStats && !stats && (
{ if (selectedChannelClaim) { prepareEdit(selectedChannelClaim.name); history.push(`/$/${PAGES.PUBLISH}`); } }} /> } />
)} {stats && (
{stats.ChannelSubs} followers} icon={ICONS.SUBSCRIBE} subtitle={
{stats.ChannelSubChange > 0 ? '+' : '-'}{' '} {__('%follower_count_weekly_change% this week', { follower_count_weekly_change: stats.ChannelSubChange || 0, })} {stats.ChannelSubChange > 0 && }
} /> {__('%all_content_views% views', { all_content_views: stats.AllContentViews })}} subtitle={ {__('%all_content_views_weekly_change% this week', { all_content_views_weekly_change: stats.AllContentViewsChange || 0, })} } />
{__('Most Viewed Claim')}
} body={
{__('%all_time_top_views% views - %all_time_views_weekly_change% this week', { all_time_top_views: stats.VideoViewsTopAllTime, all_time_views_weekly_change: stats.VideoViewChangeTopAllTime, })} {stats.VideoViewChangeTopAllTime > 0 && }
} /> )}
); }