Openstatus
www.openstatus.dev
1"use client";
2
3import { useTRPC } from "@/lib/trpc/client";
4import { useQuery } from "@tanstack/react-query";
5import { useParams } from "next/navigation";
6import { useEffect, useState } from "react";
7
8export function usePathnamePrefix() {
9 const trpc = useTRPC();
10 const { domain } = useParams<{ domain: string }>();
11 const { data: page } = useQuery({
12 ...trpc.statusPage.get.queryOptions({ slug: domain }),
13 });
14 const [prefix, setPrefix] = useState("");
15
16 useEffect(() => {
17 if (typeof window !== "undefined") {
18 const hostnames = window.location.hostname.split(".");
19 const pathnames = window.location.pathname.split("/");
20 const isCustomDomain = window.location.hostname === page?.customDomain;
21
22 if (
23 isCustomDomain ||
24 (hostnames.length > 2 &&
25 hostnames[0] !== "www" &&
26 !window.location.hostname.endsWith(".vercel.app"))
27 ) {
28 setPrefix("");
29 } else {
30 setPrefix(pathnames[1] || "");
31 }
32 }
33 }, [page?.customDomain]);
34
35 return prefix;
36}