@graphcommerce/graphcms-ui 8.1.0-canary.41 → 8.1.0-canary.43

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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # Change Log
2
2
 
3
+ ## 8.1.0-canary.43
4
+
5
+ ## 8.1.0-canary.42
6
+
7
+ ### Patch Changes
8
+
9
+ - [#2308](https://github.com/graphcommerce-org/graphcommerce/pull/2308) [`8faa5ac`](https://github.com/graphcommerce-org/graphcommerce/commit/8faa5ac618ecfdacd6d5eb751b4110c423aef97f) - Added Draft Mode support. When enabled it will be shown.
10
+ ([@paales](https://github.com/paales))
11
+
3
12
  ## 8.1.0-canary.41
4
13
 
5
14
  ## 8.1.0-canary.40
package/index.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './components'
2
2
  export * from './graphql'
3
3
  export * from './lib'
4
- export * from './links/createHygraphLink'
@@ -1,4 +1,5 @@
1
1
  import type { ApolloClient, NormalizedCacheObject, ApolloQueryResult } from '@apollo/client'
2
+ import { cacheFirst } from '@graphcommerce/graphql'
2
3
  import { HygraphAllPagesDocument, HygraphAllPagesQuery } from '../graphql'
3
4
 
4
5
  type Urls = { url: string }
@@ -8,10 +9,11 @@ export async function getAllHygraphPages(
8
9
  options: { pageSize?: number } = {},
9
10
  ) {
10
11
  const { pageSize = 100 } = options
12
+
11
13
  const query = client.query({
12
14
  query: HygraphAllPagesDocument,
13
15
  variables: { first: pageSize },
14
- fetchPolicy: process.env.NODE_ENV !== 'development' ? 'cache-first' : undefined,
16
+ fetchPolicy: cacheFirst(client),
15
17
  })
16
18
  const pages: Promise<ApolloQueryResult<HygraphAllPagesQuery>>[] = [query]
17
19
 
@@ -24,7 +26,7 @@ export async function getAllHygraphPages(
24
26
  client.query({
25
27
  query: HygraphAllPagesDocument,
26
28
  variables: { first: pageSize, skip: pageSize * (i - 1) },
27
- fetchPolicy: process.env.NODE_ENV !== 'development' ? 'cache-first' : undefined,
29
+ fetchPolicy: cacheFirst(client),
28
30
  }),
29
31
  )
30
32
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/graphcms-ui",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "8.1.0-canary.41",
5
+ "version": "8.1.0-canary.43",
6
6
  "sideEffects": false,
7
7
  "prettier": "@graphcommerce/prettier-config-pwa",
8
8
  "eslintConfig": {
@@ -12,12 +12,13 @@
12
12
  }
13
13
  },
14
14
  "peerDependencies": {
15
- "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.41",
16
- "@graphcommerce/graphql": "^8.1.0-canary.41",
17
- "@graphcommerce/image": "^8.1.0-canary.41",
18
- "@graphcommerce/next-ui": "^8.1.0-canary.41",
19
- "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.41",
20
- "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.41",
15
+ "@graphcommerce/ecommerce-ui": "^8.1.0-canary.43",
16
+ "@graphcommerce/eslint-config-pwa": "^8.1.0-canary.43",
17
+ "@graphcommerce/graphql": "^8.1.0-canary.43",
18
+ "@graphcommerce/image": "^8.1.0-canary.43",
19
+ "@graphcommerce/next-ui": "^8.1.0-canary.43",
20
+ "@graphcommerce/prettier-config-pwa": "^8.1.0-canary.43",
21
+ "@graphcommerce/typescript-config-pwa": "^8.1.0-canary.43",
21
22
  "@mui/material": "^5.10.16",
22
23
  "next": "*",
23
24
  "react": "^18.2.0",
@@ -0,0 +1,80 @@
1
+ import {
2
+ usePreviewModeForm,
3
+ type PreviewModeToolbarProps,
4
+ SelectElement,
5
+ previewModeDefaults,
6
+ } from '@graphcommerce/ecommerce-ui'
7
+ import { TypedDocumentNode, gql, useQuery } from '@graphcommerce/graphql'
8
+ import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
9
+ import { filterNonNullableKeys } from '@graphcommerce/next-ui'
10
+ import React, { useMemo } from 'react'
11
+
12
+ export const config: PluginConfig = {
13
+ type: 'component',
14
+ module: '@graphcommerce/ecommerce-ui',
15
+ }
16
+
17
+ const ContentStages = gql`
18
+ query {
19
+ __type(name: "Stage") {
20
+ name
21
+ enumValues {
22
+ name
23
+ description
24
+ }
25
+ }
26
+ }
27
+ ` as TypedDocumentNode<{
28
+ __type: {
29
+ name: 'Stage'
30
+ enumValues: {
31
+ name: string
32
+ description: string
33
+ }[]
34
+ }
35
+ }>
36
+
37
+ const HygraphConfig = React.memo(() => {
38
+ const form = usePreviewModeForm()
39
+ const { control } = form
40
+
41
+ const contentStages = useQuery(ContentStages)
42
+
43
+ const defaultValue = previewModeDefaults().hygraphStage ?? 'PUBLISHED'
44
+
45
+ return useMemo(
46
+ () => (
47
+ <SelectElement
48
+ control={control}
49
+ name='previewData.hygraphStage'
50
+ defaultValue={defaultValue}
51
+ color='secondary'
52
+ select
53
+ label='Hygraph Stage'
54
+ size='small'
55
+ sx={{ width: '150px' }}
56
+ SelectProps={{ MenuProps: { style: { zIndex: 20000 } } }}
57
+ onChange={() => {}}
58
+ options={
59
+ contentStages.loading
60
+ ? [{ id: defaultValue, label: defaultValue }]
61
+ : filterNonNullableKeys(contentStages.data?.__type.enumValues).map(({ name }) => ({
62
+ id: name,
63
+ label: name,
64
+ }))
65
+ }
66
+ />
67
+ ),
68
+ [contentStages.data?.__type.enumValues, contentStages.loading, control],
69
+ )
70
+ })
71
+
72
+ export const PreviewModeToolbar = (props: PluginProps<PreviewModeToolbarProps>) => {
73
+ const { Prev, ...rest } = props
74
+ return (
75
+ <>
76
+ <Prev {...rest} />
77
+ <HygraphConfig />
78
+ </>
79
+ )
80
+ }
@@ -4,6 +4,12 @@ import type { FunctionPlugin } from '@graphcommerce/next-config'
4
4
  export const func = 'graphqlConfig'
5
5
  export const exported = '@graphcommerce/graphql'
6
6
 
7
+ declare module '@graphcommerce/graphql/config' {
8
+ interface PreviewData {
9
+ hygraphStage?: string
10
+ }
11
+ }
12
+
7
13
  const hygraphGraphqlConfig: FunctionPlugin<typeof graphqlConfig> = (prev, config) => {
8
14
  const results = prev(config)
9
15
 
@@ -14,6 +20,12 @@ const hygraphGraphqlConfig: FunctionPlugin<typeof graphqlConfig> = (prev, config
14
20
  const hygraphLink = setContext((_, context) => {
15
21
  if (!context.headers) context.headers = {}
16
22
  context.headers['gcms-locales'] = locales.join(',')
23
+
24
+ const stage = config.previewData?.hygraphStage ?? 'DRAFT'
25
+ if (config.preview) {
26
+ context.headers['gcms-stage'] = stage
27
+ }
28
+
17
29
  return context
18
30
  })
19
31
 
@@ -0,0 +1,12 @@
1
+ import { previewModeDefaults as base } from '@graphcommerce/ecommerce-ui'
2
+ import type { FunctionPlugin, PluginConfig } from '@graphcommerce/next-config'
3
+
4
+ export const config: PluginConfig = {
5
+ type: 'function',
6
+ module: '@graphcommerce/ecommerce-ui',
7
+ }
8
+
9
+ export const previewModeDefaults: FunctionPlugin<typeof base> = (prev, ...args) => ({
10
+ ...prev(...args),
11
+ hygraphStage: 'DRAFT',
12
+ })
@@ -1,13 +0,0 @@
1
- import { setContext } from '@graphcommerce/graphql'
2
- import { storefrontConfig } from '@graphcommerce/next-ui'
3
-
4
- /** Add a gcms-locales header to make sure queries return in a certain language. */
5
- export const createHygraphLink = (locale?: string) =>
6
- setContext((_, context) => {
7
- if (!context.headers) context.headers = {}
8
-
9
- const locales = storefrontConfig(locale)?.hygraphLocales
10
- if (locales) context.headers['gcms-locales'] = locales.join(',')
11
-
12
- return context
13
- })