import { ChevronRightIcon, ExternalLinkIcon } from "lucide-react"; import { useAtomRefresh, useAtomValue } from "@effect/atom-react"; import * as Cause from "effect/Cause"; import * as Data from "effect/Data"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; import { FetchHttpClient, HttpClient } from "effect/http"; import { Atom } from "effect/reactivity"; import { useState } from "react"; import { decodeThirdPartyLicenseManifest, filterThirdPartyLicenseEntries, formatLicenseBundles, thirdPartyLicenseEntryKey, type ThirdPartyLicenseEntry, } from "@t3tools/shared/thirdPartyLicenses"; import { Button } from "../ui/button"; import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "../ui/collapsible"; import { Badge } from "../ui/badge"; import { SettingsLoadingRows, SettingsNoMatches, SettingsPageContainer, SettingsSearchField, SettingsSection, SettingsToolbar, } from "./settingsLayout"; class LicenseManifestLoadError extends Data.TaggedError("LicenseManifestLoadError")<{ readonly message: string; }> {} // Not keepAlive: a failed load must not stick for the session, so each visit to the page loads // the manifest again (a static asset the HTTP cache serves). const licenseManifestAtom = Atom.make( Effect.gen(function* () { const response = yield* HttpClient.get( `${import.meta.env.BASE_URL.replace(/\/$/, "")}/third-party-licenses.json`, ); if (response.status < 200 || response.status >= 300) { return yield* new LicenseManifestLoadError({ message: `The license manifest request failed with status ${String(response.status)}.`, }); } const body = yield* response.json; return yield* Effect.try({ try: () => decodeThirdPartyLicenseManifest(body), catch: (error) => new LicenseManifestLoadError({ message: error instanceof Error ? error.message : "The license manifest could not load.", }), }); }).pipe( Effect.catchTags({ HttpClientError: (error) => Effect.fail(new LicenseManifestLoadError({ message: error.message })), }), Effect.timeoutOrElse({ duration: "30 seconds", orElse: () => Effect.fail( new LicenseManifestLoadError({ message: "The license manifest took too long to load." }), ), }), Effect.provide(FetchHttpClient.layer), ), ).pipe(Atom.withLabel("settings:open-source-licenses")); /** Name over version and bundles, a license chip, and a chevron; the notice expands in place. */ function LicenseNoticeRow({ entry, open, onOpenChange, }: { readonly entry: ThirdPartyLicenseEntry; readonly open: boolean; readonly onOpenChange: (open: boolean) => void; }) { return (
{entry.name} {[entry.version, formatLicenseBundles(entry.bundles)].filter(Boolean).join(" · ")} {entry.license} {open ? (
{entry.sourceUrl ? ( ) : null}
                {entry.noticeText}
              
) : null}
); } /** T3 Code's own repository and license, above the third-party notices. */ function ProjectSourceSection() { return ( T3 Code pingdotgg/t3code MIT ); } function LicenseManifestError({ message, onRetry }: { message: string; onRetry: () => void }) { return (

Open-source notices are unavailable

{message}

); } export function OpenSourceLicensesPanel() { const result = useAtomValue(licenseManifestAtom); const retry = useAtomRefresh(licenseManifestAtom); const [query, setQuery] = useState(""); const [openEntryKey, setOpenEntryKey] = useState(null); const manifest = result._tag === "Success" ? result.value : null; const entries = manifest?.entries ?? []; const filteredEntries = filterThirdPartyLicenseEntries(entries, query); const errorMessage = result._tag === "Failure" && !result.waiting ? Option.match(Cause.findErrorOption(result.cause), { onNone: () => "The license manifest could not load.", onSome: (error) => error.message, }) : null; return ( {manifest ? (
{filteredEntries.length > 0 ? ( filteredEntries.map((entry) => { const entryKey = thirdPartyLicenseEntryKey(entry); return ( setOpenEntryKey(open ? entryKey : null)} /> ); }) ) : ( setQuery("")}> No licenses match “{query.trim()}”. )}
) : errorMessage !== null ? ( ) : ( )}
); }