2019-06-20 19:37:28 +07:00
|
|
|
import React, { Component, ReactElement } from 'react';
|
2019-03-28 05:39:06 +07:00
|
|
|
|
|
|
|
import BugReportIcon from '@material-ui/icons/BugReport';
|
2019-04-09 03:29:20 +07:00
|
|
|
import DownloadIcon from '@material-ui/icons/CloudDownload';
|
2019-03-28 05:39:06 +07:00
|
|
|
import HomeIcon from '@material-ui/icons/Home';
|
2019-06-20 19:37:28 +07:00
|
|
|
import List from '@material-ui/core/List';
|
|
|
|
import Tooltip from '@material-ui/core/Tooltip';
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
import { DetailContextConsumer, VersionPageConsumerProps } from '../../pages/version/Version';
|
2019-03-28 05:39:06 +07:00
|
|
|
import { Fab, ActionListItem } from './styles';
|
2019-05-16 21:25:40 +07:00
|
|
|
import { isURL } from '../../utils/url';
|
2019-07-28 15:25:22 +07:00
|
|
|
import api from '../../utils/api';
|
|
|
|
|
|
|
|
export interface Action {
|
|
|
|
icon: string;
|
|
|
|
title: string;
|
|
|
|
handler?: Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadHandler(link: string): Promise<void> {
|
|
|
|
await api.request(link, 'GET', {
|
|
|
|
headers: {
|
|
|
|
['accept']: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
|
|
|
|
},
|
|
|
|
credentials: 'include',
|
|
|
|
});
|
|
|
|
}
|
2019-03-28 05:39:06 +07:00
|
|
|
|
|
|
|
const ACTIONS = {
|
|
|
|
homepage: {
|
|
|
|
icon: <HomeIcon />,
|
|
|
|
title: 'Visit homepage',
|
|
|
|
},
|
|
|
|
issue: {
|
|
|
|
icon: <BugReportIcon />,
|
|
|
|
title: 'Open an issue',
|
|
|
|
},
|
|
|
|
tarball: {
|
|
|
|
icon: <DownloadIcon />,
|
|
|
|
title: 'Download tarball',
|
2019-07-28 15:25:22 +07:00
|
|
|
handler: downloadHandler,
|
2019-03-28 05:39:06 +07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
class ActionBar extends Component {
|
2019-06-20 19:37:28 +07:00
|
|
|
public render(): ReactElement<HTMLElement> {
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
|
|
|
<DetailContextConsumer>
|
|
|
|
{context => {
|
2019-06-20 19:37:28 +07:00
|
|
|
return this.renderActionBar(context as VersionPageConsumerProps);
|
2019-03-28 05:39:06 +07:00
|
|
|
}}
|
|
|
|
</DetailContextConsumer>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-06-25 05:54:32 +07:00
|
|
|
private renderIconsWithLink(link: string, component: JSX.Element): ReactElement<HTMLElement> {
|
2019-03-28 05:39:06 +07:00
|
|
|
return (
|
2019-04-09 03:29:20 +07:00
|
|
|
<a href={link} target={'_blank'}>
|
2019-03-28 05:39:06 +07:00
|
|
|
{component}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-08 00:30:01 +07:00
|
|
|
private renderActionBar = ({ packageMeta }) => {
|
2019-06-20 19:37:28 +07:00
|
|
|
// @ts-ignore
|
2019-04-09 03:29:20 +07:00
|
|
|
const { latest: { bugs: { url: issue } = {}, homepage, dist: { tarball } = {} } = {} } = packageMeta;
|
2019-03-28 05:39:06 +07:00
|
|
|
|
|
|
|
const actionsMap = {
|
|
|
|
homepage,
|
|
|
|
issue,
|
|
|
|
tarball,
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderList = Object.keys(actionsMap).reduce((component, value, key) => {
|
|
|
|
const link = actionsMap[value];
|
2019-05-16 21:25:40 +07:00
|
|
|
if (link && isURL(link)) {
|
2019-07-28 15:25:22 +07:00
|
|
|
const actionItem: Action = ACTIONS[value];
|
|
|
|
if (actionItem.handler) {
|
|
|
|
const fab = (
|
|
|
|
<Tooltip key={key} title={actionItem['title']}>
|
|
|
|
<Fab
|
|
|
|
/* eslint-disable react/jsx-no-bind */
|
|
|
|
onClick={() => {
|
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
|
|
actionItem.handler!(link);
|
|
|
|
}}
|
|
|
|
size={'small'}>
|
|
|
|
{actionItem['icon']}
|
|
|
|
</Fab>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
component.push(fab);
|
|
|
|
} else {
|
|
|
|
const fab = <Fab size={'small'}>{actionItem['icon']}</Fab>;
|
|
|
|
component.push(
|
|
|
|
// @ts-ignore
|
|
|
|
<Tooltip key={key} title={actionItem['title']}>
|
|
|
|
<>{this.renderIconsWithLink(link, fab)}</>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 05:39:06 +07:00
|
|
|
}
|
|
|
|
return component;
|
|
|
|
}, []);
|
|
|
|
|
2019-07-08 00:30:01 +07:00
|
|
|
if (renderList.length > 0) {
|
|
|
|
return (
|
|
|
|
<List>
|
|
|
|
<ActionListItem alignItems={'flex-start'}>{renderList}</ActionListItem>
|
|
|
|
</List>
|
|
|
|
);
|
|
|
|
}
|
2019-03-28 05:39:06 +07:00
|
|
|
|
2019-07-08 00:30:01 +07:00
|
|
|
return null;
|
2019-03-28 05:39:06 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ActionBar;
|