Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

revert to `use-project` hook

Steven Tey8d77e4bf93dd
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx
new file mode 100644
index 0000000000..e4abe8e36d
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/auth.tsx
@@ -0,0 +1,15 @@
+"use client";
+
+import useProject from "@/lib/swr/use-project";
+import ProjectExceededUsage from "@/ui/projects/project-exceeded-usage";
+import { ReactNode } from "react";
+
+export default function AnalyticsAuth({ children }: { children: ReactNode }) {
+ const { exceededUsage } = useProject();
+
+ if (exceededUsage) {
+ return <ProjectExceededUsage />;
+ }
+
+ return children;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
index 3f12c5ac99..08ed29e1bd 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/analytics/page.tsx
@@ -1,40 +1,14 @@
-import { getProject } from "@/lib/fetchers";
-import LayoutLoader from "@/ui/layout/layout-loader";
-import ProjectExceededUsage from "@/ui/projects/project-exceeded-usage";
-import ProjectNotFound from "@/ui/projects/project-not-found";
import Stats from "@/ui/stats";
+import AnalyticsAuth from "./auth";
import { Suspense } from "react";
+import LayoutLoader from "@/ui/layout/layout-loader";
-export default function ProjectAnalytics({
- params,
-}: {
- params: { slug: string };
-}) {
+export default function ProjectAnalytics() {
return (
- <Suspense fallback={<LayoutLoader />}>
- <AnalyticsAuth slug={params.slug}>
+ <AnalyticsAuth>
+ <Suspense fallback={<LayoutLoader />}>
<Stats />
- </AnalyticsAuth>
- </Suspense>
+ </Suspense>
+ </AnalyticsAuth>
);
}
-
-async function AnalyticsAuth({
- slug,
- children,
-}: {
- slug: string;
- children: React.ReactNode;
-}) {
- const project = await getProject({ slug });
-
- if (!project) {
- return <ProjectNotFound />;
- }
-
- if (project.usage > project.usageLimit) {
- return <ProjectExceededUsage />;
- }
-
- return children as JSX.Element;
-}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/auth.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/auth.tsx
new file mode 100644
index 0000000000..52a87999f7
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/auth.tsx
@@ -0,0 +1,20 @@
+"use client";
+
+import useProject from "@/lib/swr/use-project";
+import LayoutLoader from "@/ui/layout/layout-loader";
+import ProjectNotFound from "@/ui/projects/project-not-found";
+import { ReactNode } from "react";
+
+export default function ProjectAuth({ children }: { children: ReactNode }) {
+ const { loading, error } = useProject();
+
+ if (loading) {
+ return <LayoutLoader />;
+ }
+
+ if (error) {
+ return <ProjectNotFound />;
+ }
+
+ return children;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/layout.tsx
index 06cb09dfe1..c8225901f3 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/layout.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/layout.tsx
@@ -1,36 +1,6 @@
-import { ReactNode, Suspense } from "react";
-import { getProject } from "@/lib/fetchers";
-import LayoutLoader from "@/ui/layout/layout-loader";
-import ProjectNotFound from "@/ui/projects/project-not-found";
+import { ReactNode } from "react";
+import ProjectAuth from "./auth";
-export default function ProjectLayout({
- params,
- children,
-}: {
- params: {
- slug: string;
- };
- children: ReactNode;
-}) {
- return (
- <Suspense fallback={<LayoutLoader />}>
- <ProjectAuth slug={params.slug}>{children}</ProjectAuth>
- </Suspense>
- );
-}
-
-async function ProjectAuth({
- slug,
- children,
-}: {
- slug: string;
- children: ReactNode;
-}) {
- const project = await getProject({ slug });
-
- if (!project) {
- return <ProjectNotFound />;
- }
-
- return children as JSX.Element;
+export default function ProjectLayout({ children }: { children: ReactNode }) {
+ return <ProjectAuth>{children}</ProjectAuth>;
}
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index d174282112..b655b4d61c 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -148,18 +148,26 @@ export default function LinkCard({
}, [selected]);
const handlClickOnLinkCard = (e: any) => {
- // if clicked on linkRef, setSelected to true
- // else setSelected to false
- // do this via event listener
- if (linkRef.current && !linkRef.current.contains(e.target)) {
- setSelected(false);
- } else {
+ // Check if the clicked element is a linkRef or one of its descendants
+ const isLinkCardClick =
+ linkRef.current && linkRef.current.contains(e.target);
+
+ // Check if the clicked element is an <a> or <button> element
+ const isExcludedElement =
+ e.target.tagName.toLowerCase() === "a" ||
+ e.target.tagName.toLowerCase() === "button";
+
+ if (isLinkCardClick && !isExcludedElement) {
setSelected(!selected);
+ } else {
+ setSelected(false);
}
};
useEffect(() => {
- document.addEventListener("click", handlClickOnLinkCard);
+ if (isVisible) {
+ document.addEventListener("click", handlClickOnLinkCard);
+ }
return () => {
document.removeEventListener("click", handlClickOnLinkCard);
};
@@ -265,9 +273,6 @@ export default function LinkCard({
</Tooltip>
) : (
<a
- onClick={(e) => {
- e.stopPropagation(); // to avoid selecting the link card
- }}
className={cn(
"w-full max-w-[140px] truncate text-sm font-semibold text-blue-800 sm:max-w-[300px] sm:text-base md:max-w-[360px] xl:max-w-[500px]",
{
@@ -295,8 +300,7 @@ export default function LinkCard({
}
>
<button
- onClick={(e) => {
- e.stopPropagation();
+ onClick={() => {
setShowAddEditLinkModal(true);
}}
className="group rounded-full bg-gray-100 p-1.5 transition-all duration-75 hover:scale-105 active:scale-95"
@@ -307,8 +311,7 @@ export default function LinkCard({
)}
{tag?.color && (
<button
- onClick={(e) => {
- e.stopPropagation();
+ onClick={() => {
setQueryString({
router,
pathname,
@@ -369,9 +372,6 @@ export default function LinkCard({
</Tooltip>
)}
<a
- onClick={(e) => {
- e.stopPropagation(); // to avoid selecting the link card
- }}
href={url}
target="_blank"
rel="noopener noreferrer"
@@ -386,9 +386,6 @@ export default function LinkCard({
<div className="flex items-center space-x-2">
<NumberTooltip value={clicks} lastClicked={lastClicked}>
<Link
- onClick={(e) => {
- e.stopPropagation();
- }}
href={`${
slug ? `/${slug}` : ""
}/analytics?domain=${domain}&key=${key}`}
@@ -537,8 +534,7 @@ export default function LinkCard({
>
<button
type="button"
- onClick={(e) => {
- e.stopPropagation();
+ onClick={() => {
setOpenPopover(!openPopover);
}}
className="rounded-md px-1 py-2 transition-all duration-75 hover:bg-gray-100 active:bg-gray-200"