Skip to content

🎥 Make videos programmatically with React

3.6 KB
Raw Blame
import { isInsideStudio } from "@remotion/studio-protocol";
import Negotiator from "negotiator";
import { useEffect, useState } from "react";
import { Link, Outlet, useLocation } from "react-router";
import { catalog } from "../../lib/element-payload";
import { getCatalogMarkdown, getElementMarkdown } from "../../lib/markdown";
import { StudioActions } from "../components/studio-actions";
import type { Route } from "./+types/layout";

export const middleware: Route.MiddlewareFunction[] = [
  async ({ request, pattern, params }, next) => {
    if (
      (request.method !== "GET" && request.method !== "HEAD") ||
      new URL(request.url).pathname.endsWith(".data")
    ) {
      return next();
    }

    const markdownType = "text/markdown; charset=utf-8";
    const preferredType = new Negotiator({
      headers: { accept: request.headers.get("accept") ?? "" },
    }).mediaType(["text/html", markdownType]);

    let markdown: string | null = null;
    if (preferredType === markdownType) {
      switch (pattern) {
        case "/":
          markdown = getCatalogMarkdown(null);
          break;
        case "captions":
          markdown = getCatalogMarkdown("Captions");
          break;
        case "elements/:slug": {
          const entry = catalog.find(
            ({ definition }) => definition.slug === params.slug,
          );
          markdown = entry ? getElementMarkdown(entry) : null;
          break;
        }
      }
    }

    const response =
      markdown === null
        ? await next()
        : new Response(markdown, {
            headers: { "Content-Type": markdownType },
          });
    response.headers.append("Vary", "Accept");
    return response;
  },
];

export default function LibraryLayout() {
  const location = useLocation();
  const [hydrated, setHydrated] = useState(false);
  const search = hydrated ? location.search : "";
  const [insideStudio, setInsideStudio] = useState(false);

  const isCaptionPicker =
    insideStudio &&
    new URLSearchParams(search).get("remotion-studio-context") === "captions";

  useEffect(() => {
    setHydrated(true);
    setInsideStudio(isInsideStudio());
  }, []);

  return (
    <div
      data-embedded={insideStudio}
      data-caption-picker={isCaptionPicker}
      className="group/site isolate flex min-h-dvh flex-col"
    >
      <a
        className="absolute -top-24 left-6 z-50 bg-white p-3 focus:top-3"
        href="#main"
      >
        Skip to content
      </a>
      {!insideStudio && (
        <header className="border-b border-neutral-950/10">
          <div className="mx-auto flex min-h-18 max-w-7xl flex-wrap items-center justify-between gap-x-6 gap-y-3 px-6 py-4 sm:px-8 lg:px-12">
            <div className="min-w-0 shrink-0 grow text-lg font-semibold tracking-tight">
              <Link
                className="inline-flex items-center gap-2.5"
                aria-label="Homepage"
                to={{ pathname: "/", search }}
              >
                <img src="/favicon.svg" alt="" className="size-6 shrink-0" />
                Studio Supply
              </Link>
            </div>
            {(location.pathname === "/" ||
              location.pathname.replace(/\/$/, "") === "/captions") && (
              <div className="w-full min-w-0 sm:w-auto sm:max-w-80">
                <StudioActions entry={null} variant="default" />
              </div>
            )}
          </div>
        </header>
      )}
      <div className="mx-auto w-full max-w-7xl flex-1 px-6 group-data-[caption-picker=true]/site:max-w-none group-data-[caption-picker=true]/site:px-3 sm:px-8 lg:px-12">
        <Outlet context={{ insideStudio, isCaptionPicker, search }} />
      </div>
    </div>
  );
}