@atomic-solutions/woocommerce-react 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -630,7 +630,6 @@ var useCart = (options) => {
630
630
  var toWooError = (error) => error instanceof woocommerceApiClient.WooCommerceApiError ? error : new woocommerceApiClient.WooCommerceApiError({
631
631
  code: "unknown_error",
632
632
  message: error instanceof Error ? error.message : "Unknown error",
633
- statusCode: 500,
634
633
  url: "",
635
634
  method: "POST",
636
635
  originalCode: "unknown_error"
@@ -865,7 +864,6 @@ var useProcessCheckout = () => {
865
864
  const wooError = error instanceof woocommerceApiClient.WooCommerceApiError ? error : new woocommerceApiClient.WooCommerceApiError({
866
865
  code: "unknown_error",
867
866
  message: error instanceof Error ? error.message : "Unknown error",
868
- statusCode: 500,
869
867
  url: "",
870
868
  method: "POST",
871
869
  originalCode: "unknown_error"
@@ -981,64 +979,6 @@ function useStoreTheme(options = {}) {
981
979
  };
982
980
  }
983
981
 
984
- // src/utils/image.ts
985
- var SIZE_SUFFIXES = {
986
- thumbnail: "-150x150",
987
- medium: "-300x300",
988
- large: "-1024x1024",
989
- full: ""
990
- };
991
- function getPrimaryImage(images) {
992
- if (!images || images.length === 0) {
993
- return void 0;
994
- }
995
- return images[0];
996
- }
997
- function getImageUrl(images, size = "full", fallback) {
998
- const primary = getPrimaryImage(images);
999
- if (!primary) {
1000
- return fallback;
1001
- }
1002
- return getSizedImageUrl(primary.src, size);
1003
- }
1004
- function getSizedImageUrl(url, size = "full") {
1005
- if (!url || size === "full") {
1006
- return url;
1007
- }
1008
- const hasSize = Object.values(SIZE_SUFFIXES).some((suffix2) => suffix2 && url.includes(suffix2));
1009
- if (hasSize) {
1010
- let result = url;
1011
- Object.values(SIZE_SUFFIXES).forEach((suffix2) => {
1012
- if (suffix2) {
1013
- result = result.replace(suffix2, SIZE_SUFFIXES[size]);
1014
- }
1015
- });
1016
- return result;
1017
- }
1018
- const suffix = SIZE_SUFFIXES[size];
1019
- const lastDot = url.lastIndexOf(".");
1020
- if (lastDot === -1) {
1021
- return url;
1022
- }
1023
- return `${url.slice(0, lastDot)}${suffix}${url.slice(lastDot)}`;
1024
- }
1025
- function getAllImageUrls(images, size = "full") {
1026
- if (!images || images.length === 0) {
1027
- return [];
1028
- }
1029
- return images.map((img) => getSizedImageUrl(img.src, size));
1030
- }
1031
- var PLACEHOLDER_IMAGE_URL = "https://via.placeholder.com/300x300.png?text=No+Image";
1032
- function getImageUrlOrPlaceholder(images, size = "medium") {
1033
- return getImageUrl(images, size, PLACEHOLDER_IMAGE_URL) ?? PLACEHOLDER_IMAGE_URL;
1034
- }
1035
- async function preloadImage(_url) {
1036
- return Promise.resolve();
1037
- }
1038
- async function preloadImages(urls) {
1039
- await Promise.all(urls.map(preloadImage));
1040
- }
1041
-
1042
982
  // src/utils/price.ts
1043
983
  var DEFAULT_LOCALE = {
1044
984
  localeTag: "en-US",
@@ -1184,14 +1124,84 @@ function parseWooCommercePrice(priceString) {
1184
1124
  return parseFloat(cleaned.replace(/,/g, ""));
1185
1125
  }
1186
1126
 
1127
+ // src/utils/image.ts
1128
+ var SIZE_SUFFIXES = {
1129
+ thumbnail: "-150x150",
1130
+ medium: "-300x300",
1131
+ large: "-1024x1024",
1132
+ full: ""
1133
+ };
1134
+ function getPrimaryImage(images) {
1135
+ if (!images || images.length === 0) {
1136
+ return void 0;
1137
+ }
1138
+ return images[0];
1139
+ }
1140
+ function getImageUrl(images, size = "full", fallback) {
1141
+ const primary = getPrimaryImage(images);
1142
+ if (!primary) {
1143
+ return fallback;
1144
+ }
1145
+ return getSizedImageUrl(primary.src, size);
1146
+ }
1147
+ function getSizedImageUrl(url, size = "full") {
1148
+ if (!url || size === "full") {
1149
+ return url;
1150
+ }
1151
+ const hasSize = Object.values(SIZE_SUFFIXES).some((suffix2) => suffix2 && url.includes(suffix2));
1152
+ if (hasSize) {
1153
+ let result = url;
1154
+ Object.values(SIZE_SUFFIXES).forEach((suffix2) => {
1155
+ if (suffix2) {
1156
+ result = result.replace(suffix2, SIZE_SUFFIXES[size]);
1157
+ }
1158
+ });
1159
+ return result;
1160
+ }
1161
+ const suffix = SIZE_SUFFIXES[size];
1162
+ const lastDot = url.lastIndexOf(".");
1163
+ if (lastDot === -1) {
1164
+ return url;
1165
+ }
1166
+ return `${url.slice(0, lastDot)}${suffix}${url.slice(lastDot)}`;
1167
+ }
1168
+ function getAllImageUrls(images, size = "full") {
1169
+ if (!images || images.length === 0) {
1170
+ return [];
1171
+ }
1172
+ return images.map((img) => getSizedImageUrl(img.src, size));
1173
+ }
1174
+ var PLACEHOLDER_IMAGE_URL = "https://via.placeholder.com/300x300.png?text=No+Image";
1175
+ function getImageUrlOrPlaceholder(images, size = "medium") {
1176
+ return getImageUrl(images, size, PLACEHOLDER_IMAGE_URL) ?? PLACEHOLDER_IMAGE_URL;
1177
+ }
1178
+ async function preloadImage(_url) {
1179
+ return Promise.resolve();
1180
+ }
1181
+ async function preloadImages(urls) {
1182
+ await Promise.all(urls.map(preloadImage));
1183
+ }
1184
+
1185
+ // src/hooks/useProcessedImageUrl.ts
1186
+ var useProcessedImageUrl = (imageSize, fallbackOverride) => {
1187
+ const config = react.useContext(WooCommerceStoreContext)?.config;
1188
+ const transformer = config?.imageUrlTransformer;
1189
+ const fallbackImageUrl = fallbackOverride ?? config?.fallbackImageUrl;
1190
+ return react.useCallback(
1191
+ (images) => {
1192
+ const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
1193
+ return baseUrl && transformer ? transformer(baseUrl, imageSize) : baseUrl;
1194
+ },
1195
+ [imageSize, fallbackImageUrl, transformer]
1196
+ );
1197
+ };
1198
+
1187
1199
  // src/hooks/useProductsFeed.ts
1188
1200
  function useProductsFeed(options = {}) {
1189
1201
  const { params, imageSize = "medium" } = options;
1190
1202
  const storeContext = react.useContext(WooCommerceStoreContext);
1191
1203
  const storeConfig = storeContext?.config;
1192
1204
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
1193
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
1194
- const imageUrlTransformer = storeConfig?.imageUrlTransformer;
1195
1205
  const {
1196
1206
  data: productsData,
1197
1207
  isLoading,
@@ -1203,16 +1213,7 @@ function useProductsFeed(options = {}) {
1203
1213
  isRefetching
1204
1214
  } = useInfiniteProducts(params);
1205
1215
  const { treeData: categoryTree = [], findById } = useProductCategoryTree();
1206
- const getProcessedImageUrl = react.useCallback(
1207
- (images) => {
1208
- const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
1209
- if (baseUrl && imageUrlTransformer) {
1210
- return imageUrlTransformer(baseUrl, imageSize);
1211
- }
1212
- return baseUrl;
1213
- },
1214
- [imageSize, fallbackImageUrl, imageUrlTransformer]
1215
- );
1216
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1216
1217
  const products = react.useMemo(() => {
1217
1218
  if (!productsData) return [];
1218
1219
  const productList = Array.isArray(productsData) ? productsData : [];
@@ -1350,7 +1351,6 @@ function useProductView(options) {
1350
1351
  const storeContext = react.useContext(WooCommerceStoreContext);
1351
1352
  const storeConfig = storeContext?.config;
1352
1353
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
1353
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
1354
1354
  const imageUrlTransformer = storeConfig?.imageUrlTransformer;
1355
1355
  const [draftQuantity, setDraftQuantity] = react.useState(1);
1356
1356
  const { data: productData, isLoading, error, refetch } = useProduct(productId);
@@ -1358,6 +1358,7 @@ function useProductView(options) {
1358
1358
  const addToCartMutation = useAddToCart();
1359
1359
  const removeFromCartMutation = useRemoveFromCart();
1360
1360
  const updateCartMutation = useUpdateCartItem();
1361
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1361
1362
  const processImageUrl = react.useCallback(
1362
1363
  (url) => {
1363
1364
  if (!url) return url;
@@ -1373,7 +1374,6 @@ function useProductView(options) {
1373
1374
  const currencyCode = productData.prices?.currency_code;
1374
1375
  const stockStatus = productData.is_on_backorder ? "onbackorder" : productData.is_in_stock ? "instock" : "outofstock";
1375
1376
  const lowStockRemaining = productData.low_stock_remaining;
1376
- const baseImageUrl = getImageUrl(productData.images, imageSize, fallbackImageUrl);
1377
1377
  const baseImageUrls = getAllImageUrls(productData.images, imageSize);
1378
1378
  return {
1379
1379
  id: productData.id,
@@ -1383,7 +1383,7 @@ function useProductView(options) {
1383
1383
  description: productData.description,
1384
1384
  shortDescription: productData.short_description,
1385
1385
  sku: productData.sku,
1386
- imageUrl: processImageUrl(baseImageUrl),
1386
+ imageUrl: getProcessedImageUrl(productData.images),
1387
1387
  imageUrls: baseImageUrls.map((url) => processImageUrl(url) ?? url),
1388
1388
  price: formatStoreApiPriceIntl(productData.prices?.price, currencyCode, localeTag),
1389
1389
  regularPrice: formatStoreApiPriceIntl(
@@ -1413,7 +1413,7 @@ function useProductView(options) {
1413
1413
  minQuantity: productData.add_to_cart?.minimum ?? 1,
1414
1414
  raw: productData
1415
1415
  };
1416
- }, [productData, localeTag, fallbackImageUrl, imageSize, lowStockThreshold, processImageUrl]);
1416
+ }, [productData, localeTag, imageSize, lowStockThreshold, getProcessedImageUrl, processImageUrl]);
1417
1417
  const cartItem = react.useMemo(() => {
1418
1418
  return cartData?.items.find((item) => item.id === productId);
1419
1419
  }, [cartData?.items, productId]);
@@ -1496,24 +1496,13 @@ function useCartView(options = {}) {
1496
1496
  const storeContext = react.useContext(WooCommerceStoreContext);
1497
1497
  const storeConfig = storeContext?.config;
1498
1498
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
1499
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
1500
- const imageUrlTransformer = storeConfig?.imageUrlTransformer;
1501
1499
  const { data: cartData, isLoading, error, refetch } = useCart();
1502
1500
  const updateItemMutation = useUpdateCartItem();
1503
1501
  const removeItemMutation = useRemoveFromCart();
1504
1502
  const applyCouponMutation = useApplyCoupon();
1505
1503
  const removeCouponMutation = useRemoveCoupon();
1506
1504
  const selectShippingMutation = useSelectShippingRate();
1507
- const getProcessedImageUrl = react.useCallback(
1508
- (images) => {
1509
- const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
1510
- if (baseUrl && imageUrlTransformer) {
1511
- return imageUrlTransformer(baseUrl, imageSize);
1512
- }
1513
- return baseUrl;
1514
- },
1515
- [imageSize, fallbackImageUrl, imageUrlTransformer]
1516
- );
1505
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1517
1506
  const items = react.useMemo(() => {
1518
1507
  if (!cartData?.items) return [];
1519
1508
  const currencyCode = cartData.totals.currency_code;
@@ -1659,8 +1648,6 @@ function useReorderView(items, options = {}) {
1659
1648
  const storeContext = react.useContext(WooCommerceStoreContext);
1660
1649
  const storeConfig = storeContext?.config;
1661
1650
  const localeTag = options.localeTag ?? storeConfig?.locale?.localeTag ?? DEFAULT_STORE_CONFIG.locale.localeTag;
1662
- const fallbackImageUrl = options.fallbackImageUrl ?? storeConfig?.fallbackImageUrl;
1663
- const imageUrlTransformer = storeConfig?.imageUrlTransformer;
1664
1651
  const productIds = react.useMemo(() => [...new Set(items.map((item) => item.productId))], [items]);
1665
1652
  const {
1666
1653
  data: productsPage,
@@ -1671,16 +1658,7 @@ function useReorderView(items, options = {}) {
1671
1658
  { enabled: productIds.length > 0 }
1672
1659
  );
1673
1660
  const { data: cart } = useCart();
1674
- const getProcessedImageUrl = react.useCallback(
1675
- (images) => {
1676
- const baseUrl = getImageUrl(images, imageSize, fallbackImageUrl);
1677
- if (baseUrl && imageUrlTransformer) {
1678
- return imageUrlTransformer(baseUrl, imageSize);
1679
- }
1680
- return baseUrl;
1681
- },
1682
- [imageSize, fallbackImageUrl, imageUrlTransformer]
1683
- );
1661
+ const getProcessedImageUrl = useProcessedImageUrl(imageSize, options.fallbackImageUrl);
1684
1662
  const views = react.useMemo(() => {
1685
1663
  const products = new Map((productsPage?.data ?? []).map((product) => [product.id, product]));
1686
1664
  return items.map((item) => {