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.

cleanup assets code structure

Oleg Isonenf7d0e44200e5
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-manager.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-manager.tsx
index 7a5c031ca0..c5486328c5 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-manager.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-manager.tsx
@@ -6,7 +6,7 @@ import type { Asset } from "@webstudio-is/prisma-client";
import { TabName, UploadingAsset } from "../../types";
import { Header } from "../../lib/header";
import { AddAnAssetForm } from "./add-an-asset-form";
-import { AssetManagerThumbnail } from "./thumbnail";
+import { AssetThumbnail } from "./asset-thumbnail";
export const useAssetsState = (baseAssets: Array<Asset>) => {
const imageChanges = useActionData();
@@ -62,7 +62,7 @@ export const TabContent = ({
</Flex>
<Grid columns={2} gap={2}>
{assets.map((asset) => (
- <AssetManagerThumbnail key={asset.id} {...asset} />
+ <AssetThumbnail key={asset.id} {...asset} />
))}
</Grid>
</Flex>
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.tsx
new file mode 100644
index 0000000000..8989775950
--- /dev/null
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-thumbnail.tsx
@@ -0,0 +1,85 @@
+import { useEffect, useState } from "react";
+import { Box } from "@webstudio-is/design-system";
+import placeholderImage from "~/shared/images/image-placeholder.svg";
+import brokenImage from "~/shared/images/broken-image-placeholder.svg";
+import { UploadingAnimation } from "./uploading-animation";
+import type { Asset } from "@webstudio-is/prisma-client";
+import { UploadingAsset } from "../../types";
+import { AssetTooltip } from "./asset-tooltip";
+
+const useImageWithFallback = ({
+ path = placeholderImage,
+}: {
+ path?: string;
+}) => {
+ const [src, setSrc] = useState(placeholderImage);
+
+ useEffect(() => {
+ const newImage = new Image();
+ newImage.onload = () => setSrc(path);
+ newImage.onerror = () => setSrc(brokenImage);
+ newImage.src = path;
+ }, [path]);
+
+ return src;
+};
+
+type ThumbnailProps = {
+ path?: string;
+ status: Asset["status"];
+};
+
+export const Thumbnail = ({ path, status }: ThumbnailProps) => {
+ const src = useImageWithFallback({ path });
+
+ return (
+ <Box
+ css={{
+ backgroundImage: `url("${src}")`,
+ width: "100%",
+ height: "100%",
+ backgroundSize: "contain",
+ backgroundRepeat: "no-repeat",
+ backgroundPosition: "center",
+ position: "absolute",
+ left: 0,
+ top: 0,
+ ...(status === "uploading"
+ ? { filter: "blur(1px)", opacity: 0.7 }
+ : {}),
+ }}
+ ></Box>
+ );
+};
+
+export const AssetThumbnail = (asset: Asset | UploadingAsset) => {
+ const { path, alt, status, name } = asset;
+ const [isDeleting, setIsDeleting] = useState(false);
+ const isUploading = status === "uploading";
+ const isUploadedAsset = isUploading === false && "size" in asset;
+
+ return (
+ <Box
+ title={alt || name}
+ css={{
+ aspectRatio: "1/1",
+ display: "flex",
+ justifyContent: "center",
+ alignItems: "center",
+ padding: "0 $2",
+ position: "relative",
+ }}
+ >
+ <Thumbnail path={path} status={status} />
+ {isUploadedAsset && (
+ <AssetTooltip
+ asset={asset}
+ onDelete={() => {
+ setIsDeleting(true);
+ }}
+ />
+ )}
+ {(isUploading || isDeleting) && <UploadingAnimation />}
+ </Box>
+ );
+};
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-tooltip.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-tooltip.tsx
new file mode 100644
index 0000000000..7eabb24475
--- /dev/null
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/asset-tooltip.tsx
@@ -0,0 +1,48 @@
+import { useState } from "react";
+import { Button, Tooltip } from "@webstudio-is/design-system";
+import { GearIcon } from "@webstudio-is/icons";
+import type { Asset } from "@webstudio-is/prisma-client";
+import { AssetInfo } from "./asset-info";
+
+export const AssetTooltip = ({
+ asset,
+ onDelete,
+}: {
+ asset: Asset;
+ onDelete: () => void;
+}) => {
+ const [isTooltipOpen, setTooltipOpen] = useState(false);
+ const closeTooltip = () => setTooltipOpen(false);
+
+ return (
+ <Tooltip
+ open={isTooltipOpen}
+ multiline
+ onEscapeKeyDown={closeTooltip}
+ onPointerDownOutside={closeTooltip}
+ css={{ width: 240, maxWidth: 240 }}
+ content={
+ <AssetInfo
+ onDelete={onDelete}
+ onClose={() => setTooltipOpen(false)}
+ {...asset}
+ />
+ }
+ >
+ <Button
+ variant="raw"
+ title="Options"
+ onClick={() => setTooltipOpen(true)}
+ css={{
+ position: "absolute",
+ top: "$1",
+ right: "$1",
+ cursor: "pointer",
+ color: "$highContrast",
+ }}
+ >
+ <GearIcon />
+ </Button>
+ </Tooltip>
+ );
+};
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/thumbnail.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/thumbnail.tsx
deleted file mode 100644
index c4f4c40dbe..0000000000
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/thumbnail.tsx
+++ /dev/null
@@ -1,100 +0,0 @@
-import { useEffect, useState } from "react";
-import { Box, Button, Tooltip } from "@webstudio-is/design-system";
-import placeholderImage from "~/shared/images/image-placeholder.svg";
-import brokenImage from "~/shared/images/broken-image-placeholder.svg";
-import { UploadingAnimation } from "./uploading-animation";
-import { GearIcon } from "@webstudio-is/icons";
-import { AssetInfo } from "./asset-info";
-import type { Asset } from "@webstudio-is/prisma-client";
-import { UploadingAsset } from "../../types";
-
-const useImageWithFallback = ({
- path = placeholderImage,
-}: {
- path?: string;
-}) => {
- const [src, setSrc] = useState(placeholderImage);
-
- useEffect(() => {
- const newImage = new Image();
- newImage.onload = () => setSrc(path);
- newImage.onerror = () => setSrc(brokenImage);
- newImage.src = path;
- }, [path]);
-
- return src;
-};
-
-export const AssetManagerThumbnail = (asset: Asset | UploadingAsset) => {
- const { path, alt, status, name } = asset;
- const [isDeleting, setIsDeleting] = useState(false);
- const isUploading = status === "uploading";
- const src = useImageWithFallback({ path });
- const [isTooltipOpen, setTooltipOpen] = useState(false);
- const isUploadedAsset = isUploading === false && "size" in asset;
-
- const closeTooltip = () => setTooltipOpen(false);
-
- return (
- <Box
- title={alt || name}
- css={{
- aspectRatio: "1/1",
- display: "flex",
- justifyContent: "center",
- alignItems: "center",
- padding: "0 $2",
- position: "relative",
- }}
- >
- <Box
- css={{
- backgroundImage: `url("${src}")`,
- width: "100%",
- height: "100%",
- backgroundSize: "contain",
- backgroundRepeat: "no-repeat",
- backgroundPosition: "center",
- position: "absolute",
- left: 0,
- top: 0,
- ...(isUploading ? { filter: "blur(1px)", opacity: 0.7 } : {}),
- }}
- ></Box>
- {isUploadedAsset && (
- <Tooltip
- open={isTooltipOpen}
- multiline
- onEscapeKeyDown={closeTooltip}
- onPointerDownOutside={closeTooltip}
- css={{ width: 240, maxWidth: 240 }}
- content={
- <AssetInfo
- onDelete={() => setIsDeleting(true)}
- onClose={() => setTooltipOpen(false)}
- {...asset}
- />
- }
- >
- <Button
- variant="raw"
- title="Options"
- onClick={() => setTooltipOpen(true)}
- css={{
- position: "absolute",
- top: "$1",
- right: "$1",
- cursor: "pointer",
- color: "$highContrast",
- }}
- >
- <GearIcon />
- </Button>
- </Tooltip>
- )}
- {(isUploading || isDeleting) && (
- <UploadingAnimation isDeleting={isDeleting} isUploading={isUploading} />
- )}
- </Box>
- );
-};
diff --git a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/uploading-animation.tsx b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/uploading-animation.tsx
index 588feffa83..c7af5b5650 100644
--- a/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/uploading-animation.tsx
+++ b/apps/designer/app/designer/features/sidebar-left/panels/asset-manager/uploading-animation.tsx
@@ -3,36 +3,21 @@ import { ProgressRadial } from "@webstudio-is/design-system";
import { useState } from "react";
import { useInterval } from "react-use";
-const useFakeProgress = ({
- isUploading,
- isDeleting,
-}: {
- isUploading: boolean;
- isDeleting: boolean;
-}) => {
+const useFakeProgress = () => {
const [progressBarPercentage, setProgressBarPercentage] = useState(0);
// @todo rewrite this fake indication to show real progress
- useInterval(
- () => {
- setProgressBarPercentage((percentage) =>
- percentage < 60 ? percentage + 1 : percentage
- );
- },
- isUploading || isDeleting ? 100 : null
- );
+ useInterval(() => {
+ setProgressBarPercentage((percentage) =>
+ percentage < 60 ? percentage + 1 : percentage
+ );
+ }, 100);
return progressBarPercentage;
};
-export const UploadingAnimation = ({
- isUploading,
- isDeleting,
-}: {
- isUploading: boolean;
- isDeleting: boolean;
-}) => {
- const progressBarPercentage = useFakeProgress({ isUploading, isDeleting });
+export const UploadingAnimation = () => {
+ const progressBarPercentage = useFakeProgress();
return (
<ProgressRadial
css={{ position: "relative", zIndex: 10 }}