Skip to content

Open-source status page, incident response and uptime monitoring

13.0 KB
Raw Blame
"use client";

import { Sidebar as SidebarIcon } from "@openstatus/icons";
import { THEMES, THEME_KEYS } from "@openstatus/theme-store";
import { StatusBanner } from "@openstatus/ui/components/blocks/status-banner";
import {
  Status,
  StatusContent,
  StatusDescription,
  StatusHeader,
  StatusTitle,
} from "@openstatus/ui/components/blocks/status-layout";
import { Button } from "@openstatus/ui/components/ui/button";
import { Input } from "@openstatus/ui/components/ui/input";
import { Separator } from "@openstatus/ui/components/ui/separator";
import { useSidebar } from "@openstatus/ui/components/ui/sidebar";
import { Skeleton } from "@openstatus/ui/components/ui/skeleton";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@openstatus/ui/components/ui/tooltip";
import { cn } from "@openstatus/ui/lib/utils";
import { useQuery } from "@tanstack/react-query";
import { useTheme } from "next-themes";
import { useQueryStates } from "nuqs";
import { useEffect, useState } from "react";

import { Link } from "../../components/common/link";
import {
  Section,
  SectionDescription,
  SectionGroup,
  SectionGroupHeader,
  SectionHeader,
  SectionTitle,
} from "../../components/content/section";
import { recomputeStyles } from "../../components/status-page/floating-button";
import { StatusComponentStatic } from "../../components/status-page/static/status-component-static";
import { ThemePromptButton } from "../../components/themes/theme-agent-actions";
import { useThemeBuilder } from "../../components/themes/theme-builder-provider";
import { ThemePalettePicker } from "../../components/themes/theme-palette-picker";
import { ThemePasteDialog } from "../../components/themes/theme-paste-dialog";
import { ThemeSelect } from "../../components/themes/theme-select";
import { monitors } from "../../data/monitors";
import { defaultLocale } from "../../i18n/config";
import { clearThemeDraft, saveThemeDraft } from "../../lib/theme-draft";
import {
  THEME_SKILL_INSTALL_COMMAND,
  THEME_SKILL_URL,
} from "../../lib/theme-prompt";
import { useTRPC } from "../../lib/trpc/client";
import { searchParamsParsers } from "./search-params";

const MAIN_COLORS = [
  { key: "--primary", label: "Primary" },
  { key: "--success", label: "Operational" },
  { key: "--destructive", label: "Error" },
  { key: "--warning", label: "Degraded" },
  { key: "--info", label: "Maintenance" },
] as const;

// TODO: add keyboard navigation for selection?

export function Client() {
  const { resolvedTheme } = useTheme();
  const [isMounted, setIsMounted] = useState(false);
  const [{ q, t }, setSearchParams] = useQueryStates(searchParamsParsers);
  const theme = t ? THEMES[t as keyof typeof THEMES] : undefined;
  const { toggleSidebar } = useSidebar();
  const builder = useThemeBuilder();

  useEffect(() => {
    setIsMounted(true);
  }, []);

  useEffect(() => {
    if (isMounted && t) {
      recomputeStyles(t);
    }
  }, [t, isMounted]);

  return (
    <SectionGroup>
      <SectionGroupHeader>
        <h1 className="text-2xl font-bold md:text-4xl">
          Status Page Theme Explorer
        </h1>
        <h2 className="text-muted-foreground font-medium md:text-lg">
          View all the openstatus themes for your status page and learn how to
          create your own theme.
        </h2>
      </SectionGroupHeader>
      <Section>
        <SectionHeader>
          <SectionTitle>Explorer</SectionTitle>
          <SectionDescription>
            Search for your favorite status page theme.{" "}
            <Link href="#contribute-theme">Contribute your own?</Link>
          </SectionDescription>
        </SectionHeader>
        <div className="border-border bg-background outline-background sticky top-0 z-10 overflow-hidden rounded-lg border outline-[3px] sm:relative">
          <div className="relative">
            <div className="border-border bg-muted/50 absolute top-0 right-0 rounded-bl-lg border-b border-l px-2 py-0.5 text-[10px]">
              {theme?.name}
            </div>
            <div className="sm:p-8">
              <ThemePlaygroundStatus className="scale-80 sm:scale-100" />
            </div>
          </div>
        </div>
        <div className="flex gap-3">
          <ThemeSelect className="max-w-[125px] min-w-[125px]" />
          <Input
            placeholder={`Search from ${THEME_KEYS.length} themes`}
            value={q ?? ""}
            onChange={(e) => {
              if (e.target.value.length === 0) {
                setSearchParams({ q: null });
              }
              setSearchParams({ q: e.target.value.trim().toLowerCase() });
            }}
          />
          <ThemePalettePicker />
        </div>
        <ul className="grid grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-3">
          {THEME_KEYS.filter((k) => {
            const theme = THEMES[k];
            return (
              theme.author.name
                .toLowerCase()
                .includes(q?.toLowerCase() ?? "") ||
              theme.name.toLowerCase().includes(q?.toLowerCase() ?? "")
            );
          }).map((k) => {
            const theme = THEMES[k];
            const style = isMounted
              ? theme[resolvedTheme as "dark" | "light"]
              : undefined;

            return (
              <li key={k} className="group/theme-card space-y-1.5">
                <div
                  data-active={k === t}
                  data-slot="theme-card"
                  data-theme={k}
                  className="border-border focus:outline-ring/50 focus:ring-ring/50 data-[active=true]:border-ring data-[active=true]:outline-ring/50 relative h-40 cursor-pointer overflow-hidden rounded-md border transition-all outline-none focus:ring-2 data-[active=true]:outline-[3px]"
                  onClick={() => setSearchParams({ t: k })}
                  role="button"
                  tabIndex={0}
                  onKeyDown={(e) => {
                    if (e.key === "Enter" || e.key === " ") {
                      setSearchParams({ t: k });
                    }
                  }}
                >
                  {isMounted ? (
                    <div
                      className="bg-background text-foreground absolute h-full w-full"
                      style={style as React.CSSProperties}
                      inert
                    >
                      <ThemePlaygroundStatus className="pointer-events-none scale-80" />
                    </div>
                  ) : (
                    <Skeleton className="absolute h-full w-full" />
                  )}
                </div>
                <div className="flex items-start justify-between gap-2">
                  <div className="space-y-0.5 truncate">
                    <div className="text-foreground truncate text-sm leading-none font-medium">
                      {theme.name}
                    </div>
                    <div className="font-mono text-xs">
                      <Link
                        href={theme.author.url}
                        target="_blank"
                        rel="noopener noreferrer"
                        className="text-muted-foreground"
                      >
                        by {theme.author.name}
                      </Link>
                    </div>
                  </div>
                  <div className="flex gap-0.5">
                    {MAIN_COLORS.map((color) => {
                      const backgroundColor = style
                        ? style[color.key]
                        : undefined;

                      if (!isMounted) {
                        return (
                          <Skeleton
                            key={color.key}
                            className="size-3.5 rounded-sm"
                          />
                        );
                      }
                      return (
                        <TooltipProvider key={color.key}>
                          <Tooltip>
                            <TooltipTrigger>
                              <div
                                className="bg-muted-foreground size-3.5 rounded-sm border"
                                style={{ backgroundColor }}
                              />
                            </TooltipTrigger>
                            <TooltipContent>{color.label}</TooltipContent>
                          </Tooltip>
                        </TooltipProvider>
                      );
                    })}
                  </div>
                </div>
              </li>
            );
          })}
        </ul>
      </Section>
      <Separator />
      <Section>
        <SectionHeader id="contribute-theme">
          <SectionTitle>Contribute Theme</SectionTitle>
          <SectionDescription>
            Contribute your own theme to the community.
          </SectionDescription>
        </SectionHeader>
        <div className="prose dark:prose-invert prose-sm max-w-none">
          <p>
            A theme is a set of CSS variable overrides for light and dark mode
            in the <code>@openstatus/theme-store</code> package. If you are
            familiar with shadcn, you know the trick (it also lets you override{" "}
            <code>--radius</code>). Three ways to get one:
          </p>
          <ul>
            <li>
              <strong>Build it</strong> in the theme builder and copy it as a
              TypeScript file, JSON or CSS.
            </li>
            <li>
              <strong>Let your agent design it.</strong> Copy the prompt from
              the builder, or install the{" "}
              <Link href={THEME_SKILL_URL}>openstatus-theme skill</Link> with{" "}
              <code>{THEME_SKILL_INSTALL_COMMAND}</code>. Paste the reply into
              the builder to preview it.
            </li>
            <li>
              <strong>Write it by hand</strong> following the existing themes in
              the{" "}
              <Link href="https://github.com/openstatusHQ/openstatus/tree/main/packages/theme-store">
                GitHub directory
              </Link>
              .
            </li>
          </ul>
          <div className="flex flex-wrap gap-2">
            <ThemePromptButton variant="default" />
            <ThemePasteDialog variant="outline" />
            <Button variant="outline" onClick={toggleSidebar}>
              <SidebarIcon className="size-4" />
              Toggle Sidebar
            </Button>
          </div>
          <p>
            Preview the theme from the builder on a full status page (the{" "}
            <code>status</code> slug). Unchanged themes open the community theme
            switcher instead:
          </p>
          <Button
            onClick={() => {
              // NOTE: we use it to display the 'floating-theme' component
              sessionStorage.setItem("community-theme", "true");
              if (builder.isModified) saveThemeDraft(builder.theme);
              else clearThemeDraft();
              window.location.href = `/status/${defaultLocale}`;
            }}
          >
            Test it
          </Button>
          <p>
            Once you are happy, copy the TypeScript file into{" "}
            <code>packages/theme-store/src</code>, register it in{" "}
            <code>index.ts</code> and open a pull request. Or paste the CSS
            variables into your own status page&apos;s custom theme in the
            dashboard, no pull request needed.
          </p>
        </div>
      </Section>
      <Separator />
      <Section>
        <div className="prose dark:prose-invert prose-sm max-w-none">
          <p>
            Why don't we allow custom css styles to be overridden and only
            support themes?
          </p>
          <ul>
            <li>Keep it simple for the user</li>
            <li>Don't end up with a xmas tree</li>
            <li>Keep the theme consistent</li>
            <li>Avoid conflicts with other styles</li>
            <li>
              Keep the theme maintainable (but this will also mean, a change
              will affect all users)
            </li>
          </ul>
        </div>
      </Section>
    </SectionGroup>
  );
}

function ThemePlaygroundStatus({
  className,
  ...props
}: React.ComponentProps<"div"> & {}) {
  const trpc = useTRPC();
  const { data: uptimeData, isLoading } = useQuery(
    trpc.statusPage.getNoopUptime.queryOptions(),
  );
  return (
    // NOTE: we use pointer-events-none to prevent the hover card or tooltip from being interactive - the Portal container is document body and we loose the styles
    <div className={cn("h-full w-full", className)} {...props}>
      <Status variant="success">
        <StatusHeader>
          <StatusTitle>Acme Inc.</StatusTitle>
          <StatusDescription>
            Get informed about our services.
          </StatusDescription>
        </StatusHeader>
        <StatusBanner status="success" />
        <StatusContent>
          {/* TODO: create mock data */}
          <StatusComponentStatic
            status="success"
            data={uptimeData?.data || []}
            monitor={monitors[0]}
            showUptime={true}
            uptime={uptimeData?.uptime}
            isLoading={isLoading}
          />
        </StatusContent>
      </Status>
    </div>
  );
}