Files
webstudio/apps/builder/app/shared/db/canvas.server.ts
T
Ivan Starkov 482839e83a chore: Remove prisma from the Builder app (#4182)
## Description

1. What is this PR about (link the issue and add a short description)

## Steps for reproduction

1. click button
2. expect xyz

## Code Review

- [ ] hi @kof, I need you to do
  - conceptual review (architecture, feature-correctness)
  - detailed review (read every line)
  - test it on preview

## Before requesting a review

- [ ] made a self-review
- [ ] added inline comments where things may be not obvious (the "why",
not "what")

## Before merging

- [ ] tested locally and on preview environment (preview dev login:
5de6)
- [ ] updated [test
cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md)
document
- [ ] added tests
- [ ] if any new env variables are added, added them to `.env` file
2024-10-01 13:51:50 +03:00

99 lines
2.9 KiB
TypeScript

import type { Data } from "@webstudio-is/http-client";
import { loadBuildById } from "@webstudio-is/project-build/index.server";
import { loadAssetsByProject } from "@webstudio-is/asset-uploader/index.server";
import type { AppContext } from "@webstudio-is/trpc-interface/index.server";
import { findPageByIdOrPath, getStyleDeclKey } from "@webstudio-is/sdk";
import { db as projectDb } from "@webstudio-is/project/index.server";
const getPair = <Item extends { id: string }>(item: Item): [string, Item] => [
item.id,
item,
];
export const loadProductionCanvasData = async (
buildId: string,
context: AppContext
): Promise<Data> => {
const build = await loadBuildById(context, buildId);
if (build === undefined) {
throw new Error("The project is not published");
}
const { deployment } = build;
if (deployment === undefined) {
throw new Error("The project is not published");
}
const project = await projectDb.project.loadById(build.projectId, context);
const currentProjectDomains = project.domainsVirtual;
// Check that build deployment domains are still active and verified
// for examle: redeploy created few days later
if (deployment.destination !== "static") {
deployment.domains = deployment.domains.filter(
(domain) =>
project.domain === domain ||
currentProjectDomains.some(
(projectDomain) =>
projectDomain.domain === domain &&
projectDomain.status === "ACTIVE" &&
projectDomain.verified
)
);
}
const page = findPageByIdOrPath("/", build.pages);
if (page === undefined) {
throw new Error(`Page / not found`);
}
const allAssets = await loadAssetsByProject(build.projectId, context);
// Find all fonts referenced in styles
const fontFamilySet = new Set<string>();
for (const { value } of build.styles) {
if (value.type === "fontFamily") {
for (const fontFamily of value.value) {
fontFamilySet.add(fontFamily);
}
}
}
// Filter unused font assets
const assets = allAssets.filter(
(asset) =>
asset.type === "image" ||
(asset.type === "font" && fontFamilySet.has(asset.meta.family))
);
return {
build: {
id: build.id,
projectId: build.projectId,
version: build.version,
createdAt: build.createdAt,
updatedAt: build.updatedAt,
pages: build.pages,
breakpoints: build.breakpoints.map(getPair),
styles: build.styles.map((item) => [getStyleDeclKey(item), item]),
styleSources: build.styleSources.map(getPair),
styleSourceSelections: build.styleSourceSelections.map((item) => [
item.instanceId,
item,
]),
props: build.props.map(getPair),
dataSources: build.dataSources.map(getPair),
resources: build.resources.map(getPair),
instances: build.instances.map(getPair),
deployment,
},
page,
pages: [build.pages.homePage, ...build.pages.pages],
assets,
};
};