@ichaingo/link 1.3.97 → 1.3.98

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.
Files changed (2) hide show
  1. package/dist/index.js +67 -38
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -1,49 +1,78 @@
1
1
  'use client';
2
- import { jsx as k } from "react/jsx-runtime";
3
- import W from "next/link";
4
- import { usePathname as _ } from "@ichaingo/i18n/navigation";
5
- import { useLocale as v } from "next-intl";
6
- import { useRouter as y, useSearchParams as B } from "next/navigation";
7
- import { useTransition as H, useMemo as S } from "react";
8
- import { useProgressBar as T } from "@ichaingo/providers/TopProgressBarProvider";
9
- const z = ({
10
- href: r,
11
- isOldProject: c,
12
- withLocale: m = !0,
13
- baseUrl: o = "",
14
- children: p,
15
- target: a = "_self",
16
- onClick: i,
17
- prefetch: l = !1,
18
- ...f
2
+ import { jsx } from "react/jsx-runtime";
3
+ import Link from "next/link";
4
+ import { usePathname } from "@ichaingo/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 "@ichaingo/providers/TopProgressBarProvider";
9
+ const LinkBar = ({
10
+ href,
11
+ isOldProject,
12
+ withLocale = true,
13
+ baseUrl = "",
14
+ children,
15
+ target = "_self",
16
+ onClick,
17
+ prefetch = false,
18
+ ...rest
19
19
  }) => {
20
- const d = y(), g = _(), P = B(), [, $] = H(), { start: w } = T(), e = v(), n = (t) => o.includes(".ichaingo.com") || t.includes(".ichaingo.com") || o === "" && !/^(https?:\/\/|\/\/)/.test(t) ? !1 : /^(https?:\/\/|\/\/)/.test(t), x = (t) => t.replace(`/${e}`, "") === g.replace(`/${e}`, "") + P.toString(), L = (t, s) => {
21
- if (i == null || i(t), t.stopPropagation(), t.preventDefault(), x(s)) {
20
+ const router = useRouter();
21
+ const pathName = usePathname();
22
+ const searchParams = useSearchParams();
23
+ const [, startTransition] = useTransition();
24
+ const { start } = useProgressBar();
25
+ const locale = useLocale();
26
+ const isExternalLink = (url) => {
27
+ if (baseUrl.includes(".ichaingo.com") || url.includes(".ichaingo.com")) return false;
28
+ if (baseUrl === "" && !/^(https?:\/\/|\/\/)/.test(url)) return false;
29
+ return /^(https?:\/\/|\/\/)/.test(url);
30
+ };
31
+ const isCurrentPage = (url) => {
32
+ return url.replace(`/${locale}`, "") === pathName.replace(`/${locale}`, "") + searchParams.toString();
33
+ };
34
+ const handleClick = (e, href2) => {
35
+ onClick == null ? void 0 : onClick(e);
36
+ e.stopPropagation();
37
+ e.preventDefault();
38
+ if (isCurrentPage(href2)) {
22
39
  window.location.reload();
23
40
  return;
24
41
  }
25
- c || n(s) ? window.open(s, n(s) ? "_blank" : a) : (w(), $(() => {
26
- d.push(s);
27
- }));
28
- }, u = S(() => {
29
- if (n(r)) return r;
30
- const t = `/${e}`;
31
- return r === t || r.startsWith(`${t}/`) || !m ? o + r : r.startsWith("/") ? `${o}${t}${r}` : r;
32
- }, [r, e, o]);
33
- return /* @__PURE__ */ k(
34
- W,
42
+ if (isOldProject || isExternalLink(href2)) {
43
+ window.open(href2, target === "_blank" || isExternalLink(href2) ? "_blank" : target);
44
+ } else {
45
+ start();
46
+ startTransition(() => {
47
+ router.push(href2);
48
+ });
49
+ }
50
+ };
51
+ const friendlyHerf = useMemo(() => {
52
+ if (isExternalLink(href)) return href;
53
+ const localePrefix = `/${locale}`;
54
+ if (href === localePrefix || href.startsWith(`${localePrefix}/`) || !withLocale) {
55
+ return baseUrl + href;
56
+ }
57
+ if (href.startsWith("/")) {
58
+ return `${baseUrl}${localePrefix}${href}`;
59
+ }
60
+ return href;
61
+ }, [href, locale, baseUrl]);
62
+ return /* @__PURE__ */ jsx(
63
+ Link,
35
64
  {
36
- suppressHydrationWarning: !0,
37
- prefetch: l,
38
- href: u,
39
- target: n(r) ? "_blank" : a,
40
- onClick: (t) => L(t, u),
41
- rel: n(r) ? "noopener noreferrer" : void 0,
42
- ...f,
43
- children: p
65
+ suppressHydrationWarning: true,
66
+ prefetch,
67
+ href: friendlyHerf,
68
+ target: isExternalLink(href) ? "_blank" : target,
69
+ onClick: (e) => handleClick(e, friendlyHerf),
70
+ rel: isExternalLink(href) ? "noopener noreferrer" : void 0,
71
+ ...rest,
72
+ children
44
73
  }
45
74
  );
46
75
  };
47
76
  export {
48
- z as default
77
+ LinkBar as default
49
78
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ichaingo/link",
3
- "version": "1.3.97",
3
+ "version": "1.3.98",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -20,8 +20,8 @@
20
20
  "next": "^15.4.5",
21
21
  "next-intl": "^4.3.4",
22
22
  "react": "^19.1.0",
23
- "@ichaingo/providers": "1.3.97",
24
- "@ichaingo/i18n": "1.3.97"
23
+ "@ichaingo/i18n": "1.3.98",
24
+ "@ichaingo/providers": "1.3.98"
25
25
  },
26
26
  "devDependencies": {
27
27
  "rollup-plugin-preserve-use-client": "^3.0.1"