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.

prevent double rendering after upload and create types for return value from action

Oleg Isonen7998d6b13a15
diff --git a/apps/designer/app/designer/shared/assets/types.ts b/apps/designer/app/designer/shared/assets/types.ts
index 94d5f7bb43..3b4ee1b828 100644
--- a/apps/designer/app/designer/shared/assets/types.ts
+++ b/apps/designer/app/designer/shared/assets/types.ts
@@ -4,3 +4,9 @@ export type BaseAsset = Pick<
Asset,
"id" | "status" | "name" | "path" | "alt" | "size" | "width" | "height"
>;
+
+export type ActionData = {
+ uploadedAssets?: Array<Asset>;
+ deletedAsset?: Asset;
+ errors?: string;
+};
diff --git a/apps/designer/app/designer/shared/assets/use-assets.ts b/apps/designer/app/designer/shared/assets/use-assets.ts
index 5aef041960..f1c6157b00 100644
--- a/apps/designer/app/designer/shared/assets/use-assets.ts
+++ b/apps/designer/app/designer/shared/assets/use-assets.ts
@@ -1,31 +1,34 @@
import { useEffect, useState } from "react";
import { useActionData } from "@remix-run/react";
-import { BaseAsset } from "./types";
+import type { BaseAsset, ActionData } from "./types";
export const useAssets = (initialAssets: Array<BaseAsset> = []) => {
- const changes = useActionData();
+ const actionData: ActionData | undefined = useActionData();
const [assets, setAssets] = useState<BaseAsset[]>(initialAssets);
useEffect(() => {
- if (changes?.errors) {
+ const { errors, uploadedAssets, deletedAsset } = actionData ?? {};
+ if (errors) {
setAssets((currentAssets) =>
currentAssets.filter((asset) => asset.status !== "uploading")
);
}
- if (changes?.uploadedAssets?.length) {
+ if (uploadedAssets?.length) {
setAssets((currentAssets) => [
- ...changes.uploadedAssets,
+ ...uploadedAssets.filter((uploadedAsset) =>
+ currentAssets.every(
+ (currentAsset) => currentAsset.id !== uploadedAsset.id
+ )
+ ),
...currentAssets.filter((asset) => asset.status !== "uploading"),
]);
}
- if (changes?.deletedAsset?.id) {
+ if (deletedAsset?.id) {
setAssets((currentAssets) => [
- ...currentAssets.filter(
- (asset) => asset.id !== changes.deletedAsset.id
- ),
+ ...currentAssets.filter((asset) => asset.id !== deletedAsset.id),
]);
}
- }, [changes]);
+ }, [actionData]);
const onUploadAsset = (uploadedAssets: Array<BaseAsset>) =>
setAssets((assets) => [...uploadedAssets, ...assets]);
diff --git a/apps/designer/app/routes/designer/_assets.tsx b/apps/designer/app/routes/designer/_assets.tsx
index dc744c7043..8087155cb4 100644
--- a/apps/designer/app/routes/designer/_assets.tsx
+++ b/apps/designer/app/routes/designer/_assets.tsx
@@ -11,13 +11,17 @@ import {
import { toast } from "@webstudio-is/design-system";
import { useEffect } from "react";
import { zfd } from "zod-form-data";
+import type { ActionData } from "~/designer/shared/assets";
const deleteAssetSchema = zfd.formData({
assetId: zfd.text(),
assetName: zfd.text(),
});
-export const action: ActionFunction = async ({ request, params }) => {
+export const action: ActionFunction = async ({
+ request,
+ params,
+}): Promise<ActionData | undefined> => {
if (params.id === undefined) throw new Error("Project id undefined");
try {
if (request.method === "DELETE") {
@@ -53,10 +57,10 @@ export const action: ActionFunction = async ({ request, params }) => {
};
export const useAction = () => {
- const actionData = useActionData();
+ const actionData: ActionData | undefined = useActionData();
useEffect(() => {
- if (actionData && "errors" in actionData) {
+ if (actionData?.errors) {
toast.error(actionData.errors);
}
}, [actionData]);