|
1 | | -import { getServerRPCClient } from "@/lib/orpc-server"; |
2 | | -import { WebsiteLayoutClient } from "./_components/website-layout-client"; |
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useQueryClient } from "@tanstack/react-query"; |
| 4 | +import { useAtom } from "jotai"; |
| 5 | +import { useParams, usePathname } from "next/navigation"; |
| 6 | +import { toast } from "sonner"; |
| 7 | +import { WebsiteErrorState } from "@/components/website-error-state"; |
| 8 | +import { useTrackingSetup } from "@/hooks/use-tracking-setup"; |
| 9 | +import { useWebsite } from "@/hooks/use-websites"; |
| 10 | +import { isAnalyticsRefreshingAtom } from "@/stores/jotai/filterAtoms"; |
| 11 | +import { AnalyticsToolbar } from "./_components/analytics-toolbar"; |
| 12 | +import { WebsiteTrackingSetupTab } from "./_components/tabs/tracking-setup-tab"; |
| 13 | + |
| 14 | +const NO_TOOLBAR_ROUTES = [ |
| 15 | + "/assistant", |
| 16 | + "/map", |
| 17 | + "/flags", |
| 18 | + "/databunny", |
| 19 | + "/settings", |
| 20 | + "/users", |
| 21 | + "/agent", |
| 22 | + "/pulse", |
| 23 | +]; |
3 | 24 |
|
4 | 25 | interface WebsiteLayoutProps { |
5 | 26 | children: React.ReactNode; |
6 | | - params: Promise<{ id: string }>; |
7 | 27 | } |
8 | 28 |
|
9 | | -export default async function WebsiteLayout({ |
10 | | - children, |
11 | | - params, |
12 | | -}: WebsiteLayoutProps) { |
13 | | - const { id: websiteId } = await params; |
14 | | - const rpc = await getServerRPCClient(); |
| 29 | +export default function WebsiteLayout({ children }: WebsiteLayoutProps) { |
| 30 | + const { id } = useParams(); |
| 31 | + const websiteId = id as string; |
| 32 | + const pathname = usePathname(); |
| 33 | + const queryClient = useQueryClient(); |
| 34 | + const [isRefreshing, setIsRefreshing] = useAtom(isAnalyticsRefreshingAtom); |
| 35 | + |
| 36 | + const isDemoRoute = pathname?.startsWith("/demo/"); |
| 37 | + const hideToolbar = NO_TOOLBAR_ROUTES.some((route) => |
| 38 | + pathname.includes(route) |
| 39 | + ); |
| 40 | + |
| 41 | + const { |
| 42 | + data: websiteData, |
| 43 | + isLoading: isWebsiteLoading, |
| 44 | + isError: isWebsiteError, |
| 45 | + error: websiteError, |
| 46 | + } = useWebsite(websiteId); |
| 47 | + |
| 48 | + const { isTrackingSetup, isTrackingSetupLoading } = |
| 49 | + useTrackingSetup(websiteId); |
| 50 | + |
| 51 | + if (!id) { |
| 52 | + return <WebsiteErrorState error={{ data: { code: "NOT_FOUND" } }} />; |
| 53 | + } |
| 54 | + |
| 55 | + if (!isWebsiteLoading && isWebsiteError) { |
| 56 | + return <WebsiteErrorState error={websiteError} websiteId={websiteId} />; |
| 57 | + } |
15 | 58 |
|
16 | | - const [website, trackingData] = await Promise.all([ |
17 | | - rpc.websites.getById({ id: websiteId }).catch(() => null), |
18 | | - rpc.websites.isTrackingSetup({ websiteId }).catch(() => null), |
19 | | - ]); |
| 59 | + const isToolbarLoading = |
| 60 | + isWebsiteLoading || |
| 61 | + (!isDemoRoute && (isTrackingSetupLoading || isTrackingSetup === null)); |
| 62 | + |
| 63 | + const isToolbarDisabled = |
| 64 | + !isDemoRoute && (!isTrackingSetup || isToolbarLoading); |
| 65 | + |
| 66 | + const showTrackingSetup = |
| 67 | + !(isDemoRoute || isTrackingSetupLoading) && |
| 68 | + websiteData && |
| 69 | + isTrackingSetup === false; |
| 70 | + |
| 71 | + const handleRefresh = async () => { |
| 72 | + setIsRefreshing(true); |
| 73 | + try { |
| 74 | + await Promise.all([ |
| 75 | + queryClient.invalidateQueries({ queryKey: ["websites", id] }), |
| 76 | + queryClient.invalidateQueries({ |
| 77 | + queryKey: ["websites", "isTrackingSetup", id], |
| 78 | + }), |
| 79 | + queryClient.invalidateQueries({ queryKey: ["dynamic-query", id] }), |
| 80 | + queryClient.invalidateQueries({ |
| 81 | + queryKey: ["batch-dynamic-query", id], |
| 82 | + }), |
| 83 | + ]); |
| 84 | + toast.success("Data refreshed"); |
| 85 | + } catch { |
| 86 | + toast.error("Failed to refresh data"); |
| 87 | + } finally { |
| 88 | + setIsRefreshing(false); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + const renderContent = () => { |
| 93 | + if (hideToolbar) { |
| 94 | + return children; |
| 95 | + } |
| 96 | + |
| 97 | + if (showTrackingSetup) { |
| 98 | + return ( |
| 99 | + <div className="p-4"> |
| 100 | + <WebsiteTrackingSetupTab websiteId={websiteId} /> |
| 101 | + </div> |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return children; |
| 106 | + }; |
20 | 107 |
|
21 | 108 | return ( |
22 | | - <WebsiteLayoutClient |
23 | | - initialTrackingSetup={trackingData?.tracking_setup ?? false} |
24 | | - initialWebsite={website} |
25 | | - websiteId={websiteId} |
26 | | - > |
27 | | - {children} |
28 | | - </WebsiteLayoutClient> |
| 109 | + <div className="flex h-full flex-col overflow-hidden"> |
| 110 | + {!hideToolbar && ( |
| 111 | + <div className="sticky top-0 right-0 left-0 z-50 shrink-0 overscroll-contain bg-background md:top-0 md:left-84"> |
| 112 | + <AnalyticsToolbar |
| 113 | + isDisabled={isToolbarDisabled} |
| 114 | + isLoading={isToolbarLoading} |
| 115 | + isRefreshing={isRefreshing} |
| 116 | + onRefreshAction={handleRefresh} |
| 117 | + websiteId={websiteId} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + )} |
| 121 | + |
| 122 | + <div |
| 123 | + className={ |
| 124 | + hideToolbar |
| 125 | + ? "min-h-0 flex-1" |
| 126 | + : "min-h-0 flex-1 overflow-y-auto overscroll-contain" |
| 127 | + } |
| 128 | + > |
| 129 | + {renderContent()} |
| 130 | + </div> |
| 131 | + </div> |
29 | 132 | ); |
30 | 133 | } |
0 commit comments