Skip to content

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

Refactor AppSidebar and RybbitLogo components for improved admin access and loading behavior

Bill Yang8da5291197b1
diff --git a/client/src/components/AppSidebar.tsx b/client/src/components/AppSidebar.tsx
index 194096007..5c6fd9c09 100644
--- a/client/src/components/AppSidebar.tsx
+++ b/client/src/components/AppSidebar.tsx
@@ -10,10 +10,27 @@ import { IS_CLOUD } from "../lib/const";
import { cn } from "../lib/utils";
import { RybbitLogo } from "./RybbitLogo";
import { ThemeSwitcher } from "./ThemeSwitcher";
+import { authClient } from "../lib/auth";
-function AppSidebarContent() {
+function AdminLink({ isExpanded }: { isExpanded: boolean }) {
const pathname = usePathname();
const { isAdmin } = useAdminPermission();
+ if (!IS_CLOUD || !isAdmin) return null;
+
+ return (
+ <SidebarLink
+ href="/admin"
+ icon={<ShieldUser className="w-5 h-5" />}
+ label="Admin"
+ active={pathname.startsWith("/admin")}
+ expanded={isExpanded}
+ />
+ );
+}
+
+function AppSidebarContent() {
+ const pathname = usePathname();
+ const { data: session } = authClient.useSession();
const [isExpanded, setIsExpanded] = useState(false);
const embed = useEmbedablePage();
@@ -46,15 +63,7 @@ function AppSidebarContent() {
active={pathname.startsWith("/uptime")}
expanded={isExpanded}
/> */}
- {isAdmin && IS_CLOUD && (
- <SidebarLink
- href="/admin"
- icon={<ShieldUser className="w-5 h-5" />}
- label="Admin"
- active={pathname.startsWith("/admin")}
- expanded={isExpanded}
- />
- )}
+ {session?.user.role === "admin" && <AdminLink isExpanded={isExpanded} />}
</div>
<div className="flex flex-col items-start gap-2 w-full">
<div className={cn("flex items-center w-full px-0.5", isExpanded ? "justify-start" : "hidden")}>
diff --git a/client/src/components/RybbitLogo.tsx b/client/src/components/RybbitLogo.tsx
index 64890082a..a13c48210 100644
--- a/client/src/components/RybbitLogo.tsx
+++ b/client/src/components/RybbitLogo.tsx
@@ -1,11 +1,17 @@
import Image from "next/image";
+import { useEffect, useState } from "react";
import { useWhiteLabel } from "../hooks/useIsWhiteLabel";
import { Skeleton } from "./ui/skeleton";
export function RybbitLogo({ width = 32, height = 32 }: { width?: number; height?: number }) {
const { whiteLabelImage, isPending } = useWhiteLabel();
+ const [mounted, setMounted] = useState(false);
- if (isPending) {
+ useEffect(() => {
+ setMounted(true);
+ }, []);
+
+ if (!mounted || isPending) {
return <Skeleton style={{ width, height }} />;
}
@@ -15,7 +21,7 @@ export function RybbitLogo({ width = 32, height = 32 }: { width?: number; height
return (
<Image
- src={whiteLabelImage || "/rybbit.svg"}
+ src="/rybbit.svg"
alt="Rybbit"
width={width}
height={height}
@@ -26,7 +32,13 @@ export function RybbitLogo({ width = 32, height = 32 }: { width?: number; height
export function RybbitTextLogo({ width = 150, height = 34 }: { width?: number; height?: number }) {
const { whiteLabelImage, isPending } = useWhiteLabel();
- if (isPending) {
+ const [mounted, setMounted] = useState(false);
+
+ useEffect(() => {
+ setMounted(true);
+ }, []);
+
+ if (!mounted || isPending) {
return <Skeleton style={{ width, height }} />;
}
@@ -36,7 +48,7 @@ export function RybbitTextLogo({ width = 150, height = 34 }: { width?: number; h
return (
<Image
- src={whiteLabelImage || "/rybbit-text.svg"}
+ src="/rybbit-text.svg"
alt="Rybbit"
width={width}
height={height}
diff --git a/client/src/lib/subscription/useStripeSubscription.ts b/client/src/lib/subscription/useStripeSubscription.ts
index de31d06b8..9522c029a 100644
--- a/client/src/lib/subscription/useStripeSubscription.ts
+++ b/client/src/lib/subscription/useStripeSubscription.ts
@@ -1,6 +1,7 @@
import { useQuery, UseQueryResult } from "@tanstack/react-query";
import { authClient } from "@/lib/auth";
import { BACKEND_URL, IS_CLOUD } from "../const";
+import { authedFetch } from "../../api/utils";
export interface SubscriptionData {
id: string;
@@ -20,7 +21,7 @@ export interface SubscriptionData {
isOverride?: boolean;
}
-export function useStripeSubscription(): UseQueryResult<SubscriptionData, Error> {
+export function useStripeSubscription(): UseQueryResult<SubscriptionData | undefined, Error> {
const { data: activeOrg } = authClient.useActiveOrganization();
const fetchSubscription = async () => {
@@ -28,30 +29,13 @@ export function useStripeSubscription(): UseQueryResult<SubscriptionData, Error>
return undefined;
}
- const response = await fetch(`${BACKEND_URL}/stripe/subscription?organizationId=${activeOrg.id}`, {
- method: "GET",
- headers: {
- "Content-Type": "application/json",
- },
- credentials: "include",
- });
-
- if (!response.ok) {
- if (response.status === 401) {
- return null;
- } else {
- const errorData = await response.json();
- throw new Error(errorData.error || `Error: ${response.status}`);
- }
- }
-
- return await response.json();
+ return authedFetch<SubscriptionData>(`/stripe/subscription?organizationId=${activeOrg.id}`);
};
- return useQuery<SubscriptionData>({
+ return useQuery<SubscriptionData | undefined>({
queryKey: ["stripe-subscription", activeOrg?.id],
queryFn: fetchSubscription,
- staleTime: 5 * 60 * 1000, // 5 minutes
+ staleTime: 5 * 60 * 1000,
retry: false,
enabled: !!activeOrg,
});
diff --git a/server/src/index.ts b/server/src/index.ts
index 0b52e8448..8a4cdfcb5 100644
--- a/server/src/index.ts
+++ b/server/src/index.ts
@@ -250,6 +250,7 @@ const ANALYTICS_ROUTES = [
"/api/error-events/",
"/api/session-replay/",
"/api/gsc/data/",
+ "/api/gsc/status/",
];
server.addHook("onRequest", async (request, reply) => {