traxxx-web/pages/scene/+onBeforeRender.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

import { render } from 'vike/abort'; /* eslint-disable-line import/extensions */
import { fetchScenesById } from '#/src/scenes.js';
import { getRandomCampaigns } from '#/src/campaigns.js';
function getTitle(scene) {
if (scene.title) {
return scene.title;
}
if (scene.actors.length > 0) {
return `Scene with ${scene.actors.map((actor) => actor.name).join(', ')}`;
}
return 'Scene';
}
export async function onBeforeRender(pageContext) {
2024-03-21 01:54:05 +00:00
const [scene] = await fetchScenesById([Number(pageContext.routeParams.sceneId)], {
reqUser: pageContext.user,
includeAssets: true,
includePartOf: true,
2024-03-21 01:54:05 +00:00
actorStashes: true,
});
const campaigns = await getRandomCampaigns([
{
minRatio: 1.5,
entityIds: [scene.channel.id, scene.network?.id].filter(Boolean),
allowRandomFallback: false,
},
], { tagFilter: pageContext.tagFilter });
if (!scene) {
throw render(404, `Cannot find scene '${pageContext.routeParams.sceneId}'.`);
}
return {
pageContext: {
title: getTitle(scene),
pageProps: {
scene,
},
campaigns: {
scene: campaigns[0],
},
},
};
}