@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.
@@ -1,8 +1,8 @@
1
- import { createStorefrontClient as createStorefrontClient$1, SHOPIFY_STOREFRONT_ID_HEADER, getShopifyCookies, SHOPIFY_Y, SHOPIFY_STOREFRONT_Y_HEADER, SHOPIFY_S, SHOPIFY_STOREFRONT_S_HEADER } from '@shopify/hydrogen-react';
1
+ import { createStorefrontClient as createStorefrontClient$1, SHOPIFY_STOREFRONT_ID_HEADER, getShopifyCookies, SHOPIFY_Y, SHOPIFY_STOREFRONT_Y_HEADER, SHOPIFY_S, SHOPIFY_STOREFRONT_S_HEADER, flattenConnection } from '@shopify/hydrogen-react';
2
2
  export { AnalyticsEventName, AnalyticsPageType, ExternalVideo, IMAGE_FRAGMENT, Image, MediaFile, ModelViewer, Money, ShopPayButton, ShopifySalesChannel, Video, flattenConnection, getClientBrowserParameters, getShopifyCookies, parseGid, parseMetafield, sendShopifyAnalytics, storefrontApiCustomScalars, useMoney, useShopifyCookies } from '@shopify/hydrogen-react';
3
3
  import { redirect } from '@remix-run/server-runtime';
4
4
  import { lazy, useMemo, createElement, Suspense, Fragment } from 'react';
5
- import { useMatches, useLocation } from '@remix-run/react';
5
+ import { useMatches, useLocation, useNavigation, Link } from '@remix-run/react';
6
6
 
7
7
  // src/storefront.ts
8
8
 
@@ -332,7 +332,7 @@ var warnOnce = (string) => {
332
332
  };
333
333
 
334
334
  // src/version.ts
335
- var LIB_VERSION = "2023.4.1";
335
+ var LIB_VERSION = "2023.4.2";
336
336
 
337
337
  // src/storefront.ts
338
338
  var StorefrontApiError = class extends Error {
@@ -1108,7 +1108,149 @@ function recursivelyInvokeOrReturn(value, ...rest) {
1108
1108
  }
1109
1109
  return value;
1110
1110
  }
1111
+ function Pagination({
1112
+ connection,
1113
+ children = () => {
1114
+ console.warn("<Pagination> requires children to work properly");
1115
+ return null;
1116
+ }
1117
+ }) {
1118
+ const transition = useNavigation();
1119
+ const isLoading = transition.state === "loading";
1120
+ const {
1121
+ endCursor,
1122
+ hasNextPage,
1123
+ hasPreviousPage,
1124
+ nextPageUrl,
1125
+ nodes,
1126
+ previousPageUrl,
1127
+ startCursor
1128
+ } = usePagination(connection);
1129
+ const state = useMemo(
1130
+ () => ({
1131
+ pageInfo: {
1132
+ endCursor,
1133
+ hasPreviousPage,
1134
+ startCursor
1135
+ },
1136
+ nodes
1137
+ }),
1138
+ [endCursor, hasPreviousPage, startCursor, nodes]
1139
+ );
1140
+ const NextLink = useMemo(
1141
+ () => function NextLink2(props) {
1142
+ return hasNextPage ? createElement(Link, {
1143
+ preventScrollReset: true,
1144
+ ...props,
1145
+ to: nextPageUrl,
1146
+ state,
1147
+ replace: true
1148
+ }) : null;
1149
+ },
1150
+ [hasNextPage, nextPageUrl]
1151
+ );
1152
+ const PreviousLink = useMemo(
1153
+ () => function PrevLink(props) {
1154
+ return hasPreviousPage ? createElement(Link, {
1155
+ preventScrollReset: true,
1156
+ ...props,
1157
+ to: previousPageUrl,
1158
+ state,
1159
+ replace: true
1160
+ }) : null;
1161
+ },
1162
+ [hasPreviousPage, previousPageUrl]
1163
+ );
1164
+ return children({
1165
+ state,
1166
+ hasNextPage,
1167
+ hasPreviousPage,
1168
+ isLoading,
1169
+ nextPageUrl,
1170
+ nodes,
1171
+ previousPageUrl,
1172
+ NextLink,
1173
+ PreviousLink
1174
+ });
1175
+ }
1176
+ function usePagination(connection) {
1177
+ const { state, search } = useLocation();
1178
+ const params = new URLSearchParams(search);
1179
+ const direction = params.get("direction");
1180
+ const isPrevious = direction === "previous";
1181
+ const nodes = useMemo(() => {
1182
+ if (!state || !state?.nodes) {
1183
+ return flattenConnection(connection);
1184
+ }
1185
+ if (isPrevious) {
1186
+ return [...flattenConnection(connection), ...state.nodes];
1187
+ } else {
1188
+ return [...state.nodes, ...flattenConnection(connection)];
1189
+ }
1190
+ }, [state, connection]);
1191
+ const currentPageInfo = useMemo(() => {
1192
+ let pageStartCursor = state?.pageInfo?.startCursor === void 0 ? connection.pageInfo.startCursor : state.pageInfo.startCursor;
1193
+ let pageEndCursor = state?.pageInfo?.endCursor === void 0 ? connection.pageInfo.endCursor : state.pageInfo.endCursor;
1194
+ if (state?.nodes) {
1195
+ if (isPrevious) {
1196
+ pageStartCursor = connection.pageInfo.startCursor;
1197
+ } else {
1198
+ pageEndCursor = connection.pageInfo.endCursor;
1199
+ }
1200
+ }
1201
+ const previousPageExists = state?.pageInfo?.hasPreviousPage === void 0 ? connection.pageInfo.hasPreviousPage : state.pageInfo.hasPreviousPage;
1202
+ const nextPageExists = state?.pageInfo?.hasNextPage === void 0 ? connection.pageInfo.hasNextPage : state.pageInfo.hasNextPage;
1203
+ return {
1204
+ startCursor: pageStartCursor,
1205
+ endCursor: pageEndCursor,
1206
+ hasPreviousPage: previousPageExists,
1207
+ hasNextPage: nextPageExists
1208
+ };
1209
+ }, [
1210
+ isPrevious,
1211
+ state,
1212
+ connection.pageInfo.hasNextPage,
1213
+ connection.pageInfo.hasPreviousPage,
1214
+ connection.pageInfo.startCursor,
1215
+ connection.pageInfo.endCursor
1216
+ ]);
1217
+ const previousPageUrl = useMemo(() => {
1218
+ const params2 = new URLSearchParams(search);
1219
+ params2.set("direction", "previous");
1220
+ currentPageInfo.startCursor && params2.set("cursor", currentPageInfo.startCursor);
1221
+ return `?${params2.toString()}`;
1222
+ }, [search, currentPageInfo.startCursor]);
1223
+ const nextPageUrl = useMemo(() => {
1224
+ const params2 = new URLSearchParams(search);
1225
+ params2.set("direction", "next");
1226
+ currentPageInfo.endCursor && params2.set("cursor", currentPageInfo.endCursor);
1227
+ return `?${params2.toString()}`;
1228
+ }, [search, currentPageInfo.endCursor]);
1229
+ return { ...currentPageInfo, previousPageUrl, nextPageUrl, nodes };
1230
+ }
1231
+ function getPaginationVariables(request, options = { pageBy: 20 }) {
1232
+ if (!(request instanceof Request)) {
1233
+ throw new Error(
1234
+ "getPaginationVariables must be called with the Request object passed to your loader function"
1235
+ );
1236
+ }
1237
+ const { pageBy } = options;
1238
+ const searchParams = new URLSearchParams(new URL(request.url).search);
1239
+ const cursor = searchParams.get("cursor") ?? void 0;
1240
+ const direction = searchParams.get("direction") === "previous" ? "previous" : "next";
1241
+ const isPrevious = direction === "previous";
1242
+ const prevPage = {
1243
+ last: pageBy,
1244
+ startCursor: cursor ?? null
1245
+ };
1246
+ const nextPage = {
1247
+ first: pageBy,
1248
+ endCursor: cursor ?? null
1249
+ };
1250
+ const variables = isPrevious ? prevPage : nextPage;
1251
+ return variables;
1252
+ }
1111
1253
 
1112
- export { CacheCustom, CacheLong, CacheNone, CacheShort, InMemoryCache, Seo, createStorefrontClient, createWithCache_unstable, generateCacheControlHeader, graphiqlLoader, isStorefrontApiError, storefrontRedirect };
1254
+ export { CacheCustom, CacheLong, CacheNone, CacheShort, InMemoryCache, Pagination as Pagination__unstable, Seo, createStorefrontClient, createWithCache_unstable, generateCacheControlHeader, getPaginationVariables as getPaginationVariables__unstable, graphiqlLoader, isStorefrontApiError, storefrontRedirect };
1113
1255
  //# sourceMappingURL=out.js.map
1114
1256
  //# sourceMappingURL=index.js.map