@graphcommerce/ecommerce-ui 8.1.0-canary.41 → 8.1.0-canary.42
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 +2 -0
- package/components/PreviewMode/LightTooltip.tsx +11 -0
- package/components/PreviewMode/PreviewMode.tsx +148 -0
- package/components/PreviewMode/PreviewModeActions.tsx +6 -0
- package/components/PreviewMode/PreviewModeToolbar.tsx +6 -0
- package/components/PreviewMode/index.ts +5 -0
- package/components/PreviewMode/previewModeDefaults.ts +5 -0
- package/components/PreviewMode/usePreviewModeForm.ts +6 -0
- package/components/index.ts +1 -0
- package/package.json +7 -7
- package/plugins/PreviewModeFramerNextPages.tsx +38 -0
- package/route/preview.ts +60 -0
package/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { styled, Tooltip, tooltipClasses } from '@mui/material'
|
|
2
|
+
|
|
3
|
+
export const LightTooltip = styled<typeof Tooltip>(({ className, ...props }) => (
|
|
4
|
+
<Tooltip {...props} classes={{ popper: className }} />
|
|
5
|
+
))(({ theme }) => ({
|
|
6
|
+
[`& .${tooltipClasses.tooltip}`]: {
|
|
7
|
+
backgroundColor: theme.palette.common.white,
|
|
8
|
+
color: theme.palette.text.primary,
|
|
9
|
+
boxShadow: theme.shadows[1],
|
|
10
|
+
},
|
|
11
|
+
}))
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { PreviewData } from '@graphcommerce/graphql'
|
|
2
|
+
import {
|
|
3
|
+
Button,
|
|
4
|
+
IconSvg,
|
|
5
|
+
MessageSnackbar,
|
|
6
|
+
iconChevronRight,
|
|
7
|
+
iconClose,
|
|
8
|
+
iconContrast,
|
|
9
|
+
iconInfo,
|
|
10
|
+
iconRefresh,
|
|
11
|
+
} from '@graphcommerce/next-ui'
|
|
12
|
+
import { FormAutoSubmit, FormPersist, FormProvider, useForm } from '@graphcommerce/react-hook-form'
|
|
13
|
+
import { Box, IconButton } from '@mui/material'
|
|
14
|
+
import { useRouter } from 'next/router'
|
|
15
|
+
import { TextFieldElement } from '../FormComponents'
|
|
16
|
+
import { LightTooltip } from './LightTooltip'
|
|
17
|
+
import { PreviewModeActions } from './PreviewModeActions'
|
|
18
|
+
import { PreviewModeToolbar } from './PreviewModeToolbar'
|
|
19
|
+
import { previewModeDefaults } from './previewModeDefaults'
|
|
20
|
+
|
|
21
|
+
export function getPreviewUrl() {
|
|
22
|
+
const url = new URL(window.location.href)
|
|
23
|
+
url.pathname = '/api/preview'
|
|
24
|
+
;[...url.searchParams.entries()].forEach(([key]) => url.searchParams.delete(key))
|
|
25
|
+
return url
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function PreviewModeEnabled() {
|
|
29
|
+
const form = useForm<{ secret: string; previewData: PreviewData }>({
|
|
30
|
+
defaultValues: { previewData: previewModeDefaults() },
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const submit = form.handleSubmit((formValues) => {
|
|
34
|
+
const url = getPreviewUrl()
|
|
35
|
+
url.searchParams.append('action', 'update')
|
|
36
|
+
|
|
37
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
38
|
+
url.searchParams.append(key, JSON.stringify(value))
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
window.location.href = url.toString()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const exitHandler = form.handleSubmit(() => {
|
|
45
|
+
const url = getPreviewUrl()
|
|
46
|
+
url.searchParams.append('action', 'exit')
|
|
47
|
+
|
|
48
|
+
window.location.href = url.toString()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const revalidateHandler = form.handleSubmit((formValues) => {
|
|
52
|
+
const url = getPreviewUrl()
|
|
53
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
54
|
+
url.searchParams.append(key, `${value}`)
|
|
55
|
+
})
|
|
56
|
+
url.searchParams.append('action', 'revalidate')
|
|
57
|
+
window.location.href = url.toString()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<FormProvider {...form}>
|
|
62
|
+
<MessageSnackbar
|
|
63
|
+
variant='pill'
|
|
64
|
+
severity='warning'
|
|
65
|
+
disableBackdropClick
|
|
66
|
+
disableClose
|
|
67
|
+
open
|
|
68
|
+
icon={iconContrast}
|
|
69
|
+
onClose={() => {}}
|
|
70
|
+
action={
|
|
71
|
+
<>
|
|
72
|
+
<PreviewModeActions />
|
|
73
|
+
<LightTooltip title='Revalidate / Regenerate Page' placement='top'>
|
|
74
|
+
<IconButton color='secondary' type='submit' onClick={revalidateHandler}>
|
|
75
|
+
<IconSvg src={iconRefresh} />
|
|
76
|
+
</IconButton>
|
|
77
|
+
</LightTooltip>
|
|
78
|
+
<LightTooltip title='Stop preview mode' placement='top'>
|
|
79
|
+
<IconButton color='secondary' type='submit' onClick={exitHandler}>
|
|
80
|
+
<IconSvg src={iconClose} />
|
|
81
|
+
</IconButton>
|
|
82
|
+
</LightTooltip>
|
|
83
|
+
</>
|
|
84
|
+
}
|
|
85
|
+
>
|
|
86
|
+
<Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
|
|
87
|
+
<Box sx={{ display: 'flex', placeItems: 'center' }}>
|
|
88
|
+
Preview Mode{' '}
|
|
89
|
+
<LightTooltip title='You are currently viewing the website in Preview Mode (caches are disabled).'>
|
|
90
|
+
<IconButton size='small'>
|
|
91
|
+
<IconSvg src={iconInfo} />
|
|
92
|
+
</IconButton>
|
|
93
|
+
</LightTooltip>
|
|
94
|
+
</Box>
|
|
95
|
+
<PreviewModeToolbar />
|
|
96
|
+
</Box>
|
|
97
|
+
</MessageSnackbar>
|
|
98
|
+
<FormPersist form={form} name='PreviewModePreviewData' />
|
|
99
|
+
<FormAutoSubmit control={form.control} submit={submit} />
|
|
100
|
+
</FormProvider>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function PreviewModeDisabled() {
|
|
105
|
+
const form = useForm<{ secret: string }>({})
|
|
106
|
+
|
|
107
|
+
const submit = form.handleSubmit((formValues) => {
|
|
108
|
+
const url = getPreviewUrl()
|
|
109
|
+
url.searchParams.append('action', 'enable')
|
|
110
|
+
|
|
111
|
+
Object.entries(formValues).forEach(([key, value]) => {
|
|
112
|
+
url.searchParams.append(key, typeof value === 'string' ? value : JSON.stringify(value))
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
window.location.href = url.toString()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<FormProvider {...form}>
|
|
120
|
+
<MessageSnackbar
|
|
121
|
+
variant='pill'
|
|
122
|
+
severity='warning'
|
|
123
|
+
disableBackdropClick
|
|
124
|
+
disableClose
|
|
125
|
+
open
|
|
126
|
+
icon={iconContrast}
|
|
127
|
+
onClose={() => {}}
|
|
128
|
+
action={
|
|
129
|
+
<IconButton color='secondary' type='submit' onClick={submit}>
|
|
130
|
+
<IconSvg src={iconChevronRight} />
|
|
131
|
+
</IconButton>
|
|
132
|
+
}
|
|
133
|
+
>
|
|
134
|
+
<Box sx={{ display: 'grid', gridAutoFlow: 'column', placeItems: 'center', gap: 4 }}>
|
|
135
|
+
<Box sx={{ display: 'flex', placeItems: 'center' }}>Preview Mode</Box>
|
|
136
|
+
<TextFieldElement control={form.control} name='secret' label='Secret' />
|
|
137
|
+
</Box>
|
|
138
|
+
</MessageSnackbar>
|
|
139
|
+
<FormPersist form={form} name='PreviewModePreviewData' />
|
|
140
|
+
</FormProvider>
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function PreviewMode() {
|
|
145
|
+
const router = useRouter()
|
|
146
|
+
|
|
147
|
+
return router.isPreview ? <PreviewModeEnabled /> : <PreviewModeDisabled />
|
|
148
|
+
}
|
package/components/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@graphcommerce/ecommerce-ui",
|
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
|
5
|
-
"version": "8.1.0-canary.
|
|
5
|
+
"version": "8.1.0-canary.42",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
8
8
|
"eslintConfig": {
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
14
|
"peerDependencies": {
|
|
15
|
-
"@graphcommerce/eslint-config-pwa": "^8.1.0-canary.
|
|
16
|
-
"@graphcommerce/graphql": "^8.1.0-canary.
|
|
17
|
-
"@graphcommerce/next-ui": "^8.1.0-canary.
|
|
18
|
-
"@graphcommerce/prettier-config-pwa": "^8.1.0-canary.
|
|
19
|
-
"@graphcommerce/react-hook-form": "^8.1.0-canary.
|
|
20
|
-
"@graphcommerce/typescript-config-pwa": "^8.1.0-canary.
|
|
15
|
+
"@graphcommerce/eslint-config-pwa": "^8.1.0-canary.42",
|
|
16
|
+
"@graphcommerce/graphql": "^8.1.0-canary.42",
|
|
17
|
+
"@graphcommerce/next-ui": "^8.1.0-canary.42",
|
|
18
|
+
"@graphcommerce/prettier-config-pwa": "^8.1.0-canary.42",
|
|
19
|
+
"@graphcommerce/react-hook-form": "^8.1.0-canary.42",
|
|
20
|
+
"@graphcommerce/typescript-config-pwa": "^8.1.0-canary.42",
|
|
21
21
|
"@lingui/core": "^4.2.1",
|
|
22
22
|
"@lingui/macro": "^4.2.1",
|
|
23
23
|
"@lingui/react": "^4.2.1",
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { PagesProps } from '@graphcommerce/framer-next-pages'
|
|
2
|
+
import type { PluginConfig, PluginProps } from '@graphcommerce/next-config'
|
|
3
|
+
import dynamic from 'next/dynamic'
|
|
4
|
+
import { useEffect, useState } from 'react'
|
|
5
|
+
|
|
6
|
+
const PreviewMode = dynamic(
|
|
7
|
+
async () => (await import('../components/PreviewMode/PreviewMode')).PreviewMode,
|
|
8
|
+
{},
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
export const config: PluginConfig = {
|
|
12
|
+
type: 'component',
|
|
13
|
+
module: '@graphcommerce/framer-next-pages',
|
|
14
|
+
ifConfig: 'previewSecret',
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function FramerNextPages(props: PluginProps<PagesProps>) {
|
|
18
|
+
const { Prev, router, ...rest } = props
|
|
19
|
+
const [enabled, setEnabled] = useState(router.isPreview)
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const handler = (e: KeyboardEvent) => {
|
|
23
|
+
if (e.altKey && e.code === 'Backquote') {
|
|
24
|
+
setEnabled((prev) => !prev)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
window.addEventListener('keydown', handler, false)
|
|
29
|
+
return () => window.removeEventListener('keydown', handler)
|
|
30
|
+
}, [])
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<>
|
|
34
|
+
<Prev {...rest} router={router} />
|
|
35
|
+
{enabled && <PreviewMode />}
|
|
36
|
+
</>
|
|
37
|
+
)
|
|
38
|
+
}
|
package/route/preview.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PreviewData } from '@graphcommerce/graphql'
|
|
2
|
+
import { NextApiRequest, NextApiResponse } from 'next'
|
|
3
|
+
import { previewModeDefaults } from '../components/PreviewMode/previewModeDefaults'
|
|
4
|
+
|
|
5
|
+
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
6
|
+
const { action } = req.query
|
|
7
|
+
|
|
8
|
+
// const domain = req.url ? new URL(req.url) : undefined
|
|
9
|
+
const referer = req.headers.referer ? new URL(req.headers.referer) : undefined
|
|
10
|
+
const redirectTo =
|
|
11
|
+
req.headers.redirectTo ??
|
|
12
|
+
(referer && req.headers.host === referer.host ? referer.pathname : '/')
|
|
13
|
+
|
|
14
|
+
if (!action) {
|
|
15
|
+
res.status(400).json({ message: 'No action provided' })
|
|
16
|
+
res.end()
|
|
17
|
+
return
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (action === 'enable' && req.query.secret) {
|
|
21
|
+
if (req.query.secret !== import.meta.graphCommerce.previewSecret) {
|
|
22
|
+
// This secret should only be known to this API route and the CMS
|
|
23
|
+
res.status(401).json({ message: 'Invalid token' })
|
|
24
|
+
res.end()
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
res.setDraftMode({ enable: true })
|
|
29
|
+
const previewData = req.query.previewDat
|
|
30
|
+
? (JSON.parse(`${req.query.previewData}`) as PreviewData)
|
|
31
|
+
: previewModeDefaults()
|
|
32
|
+
|
|
33
|
+
res.setPreviewData(previewData)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (action === 'exit') {
|
|
37
|
+
res.setDraftMode({ enable: false })
|
|
38
|
+
res.clearPreviewData()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (action === 'revalidate') {
|
|
42
|
+
const url = referer ? new URL(referer) : undefined
|
|
43
|
+
|
|
44
|
+
if (!url) {
|
|
45
|
+
res.status(401).json({ message: 'No referer header found' })
|
|
46
|
+
res.end()
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
await res.revalidate(url.pathname)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (action === 'update' && req.preview && req.query.previewData) {
|
|
54
|
+
// todo we should probabaly validate this.
|
|
55
|
+
res.setPreviewData(JSON.parse(`${req.query.previewData}`) as PreviewData)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
res.writeHead(307, { Location: redirectTo })
|
|
59
|
+
res.end()
|
|
60
|
+
}
|