Skip to content

Commit 78d98a5

Browse files
authored
refactor: pass wsImageLoader directly (#4567)
Split our saas image loader into separate function which is passed directly everywhere without createImageLoader.
1 parent 729978a commit 78d98a5

File tree

18 files changed

+91
-150
lines changed

18 files changed

+91
-150
lines changed

apps/builder/app/builder/builder.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import type { Build } from "@webstudio-is/project-build";
66
import type { Project } from "@webstudio-is/project";
77
import { theme, Box, type CSS, Flex, Grid } from "@webstudio-is/design-system";
88
import type { AuthPermit } from "@webstudio-is/trpc-interface/index.server";
9-
import { createImageLoader } from "@webstudio-is/image";
109
import { registerContainers, createObjectPool } from "~/shared/sync";
1110
import {
1211
ServerSyncStorage,
@@ -31,7 +30,6 @@ import {
3130
subscribeResources,
3231
$authTokenPermissions,
3332
$publisherHost,
34-
$imageLoader,
3533
$isDesignMode,
3634
$isContentMode,
3735
$userPlanFeatures,
@@ -236,7 +234,6 @@ export const Builder = ({
236234
// additional data stores
237235
$project.set(project);
238236
$publisherHost.set(publisherHost);
239-
$imageLoader.set(createImageLoader({}));
240237
$authPermit.set(authPermit);
241238
$authToken.set(authToken);
242239
$userPlanFeatures.set(userPlanFeatures);

apps/builder/app/builder/features/marketplace/card.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { forwardRef } from "react";
2-
import { useStore } from "@nanostores/react";
32
import {
43
Flex,
54
Text,
@@ -8,9 +7,8 @@ import {
87
css,
98
rawTheme,
109
} from "@webstudio-is/design-system";
11-
import { Image } from "@webstudio-is/image";
10+
import { Image, wsImageLoader } from "@webstudio-is/image";
1211
import { SpinnerIcon } from "@webstudio-is/icons";
13-
import { $imageLoader } from "~/shared/nano-states";
1412

1513
const focusOutline = focusRingStyle();
1614

@@ -60,7 +58,6 @@ type ThumbnailProps = {
6058
};
6159

6260
const Thumbnail = ({ image, state, alt }: ThumbnailProps) => {
63-
const imageLoader = useStore($imageLoader);
6461
return (
6562
<Flex className={imageContainerStyle()}>
6663
{image === "" || image === undefined ? (
@@ -79,7 +76,7 @@ const Thumbnail = ({ image, state, alt }: ThumbnailProps) => {
7976
<Image
8077
src={image.name}
8178
width={rawTheme.spacing[28]}
82-
loader={imageLoader}
79+
loader={wsImageLoader}
8380
className={imageStyle({ hasAsset: true })}
8481
/>
8582
)}

apps/builder/app/builder/features/pages/search-preview.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { useStore } from "@nanostores/react";
21
import { Box, Flex, Grid } from "@webstudio-is/design-system";
3-
import { Image } from "@webstudio-is/image";
4-
import { $imageLoader } from "~/shared/nano-states";
2+
import { Image, wsImageLoader } from "@webstudio-is/image";
53
import { formatUrl, truncateByWords, truncate } from "./social-utils";
64

75
/**
@@ -67,8 +65,6 @@ const VerticalThreePointIcon = () => (
6765
);
6866

6967
export const SearchPreview = (props: SearchPreviewProps) => {
70-
const imageLoader = useStore($imageLoader);
71-
7268
return (
7369
<Grid
7470
gap={1}
@@ -99,7 +95,7 @@ export const SearchPreview = (props: SearchPreviewProps) => {
9995
<Image
10096
width={18}
10197
height={18}
102-
loader={imageLoader}
98+
loader={wsImageLoader}
10399
src={props.faviconUrl}
104100
/>
105101
</Flex>

apps/builder/app/builder/features/pages/social-preview.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Box, Grid, Label, css, theme } from "@webstudio-is/design-system";
2-
import { Image } from "@webstudio-is/image";
2+
import { Image, wsImageLoader } from "@webstudio-is/image";
33
import { truncateByWords, truncate } from "./social-utils";
4-
import { useStore } from "@nanostores/react";
5-
import { $imageLoader } from "~/shared/nano-states";
64

75
type SocialPreviewProps = {
86
ogImageUrl?: string;
@@ -33,8 +31,6 @@ export const SocialPreview = ({
3331
ogTitle,
3432
ogUrl,
3533
}: SocialPreviewProps) => {
36-
const imageLoader = useStore($imageLoader);
37-
3834
return (
3935
<Grid gap={1}>
4036
<Label>Social Sharing Preview</Label>
@@ -49,7 +45,7 @@ export const SocialPreview = ({
4945
>
5046
<Image
5147
src={ogImageUrl}
52-
loader={imageLoader}
48+
loader={wsImageLoader}
5349
className={imgStyle({
5450
hasImage:
5551
ogImageUrl === undefined || ogImageUrl === "" ? false : true,

apps/builder/app/builder/features/project-settings/section-general.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@ import {
1717
TextArea,
1818
} from "@webstudio-is/design-system";
1919
import { InfoCircleIcon } from "@webstudio-is/icons";
20-
import { ImageControl } from "./image-control";
21-
import { Image } from "@webstudio-is/image";
20+
import { Image, wsImageLoader } from "@webstudio-is/image";
2221
import type { ProjectMeta } from "@webstudio-is/sdk";
23-
import {
24-
$assets,
25-
$imageLoader,
26-
$pages,
27-
$userPlanFeatures,
28-
} from "~/shared/nano-states";
22+
import { ImageControl } from "./image-control";
23+
import { $assets, $pages, $userPlanFeatures } from "~/shared/nano-states";
2924
import { serverSyncStore } from "~/shared/sync";
3025
import { sectionSpacing } from "./utils";
3126
import { CodeEditor } from "~/builder/shared/code-editor";
@@ -96,7 +91,6 @@ export const SectionGeneral = () => {
9691
const assets = useStore($assets);
9792
const asset = assets.get(meta.faviconAssetId ?? "");
9893
const favIconUrl = asset ? `${asset.name}` : undefined;
99-
const imageLoader = useStore($imageLoader);
10094

10195
const handleSave = <Name extends keyof ProjectMeta>(
10296
name: keyof ProjectMeta
@@ -176,7 +170,7 @@ export const SectionGeneral = () => {
176170
height={72}
177171
className={imgStyle()}
178172
src={favIconUrl}
179-
loader={imageLoader}
173+
loader={wsImageLoader}
180174
/>
181175

182176
<Grid gap={2}>

apps/builder/app/builder/features/project-settings/section-marketplace.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@ import {
1616
Select,
1717
Box,
1818
} from "@webstudio-is/design-system";
19-
import { ImageControl } from "./image-control";
20-
import {
21-
$assets,
22-
$imageLoader,
23-
$marketplaceProduct,
24-
$project,
25-
} from "~/shared/nano-states";
26-
import { Image } from "@webstudio-is/image";
27-
import { useIds } from "~/shared/form-utils";
19+
import { Image, wsImageLoader } from "@webstudio-is/image";
2820
import { useState } from "react";
2921
import {
3022
MarketplaceProduct,
3123
marketplaceCategories,
3224
} from "@webstudio-is/project-build";
33-
import { serverSyncStore } from "~/shared/sync";
25+
import { ImageControl } from "./image-control";
26+
import { $assets, $marketplaceProduct, $project } from "~/shared/nano-states";
27+
import { useIds } from "~/shared/form-utils";
3428
import { MarketplaceApprovalStatus } from "@webstudio-is/project";
29+
import { serverSyncStore } from "~/shared/sync";
3530
import { trpcClient } from "~/shared/trpc/trpc-client";
3631
import { rightPanelWidth, sectionSpacing } from "./utils";
3732

@@ -123,7 +118,6 @@ const useMarketplaceApprovalStatus = () => {
123118

124119
export const SectionMarketplace = () => {
125120
const project = useStore($project);
126-
const imageLoader = useStore($imageLoader);
127121
const approval = useMarketplaceApprovalStatus();
128122
const [data, setData] = useState(() => $marketplaceProduct.get());
129123
const ids = useIds([
@@ -221,7 +215,7 @@ export const SectionMarketplace = () => {
221215
hasAsset: asset !== undefined,
222216
})}
223217
src={asset ? `${asset.name}` : undefined}
224-
loader={imageLoader}
218+
loader={wsImageLoader}
225219
/>
226220
</Box>
227221

apps/builder/app/builder/features/style-panel/sections/backgrounds/background-thumbnail.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useStore } from "@nanostores/react";
22
import type { Assets } from "@webstudio-is/sdk";
3-
import { Image as WebstudioImage } from "@webstudio-is/image";
3+
import { Image as WebstudioImage, wsImageLoader } from "@webstudio-is/image";
44
import { styled, theme } from "@webstudio-is/design-system";
55
import {
66
StyleValue,
77
toValue,
88
type StyleProperty,
99
} from "@webstudio-is/css-engine";
10-
import { $assets, $imageLoader } from "~/shared/nano-states";
10+
import { $assets } from "~/shared/nano-states";
1111
import brokenImage from "~/shared/images/broken-image-placeholder.svg";
1212
import { toPascalCase } from "../../shared/keyword-utils";
1313
import { useComputedStyles } from "../../shared/model";
@@ -115,7 +115,6 @@ type RepeatedProperty = (typeof repeatedProperties)[number];
115115

116116
export const BackgroundThumbnail = ({ index }: { index: number }) => {
117117
const assets = useStore($assets);
118-
const imageLoader = useStore($imageLoader);
119118
const styles = useComputedStyles(repeatedProperties);
120119
const [backgroundImage] = styles;
121120
const backgroundImageValue = getComputedRepeatedItem(backgroundImage, index);
@@ -131,7 +130,7 @@ export const BackgroundThumbnail = ({ index }: { index: number }) => {
131130
return (
132131
<StyledWebstudioImage
133132
key={asset.id}
134-
loader={imageLoader}
133+
loader={wsImageLoader}
135134
src={asset.name}
136135
width={thumbSize}
137136
optimize={true}
@@ -146,7 +145,7 @@ export const BackgroundThumbnail = ({ index }: { index: number }) => {
146145
return (
147146
<StyledWebstudioImage
148147
key={backgroundImageValue.value.url}
149-
loader={imageLoader}
148+
loader={wsImageLoader}
150149
src={backgroundImageValue.value.url}
151150
width={thumbSize}
152151
optimize={true}

apps/builder/app/builder/shared/assets/use-assets.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useMemo } from "react";
2+
import { computed } from "nanostores";
23
import { useStore } from "@nanostores/react";
34
import warnOnce from "warn-once";
45
import type { Asset } from "@webstudio-is/sdk";
@@ -19,19 +20,16 @@ import {
1920
$uploadingFilesDataStore,
2021
type UploadingFileData,
2122
} from "~/shared/nano-states";
22-
import { computed } from "nanostores";
2323
import { serverSyncStore } from "~/shared/sync";
24-
2524
import {
2625
getFileName,
2726
getMimeType,
2827
getSha256Hash,
2928
getSha256HashOfFile,
3029
uploadingFileDataToAsset,
3130
} from "./asset-utils";
32-
import { Image } from "@webstudio-is/image";
31+
import { Image, wsImageLoader } from "@webstudio-is/image";
3332
import invariant from "tiny-invariant";
34-
import { $imageLoader } from "~/shared/nano-states";
3533
import { fetch } from "~/shared/fetch.client";
3634

3735
export const deleteAssets = (assetIds: Asset["id"][]) => {
@@ -251,16 +249,14 @@ const imageWidth = css({
251249
});
252250

253251
const ToastImageInfo = ({ objectURL }: { objectURL: string }) => {
254-
const imageLoader = useStore($imageLoader);
255-
256252
return (
257253
<Box css={{ width: theme.spacing[18] }}>
258254
<Image
259255
className={imageWidth()}
260256
src={objectURL}
261257
optimize={false}
262258
width={64}
263-
loader={imageLoader}
259+
loader={wsImageLoader}
264260
/>
265261
</Box>
266262
);

apps/builder/app/builder/shared/image-manager/image.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import { useStore } from "@nanostores/react";
2-
3-
import { Image as WebstudioImage } from "@webstudio-is/image";
4-
5-
import { $imageLoader } from "~/shared/nano-states";
1+
import { Image as WebstudioImage, wsImageLoader } from "@webstudio-is/image";
62

73
type ImageProps = {
84
assetId: string;
@@ -22,7 +18,6 @@ export const Image = ({
2218
width,
2319
}: ImageProps) => {
2420
const optimize = objectURL === undefined;
25-
const imageLoader = useStore($imageLoader);
2621

2722
// Avoid image flickering on switching from preview to asset (during upload)
2823
// Possible optimisation, we can set it to "sync" only if asset.path has changed or add isNew prop to UploadedAssetContainer
@@ -43,7 +38,7 @@ export const Image = ({
4338
maxWidth: "100%",
4439
}}
4540
key={assetId}
46-
loader={imageLoader}
41+
loader={wsImageLoader}
4742
decoding={decoding}
4843
src={src}
4944
width={width}

apps/builder/app/canvas/canvas.tsx

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
coreMetas,
99
corePropsMetas,
1010
} from "@webstudio-is/react-sdk";
11+
import { wsImageLoader } from "@webstudio-is/image";
1112
import { ReactSdkContext } from "@webstudio-is/react-sdk/runtime";
1213
import * as baseComponents from "@webstudio-is/sdk-components-react";
1314
import * as baseComponentMetas from "@webstudio-is/sdk-components-react/metas";
@@ -61,7 +62,6 @@ import { subscribeInstanceHovering } from "./instance-hovering";
6162
import { useHashLinkSync } from "~/shared/pages";
6263
import { useMount } from "~/shared/hook-utils/use-mount";
6364
import { subscribeInterceptedEvents } from "./interceptor";
64-
import { createImageLoader } from "@webstudio-is/image";
6565
import { subscribeCommands } from "~/canvas/shared/commands";
6666
import { updateCollaborativeInstanceRect } from "./collaborative-instance";
6767
import { $params } from "./stores";
@@ -102,7 +102,6 @@ const useElementsTree = (
102102
const page = useStore($selectedPage);
103103
const isPreviewMode = useStore($isPreviewMode);
104104
const rootInstanceId = page?.rootInstanceId ?? "";
105-
const imageLoader = useMemo(() => createImageLoader({}), []);
106105

107106
if (typeof window === "undefined") {
108107
// @todo remove after https://github.com/webstudio-is/webstudio/issues/1313 now its needed to be sure that no leaks exists
@@ -120,7 +119,7 @@ const useElementsTree = (
120119
value={{
121120
renderer: isPreviewMode ? "preview" : "canvas",
122121
assetBaseUrl: params.assetBaseUrl,
123-
imageLoader,
122+
imageLoader: wsImageLoader,
124123
resources: {},
125124
}}
126125
>
@@ -135,14 +134,7 @@ const useElementsTree = (
135134
})}
136135
</ReactSdkContext.Provider>
137136
);
138-
}, [
139-
params,
140-
instances,
141-
rootInstanceId,
142-
components,
143-
isPreviewMode,
144-
imageLoader,
145-
]);
137+
}, [params, instances, rootInstanceId, components, isPreviewMode]);
146138
};
147139

148140
const DesignMode = () => {

0 commit comments

Comments
 (0)