Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

feat(api): introduce useCurrentSite hook for site-specific data retrieval

Bill Yangb41a7c98dcec
diff --git a/client/src/api/admin/sites.ts b/client/src/api/admin/sites.ts
index b53f4eae3..9cf651dd1 100644
--- a/client/src/api/admin/sites.ts
+++ b/client/src/api/admin/sites.ts
@@ -2,6 +2,7 @@ import { useQuery } from "@tanstack/react-query";
import { BACKEND_URL } from "../../lib/const";
import { useStore } from "../../lib/store";
import { authedFetchWithError } from "../utils";
+import { usePathname } from "next/navigation";
export type SiteResponse = {
siteId: number;
@@ -168,3 +169,10 @@ export function changeSiteBlockBots(siteId: number, blockBots: boolean) {
},
});
}
+
+export const useCurrentSite = () => {
+ const { data: sites } = useGetSites();
+ const pathname = usePathname();
+
+ return sites?.find((site) => site.siteId === Number(pathname.split("/")[1]));
+};
diff --git a/client/src/app/[site]/components/Header/UsageBanners.tsx b/client/src/app/[site]/components/Header/UsageBanners.tsx
index 2c6b24279..4980e45f4 100644
--- a/client/src/app/[site]/components/Header/UsageBanners.tsx
+++ b/client/src/app/[site]/components/Header/UsageBanners.tsx
@@ -1,7 +1,6 @@
import { AlertTriangle, ArrowRight } from "lucide-react";
import Link from "next/link";
-import { usePathname } from "next/navigation";
-import { useGetSites } from "../../../../api/admin/sites";
+import { useCurrentSite } from "../../../../api/admin/sites";
import {
Alert,
AlertDescription,
@@ -10,12 +9,7 @@ import {
import { Button } from "../../../../components/ui/button";
export function UsageBanners() {
- const { data: sites } = useGetSites();
- const pathname = usePathname();
-
- const site = sites?.find(
- (site) => site.siteId === Number(pathname.split("/")[1])
- );
+ const site = useCurrentSite();
if (!site) return null;
diff --git a/client/src/app/admin/components/users/OrgUsersList.tsx b/client/src/app/admin/components/users/OrgUsersList.tsx
index 80acf0e53..c4e688cca 100644
--- a/client/src/app/admin/components/users/OrgUsersList.tsx
+++ b/client/src/app/admin/components/users/OrgUsersList.tsx
@@ -296,8 +296,8 @@ export function OrgUsersList() {
await authClient.admin.impersonateUser({
userId,
});
- router.push("/");
window.location.reload();
+ router.push("/");
return true;
} catch (err) {
const errorMessage =
diff --git a/client/src/app/admin/page.tsx b/client/src/app/admin/page.tsx
index 9717309c8..ebbc38604 100644
--- a/client/src/app/admin/page.tsx
+++ b/client/src/app/admin/page.tsx
@@ -1,14 +1,13 @@
"use client";
-import { useState, useEffect } from "react";
import { StandardPage } from "@/components/StandardPage";
-import { Users } from "./components/users/Users";
-import { Sites } from "./components/sites/Sites";
-import { OrgUsersList } from "./components/users/OrgUsersList";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
-import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
-import { AlertCircle } from "lucide-react";
import { authClient } from "@/lib/auth";
+import { redirect } from "next/navigation";
+import { useEffect, useState } from "react";
+import { Sites } from "./components/sites/Sites";
+import { OrgUsersList } from "./components/users/OrgUsersList";
+import { Users } from "./components/users/Users";
export default function AdminPage() {
const [isAdmin, setIsAdmin] = useState(false);
@@ -38,18 +37,7 @@ export default function AdminPage() {
// If not admin, show access denied
if (!isAdmin && !isCheckingAdmin) {
- return (
- <div className="container mt-8">
- <Alert variant="destructive">
- <AlertCircle className="h-4 w-4" />
- <AlertTitle>Access Denied</AlertTitle>
- <AlertDescription>
- You don't have permission to access this page. Please contact an
- administrator if you believe this is an error.
- </AlertDescription>
- </Alert>
- </div>
- );
+ redirect("/");
}
return (
diff --git a/client/src/app/settings/subscription/page.tsx b/client/src/app/settings/subscription/page.tsx
index 1403d2a3e..c557a50f6 100644
--- a/client/src/app/settings/subscription/page.tsx
+++ b/client/src/app/settings/subscription/page.tsx
@@ -1,6 +1,11 @@
"use client";
-import { Card, CardContent } from "@/components/ui/card";
+import {
+ Card,
+ CardContent,
+ CardDescription,
+ CardTitle,
+} from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { ProPlan } from "./components/ProPlan";
import { useStripeSubscription } from "./utils/useStripeSubscription";
@@ -10,6 +15,7 @@ import { TrialPlan } from "./components/TrialPlan";
import { ExpiredTrialPlan } from "./components/ExpiredTrialPlan";
import { useSetPageTitle } from "../../../hooks/useSetPageTitle";
import { FreePlan } from "./components/FreePlan";
+import { Building } from "lucide-react";
export default function SubscriptionPage() {
useSetPageTitle("Rybbit · Subscription");
@@ -31,6 +37,20 @@ export default function SubscriptionPage() {
);
}
+ if (organizations[0].role !== "owner") {
+ return (
+ <Card className="p-6 flex flex-col items-center text-center w-full">
+ <div className="mx-auto w-12 h-12 rounded-full bg-primary/10 flex items-center justify-center mb-4">
+ <Building className="h-6 w-6 text-primary" />
+ </div>
+ <CardTitle className="mb-2 text-xl">Not an owner</CardTitle>
+ <CardDescription className="mb-6">
+ Only the owner of the organization can manage the subscription.
+ </CardDescription>
+ </Card>
+ );
+ }
+
if (!activeSubscription) {
return <ExpiredTrialPlan />;
}
diff --git a/client/src/components/DisabledOverlay.tsx b/client/src/components/DisabledOverlay.tsx
index 9eb280965..8b5d9e9d5 100644
--- a/client/src/components/DisabledOverlay.tsx
+++ b/client/src/components/DisabledOverlay.tsx
@@ -3,6 +3,8 @@ import Link from "next/link";
import React, { ReactNode } from "react";
import { useStripeSubscription } from "../app/settings/subscription/utils/useStripeSubscription";
import { Button } from "./ui/button";
+import { useCurrentSite } from "../api/admin/sites";
+import { DEFAULT_EVENT_LIMIT } from "../app/settings/subscription/utils/constants";
interface DisabledOverlayProps {
children: ReactNode;
@@ -13,7 +15,7 @@ interface DisabledOverlayProps {
style?: React.CSSProperties;
}
-function defaultMessage(message: string) {
+function ownerMessage(message: string) {
return (
<div className="bg-neutral-900 rounded-lg border border-neutral-700 shadow-xl flex items-center gap-3 p-4">
<Crown className="h-5 w-5 text-amber-500 flex-shrink-0" />
@@ -32,6 +34,21 @@ function defaultMessage(message: string) {
);
}
+function userMessage(message: string) {
+ return (
+ <div className="bg-neutral-900 rounded-lg border border-neutral-700 shadow-xl flex items-center gap-3 p-4">
+ <Crown className="h-5 w-5 text-amber-500 flex-shrink-0" />
+ <div className="flex-1">
+ <p className="text-sm text-muted-foreground">
+ Ask your organization owner to upgrade to{" "}
+ <span className="font-medium text-foreground">Pro</span> to unlock{" "}
+ {message}
+ </p>
+ </div>
+ </div>
+ );
+}
+
export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
children,
message = "this feature",
@@ -40,9 +57,9 @@ export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
showMessage = true,
style,
}) => {
- const { data: subscription } = useStripeSubscription();
+ const site = useCurrentSite();
- const disabled = subscription?.planName === "free";
+ const disabled = site?.eventLimit === DEFAULT_EVENT_LIMIT;
if (!disabled) {
return <>{children}</>;
@@ -65,7 +82,7 @@ export const DisabledOverlay: React.FC<DisabledOverlayProps> = ({
>
{showMessage && (
<div className="flex items-center justify-center">
- {defaultMessage(message)}
+ {site.isOwner ? ownerMessage(message) : userMessage(message)}
</div>
)}
</div>
diff --git a/client/src/components/FreePlanBanner.tsx b/client/src/components/FreePlanBanner.tsx
index 1469f8976..b56638440 100644
--- a/client/src/components/FreePlanBanner.tsx
+++ b/client/src/components/FreePlanBanner.tsx
@@ -1,16 +1,11 @@
import { ArrowRight } from "lucide-react";
import Link from "next/link";
-import { usePathname } from "next/navigation";
-import { useGetSites } from "../api/admin/sites";
+import { useCurrentSite } from "../api/admin/sites";
import { DEFAULT_EVENT_LIMIT } from "../app/settings/subscription/utils/constants";
import { Button } from "./ui/button";
export function FreePlanBanner() {
- const { data: sites } = useGetSites();
- const pathname = usePathname();
- const site = sites?.find(
- (site) => site.siteId === Number(pathname.split("/")[1])
- );
+ const site = useCurrentSite();
if (!site) return null;
@@ -29,11 +24,13 @@ export function FreePlanBanner() {
{formatNumber(site.eventLimit)} events
</span>
</div>
- <Button variant="success" size="sm" asChild>
- <Link href="/subscribe">
- Upgrade <ArrowRight className="ml-1 h-3 w-3" />
- </Link>
- </Button>
+ {site.isOwner && (
+ <Button variant="success" size="sm" asChild>
+ <Link href="/subscribe">
+ Upgrade <ArrowRight className="ml-1 h-3 w-3" />
+ </Link>
+ </Button>
+ )}
</div>
);
}
diff --git a/client/src/hooks/useAdminUsers.ts b/client/src/hooks/useAdminUsers.ts
index 895549de8..971662953 100644
--- a/client/src/hooks/useAdminUsers.ts
+++ b/client/src/hooks/useAdminUsers.ts
@@ -5,8 +5,10 @@ import { useQuery } from "@tanstack/react-query";
import { authClient } from "@/lib/auth";
import { ColumnFiltersState, SortingState } from "@tanstack/react-table";
import { AdminUser } from "@/types/admin";
+import { useRouter } from "next/navigation";
export function useAdminUsers() {
+ const router = useRouter();
// Table state
const [sorting, setSorting] = useState<SortingState>([
{ id: "createdAt", desc: true },
@@ -86,6 +88,8 @@ export function useAdminUsers() {
await authClient.admin.impersonateUser({
userId,
});
+ console.log("impersonated");
+ router.push("/");
// Redirect to home after impersonation
return true;
} catch (err) {