Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/components/misc/A.tsx
1450 views
1
/*
2
* This file is part of CoCalc: Copyright © 2021 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import Link from "next/link";
7
import { join } from "path";
8
9
import basePath from "lib/base-path";
10
11
export default function A(props: any) {
12
const { href } = props;
13
if (href == null) {
14
return <a {...copyWithout(props, new Set(["external"]))} />;
15
}
16
if (href.includes("://") || href.startsWith("mailto:")) {
17
return (
18
<a
19
{...copyWithout(props, new Set(["external"]))}
20
target={"_blank"}
21
rel={"noopener"}
22
/>
23
);
24
}
25
if (
26
props.external ||
27
href.startsWith("/projects") ||
28
href.startsWith("/settings")
29
) {
30
const props2 = copyWithout(props, new Set(["external"]));
31
if (!href.startsWith(basePath)) {
32
// @ts-ignore
33
props2.href = join(basePath, href);
34
}
35
return <a {...props2} target={"_blank"} rel={"noopener"} />;
36
}
37
return (
38
<Link href={href} {...copyWithout(props, new Set(["external", "href"]))} />
39
);
40
}
41
42
function copyWithout(props, without: Set<string>) {
43
const props2 = {};
44
for (const key in props) {
45
if (!without.has(key)) {
46
props2[key] = props[key];
47
}
48
}
49
return props2;
50
}
51
52