Skip to content

Commit dc065a2

Browse files
committed
packageEdit: use two-phase package listing fetch
Attempt to fetch package listing data first as unauthenticated user in hopes of hitting cache. If this results in 404, retry as authenticated user in hopes of having permissions to view a rejected package listing. Since we block rendering be awaiting permissions and listing, we might as well trigger filter loading before we know whether user can access the page or not. A little bit on overfetch is preferred to awaiting in phases.
1 parent 949a230 commit dc065a2

File tree

1 file changed

+12
-25
lines changed

1 file changed

+12
-25
lines changed

apps/cyberstorm-remix/app/p/packageEdit.tsx

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import {
1818
type PackageListingUpdateRequestData,
1919
packageUnlist,
2020
} from "@thunderstore/thunderstore-api";
21-
import { DapperTs } from "@thunderstore/dapper-ts";
2221
import { type OutletContextShape } from "~/root";
23-
import { getSessionTools } from "cyberstorm/security/publicEnvVariables";
2422
import { PageHeader } from "~/commonComponents/PageHeader/PageHeader";
2523
import { useStrongForm } from "cyberstorm/utils/StrongForm/useStrongForm";
2624
import { useReducer } from "react";
2725
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
2826
import { faBan, faCheck } from "@fortawesome/pro-solid-svg-icons";
2927
import { ApiAction } from "@thunderstore/ts-api-react-actions";
28+
import { getDapperForRequest } from "cyberstorm/utils/dapperSingleton";
29+
import { getPrivateListing } from "app/p/listingUtils";
3030

3131
export const meta: MetaFunction<typeof clientLoader> = ({ data }) => {
3232
return [
@@ -44,28 +44,23 @@ export async function loader({ params }: LoaderFunctionArgs) {
4444
return undefined;
4545
}
4646

47-
// TODO: Needs to check if package is available for the logged in user
48-
export async function clientLoader({ params }: LoaderFunctionArgs) {
47+
export async function clientLoader({ params, request }: LoaderFunctionArgs) {
4948
const { communityId, namespaceId, packageId } = params;
5049

5150
if (!communityId || !namespaceId || !packageId) {
5251
throw package404;
5352
}
5453

5554
try {
56-
const tools = getSessionTools();
57-
const dapper = new DapperTs(() => {
58-
return {
59-
apiHost: tools?.getConfig().apiHost,
60-
sessionId: tools?.getConfig().sessionId,
61-
};
62-
});
55+
const dapper = getDapperForRequest(request);
6356

64-
const permissions = await dapper.getPackagePermissions(
65-
communityId,
66-
namespaceId,
67-
packageId
68-
);
57+
// Fetch everything at once for faster page load, even if we may
58+
// overfetch in case we lack authentication/authorization.
59+
const [listing, permissions, filters] = await Promise.all([
60+
getPrivateListing(dapper, { communityId, namespaceId, packageId }),
61+
dapper.getPackagePermissions(communityId, namespaceId, packageId),
62+
dapper.getCommunityFilters(communityId),
63+
]);
6964

7065
if (!permissions) {
7166
throw new Response("Unauthenticated", { status: 401 });
@@ -76,15 +71,7 @@ export async function clientLoader({ params }: LoaderFunctionArgs) {
7671
throw new Response("Unauthorized", { status: 403 });
7772
}
7873

79-
return {
80-
listing: await dapper.getPackageListingDetails(
81-
communityId,
82-
namespaceId,
83-
packageId
84-
),
85-
filters: await dapper.getCommunityFilters(communityId),
86-
permissions: permissions,
87-
};
74+
return { listing, permissions, filters };
8875
} catch (error) {
8976
throw error instanceof ApiError ? package404 : error;
9077
}

0 commit comments

Comments
 (0)