11import { ChevronRightIcon , ExternalLinkIcon , SearchIcon } from "lucide-react" ;
2- import { useCallback , useEffect , useMemo , useState } from "react" ;
2+ import { useCallback , useEffect , useId , useMemo , useState } from "react" ;
33
44import {
55 decodeThirdPartyLicenseManifest ,
@@ -11,7 +11,6 @@ import {
1111} from "@t3tools/shared/thirdPartyLicenses" ;
1212
1313import { Button } from "../ui/button" ;
14- import { Collapsible , CollapsiblePanel , CollapsibleTrigger } from "../ui/collapsible" ;
1514import { InputGroup , InputGroupAddon , InputGroupInput } from "../ui/input-group" ;
1615import { Tooltip , TooltipPopup , TooltipTrigger } from "../ui/tooltip" ;
1716import { SettingsPageContainer , SettingsSection } from "./settingsLayout" ;
@@ -21,17 +20,26 @@ type LicenseManifestState =
2120 | { readonly status : "error" ; readonly message : string }
2221 | { readonly status : "ready" ; readonly manifest : ThirdPartyLicenseManifest } ;
2322
24- async function loadLicenseManifest ( signal : AbortSignal ) : Promise < ThirdPartyLicenseManifest > {
23+ let licenseManifestPromise : Promise < ThirdPartyLicenseManifest > | undefined ;
24+
25+ async function fetchLicenseManifest ( ) : Promise < ThirdPartyLicenseManifest > {
2526 const response = await fetch (
2627 `${ import . meta. env . BASE_URL . replace ( / \/ $ / , "" ) } /third-party-licenses.json` ,
27- { signal } ,
2828 ) ;
2929 if ( ! response . ok ) {
3030 throw new Error ( `The license manifest request failed with status ${ String ( response . status ) } .` ) ;
3131 }
3232 return decodeThirdPartyLicenseManifest ( ( await response . json ( ) ) as unknown ) ;
3333}
3434
35+ function loadLicenseManifest ( ) : Promise < ThirdPartyLicenseManifest > {
36+ licenseManifestPromise ??= fetchLicenseManifest ( ) . catch ( ( error : unknown ) => {
37+ licenseManifestPromise = undefined ;
38+ throw error ;
39+ } ) ;
40+ return licenseManifestPromise ;
41+ }
42+
3543function LicenseNoticeRow ( {
3644 entry,
3745 open,
@@ -41,49 +49,53 @@ function LicenseNoticeRow({
4149 readonly open : boolean ;
4250 readonly onOpenChange : ( open : boolean ) => void ;
4351} ) {
52+ const noticeId = useId ( ) ;
4453 return (
45- < Collapsible open = { open } onOpenChange = { onOpenChange } >
46- < article >
47- < div className = "flex min-h-10 items-center hover:bg-muted/35 sm:min-h-9" >
48- < CollapsibleTrigger className = "group flex min-h-10 min-w-0 flex-1 items-center gap-2.5 px-3 text-left sm:min-h-9 sm:px-4" >
49- < ChevronRightIcon
50- aria-hidden
51- className = "size-3.5 shrink-0 text-muted-foreground transition-transform duration-200 group-data-panel-open:rotate-90"
52- />
53- < span className = "flex min-w-0 flex-1 items-baseline gap-2" >
54- < span className = "truncate font-medium text-foreground" > { entry . name } </ span >
55- { entry . version ? (
56- < code className = "shrink-0 text-xs text-muted-foreground" > { entry . version } </ code >
57- ) : null }
58- </ span >
59- < span className = "max-w-[42%] shrink-0 truncate text-xs text-muted-foreground" >
60- { entry . license } · { formatLicenseBundles ( entry . bundles ) }
61- </ span >
62- </ CollapsibleTrigger >
63- { entry . sourceUrl ? (
64- < Button
65- aria-label = { `View project source for ${ entry . name } ` }
66- className = "me-3 shrink-0 sm:me-4"
67- render = { < a href = { entry . sourceUrl } rel = "noreferrer noopener" target = "_blank" /> }
68- size = "icon-micro"
69- title = "Project source"
70- variant = "ghost-muted"
71- >
72- < ExternalLinkIcon aria-hidden className = "size-3" />
73- </ Button >
74- ) : null }
54+ < article >
55+ < div className = "flex min-h-10 items-center hover:bg-muted/35 sm:min-h-9" >
56+ < button
57+ type = "button"
58+ aria-expanded = { open }
59+ aria-controls = { open ? noticeId : undefined }
60+ onClick = { ( ) => onOpenChange ( ! open ) }
61+ className = "flex min-h-10 min-w-0 flex-1 items-center gap-2.5 px-3 text-left sm:min-h-9 sm:px-4"
62+ >
63+ < ChevronRightIcon
64+ aria-hidden
65+ data-open = { open ? "" : undefined }
66+ className = "size-3.5 shrink-0 text-muted-foreground transition-transform duration-200 data-open:rotate-90"
67+ />
68+ < span className = "flex min-w-0 flex-1 items-baseline gap-2" >
69+ < span className = "truncate font-medium text-foreground" > { entry . name } </ span >
70+ { entry . version ? (
71+ < code className = "shrink-0 text-xs text-muted-foreground" > { entry . version } </ code >
72+ ) : null }
73+ </ span >
74+ < span className = "max-w-[42%] shrink-0 truncate text-xs text-muted-foreground" >
75+ { entry . license } · { formatLicenseBundles ( entry . bundles ) }
76+ </ span >
77+ </ button >
78+ { entry . sourceUrl ? (
79+ < Button
80+ aria-label = { `View project source for ${ entry . name } ` }
81+ className = "me-3 shrink-0 sm:me-4"
82+ render = { < a href = { entry . sourceUrl } rel = "noreferrer noopener" target = "_blank" /> }
83+ size = "icon-micro"
84+ title = "Project source"
85+ variant = "ghost-muted"
86+ >
87+ < ExternalLinkIcon aria-hidden className = "size-3" />
88+ </ Button >
89+ ) : null }
90+ </ div >
91+ { open ? (
92+ < div id = { noticeId } className = "px-9 pt-1 pb-4 sm:px-10" >
93+ < pre className = "max-w-[76ch] whitespace-pre-wrap break-words font-mono text-xs/5 text-foreground/80" >
94+ { entry . noticeText }
95+ </ pre >
7596 </ div >
76- < CollapsiblePanel >
77- { open ? (
78- < div className = "px-9 pt-1 pb-4 sm:px-10" >
79- < pre className = "max-w-[76ch] whitespace-pre-wrap break-words font-mono text-xs/5 text-foreground/80" >
80- { entry . noticeText }
81- </ pre >
82- </ div >
83- ) : null }
84- </ CollapsiblePanel >
85- </ article >
86- </ Collapsible >
97+ ) : null }
98+ </ article >
8799 ) ;
88100}
89101
@@ -198,19 +210,23 @@ export function OpenSourceLicensesPanel() {
198210 const [ requestVersion , setRequestVersion ] = useState ( 0 ) ;
199211
200212 useEffect ( ( ) => {
201- const controller = new AbortController ( ) ;
213+ let cancelled = false ;
202214 setState ( { status : "loading" } ) ;
203- void loadLicenseManifest ( controller . signal ) . then (
204- ( manifest ) => setState ( { status : "ready" , manifest } ) ,
215+ void loadLicenseManifest ( ) . then (
216+ ( manifest ) => {
217+ if ( ! cancelled ) setState ( { status : "ready" , manifest } ) ;
218+ } ,
205219 ( error : unknown ) => {
206- if ( controller . signal . aborted ) return ;
220+ if ( cancelled ) return ;
207221 setState ( {
208222 status : "error" ,
209223 message : error instanceof Error ? error . message : "The license manifest could not load." ,
210224 } ) ;
211225 } ,
212226 ) ;
213- return ( ) => controller . abort ( ) ;
227+ return ( ) => {
228+ cancelled = true ;
229+ } ;
214230 } , [ requestVersion ] ) ;
215231
216232 const entries = state . status === "ready" ? state . manifest . entries : [ ] ;
@@ -247,7 +263,11 @@ export function OpenSourceLicensesPanel() {
247263 key = { entryKey }
248264 entry = { entry }
249265 open = { openEntryKey === entryKey }
250- onOpenChange = { ( open ) => setOpenEntryKey ( open ? entryKey : null ) }
266+ onOpenChange = { ( open ) =>
267+ setOpenEntryKey ( ( current ) =>
268+ open ? entryKey : current === entryKey ? null : current ,
269+ )
270+ }
251271 />
252272 ) ;
253273 } )
0 commit comments