11import { Await , useLoaderData } from "react-router" ;
22import { type LoaderFunctionArgs } from "react-router" ;
3- import { DapperTs } from "@thunderstore/dapper-ts" ;
4- import {
5- getPublicEnvVariables ,
6- getSessionTools ,
7- } from "cyberstorm/security/publicEnvVariables" ;
83import { SkeletonBox } from "@thunderstore/cyberstorm" ;
94import { Suspense } from "react" ;
105import "./Changelog.css" ;
6+ import { handleLoaderError } from "cyberstorm/utils/errors/handleLoaderError" ;
7+ import { createNotFoundMapping } from "cyberstorm/utils/errors/loaderMappings" ;
8+ import { throwUserFacingPayloadResponse } from "cyberstorm/utils/errors/userFacingErrorResponse" ;
9+ import {
10+ NimbusAwaitErrorElement ,
11+ NimbusDefaultRouteErrorBoundary ,
12+ } from "cyberstorm/utils/errors/NimbusErrorBoundary" ;
13+ import { getLoaderTools } from "cyberstorm/utils/getLoaderTools" ;
1114
1215export async function loader ( { params } : LoaderFunctionArgs ) {
1316 if ( params . namespaceId && params . packageId ) {
14- const publicEnvVariables = getPublicEnvVariables ( [ "VITE_API_URL" ] ) ;
15- const dapper = new DapperTs ( ( ) => {
16- return {
17- apiHost : publicEnvVariables . VITE_API_URL ,
18- sessionId : undefined ,
19- } ;
20- } ) ;
21- return {
22- changelog : dapper . getPackageChangelog (
17+ const { dapper } = getLoaderTools ( ) ;
18+ try {
19+ const changelog = await dapper . getPackageChangelog (
2320 params . namespaceId ,
2421 params . packageId
25- ) ,
26- } ;
22+ ) ;
23+
24+ return {
25+ changelog,
26+ } ;
27+ } catch ( error ) {
28+ handleLoaderError ( error , {
29+ mappings : [
30+ createNotFoundMapping (
31+ "Changelog not available." ,
32+ "We could not find a changelog for this package."
33+ ) ,
34+ ] ,
35+ } ) ;
36+ }
2737 }
28- return {
29- status : "error" ,
30- message : "Failed to load changelog" ,
31- changelog : { html : "" } ,
32- } ;
38+ throwUserFacingPayloadResponse ( {
39+ headline : "Changelog not available." ,
40+ description : "We could not find a changelog for this package." ,
41+ category : "not_found" ,
42+ status : 404 ,
43+ } ) ;
3344}
3445
35- export async function clientLoader ( { params } : LoaderFunctionArgs ) {
46+ export function clientLoader ( { params } : LoaderFunctionArgs ) {
3647 if ( params . namespaceId && params . packageId ) {
37- const tools = getSessionTools ( ) ;
38- const dapper = new DapperTs ( ( ) => {
39- return {
40- apiHost : tools ?. getConfig ( ) . apiHost ,
41- sessionId : tools ?. getConfig ( ) . sessionId ,
42- } ;
43- } ) ;
48+ const { dapper } = getLoaderTools ( ) ;
4449 return {
4550 changelog : dapper . getPackageChangelog (
4651 params . namespaceId ,
4752 params . packageId
4853 ) ,
4954 } ;
5055 }
51- return {
52- status : "error" ,
53- message : "Failed to load changelog" ,
54- changelog : { html : "" } ,
55- } ;
56+ throwUserFacingPayloadResponse ( {
57+ headline : "Changelog not available." ,
58+ description : "We could not find a changelog for this package." ,
59+ category : "not_found" ,
60+ status : 404 ,
61+ } ) ;
5662}
5763
5864export default function Changelog ( ) {
59- const { status, message, changelog } = useLoaderData <
60- typeof loader | typeof clientLoader
61- > ( ) ;
65+ const { changelog } = useLoaderData < typeof loader | typeof clientLoader > ( ) ;
6266
63- if ( status === "error" ) return < div > { message } </ div > ;
6467 return (
6568 < Suspense
6669 fallback = { < SkeletonBox className = "package-changelog__skeleton" /> }
6770 >
68- < Await
69- resolve = { changelog }
70- errorElement = { < div > Error occurred while loading changelog</ div > }
71- >
71+ < Await resolve = { changelog } errorElement = { < NimbusAwaitErrorElement /> } >
7272 { ( resolvedValue ) => (
7373 < >
7474 < div className = "markdown-wrapper" >
@@ -83,3 +83,7 @@ export default function Changelog() {
8383 </ Suspense >
8484 ) ;
8585}
86+
87+ export function ErrorBoundary ( ) {
88+ return < NimbusDefaultRouteErrorBoundary /> ;
89+ }
0 commit comments