ChannelsByOwner.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { useState } from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Segment, Tab } from 'semantic-ui-react';
  4. import { AccountId } from '@polkadot/types/interfaces';
  5. import { ChannelEntity } from '../entities/ChannelEntity';
  6. import { YouHaveNoChannels } from './YouHaveNoChannels';
  7. import { ChannelContentTypeValue } from '@joystream/types/content-working-group';
  8. import { ChannelPreview } from './ChannelPreview';
  9. export type ChannelsByOwnerProps = {
  10. accountId: AccountId,
  11. suspended?: boolean,
  12. channels?: ChannelEntity[]
  13. };
  14. const TabsAndChannels = (props: ChannelsByOwnerProps) => {
  15. const { channels: allChannels = [] } = props;
  16. const [ channels, setChannels ] = useState(allChannels);
  17. let videoChannelsCount = 0;
  18. let musicChannelsCount = 0;
  19. allChannels.forEach(x => {
  20. if (x.content === 'Video') {
  21. videoChannelsCount++;
  22. } else if (x.content === 'Music') {
  23. musicChannelsCount++;
  24. }
  25. });
  26. const panes = [
  27. { menuItem: `All channels (${allChannels.length})` },
  28. { menuItem: `Video channels (${videoChannelsCount})` },
  29. { menuItem: `Music channels (${musicChannelsCount})` }
  30. ];
  31. const contentTypeByTabIndex: Array<ChannelContentTypeValue | undefined> =
  32. [ undefined, 'Video', 'Music' ];
  33. const switchTab = (activeIndex: number) => {
  34. const activeContentType = contentTypeByTabIndex[activeIndex];
  35. if (activeContentType === undefined) {
  36. setChannels(allChannels)
  37. } else {
  38. setChannels(allChannels.filter(
  39. (x) => x.content === activeContentType)
  40. )
  41. }
  42. }
  43. return <>
  44. <Tab
  45. panes={panes}
  46. menu={{ secondary: true }}
  47. style={{ display: 'inline-flex', margin: '0 2rem 1rem 0' }}
  48. onTabChange={(_e, data) => switchTab(data.activeIndex as number)}
  49. />
  50. <Link to={`/media/channels/new`} className='ui button'>
  51. <i className='icon plus' />
  52. Create Channel
  53. </Link>
  54. {channels.map((channel) =>
  55. <Segment key={channel.id} padded style={{ backgroundColor: '#fff' }}>
  56. <ChannelPreview channel={channel} withDescription />
  57. </Segment>
  58. )}
  59. </>
  60. }
  61. export function ChannelsByOwner (props: ChannelsByOwnerProps) {
  62. const { suspended = false, channels = [] } = props;
  63. return <div className='JoyChannels'>
  64. {!channels.length
  65. ? <YouHaveNoChannels suspended={suspended} />
  66. : <TabsAndChannels {...props} />
  67. }</div>;
  68. }