tidy SubscribeButton

This commit is contained in:
Collin M. Barrett 2019-08-24 11:30:31 -05:00
parent 089da5e7b8
commit 833ddae587

View file

@ -14,30 +14,37 @@ interface Props {
};
export const SubscribeButton = (props: Props): JSX.Element =>
(props.viewUrlMirrors && props.viewUrlMirrors.length > 0)
? <SubscribeButtonDropdown {...props} />
(props.viewUrlMirrors && props.viewUrlMirrors.length)
? <ButtonDropdown {...props} />
: <div>
<Button className={styles.single} size="small" {...buildButtonProps(props.name, props.viewUrl)}>Subscribe</Button>
</div>;
const SubscribeButtonDropdown = (props: Props): JSX.Element => {
const ButtonDropdown = (props: Props): JSX.Element => {
var buttonProps = buildButtonProps(props.name, props.viewUrl);
//TODO: pass title (ref https://github.com/ant-design/ant-design/issues/18122)
return <Dropdown.Button size="small"
type={buttonProps.type as DropdownButtonType}
href={buttonProps.href}
overlay={
<Menu>
{props.viewUrlMirrors.map((viewUrlMirror: string, i: number) =>
<Menu.Item key={i}>
<Button className={styles.sub} size="small" {...buildButtonProps(props.name, viewUrlMirror)}>{`Mirror ${i + 1}`}</Button>
</Menu.Item>)}
</Menu>
}>
overlay={<DropdownOverlay viewUrlMirrors={props.viewUrlMirrors} name={props.name} />}>
Subscribe
</Dropdown.Button>;
};
interface DropdownOverlayProps {
name: string;
viewUrlMirrors: string[];
};
const DropdownOverlay = (props: DropdownOverlayProps) =>
<Menu>
{props.viewUrlMirrors.map((viewUrlMirror: string, i: number) =>
<Menu.Item key={i}>
<Button className={styles.sub} size="small" {...buildButtonProps(props.name, viewUrlMirror)}>{`Mirror ${i + 1}`}</Button>
</Menu.Item>
)}
</Menu>;
const buildButtonProps = (name: string, viewUrl: string): ButtonProps => {
let type: ButtonType = "primary";
@ -45,38 +52,30 @@ const buildButtonProps = (name: string, viewUrl: string): ButtonProps => {
const hrefTitle = `${encodeURIComponent(name)}`;
let href = `abp:subscribe?location=${hrefLocation}&amp;title=${hrefTitle}`;
const defaultTitle = (prefix?: string) => `${prefix || ""}Subscribe to ${name} with a browser extension supporting the "abp:" protocol (e.g. uBlock Origin, Adblock Plus).`;
let title: string;
let prefixes: string[] = [];
let message = `Subscribe to ${name} with a browser extension supporting the "abp:" protocol (e.g. uBlock Origin, Adblock Plus).`;
switch (true) {
// HTTP protocols
case viewUrl.includes(".onion/"):
type = "dashed";
title = defaultTitle("Tor address - ");
break;
case viewUrl.includes("http://"):
type = "danger";
title = defaultTitle("Not Secure - ");
break;
// Software protocols
case viewUrl.includes(".tpl"):
href = `javascript:window.external.msAddTrackingProtectionList('${hrefLocation}', '${hrefTitle}')`;
title = `Subscribe to ${name} with Internet Explorer's Tracking Protection List feature.`;
break;
case viewUrl.includes(".lsrules"):
href = `x-littlesnitch:subscribe-rules?url=${hrefLocation}`;
title = `Subscribe to ${name} with Little Snitch's rule group subscription feature.`;
break;
case viewUrl.includes("?hostformat=littlesnitch"):
href = `x-littlesnitch:subscribe-rules?url=${hrefLocation}`;
title = `Subscribe to ${name} with Little Snitch's rule group subscription feature.`;
break;
default:
title = defaultTitle();
break;
// HTTP protocols
if (viewUrl.includes(".onion/")) {
type = "dashed";
prefixes.push("Tor");
}
if (viewUrl.includes("http://")) {
type = "danger";
prefixes.push("Insecure");
}
return { type, href, title } as ButtonProps
// Software protocols
if (viewUrl.includes(".tpl")) {
href = `javascript:window.external.msAddTrackingProtectionList('${hrefLocation}', '${hrefTitle}')`;
message = `Subscribe to ${name} with Internet Explorer's Tracking Protection List feature.`;
}
if (viewUrl.includes(".lsrules") || viewUrl.includes("?hostformat=littlesnitch")) {
href = `x-littlesnitch:subscribe-rules?url=${hrefLocation}`;
message = `Subscribe to ${name} with Little Snitch's rule group subscription feature.`;
}
const title = `${prefixes.length ? prefixes.join(" | ") : ""}${message}`;
return { type, href, title }
}