|
| 1 | +import {useCallback, useEffect, useRef} from 'react'; |
| 2 | +import {useParams} from 'react-router'; |
| 3 | +import {useDispatch} from 'react-redux'; |
| 4 | +import cn from 'bem-cn-lite'; |
| 5 | +import {Link as ExternalLink} from '@gravity-ui/uikit'; |
| 6 | + |
| 7 | +import {backend} from '../../store'; |
| 8 | +import {getTablet, getTabletDescribe} from '../../store/reducers/tablet'; |
| 9 | +import {useAutofetcher, useTypedSelector} from '../../utils/hooks'; |
| 10 | +import '../../services/api'; |
| 11 | + |
| 12 | +import EntityStatus from '../../components/EntityStatus/EntityStatus'; |
| 13 | +import {Tag} from '../../components/Tag'; |
| 14 | +import {Icon} from '../../components/Icon'; |
| 15 | +import {EmptyState} from '../../components/EmptyState'; |
| 16 | +import {Loader} from '../../components/Loader'; |
| 17 | + |
| 18 | +import {TabletTable} from './TabletTable'; |
| 19 | +import {TabletInfo} from './TabletInfo'; |
| 20 | +import {TabletControls} from './TabletControls'; |
| 21 | + |
| 22 | +import './Tablet.scss'; |
| 23 | + |
| 24 | +export const b = cn('tablet-page'); |
| 25 | + |
| 26 | +export const Tablet = () => { |
| 27 | + const isFirstDataFetchRef = useRef(true); |
| 28 | + |
| 29 | + const dispatch = useDispatch(); |
| 30 | + |
| 31 | + const params = useParams<{id: string}>(); |
| 32 | + const {id} = params; |
| 33 | + |
| 34 | + const { |
| 35 | + data: tablet = {}, |
| 36 | + loading, |
| 37 | + id: tabletId, |
| 38 | + history = [], |
| 39 | + tenantPath, |
| 40 | + } = useTypedSelector((state) => state.tablet); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (isFirstDataFetchRef.current && tablet && tablet.TenantId) { |
| 44 | + dispatch(getTabletDescribe(tablet.TenantId)); |
| 45 | + isFirstDataFetchRef.current = false; |
| 46 | + } |
| 47 | + }, [dispatch, tablet]); |
| 48 | + |
| 49 | + const fetchData = useCallback(() => { |
| 50 | + dispatch(getTablet(id)); |
| 51 | + }, [dispatch, id]); |
| 52 | + |
| 53 | + useAutofetcher(fetchData, [fetchData], true); |
| 54 | + |
| 55 | + const renderExternalLinks = (link: {name: string; path: string}, index: number) => { |
| 56 | + return ( |
| 57 | + <li key={index} className={b('link', {external: true})}> |
| 58 | + <ExternalLink href={`${backend}${link.path}`} target="_blank"> |
| 59 | + {link.name} |
| 60 | + </ExternalLink> |
| 61 | + </li> |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + if (loading && id !== tabletId && isFirstDataFetchRef.current) { |
| 66 | + return <Loader size="l" />; |
| 67 | + } |
| 68 | + |
| 69 | + if (!tablet || !Object.keys(tablet).length) { |
| 70 | + return ( |
| 71 | + <div className={b('placeholder')}> |
| 72 | + <EmptyState title="The tablet was not found" /> |
| 73 | + </div> |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + const {TabletId, Overall, Leader} = tablet; |
| 78 | + |
| 79 | + const externalLinks = [ |
| 80 | + { |
| 81 | + name: 'Internal viewer - tablet', |
| 82 | + path: `/tablets?TabletID=${TabletId}`, |
| 83 | + }, |
| 84 | + ]; |
| 85 | + |
| 86 | + return ( |
| 87 | + <div className={b()}> |
| 88 | + <div className={b('pane-wrapper')}> |
| 89 | + <div className={b('left-pane')}> |
| 90 | + <ul className={b('links')}>{externalLinks.map(renderExternalLinks)}</ul> |
| 91 | + <div className={b('row', {header: true})}> |
| 92 | + <span className={b('title')}>Tablet</span> |
| 93 | + <EntityStatus status={Overall} name={TabletId} /> |
| 94 | + <a |
| 95 | + rel="noopener noreferrer" |
| 96 | + className={b('link', {external: true})} |
| 97 | + href={`${backend}/tablets?TabletID=${TabletId}`} |
| 98 | + target="_blank" |
| 99 | + > |
| 100 | + <Icon name="external" /> |
| 101 | + </a> |
| 102 | + {Leader && <Tag text="Leader" type="blue" />} |
| 103 | + </div> |
| 104 | + <TabletInfo tablet={tablet} tenantPath={tenantPath} /> |
| 105 | + <TabletControls tablet={tablet} /> |
| 106 | + </div> |
| 107 | + <div className={b('rigth-pane')}> |
| 108 | + <TabletTable history={history} /> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +}; |
0 commit comments