@okam/directus-block 1.1.1 → 1.2.0
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/.eslintrc.js +21 -0
- package/.storybook/.eslintrc.js +29 -0
- package/.storybook/main.ts +33 -0
- package/.storybook/preview.ts +0 -0
- package/.storybook/tsconfig.json +17 -0
- package/CHANGELOG.md +21 -0
- package/README.md +161 -0
- package/package.json +1 -1
- package/project.json +57 -0
- package/src/blocks/BlockWysiwyg/config.tsx +10 -0
- package/src/blocks/BlockWysiwyg/index.tsx +38 -0
- package/src/blocks/BlockWysiwyg/interface.ts +13 -0
- package/src/components/BlockDispatcher/config.ts +10 -0
- package/src/components/BlockDispatcher/index.tsx +30 -0
- package/src/components/BlockDispatcher/interface.ts +17 -0
- package/src/components/BlockSerializer/index.tsx +40 -0
- package/src/components/BlockSerializer/interface.ts +51 -0
- package/src/generated/fragment-masking.ts +66 -0
- package/src/generated/graphql.ts +31 -0
- package/src/index.ts +30 -0
- package/src/types/block.ts +40 -0
- package/src/utils/get-block-props.ts +44 -0
- package/src/utils/index.ts +5 -0
- package/src/utils/merge-configs.ts +21 -0
- package/tsconfig.json +20 -0
- package/tsconfig.lib.json +25 -0
- package/vite.config.ts +51 -0
- package/blocks/BlockWysiwyg/config.d.ts +0 -3
- package/blocks/BlockWysiwyg/index.d.ts +0 -4
- package/blocks/BlockWysiwyg/interface.d.ts +0 -17
- package/components/BlockDispatcher/config.d.ts +0 -3
- package/components/BlockDispatcher/index.d.ts +0 -5
- package/components/BlockDispatcher/interface.d.ts +0 -16
- package/components/BlockSerializer/index.d.ts +0 -3
- package/components/BlockSerializer/interface.d.ts +0 -31
- package/generated/fragment-masking.d.ts +0 -15
- package/generated/graphql.d.ts +0 -64
- package/index.d.ts +0 -10
- package/index.js +0 -96
- package/index.mjs +0 -6019
- package/style.css +0 -1
- package/types/block.d.ts +0 -27
- package/utils/get-block-props.d.ts +0 -17
- package/utils/index.d.ts +0 -4
- package/utils/merge-configs.d.ts +0 -9
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const { getDefaultIgnorePatterns } = require("../../../linter/helpers");
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
extends: ['plugin:@nx/react', '../../../.eslintrc.js', '../../../linter/bases/typescript', '../../../linter/bases/regexp', '../../../linter/bases/jest', '../../../linter/bases/rtl', '../../../linter/bases/react', '../../../linter/bases/prettier'],
|
|
5
|
+
ignorePatterns: ['!**/*', ...getDefaultIgnorePatterns()],
|
|
6
|
+
overrides: [
|
|
7
|
+
{
|
|
8
|
+
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
|
|
9
|
+
// We set parserOptions.project for the project to allow TypeScript to create the type-checker behind the scenes when we run linting
|
|
10
|
+
parserOptions: {
|
|
11
|
+
project: `${__dirname}/tsconfig.*?.json`,
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
"@nx/dependency-checks": "error"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
],
|
|
18
|
+
rules: {
|
|
19
|
+
'react/react-in-jsx-scope': 'off',
|
|
20
|
+
},
|
|
21
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const { getDefaultIgnorePatterns } = require('../../../../linter/helpers')
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
extends: [
|
|
5
|
+
'plugin:@nx/react',
|
|
6
|
+
'../../../../.eslintrc.js',
|
|
7
|
+
'../../../../linter/bases/typescript',
|
|
8
|
+
'../../../../linter/bases/regexp',
|
|
9
|
+
'../../../../linter/bases/jest',
|
|
10
|
+
'../../../../linter/bases/rtl',
|
|
11
|
+
'../../../../linter/bases/storybook',
|
|
12
|
+
'../../../../linter/bases/react',
|
|
13
|
+
'../../../../linter/bases/prettier',
|
|
14
|
+
'plugin:storybook/recommended',
|
|
15
|
+
],
|
|
16
|
+
ignorePatterns: ['!**/*', ...getDefaultIgnorePatterns()],
|
|
17
|
+
overrides: [
|
|
18
|
+
{
|
|
19
|
+
files: ['*.ts', '*.tsx', '*.js', '*.jsx'],
|
|
20
|
+
// We set parserOptions.project for the project to allow TypeScript to create the type-checker behind the scenes when we run linting
|
|
21
|
+
parserOptions: {
|
|
22
|
+
project: `${__dirname}/tsconfig.*?.json`,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
rules: {
|
|
27
|
+
'react/react-in-jsx-scope': 'off',
|
|
28
|
+
},
|
|
29
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-ts-comment */
|
|
2
|
+
import type { StorybookConfig } from '@storybook/nextjs'
|
|
3
|
+
|
|
4
|
+
const config: StorybookConfig = {
|
|
5
|
+
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
|
|
6
|
+
addons: [
|
|
7
|
+
'@storybook/addon-a11y',
|
|
8
|
+
'@storybook/addon-links',
|
|
9
|
+
'@storybook/addon-essentials',
|
|
10
|
+
'@storybook/addon-interactions',
|
|
11
|
+
{
|
|
12
|
+
name: '@storybook/addon-styling',
|
|
13
|
+
options: {
|
|
14
|
+
// Check out https://github.com/storybookjs/addon-styling/blob/main/docs/api.md
|
|
15
|
+
// For more details on this addon's options.
|
|
16
|
+
postCss: true,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
framework: {
|
|
21
|
+
name: '@storybook/nextjs',
|
|
22
|
+
options: {},
|
|
23
|
+
},
|
|
24
|
+
docs: {
|
|
25
|
+
autodocs: 'tag',
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default config
|
|
30
|
+
|
|
31
|
+
// To customize your Vite configuration you can use the viteFinal field.
|
|
32
|
+
// Check https://storybook.js.org/docs/react/builders/vite#configuration
|
|
33
|
+
// and https://nx.dev/packages/storybook/documents/custom-builder-configs
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"emitDecoratorMetadata": true
|
|
5
|
+
},
|
|
6
|
+
|
|
7
|
+
"exclude": ["../**/*.spec.ts"],
|
|
8
|
+
"include": [
|
|
9
|
+
"../src/**/*.stories.ts",
|
|
10
|
+
"../src/**/*.stories.js",
|
|
11
|
+
"../src/**/*.stories.jsx",
|
|
12
|
+
"../src/**/*.stories.tsx",
|
|
13
|
+
"../src/**/*.stories.mdx",
|
|
14
|
+
"*.ts",
|
|
15
|
+
"*.js"
|
|
16
|
+
]
|
|
17
|
+
}
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
## 1.2.0 (2024-06-28)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### 🚀 Features
|
|
5
|
+
|
|
6
|
+
- STACK-72-Lib-Create-directus-block ([#115](https://github.com/OKAMca/stack/pull/115))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### 🩹 Fixes
|
|
10
|
+
|
|
11
|
+
- publishing for new libs ([#125](https://github.com/OKAMca/stack/pull/125))
|
|
12
|
+
|
|
13
|
+
- **directus-block:** update package version ([9f93408](https://github.com/OKAMca/stack/commit/9f93408))
|
|
14
|
+
|
|
15
|
+
- **block-directus:** add server only to get block props ([330e76f](https://github.com/OKAMca/stack/commit/330e76f))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### ❤️ Thank You
|
|
19
|
+
|
|
20
|
+
- Marie-Maxime Tanguay @marie-maxime
|
|
21
|
+
- Pierre-Olivier Clerson @poclerson
|
package/README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
# directus-block
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build directus-block` to build the library.
|
|
8
|
+
|
|
9
|
+
## Components
|
|
10
|
+
|
|
11
|
+
### Dispatcher
|
|
12
|
+
|
|
13
|
+
#### Props
|
|
14
|
+
|
|
15
|
+
- config: Configuration the block dispatcher will use. This configuration will get merged with the base config of directus-block. In case of overrides, the passed configuration will always win over the directus-block configuration.
|
|
16
|
+
- blocks: Array of `TBlockSerializerProps` containing the actual blocks data. This is the prop that will be used by the block dispatcher to iterate through its children.
|
|
17
|
+
- block: In case you want to pass a single block
|
|
18
|
+
- children: Function receiving the current block `TBlockSerializerProps` as props. Will use BlockSerializer alone by default. This is useful in scenarios where you want every block to be wrapped in other components:
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
<BlockDispatcher config={baseBlockDispatcherConfig} blocks={blocks}>
|
|
22
|
+
{(block) => (
|
|
23
|
+
<Container tokens={{ isLowestContainerLevel: true }}>
|
|
24
|
+
<ErrorBoundary fallback={<ErrorFallback />}>
|
|
25
|
+
<BlockSerializer {...block} />
|
|
26
|
+
</ErrorBoundary>
|
|
27
|
+
</Container>
|
|
28
|
+
)}
|
|
29
|
+
</BlockDispatcher>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
This would allow to remove a lot of repetitive code accross blocks definitions, for example as they all need to be wrapped in a `Container` and an `ErrorBoundary`
|
|
33
|
+
|
|
34
|
+
### Serializer
|
|
35
|
+
|
|
36
|
+
This component calls the good component in the configuration from the `collection` prop
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
A configuration uses the `components` prop to map a key value, like so:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
const config = {
|
|
44
|
+
components: {
|
|
45
|
+
block_wysiwyg: {
|
|
46
|
+
default: (props) => <BlockWysiwyg {...props} />,
|
|
47
|
+
document: BlockWysiwygDocument,
|
|
48
|
+
defaultVariant: 'reversed',
|
|
49
|
+
getVariant: (props) => props.settings.variants,
|
|
50
|
+
variants: {
|
|
51
|
+
reversed: (props) => (
|
|
52
|
+
<ReversedThemeProvider>
|
|
53
|
+
<BlockWysiwyg {...props} />
|
|
54
|
+
</ReversedThemeProvider>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Props
|
|
63
|
+
|
|
64
|
+
- default: The default component if no variants/invalid variants are used
|
|
65
|
+
- defaultVariant: Overrides the default use of the `default` prop, instead mapping the default component on a specific variant
|
|
66
|
+
- getVariant: Callback to specify a different variant path from the one in the block's settings
|
|
67
|
+
- variants: key-value mapping of `{[variant]: component}`
|
|
68
|
+
- document: The necessary document for querying the data. This prop can either be passed directly to the block or in the config. Since Stack blocks don't yet have their own documents, you can override them and add their document like so:
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
import { blockWysiwygConfig } from '@okam/directus-block'
|
|
72
|
+
|
|
73
|
+
const brandConfig = {
|
|
74
|
+
components: {
|
|
75
|
+
block_wysiwyg: {
|
|
76
|
+
document: BlockWysiwygDocument,
|
|
77
|
+
...blockWysiwygConfig.block_wysiwyg,
|
|
78
|
+
},
|
|
79
|
+
block_image: {
|
|
80
|
+
default: (props) => <BrandBlockImage {...props} />
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
<BlockDispatcher config={brandConfig} />
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Overriding the configuration with brand blocks
|
|
89
|
+
|
|
90
|
+
To override the base block dispatcher configuration, you name a block component configuration with the same key as the ones in the configuration.
|
|
91
|
+
|
|
92
|
+
For example, if this is the base configuration:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
const baseConfig = {
|
|
96
|
+
components: {
|
|
97
|
+
block_wysiwyg: {
|
|
98
|
+
default: (props) => <BlockWysiwyg {...props}>,
|
|
99
|
+
},
|
|
100
|
+
block_hero: {
|
|
101
|
+
default: (props) => <BlockHero {...props}>,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
But that you to call a branded wysiwyg component from the dispatcher configuration, you may pass the following configuration:
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
const brandConfig = {
|
|
111
|
+
components: {
|
|
112
|
+
block_wysiwyg: {
|
|
113
|
+
default: (props) => <BrandBlockWysiwyg {...props} />
|
|
114
|
+
},
|
|
115
|
+
block_image: {
|
|
116
|
+
default: (props) => <BrandBlockImage {...props} />
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
<BlockDispatcher config={brandConfig} />
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
In this example, the BlockWysiwyg definition would be overriden. However, you would now also have access to the `block_image`, and still retain access to the `block_hero`
|
|
125
|
+
|
|
126
|
+
You could also re-use only a part of the base configuration while overriding another part
|
|
127
|
+
|
|
128
|
+
```tsx
|
|
129
|
+
import { blockWysiwygConfig } from '@okam/directus-block'
|
|
130
|
+
|
|
131
|
+
const brandConfig = {
|
|
132
|
+
components: {
|
|
133
|
+
block_wysiwyg: {
|
|
134
|
+
default: (props) => <BrandBlockWysiwyg {...props} />,
|
|
135
|
+
variants: blockWysiwygConfig.block_wysiwyg.variants,
|
|
136
|
+
},
|
|
137
|
+
block_image: {
|
|
138
|
+
default: (props) => <BrandBlockImage {...props} />
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
<BlockDispatcher config={brandConfig} />
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Extending the configuration with stack blocks
|
|
147
|
+
|
|
148
|
+
Some blocks may be in the Stack without being in the base configuration. To use them, simply import their own configuration from the stack and spread them in yours
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
import { blockHeroConfig } from '@okam/directus-block'
|
|
152
|
+
|
|
153
|
+
const brandConfigWithStackBlocks = {
|
|
154
|
+
components: {
|
|
155
|
+
block_image: {
|
|
156
|
+
default: (props) => <BrandBlockImage {...props} />
|
|
157
|
+
},
|
|
158
|
+
...blockHeroConfig,
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
package/package.json
CHANGED
package/project.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "directus-block",
|
|
3
|
+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "libs/directus/directus-block/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"lint": {
|
|
9
|
+
"executor": "@nx/linter:eslint",
|
|
10
|
+
"outputs": ["{options.outputFile}"],
|
|
11
|
+
"options": {
|
|
12
|
+
"lintFilePatterns": ["libs/directus/directus-block/**/*.{ts,tsx,js,jsx}"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"build": {
|
|
16
|
+
"executor": "@nx/vite:build",
|
|
17
|
+
"outputs": ["{options.outputPath}"],
|
|
18
|
+
"defaultConfiguration": "production",
|
|
19
|
+
"options": {
|
|
20
|
+
"outputPath": "dist/libs/directus/directus-block"
|
|
21
|
+
},
|
|
22
|
+
"configurations": {
|
|
23
|
+
"development": {
|
|
24
|
+
"mode": "development"
|
|
25
|
+
},
|
|
26
|
+
"production": {
|
|
27
|
+
"mode": "production"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"storybook": {
|
|
32
|
+
"executor": "@nx/storybook:storybook",
|
|
33
|
+
"options": {
|
|
34
|
+
"port": 4400,
|
|
35
|
+
"configDir": "libs/directus/directus-block/.storybook"
|
|
36
|
+
},
|
|
37
|
+
"configurations": {
|
|
38
|
+
"ci": {
|
|
39
|
+
"quiet": true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"build-storybook": {
|
|
44
|
+
"executor": "@nx/storybook:build",
|
|
45
|
+
"outputs": ["{options.outputDir}"],
|
|
46
|
+
"options": {
|
|
47
|
+
"outputDir": "dist/storybook/directus-block",
|
|
48
|
+
"configDir": "libs/directus/directus-block/.storybook"
|
|
49
|
+
},
|
|
50
|
+
"configurations": {
|
|
51
|
+
"ci": {
|
|
52
|
+
"quiet": true
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TBlockSerializerConfigComponent } from '../../components/BlockSerializer/interface'
|
|
2
|
+
import BlockWysiwyg from '.'
|
|
3
|
+
|
|
4
|
+
const blockWysiwygConfig: TBlockSerializerConfigComponent = {
|
|
5
|
+
block_wysiwyg: {
|
|
6
|
+
default: (props) => <BlockWysiwyg {...props} />,
|
|
7
|
+
},
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default blockWysiwygConfig
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Box, WysiwygBlock } from '@okam/stack-ui'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
import type { TBlockSerializerProps } from '../../components/BlockSerializer/interface'
|
|
4
|
+
import { BlockSettingsFragmentDoc } from '../../generated/graphql'
|
|
5
|
+
import { getBlockProps, getFragment } from '../../utils'
|
|
6
|
+
import type { BlockWysiwygFragment } from './interface'
|
|
7
|
+
|
|
8
|
+
const BlockWysiwyg = async (props: TBlockSerializerProps<BlockWysiwygFragment>) => {
|
|
9
|
+
const { variables, themeName = 'wysiwyg', tokens, item, document } = props
|
|
10
|
+
|
|
11
|
+
const propsWithFallback = await getBlockProps({
|
|
12
|
+
item,
|
|
13
|
+
blockKey: 'block_wysiwyg_by_id',
|
|
14
|
+
document,
|
|
15
|
+
variables,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
if (!propsWithFallback) return null
|
|
19
|
+
|
|
20
|
+
const { content, title, level, settings } = propsWithFallback
|
|
21
|
+
|
|
22
|
+
const { tokens: cmsTokens } = getFragment(BlockSettingsFragmentDoc, settings) ?? {}
|
|
23
|
+
|
|
24
|
+
if (!content && !(title && level)) return null
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<>
|
|
28
|
+
{title && level && (
|
|
29
|
+
<Box as="span" themeName={themeName} tokens={{ ...tokens, ...cmsTokens }}>
|
|
30
|
+
{React.createElement(level, {}, title)}
|
|
31
|
+
</Box>
|
|
32
|
+
)}
|
|
33
|
+
{content && <WysiwygBlock themeName={themeName} tokens={{ ...tokens, ...cmsTokens }} content={content} />}
|
|
34
|
+
</>
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default BlockWysiwyg
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
+
import type { BlockSettingsFragment } from '../../generated/graphql'
|
|
3
|
+
|
|
4
|
+
export type BlockWysiwygFragment = {
|
|
5
|
+
__typename?: 'block_wysiwyg'
|
|
6
|
+
title?: string | null
|
|
7
|
+
content?: string | null
|
|
8
|
+
level?: string | null
|
|
9
|
+
variant?: string | null
|
|
10
|
+
settings?:
|
|
11
|
+
| ({ __typename?: 'block_settings' } & { ' $fragmentRefs'?: { BlockSettingsFragment: BlockSettingsFragment } })
|
|
12
|
+
| null
|
|
13
|
+
} & { ' $fragmentName'?: 'BlockWysiwygFragment' }
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import blockWysiwygConfig from '../../blocks/BlockWysiwyg/config'
|
|
2
|
+
import type { TBlockSerializerConfig } from '../BlockSerializer/interface'
|
|
3
|
+
|
|
4
|
+
const baseConfig: TBlockSerializerConfig = {
|
|
5
|
+
components: {
|
|
6
|
+
...blockWysiwygConfig,
|
|
7
|
+
},
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default baseConfig
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Nullable } from '@okam/stack-ui'
|
|
2
|
+
import { mergeConfigs } from '../../utils'
|
|
3
|
+
import BlockSerializer from '../BlockSerializer'
|
|
4
|
+
import type { TBlockSerializerProps } from '../BlockSerializer/interface'
|
|
5
|
+
import baseConfig from './config'
|
|
6
|
+
import type { TBlockDispatcherProps } from './interface'
|
|
7
|
+
|
|
8
|
+
const BlockDispatcher = (props: TBlockDispatcherProps) => {
|
|
9
|
+
const { children, config, ...rest } = props
|
|
10
|
+
|
|
11
|
+
const finalConfig = mergeConfigs(baseConfig, config)
|
|
12
|
+
|
|
13
|
+
const renderBlock = (blockProps: Nullable<TBlockSerializerProps>) => {
|
|
14
|
+
return children ? (
|
|
15
|
+
children({ ...rest, ...blockProps, config: finalConfig })
|
|
16
|
+
) : (
|
|
17
|
+
<BlockSerializer {...rest} {...blockProps} config={finalConfig} />
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if ('blocks' in props) {
|
|
22
|
+
const { blocks } = props
|
|
23
|
+
return blocks.map(renderBlock)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const { block } = props
|
|
27
|
+
return renderBlock(block)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default BlockDispatcher
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
+
import type { TDefaultComponent, Nullable } from '@okam/stack-ui'
|
|
3
|
+
import type React from 'react'
|
|
4
|
+
import type { TAdditionalProps } from '../../types/block'
|
|
5
|
+
import type { TBlockSerializerProps, TBlockSerializerConfig } from '../BlockSerializer/interface'
|
|
6
|
+
|
|
7
|
+
interface TBaseBlockDispatcherProps<AdditionalProps extends TAdditionalProps = TAdditionalProps>
|
|
8
|
+
extends Omit<TDefaultComponent, 'children'> {
|
|
9
|
+
config?: TBlockSerializerConfig
|
|
10
|
+
defaultVariant?: string
|
|
11
|
+
additionalProps?: AdditionalProps
|
|
12
|
+
children?: (props: TBlockSerializerProps) => React.ReactNode
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type TBlockDispatcherProps<AdditionalProps extends TAdditionalProps = TAdditionalProps> =
|
|
16
|
+
| (TBaseBlockDispatcherProps<AdditionalProps> & { block: Nullable<TBlockSerializerProps> })
|
|
17
|
+
| (TBaseBlockDispatcherProps<AdditionalProps> & { blocks: Nullable<TBlockSerializerProps>[] })
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { BlockSettingsFragmentDoc } from '../../generated/graphql'
|
|
2
|
+
import { getFragment } from '../../utils'
|
|
3
|
+
import type { TBlockSerializerProps } from './interface'
|
|
4
|
+
|
|
5
|
+
const BlockSerializer = (props: TBlockSerializerProps) => {
|
|
6
|
+
const { item, collection, config, variables, defaultVariant, ...rest } = props
|
|
7
|
+
|
|
8
|
+
if (!collection || !item || !config) return null
|
|
9
|
+
|
|
10
|
+
const blockConfig = config?.components?.[collection]
|
|
11
|
+
|
|
12
|
+
if (!blockConfig) return null
|
|
13
|
+
|
|
14
|
+
const { id, settings } = item
|
|
15
|
+
|
|
16
|
+
const { variant } = getFragment(BlockSettingsFragmentDoc, settings) ?? {}
|
|
17
|
+
|
|
18
|
+
const variantWithFallback = blockConfig.getVariant?.(props) ?? variant ?? defaultVariant
|
|
19
|
+
|
|
20
|
+
const { default: defaultBlockComponent, document } = blockConfig
|
|
21
|
+
|
|
22
|
+
const variantBlockComponent = blockConfig.variants?.[variantWithFallback ?? '']
|
|
23
|
+
const BlockComponent = variantBlockComponent ?? defaultBlockComponent
|
|
24
|
+
|
|
25
|
+
if (!BlockComponent) return null
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<BlockComponent
|
|
29
|
+
key={id}
|
|
30
|
+
document={document}
|
|
31
|
+
config={config}
|
|
32
|
+
collection={collection}
|
|
33
|
+
item={item}
|
|
34
|
+
{...(id ? { variables: { ...variables, id } } : { variables })}
|
|
35
|
+
{...rest}
|
|
36
|
+
/>
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default BlockSerializer
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type TypedDocumentNode } from '@graphql-typed-document-node/core'
|
|
2
|
+
import type { Nullable, TDefaultComponent } from '@okam/stack-ui'
|
|
3
|
+
import type { Variables } from 'graphql-request'
|
|
4
|
+
import type { FunctionComponent } from 'react'
|
|
5
|
+
import type {
|
|
6
|
+
TCommonBlockFragment,
|
|
7
|
+
TAdditionalProps,
|
|
8
|
+
TBlockQuery,
|
|
9
|
+
TBlockVariables,
|
|
10
|
+
TBlockDocument,
|
|
11
|
+
} from '../../types/block'
|
|
12
|
+
|
|
13
|
+
export interface TBlock<
|
|
14
|
+
BlockFragment extends TCommonBlockFragment = TCommonBlockFragment,
|
|
15
|
+
BlockVariables extends Variables = Variables,
|
|
16
|
+
> {
|
|
17
|
+
collection?: Nullable<string>
|
|
18
|
+
item?: Nullable<BlockFragment>
|
|
19
|
+
variables?: TBlockVariables<BlockVariables>
|
|
20
|
+
document?: TypedDocumentNode<TBlockQuery<BlockFragment>, BlockVariables>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type TBlockSerializerProps<
|
|
24
|
+
BlockFragment extends TCommonBlockFragment = TCommonBlockFragment,
|
|
25
|
+
BlockVariables extends Variables = TBlockVariables,
|
|
26
|
+
AdditionalProps extends TAdditionalProps = TAdditionalProps,
|
|
27
|
+
> = TBlock<BlockFragment, BlockVariables> &
|
|
28
|
+
TDefaultComponent & {
|
|
29
|
+
config?: TBlockSerializerConfig
|
|
30
|
+
defaultVariant?: string
|
|
31
|
+
additionalProps?: AdditionalProps
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type TBlockFunctionComponent<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment> =
|
|
35
|
+
FunctionComponent<TBlockSerializerProps<BlockFragment>>
|
|
36
|
+
|
|
37
|
+
export type TBlockSerializerConfigComponent<BlockFragment extends TCommonBlockFragment = TCommonBlockFragment> = {
|
|
38
|
+
[blockKey: string]: {
|
|
39
|
+
default: TBlockFunctionComponent<BlockFragment>
|
|
40
|
+
document?: TBlockDocument<BlockFragment>
|
|
41
|
+
defaultVariant?: string
|
|
42
|
+
getVariant?: (props: TBlockSerializerProps<BlockFragment>) => Nullable<string>
|
|
43
|
+
variants?: {
|
|
44
|
+
[blockVariant: string]: TBlockFunctionComponent<BlockFragment>
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface TBlockSerializerConfig {
|
|
50
|
+
components: TBlockSerializerConfigComponent
|
|
51
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
3
|
+
import { FragmentDefinitionNode } from 'graphql'
|
|
4
|
+
import { Incremental } from './graphql';
|
|
5
|
+
|
|
6
|
+
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
|
|
7
|
+
infer TType,
|
|
8
|
+
any
|
|
9
|
+
>
|
|
10
|
+
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
|
|
11
|
+
? TKey extends string
|
|
12
|
+
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
|
|
13
|
+
: never
|
|
14
|
+
: never
|
|
15
|
+
: never;
|
|
16
|
+
|
|
17
|
+
// return non-nullable if `fragmentType` is non-nullable
|
|
18
|
+
export function useFragment<TType>(
|
|
19
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
20
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
|
|
21
|
+
): TType;
|
|
22
|
+
// return nullable if `fragmentType` is nullable
|
|
23
|
+
export function useFragment<TType>(
|
|
24
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
25
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
|
|
26
|
+
): TType | null | undefined;
|
|
27
|
+
// return array of non-nullable if `fragmentType` is array of non-nullable
|
|
28
|
+
export function useFragment<TType>(
|
|
29
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
30
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
31
|
+
): ReadonlyArray<TType>;
|
|
32
|
+
// return array of nullable if `fragmentType` is array of nullable
|
|
33
|
+
export function useFragment<TType>(
|
|
34
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
35
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
36
|
+
): ReadonlyArray<TType> | null | undefined;
|
|
37
|
+
export function useFragment<TType>(
|
|
38
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
39
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
40
|
+
): TType | ReadonlyArray<TType> | null | undefined {
|
|
41
|
+
return fragmentType as any;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function makeFragmentData<
|
|
45
|
+
F extends DocumentTypeDecoration<any, any>,
|
|
46
|
+
FT extends ResultOf<F>
|
|
47
|
+
>(data: FT, _fragment: F): FragmentType<F> {
|
|
48
|
+
return data as FragmentType<F>;
|
|
49
|
+
}
|
|
50
|
+
export function isFragmentReady<TQuery, TFrag>(
|
|
51
|
+
queryNode: DocumentTypeDecoration<TQuery, any>,
|
|
52
|
+
fragmentNode: TypedDocumentNode<TFrag>,
|
|
53
|
+
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
|
|
54
|
+
): data is FragmentType<typeof fragmentNode> {
|
|
55
|
+
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
|
|
56
|
+
?.deferredFields;
|
|
57
|
+
|
|
58
|
+
if (!deferredFields) return true;
|
|
59
|
+
|
|
60
|
+
const fragDef = (fragmentNode as any).definitions[0] as FragmentDefinitionNode | undefined;
|
|
61
|
+
const fragName = fragDef?.name?.value;
|
|
62
|
+
|
|
63
|
+
const fields = (fragName && deferredFields[fragName]) || [];
|
|
64
|
+
//@ts-ignore
|
|
65
|
+
return fields.length > 0 && fields.every((field) => data && field in data);
|
|
66
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* eslint-disable prettier/prettier */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
4
|
+
import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
|
|
5
|
+
|
|
6
|
+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
|
|
7
|
+
export type Incremental<T> = T | { [P in keyof T]?: P extends ' $fragmentName' | '__typename' ? T[P] : never }
|
|
8
|
+
|
|
9
|
+
export type Scalars = {
|
|
10
|
+
ID: { input: string; output: string; }
|
|
11
|
+
String: { input: string; output: string; }
|
|
12
|
+
Boolean: { input: boolean; output: boolean; }
|
|
13
|
+
Int: { input: number; output: number; }
|
|
14
|
+
Float: { input: number; output: number; }
|
|
15
|
+
/** ISO8601 Date values */
|
|
16
|
+
Date: { input: any; output: any; }
|
|
17
|
+
/** BigInt value */
|
|
18
|
+
GraphQLBigInt: { input: any; output: any; }
|
|
19
|
+
/** A Float or a String */
|
|
20
|
+
GraphQLStringOrFloat: { input: any; output: any; }
|
|
21
|
+
/** Hashed string values */
|
|
22
|
+
Hash: { input: any; output: any; }
|
|
23
|
+
/** The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). */
|
|
24
|
+
JSON: { input: any; output: any; }
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type BlockSettingsFragment = { __typename?: 'block_settings'; tokens?: any | null; variant?: string | null } & {
|
|
28
|
+
' $fragmentName'?: 'BlockSettingsFragment'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const BlockSettingsFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"BlockSettings"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"block_settings"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"tokens"}},{"kind":"Field","name":{"kind":"Name","value":"variant"}}]}}]} as unknown as DocumentNode<BlockSettingsFragment, unknown>;
|