mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-10-28 07:37:36 +09:00
## 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: 0000) - [ ] 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
108 lines
3.1 KiB
TypeScript
108 lines
3.1 KiB
TypeScript
import { getStyleDeclKey, type WebstudioData } from "@webstudio-is/sdk";
|
|
import type { MarketplaceProduct } from "@webstudio-is/project-build";
|
|
import type { Project } from "@webstudio-is/project";
|
|
import type { loader } from "~/routes/rest.data.$projectId";
|
|
import {
|
|
$project,
|
|
$assets,
|
|
$breakpoints,
|
|
$dataSources,
|
|
$instances,
|
|
$marketplaceProduct,
|
|
$pages,
|
|
$props,
|
|
$resources,
|
|
$styleSourceSelections,
|
|
$styleSources,
|
|
$styles,
|
|
} from "./nano-states";
|
|
import { fetch } from "~/shared/fetch.client";
|
|
|
|
export type BuilderData = WebstudioData & {
|
|
marketplaceProduct: undefined | MarketplaceProduct;
|
|
project: Project;
|
|
};
|
|
|
|
export type LoadedBuilderData = BuilderData &
|
|
Pick<
|
|
Awaited<ReturnType<typeof loader>>,
|
|
"id" | "version" | "publisherHost" | "projectId"
|
|
>;
|
|
|
|
export const getBuilderData = (): BuilderData => {
|
|
const pages = $pages.get();
|
|
if (pages === undefined) {
|
|
throw Error(`Cannot get webstudio data with empty pages`);
|
|
}
|
|
const project = $project.get();
|
|
if (project === undefined) {
|
|
throw Error(`Cannot get webstudio data with empty project`);
|
|
}
|
|
return {
|
|
pages,
|
|
project,
|
|
instances: $instances.get(),
|
|
props: $props.get(),
|
|
dataSources: $dataSources.get(),
|
|
resources: $resources.get(),
|
|
breakpoints: $breakpoints.get(),
|
|
styleSourceSelections: $styleSourceSelections.get(),
|
|
styleSources: $styleSources.get(),
|
|
styles: $styles.get(),
|
|
assets: $assets.get(),
|
|
marketplaceProduct: $marketplaceProduct.get(),
|
|
};
|
|
};
|
|
|
|
const getPair = <Item extends { id: string }>(item: Item) =>
|
|
[item.id, item] as const;
|
|
|
|
export const loadBuilderData = async ({
|
|
projectId,
|
|
signal,
|
|
}: {
|
|
projectId: string;
|
|
signal: AbortSignal;
|
|
}): Promise<LoadedBuilderData> => {
|
|
const currentUrl = new URL(location.href);
|
|
const url = new URL(`/rest/data/${projectId}`, currentUrl.origin);
|
|
const headers = new Headers();
|
|
|
|
const response = await fetch(url, { headers, signal });
|
|
|
|
if (response.ok) {
|
|
const data = (await response.json()) as Awaited<ReturnType<typeof loader>>;
|
|
return {
|
|
id: data.id,
|
|
version: data.version,
|
|
projectId: data.projectId,
|
|
project: data.project,
|
|
publisherHost: data.publisherHost,
|
|
assets: new Map(data.assets.map(getPair)),
|
|
instances: new Map(data.instances.map(getPair)),
|
|
dataSources: new Map(data.dataSources.map(getPair)),
|
|
resources: new Map(data.resources.map(getPair)),
|
|
props: new Map(data.props.map(getPair)),
|
|
pages: data.pages,
|
|
breakpoints: new Map(data.breakpoints.map(getPair)),
|
|
styleSources: new Map(data.styleSources.map(getPair)),
|
|
styleSourceSelections: new Map(
|
|
data.styleSourceSelections.map((item) => [item.instanceId, item])
|
|
),
|
|
styles: new Map(data.styles.map((item) => [getStyleDeclKey(item), item])),
|
|
marketplaceProduct: data.marketplaceProduct,
|
|
};
|
|
}
|
|
|
|
const text = await response.text();
|
|
|
|
// No toasts available in this context
|
|
alert(
|
|
`Unable to load builder data. Response status: ${response.status}. Response text: ${text}`
|
|
);
|
|
|
|
throw Error(
|
|
`Unable to load builder data. Response status: ${response.status}. Response text: ${text}`
|
|
);
|
|
};
|