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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
better types
Oleg Isonen589ca862f6ac
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsxindex 3abc279822..e712f1890a 100644--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx@@ -4,7 +4,7 @@ import placeholderImage from "~/shared/images/image-placeholder.svg";import brokenImage from "~/shared/images/broken-image-placeholder.svg";import { UploadingAnimation } from "./uploading-animation";import { AssetInfoTrigger, assetInfoTriggerCssVars } from "./asset-info-tigger";-import type { BaseAsset } from "~/designer/shared/assets";+import type { BaseAsset, PreviewAsset } from "~/designer/shared/assets";const useImageWithFallback = ({path = placeholderImage,@@ -51,15 +51,17 @@ export const Thumbnail = ({ path, status }: ThumbnailProps) => {);};-export const AssetThumbnail = (asset: BaseAsset) => {- const { path, description, status, name } = asset;+export const AssetThumbnail = (asset: BaseAsset | PreviewAsset) => {+ const { path, status, name } = asset;+ const description =+ "description" in asset && asset.description ? asset.description : name;const [isDeleting, setIsDeleting] = useState(false);const isUploading = status === "uploading";const isUploadedAsset = isUploading === false && "size" in asset;return (<Box- title={description || name}+ title={description}css={{aspectRatio: "1/1",display: "flex",diff --git a/apps/designer/app/designer/shared/assets/asset-upload.tsx b/apps/designer/app/designer/shared/assets/asset-upload.tsxindex 08cbd80ea3..3d7c8c28b7 100644--- a/apps/designer/app/designer/shared/assets/asset-upload.tsx+++ b/apps/designer/app/designer/shared/assets/asset-upload.tsx@@ -4,25 +4,25 @@ import { ChangeEvent, useRef } from "react";import { Button, Flex, Text } from "@webstudio-is/design-system";import { UploadIcon } from "@webstudio-is/icons";import { type AssetType, FONT_FORMATS } from "@webstudio-is/asset-uploader";-import type { BaseAsset } from "./types";+import type { PreviewAsset } from "./types";-const readAssets = (fileList: FileList): Promise<BaseAsset[]> => {- const assets: Array<Promise<BaseAsset>> = Array.from(fileList).map(+const readAssets = (fileList: FileList): Promise<PreviewAsset[]> => {+ const assets: Array<Promise<PreviewAsset>> = Array.from(fileList).map((file) =>- new Promise((resolve) => {+ new Promise((resolve, reject) => {const reader = new FileReader();reader.addEventListener("load", (event) => {const dataUri = event?.target?.result;- const path = dataUri ? String(dataUri) : undefined;+ if (dataUri === undefined) {+ return reject(new Error("Could not read file"));+ }+resolve({- path,+ format: file.type.split("/")[1],+ path: String(dataUri),name: file.name,id: ObjectID().toString(),status: "uploading",- description: file.name,- size: file.size,- meta: {} as any,- format: "unknown",});});reader.readAsDataURL(file);@@ -34,11 +34,11 @@ const readAssets = (fileList: FileList): Promise<BaseAsset[]> => {const acceptMap = {image: "image/*",- font: FONT_FORMATS.map((format: string) => `.${format}`).join(", "),+ font: FONT_FORMATS.map((format) => `.${format}`).join(", "),};type AssetUploadProps = {- onSubmit: (assets: Array<BaseAsset>) => void;+ onSubmit: (assets: Array<PreviewAsset>) => void;type: AssetType;};diff --git a/apps/designer/app/designer/shared/assets/types.ts b/apps/designer/app/designer/shared/assets/types.tsindex af0a1b919c..18f654dddd 100644--- a/apps/designer/app/designer/shared/assets/types.ts+++ b/apps/designer/app/designer/shared/assets/types.ts@@ -5,6 +5,11 @@ export type BaseAsset = Pick<"id" | "status" | "name" | "path" | "description" | "size" | "meta" | "format">;+export type PreviewAsset = Pick<+ BaseAsset,+ "path" | "name" | "id" | "status" | "format"+>;+export type ActionData = {uploadedAssets?: Array<Asset>;deletedAsset?: Asset;diff --git a/apps/designer/app/designer/shared/assets/use-assets.ts b/apps/designer/app/designer/shared/assets/use-assets.tsindex 3e5d5c4a7a..9f94dc05e9 100644--- a/apps/designer/app/designer/shared/assets/use-assets.ts+++ b/apps/designer/app/designer/shared/assets/use-assets.ts@@ -1,6 +1,6 @@import { useEffect, useState } from "react";import { useActionData } from "@remix-run/react";-import type { BaseAsset, ActionData } from "./types";+import type { BaseAsset, PreviewAsset, ActionData } from "./types";import { type AssetType, filterByType } from "@webstudio-is/asset-uploader";export const useAssets = (@@ -8,7 +8,8 @@ export const useAssets = (type: AssetType) => {const actionData: ActionData | undefined = useActionData();- const [assets, setAssets] = useState<BaseAsset[]>(initialAssets);+ const [assets, setAssets] =+ useState<Array<BaseAsset | PreviewAsset>>(initialAssets);useEffect(() => {const { errors, uploadedAssets, deletedAsset } = actionData ?? {};@@ -34,11 +35,11 @@ export const useAssets = (}}, [actionData]);- const onUploadAsset = (uploadedAssets: Array<BaseAsset>) =>+ const onUploadAsset = (uploadedAssets: Array<BaseAsset | PreviewAsset>) =>setAssets((assets) => [...uploadedAssets, ...assets]);return {- assets: filterByType<BaseAsset>(assets, type),+ assets: filterByType<BaseAsset | PreviewAsset>(assets, type),onUploadAsset,};};diff --git a/packages/asset-uploader/src/types.ts b/packages/asset-uploader/src/types.tsindex 0fceb9d21e..7494f95981 100644--- a/packages/asset-uploader/src/types.ts+++ b/packages/asset-uploader/src/types.ts@@ -2,7 +2,7 @@ import { Asset as DbAsset } from "@webstudio-is/prisma-client";import type { FontMeta, ImageMeta } from "./utils/format-asset";export type Asset = Omit<DbAsset, "meta"> & {- path?: string;+ path: string;status?: "uploading" | "uploaded";meta: ImageMeta | FontMeta;};