@shopify/hydrogen 2023.4.1 → 2023.4.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.
@@ -422,7 +422,7 @@ var warnOnce = (string) => {
422
422
  };
423
423
 
424
424
  // src/version.ts
425
- var LIB_VERSION = "2023.4.1";
425
+ var LIB_VERSION = "2023.4.2";
426
426
 
427
427
  // src/storefront.ts
428
428
  var StorefrontApiError = class extends Error {
@@ -1198,6 +1198,148 @@ function recursivelyInvokeOrReturn(value, ...rest) {
1198
1198
  }
1199
1199
  return value;
1200
1200
  }
1201
+ function Pagination({
1202
+ connection,
1203
+ children = () => {
1204
+ console.warn("<Pagination> requires children to work properly");
1205
+ return null;
1206
+ }
1207
+ }) {
1208
+ const transition = react$1.useNavigation();
1209
+ const isLoading = transition.state === "loading";
1210
+ const {
1211
+ endCursor,
1212
+ hasNextPage,
1213
+ hasPreviousPage,
1214
+ nextPageUrl,
1215
+ nodes,
1216
+ previousPageUrl,
1217
+ startCursor
1218
+ } = usePagination(connection);
1219
+ const state = react.useMemo(
1220
+ () => ({
1221
+ pageInfo: {
1222
+ endCursor,
1223
+ hasPreviousPage,
1224
+ startCursor
1225
+ },
1226
+ nodes
1227
+ }),
1228
+ [endCursor, hasPreviousPage, startCursor, nodes]
1229
+ );
1230
+ const NextLink = react.useMemo(
1231
+ () => function NextLink2(props) {
1232
+ return hasNextPage ? react.createElement(react$1.Link, {
1233
+ preventScrollReset: true,
1234
+ ...props,
1235
+ to: nextPageUrl,
1236
+ state,
1237
+ replace: true
1238
+ }) : null;
1239
+ },
1240
+ [hasNextPage, nextPageUrl]
1241
+ );
1242
+ const PreviousLink = react.useMemo(
1243
+ () => function PrevLink(props) {
1244
+ return hasPreviousPage ? react.createElement(react$1.Link, {
1245
+ preventScrollReset: true,
1246
+ ...props,
1247
+ to: previousPageUrl,
1248
+ state,
1249
+ replace: true
1250
+ }) : null;
1251
+ },
1252
+ [hasPreviousPage, previousPageUrl]
1253
+ );
1254
+ return children({
1255
+ state,
1256
+ hasNextPage,
1257
+ hasPreviousPage,
1258
+ isLoading,
1259
+ nextPageUrl,
1260
+ nodes,
1261
+ previousPageUrl,
1262
+ NextLink,
1263
+ PreviousLink
1264
+ });
1265
+ }
1266
+ function usePagination(connection) {
1267
+ const { state, search } = react$1.useLocation();
1268
+ const params = new URLSearchParams(search);
1269
+ const direction = params.get("direction");
1270
+ const isPrevious = direction === "previous";
1271
+ const nodes = react.useMemo(() => {
1272
+ if (!state || !state?.nodes) {
1273
+ return hydrogenReact.flattenConnection(connection);
1274
+ }
1275
+ if (isPrevious) {
1276
+ return [...hydrogenReact.flattenConnection(connection), ...state.nodes];
1277
+ } else {
1278
+ return [...state.nodes, ...hydrogenReact.flattenConnection(connection)];
1279
+ }
1280
+ }, [state, connection]);
1281
+ const currentPageInfo = react.useMemo(() => {
1282
+ let pageStartCursor = state?.pageInfo?.startCursor === void 0 ? connection.pageInfo.startCursor : state.pageInfo.startCursor;
1283
+ let pageEndCursor = state?.pageInfo?.endCursor === void 0 ? connection.pageInfo.endCursor : state.pageInfo.endCursor;
1284
+ if (state?.nodes) {
1285
+ if (isPrevious) {
1286
+ pageStartCursor = connection.pageInfo.startCursor;
1287
+ } else {
1288
+ pageEndCursor = connection.pageInfo.endCursor;
1289
+ }
1290
+ }
1291
+ const previousPageExists = state?.pageInfo?.hasPreviousPage === void 0 ? connection.pageInfo.hasPreviousPage : state.pageInfo.hasPreviousPage;
1292
+ const nextPageExists = state?.pageInfo?.hasNextPage === void 0 ? connection.pageInfo.hasNextPage : state.pageInfo.hasNextPage;
1293
+ return {
1294
+ startCursor: pageStartCursor,
1295
+ endCursor: pageEndCursor,
1296
+ hasPreviousPage: previousPageExists,
1297
+ hasNextPage: nextPageExists
1298
+ };
1299
+ }, [
1300
+ isPrevious,
1301
+ state,
1302
+ connection.pageInfo.hasNextPage,
1303
+ connection.pageInfo.hasPreviousPage,
1304
+ connection.pageInfo.startCursor,
1305
+ connection.pageInfo.endCursor
1306
+ ]);
1307
+ const previousPageUrl = react.useMemo(() => {
1308
+ const params2 = new URLSearchParams(search);
1309
+ params2.set("direction", "previous");
1310
+ currentPageInfo.startCursor && params2.set("cursor", currentPageInfo.startCursor);
1311
+ return `?${params2.toString()}`;
1312
+ }, [search, currentPageInfo.startCursor]);
1313
+ const nextPageUrl = react.useMemo(() => {
1314
+ const params2 = new URLSearchParams(search);
1315
+ params2.set("direction", "next");
1316
+ currentPageInfo.endCursor && params2.set("cursor", currentPageInfo.endCursor);
1317
+ return `?${params2.toString()}`;
1318
+ }, [search, currentPageInfo.endCursor]);
1319
+ return { ...currentPageInfo, previousPageUrl, nextPageUrl, nodes };
1320
+ }
1321
+ function getPaginationVariables(request, options = { pageBy: 20 }) {
1322
+ if (!(request instanceof Request)) {
1323
+ throw new Error(
1324
+ "getPaginationVariables must be called with the Request object passed to your loader function"
1325
+ );
1326
+ }
1327
+ const { pageBy } = options;
1328
+ const searchParams = new URLSearchParams(new URL(request.url).search);
1329
+ const cursor = searchParams.get("cursor") ?? void 0;
1330
+ const direction = searchParams.get("direction") === "previous" ? "previous" : "next";
1331
+ const isPrevious = direction === "previous";
1332
+ const prevPage = {
1333
+ last: pageBy,
1334
+ startCursor: cursor ?? null
1335
+ };
1336
+ const nextPage = {
1337
+ first: pageBy,
1338
+ endCursor: cursor ?? null
1339
+ };
1340
+ const variables = isPrevious ? prevPage : nextPage;
1341
+ return variables;
1342
+ }
1201
1343
 
1202
1344
  Object.defineProperty(exports, 'AnalyticsEventName', {
1203
1345
  enumerable: true,
@@ -1284,10 +1426,12 @@ exports.CacheLong = CacheLong;
1284
1426
  exports.CacheNone = CacheNone;
1285
1427
  exports.CacheShort = CacheShort;
1286
1428
  exports.InMemoryCache = InMemoryCache;
1429
+ exports.Pagination__unstable = Pagination;
1287
1430
  exports.Seo = Seo;
1288
1431
  exports.createStorefrontClient = createStorefrontClient;
1289
1432
  exports.createWithCache_unstable = createWithCache_unstable;
1290
1433
  exports.generateCacheControlHeader = generateCacheControlHeader;
1434
+ exports.getPaginationVariables__unstable = getPaginationVariables;
1291
1435
  exports.graphiqlLoader = graphiqlLoader;
1292
1436
  exports.isStorefrontApiError = isStorefrontApiError;
1293
1437
  exports.storefrontRedirect = storefrontRedirect;