Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

only load assets when panel opens

Oleg Isonen06737a23caf9
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index cc5f74ec36..0c0b66bc09 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -38,11 +38,10 @@ import {
} from "~/shared/nano-states";
import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
-import { useSetAssets } from "./shared/assets";
import { getBuildUrl } from "~/shared/router-utils";
import { useSubscribeDesignTokens } from "./shared/design-tokens-manager";
import { useInstanceCopyPaste } from "~/shared/copy-paste";
-import type { Asset } from "@webstudio-is/asset-uploader";
+import { usePublishAssets } from "./shared/assets";
export const links = () => {
return [
@@ -278,7 +277,6 @@ export type DesignerProps = {
pages: Pages;
pageId: string;
buildOrigin: string;
- assets: Array<Asset>;
};
export const Designer = ({
@@ -286,7 +284,6 @@ export const Designer = ({
pages,
pageId,
buildOrigin,
- assets,
}: DesignerProps) => {
useSubscribeSyncStatus();
useSubscribeRootInstance();
@@ -298,7 +295,7 @@ export const Designer = ({
useSetPages(pages);
useSetCurrentPageId(pageId);
const [publish, publishRef] = usePublish();
- useSetAssets({ assets, publish });
+ usePublishAssets(publish);
const [isPreviewMode] = useIsPreviewMode();
usePublishShortcuts(publish);
const onRefReadCanvasWidth = useUpdateCanvasWidth();
diff --git a/apps/designer/app/designer/shared/assets/index.ts b/apps/designer/app/designer/shared/assets/index.ts
index f55e12259a..dc3e837c4d 100644
--- a/apps/designer/app/designer/shared/assets/index.ts
+++ b/apps/designer/app/designer/shared/assets/index.ts
@@ -1,6 +1,5 @@
export * from "./use-assets";
export * from "./types";
-export * from "./use-set-assets";
export * from "./separator";
export * from "./use-search";
export * from "./assets-shell";
diff --git a/apps/designer/app/designer/shared/assets/use-assets.ts b/apps/designer/app/designer/shared/assets/use-assets.ts
index 73ff3660c4..663c6d8b71 100644
--- a/apps/designer/app/designer/shared/assets/use-assets.ts
+++ b/apps/designer/app/designer/shared/assets/use-assets.ts
@@ -1,12 +1,13 @@
+import { useEffect, useMemo } from "react";
import {
AssetType,
filterByType,
MAX_UPLOAD_SIZE,
toBytes,
+ type Asset,
} from "@webstudio-is/asset-uploader";
import { toast } from "@webstudio-is/design-system";
import ObjectID from "bson-objectid";
-import { useMemo } from "react";
import { restAssetsPath } from "~/shared/router-utils";
import { useAssets as useAssetsState, useProject } from "../nano-states";
import { sanitizeS3Key } from "@webstudio-is/asset-uploader";
@@ -18,6 +19,26 @@ import {
normalizeErrors,
toastUnknownFieldErrors,
} from "~/shared/form-utils";
+import { Publish } from "~/shared/pubsub";
+import { useFetcher } from "@remix-run/react";
+
+declare module "~/shared/pubsub" {
+ export interface PubsubMap {
+ updateAssets: Array<Asset>;
+ }
+}
+
+export const usePublishAssets = (publish: Publish) => {
+ const [assets] = useAssetsState();
+ useEffect(() => {
+ publish({
+ type: "updateAssets",
+ payload: assets.filter(
+ (asset) => asset.status === "uploaded"
+ ) as Array<Asset>, // TS doesn't understand we filtered out PrevewAssets
+ });
+ }, [assets, publish]);
+};
export type UploadData = FetcherData<ActionData>;
@@ -77,11 +98,25 @@ const toFormsData = (type: AssetType, input: HTMLInputElement) => {
};
export const useAssets = (type: AssetType) => {
- const submit = usePersistentFetcher();
- const [assets, setAssets] = useAssetsState();
const [project] = useProject();
+ const [assets, setAssets] = useAssetsState();
+ const { load, data } = useFetcher();
+ const submit = usePersistentFetcher();
+
const action = project && restAssetsPath({ projectId: project.id });
+ useEffect(() => {
+ if (action && assets.length === 0) {
+ load(action);
+ }
+ }, [action, assets.length, load]);
+
+ useEffect(() => {
+ if (data !== undefined) {
+ setAssets(data);
+ }
+ }, [data, setAssets]);
+
const handleDelete = (ids: Array<string>) => {
const formData = new FormData();
for (const id of ids) {
@@ -114,6 +149,12 @@ export const useAssets = (type: AssetType) => {
if (data.status === "error") {
return toastUnknownFieldErrors(normalizeErrors(data.errors), []);
}
+
+ // @todo currently waiting until all of them are fetched
+ // should update each individually when its loaded
+ if (action) {
+ load(action);
+ }
}
);
}
diff --git a/apps/designer/app/designer/shared/assets/use-set-assets.ts b/apps/designer/app/designer/shared/assets/use-set-assets.ts
deleted file mode 100644
index 23ae061c94..0000000000
--- a/apps/designer/app/designer/shared/assets/use-set-assets.ts
+++ /dev/null
@@ -1,25 +0,0 @@
-import type { Asset } from "@webstudio-is/asset-uploader";
-import { useEffect } from "react";
-import { Publish } from "~/shared/pubsub";
-import { useAssets } from "../nano-states";
-
-declare module "~/shared/pubsub" {
- export interface PubsubMap {
- updateAssets: Array<Asset>;
- }
-}
-
-type UseSetAssets = {
- assets?: Array<Asset>;
- publish: Publish;
-};
-
-export const useSetAssets = ({ assets, publish }: UseSetAssets) => {
- const [, setAssets] = useAssets();
- useEffect(() => {
- if (assets) {
- setAssets(assets);
- publish({ type: "updateAssets", payload: assets });
- }
- }, [assets, setAssets, publish]);
-};
diff --git a/apps/designer/app/routes/designer/$projectId.tsx b/apps/designer/app/routes/designer/$projectId.tsx
index 5cfe7b76e8..52ffcc5cea 100644
--- a/apps/designer/app/routes/designer/$projectId.tsx
+++ b/apps/designer/app/routes/designer/$projectId.tsx
@@ -5,7 +5,6 @@ import { db } from "@webstudio-is/project/server";
import { ErrorMessage } from "~/shared/error";
import { sentryException } from "~/shared/sentry";
import { getBuildOrigin } from "~/shared/router-utils";
-import { loadByProject } from "@webstudio-is/asset-uploader/server";
export { links };
@@ -27,14 +26,12 @@ export const loader: LoaderFunction = async ({
}
const devBuild = await db.build.loadByProjectId(project.id, "dev");
- const assets = await loadByProject(project.id);
return {
project,
pages: devBuild.pages,
pageId: pageIdParam || devBuild.pages.homePage.id,
buildOrigin: getBuildOrigin(request),
- assets,
};
};
diff --git a/apps/designer/app/routes/rest/assets.$projectId.tsx b/apps/designer/app/routes/rest/assets.$projectId.tsx
index 07481e3023..f1b541cea9 100644
--- a/apps/designer/app/routes/rest/assets.$projectId.tsx
+++ b/apps/designer/app/routes/rest/assets.$projectId.tsx
@@ -1,8 +1,10 @@
-import { ActionFunction } from "@remix-run/node";
+import { ActionFunction, LoaderFunction } from "@remix-run/node";
import { useActionData } from "@remix-run/react";
+import type { Asset } from "@webstudio-is/asset-uploader";
import {
deleteAssets,
uploadAssets,
+ loadByProject,
} from "@webstudio-is/asset-uploader/server";
import { toast } from "@webstudio-is/design-system";
import { useEffect } from "react";
@@ -14,6 +16,13 @@ const DeleteAssets = zfd.formData({
assetId: zfd.repeatableOfType(zfd.text()),
});
+export const loader: LoaderFunction = async ({
+ params,
+}): Promise<Array<Asset>> => {
+ if (params.projectId === undefined) throw new Error("Project id undefined");
+ return await loadByProject(params.projectId);
+};
+
export const action: ActionFunction = async ({
request,
params,