@0xchain/link 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # link
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build link` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test link` to execute the unit tests via [Vitest](https://vitest.dev/).
@@ -0,0 +1,13 @@
1
+ import { LinkProps } from 'next/link';
2
+ interface LinkBarProps extends LinkProps {
3
+ href: string;
4
+ isOldProject?: boolean;
5
+ withLocale?: boolean;
6
+ baseUrl?: string;
7
+ children?: React.ReactNode;
8
+ prefetch?: boolean;
9
+ [key: string]: any;
10
+ }
11
+ declare const LinkBar: React.FC<LinkBarProps>;
12
+ export default LinkBar;
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAO3C,UAAU,YAAa,SAAQ,SAAS;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC;IAC3B,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,QAAA,MAAM,OAAO,EAAE,KAAK,CAAC,EAAE,CAAC,YAAY,CAiFnC,CAAC;AAEF,eAAe,OAAO,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ 'use client';
2
+ import { jsx } from "react/jsx-runtime";
3
+ import Link from "next/link";
4
+ import { usePathname } from "@0xchain/i18n/navigation";
5
+ import { useLocale } from "next-intl";
6
+ import { useRouter, useSearchParams } from "next/navigation";
7
+ import { useTransition, useMemo } from "react";
8
+ import { useProgressBar } from "@0xchain/providers/TopProgressBarProvider";
9
+ import { twMerge } from "tailwind-merge";
10
+ const LinkBar = ({
11
+ href,
12
+ isOldProject,
13
+ withLocale = true,
14
+ baseUrl = "",
15
+ children,
16
+ target = "_self",
17
+ onClick,
18
+ prefetch = false,
19
+ className,
20
+ ...rest
21
+ }) => {
22
+ const router = useRouter();
23
+ const pathName = usePathname();
24
+ const searchParams = useSearchParams();
25
+ const [, startTransition] = useTransition();
26
+ const { start } = useProgressBar();
27
+ const locale = useLocale();
28
+ const isExternalLink = (url) => {
29
+ if (baseUrl.includes(".ichatgo.ai") && target !== "_blank" || url.includes(".ichatgo.ai")) return false;
30
+ if (baseUrl === "" && !/^(https?:\/\/|\/\/)/.test(url)) return false;
31
+ return /^(https?:\/\/|\/\/)/.test(url);
32
+ };
33
+ const isCurrentPage = (url) => {
34
+ return url.replace(`/${locale}`, "") === pathName.replace(`/${locale}`, "") + searchParams.toString();
35
+ };
36
+ const handleClick = (e, href2) => {
37
+ onClick == null ? void 0 : onClick(e);
38
+ e.stopPropagation();
39
+ e.preventDefault();
40
+ if (isCurrentPage(href2)) {
41
+ window.location.reload();
42
+ return;
43
+ }
44
+ if (isOldProject || isExternalLink(href2) || target === "_blank") {
45
+ window.open(href2, isExternalLink(href2) ? "_blank" : target);
46
+ } else {
47
+ start();
48
+ startTransition(() => {
49
+ router.push(href2);
50
+ });
51
+ }
52
+ };
53
+ const friendlyHerf = useMemo(() => {
54
+ if (isExternalLink(href)) return href;
55
+ const localePrefix = `/${locale}`;
56
+ if (href === localePrefix || href.startsWith(`${localePrefix}/`) || !withLocale) {
57
+ return baseUrl + href;
58
+ }
59
+ if (href.startsWith("/")) {
60
+ return `${baseUrl}${localePrefix}${href}`;
61
+ }
62
+ return href;
63
+ }, [href, locale, baseUrl]);
64
+ return /* @__PURE__ */ jsx(
65
+ Link,
66
+ {
67
+ className: twMerge(":focus-visible:border-none! :focus-visible:ring-0! :focus-visible:ring-offset-0!", className),
68
+ suppressHydrationWarning: true,
69
+ prefetch,
70
+ href: friendlyHerf,
71
+ target: isExternalLink(href) ? "_blank" : target,
72
+ onClick: (e) => handleClick(e, friendlyHerf),
73
+ rel: isExternalLink(href) ? "noopener noreferrer" : void 0,
74
+ ...rest,
75
+ children
76
+ }
77
+ );
78
+ };
79
+ export {
80
+ LinkBar as default
81
+ };
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@0xchain/link",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "!**/*.tsbuildinfo"
18
+ ],
19
+ "devDependencies": {
20
+ "rollup-plugin-preserve-use-client": "3.0.1"
21
+ },
22
+ "dependencies": {
23
+ "next-intl": "4.6.1",
24
+ "react": "19.1.1",
25
+ "tailwind-merge": "3.3.1",
26
+ "@0xchain/i18n": "0.0.1",
27
+ "@0xchain/providers": "0.0.1"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ }
32
+ }