sanity-plugin-dashboard-widget-document-list 0.0.13 → 3.0.0-studio-v3.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.
- package/README.md +92 -48
- package/lib/cjs/index.js +224 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/esm/index.js +217 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/index.d.ts +18 -0
- package/lib/types/index.d.ts.map +1 -0
- package/package.json +49 -18
- package/src/DocumentList.tsx +153 -0
- package/src/index.tsx +17 -0
- package/src/sanityConnector.ts +67 -0
- package/lib/DocumentList.css +0 -34
- package/lib/DocumentList.js +0 -196
- package/lib/DocumentList.js.map +0 -1
- package/lib/index.js +0 -17
- package/lib/index.js.map +0 -1
- package/lib/sanityConnector.js +0 -64
- package/lib/sanityConnector.js.map +0 -1
- package/sanity.json +0 -12
package/README.md
CHANGED
|
@@ -1,101 +1,116 @@
|
|
|
1
1
|
# dashboard-widget-document-list
|
|
2
|
-
|
|
2
|
+
[dashboard-plugin]: https://github.com/sanity-io/v3-temp-sanity-dashboard/tree/v3
|
|
3
3
|
|
|
4
|
+
> **NOTE**
|
|
5
|
+
>
|
|
6
|
+
> This is the **Sanity Studio v3 version** of dashboard-widget-document-list.
|
|
7
|
+
>
|
|
8
|
+
> For the v2 version, please refer to the [v2-branch](https://github.com/sanity-io/dashboard-widget-document-list).
|
|
4
9
|
|
|
10
|
+
## What is it?
|
|
5
11
|
|
|
6
|
-
|
|
7
|
-
Assuming you already have a functional Dashboard in your Sanity Content Studio.
|
|
12
|
+
Dashboard widget for the Sanity Content Studio which displays a list of documents.
|
|
8
13
|
|
|
9
|
-
|
|
14
|
+
## Install
|
|
10
15
|
|
|
11
16
|
```
|
|
12
|
-
|
|
17
|
+
npm install --save dashboard-widget-document-list@studio-v3
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
or
|
|
21
|
+
|
|
13
22
|
```
|
|
23
|
+
yarn add dashboard-widget-document-list@studio-v3
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Ensure that you have followed install and usage instructions for [@sanity/dashboard][dashboard-plugin].
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
Add dashboard-widget-document-list as a widget to @sanity/dashboard plugin in sanity.config.ts (or .js):
|
|
14
31
|
|
|
15
|
-
|
|
16
|
-
|
|
32
|
+
```js
|
|
33
|
+
import { dashboardTool } from "@sanity/dashboard";
|
|
34
|
+
import { catsWidget } from "sanity-plugin-dashboard-widget-document-list";
|
|
35
|
+
|
|
36
|
+
export default createConfig({
|
|
37
|
+
// ...
|
|
38
|
+
plugins: [
|
|
39
|
+
dashboardTool({
|
|
40
|
+
widgets: [
|
|
41
|
+
documentListWidget(),
|
|
42
|
+
],
|
|
43
|
+
}
|
|
44
|
+
),
|
|
45
|
+
]
|
|
46
|
+
})
|
|
47
|
+
```
|
|
17
48
|
|
|
18
49
|
*Note*: If a document in the result (as returned by the backend) has a draft, that draft is rendered instead of the published document.
|
|
19
50
|
|
|
20
|
-
|
|
51
|
+
## Options
|
|
52
|
+
|
|
53
|
+
There are some options available, as specified by [DocumentListConfig](src/DocumentList.tsx):
|
|
21
54
|
|
|
22
55
|
### `title` (string)
|
|
23
56
|
Widget title
|
|
24
57
|
|
|
25
58
|
```js
|
|
26
|
-
{
|
|
27
|
-
name: 'document-list',
|
|
28
|
-
options: {
|
|
59
|
+
documentListWidget({
|
|
29
60
|
title: 'Some documents'
|
|
30
|
-
|
|
31
|
-
}
|
|
61
|
+
})
|
|
32
62
|
```
|
|
33
63
|
|
|
34
64
|
### `order` (string)
|
|
35
65
|
Field and direction to order by when docs are rendered
|
|
36
66
|
|
|
37
67
|
```js
|
|
38
|
-
{
|
|
39
|
-
name: 'document-list',
|
|
40
|
-
options: {
|
|
68
|
+
documentListWidget({
|
|
41
69
|
title: 'Last edited',
|
|
42
70
|
order: '_updatedAt desc'
|
|
43
|
-
|
|
44
|
-
}
|
|
71
|
+
})
|
|
45
72
|
```
|
|
46
73
|
|
|
47
74
|
### `limit` (number)
|
|
48
75
|
Number of docs rendered
|
|
49
76
|
|
|
50
77
|
```js
|
|
51
|
-
{
|
|
52
|
-
name: 'document-list',
|
|
53
|
-
options: {
|
|
78
|
+
documentListWidget({
|
|
54
79
|
title: 'Last edited',
|
|
55
80
|
order: '_updatedAt desc',
|
|
56
81
|
limit: 3
|
|
57
|
-
|
|
58
|
-
}
|
|
82
|
+
})
|
|
59
83
|
```
|
|
60
84
|
|
|
61
85
|
### `types` (array)
|
|
62
86
|
Array of strings signifying which document (schema) types are fetched
|
|
63
87
|
|
|
64
88
|
```js
|
|
65
|
-
{
|
|
66
|
-
name: 'document-list',
|
|
67
|
-
options: {
|
|
89
|
+
documentListWidget({
|
|
68
90
|
title: 'Last edited',
|
|
69
91
|
order: '_updatedAt desc',
|
|
70
92
|
types: ['book', 'author']
|
|
71
|
-
|
|
72
|
-
}
|
|
93
|
+
})
|
|
73
94
|
```
|
|
74
95
|
|
|
75
96
|
### `query` (string) and `params` (object)
|
|
76
97
|
Customized GROQ query with params for maximum control. If you use the query option, the `types`, `order`, and `limit` options will cease to function. You're on your own.
|
|
77
98
|
|
|
78
99
|
```js
|
|
79
|
-
{
|
|
80
|
-
name: 'document-list',
|
|
81
|
-
options: {
|
|
100
|
+
documentListWidget({
|
|
82
101
|
title: 'Published books by title',
|
|
83
102
|
query: '*[_type == "book" && published == true] | order(title asc) [0...10]'
|
|
84
|
-
|
|
85
|
-
}
|
|
103
|
+
})
|
|
86
104
|
```
|
|
87
105
|
|
|
88
106
|
```js
|
|
89
|
-
{
|
|
90
|
-
name: 'document-list',
|
|
91
|
-
options: {
|
|
107
|
+
documentListWidget({
|
|
92
108
|
title: 'My favorite documents',
|
|
93
109
|
query: '*[_id in $ids]',
|
|
94
110
|
params: {
|
|
95
111
|
ids: ['ab2', 'c5z', '654']
|
|
96
112
|
}
|
|
97
|
-
|
|
98
|
-
}
|
|
113
|
+
})
|
|
99
114
|
```
|
|
100
115
|
|
|
101
116
|
### `createButtonText` (string)
|
|
@@ -103,14 +118,11 @@ Customized GROQ query with params for maximum control. If you use the query opti
|
|
|
103
118
|
You can override the button default button text (`Create new ${types[0]}`) by setting `createButtonText` to a string of your choice. This doesn't support dynamic variables.
|
|
104
119
|
|
|
105
120
|
```js
|
|
106
|
-
{
|
|
107
|
-
name: 'document-list',
|
|
108
|
-
options: {
|
|
121
|
+
documentListWidget({
|
|
109
122
|
title: 'Blog posts',
|
|
110
123
|
query: '*[_type == "post"]',
|
|
111
124
|
createButtonText: 'Create new blog post'
|
|
112
|
-
|
|
113
|
-
}
|
|
125
|
+
})
|
|
114
126
|
```
|
|
115
127
|
|
|
116
128
|
### `showCreateButton` (boolean)
|
|
@@ -118,10 +130,42 @@ You can override the button default button text (`Create new ${types[0]}`) by se
|
|
|
118
130
|
You can disable the create button altogether by passing a `showCreateButton` boolean:
|
|
119
131
|
|
|
120
132
|
```js
|
|
121
|
-
{
|
|
122
|
-
name: 'document-list',
|
|
123
|
-
options: {
|
|
133
|
+
documentListWidget({
|
|
124
134
|
showCreateButton: false
|
|
125
|
-
|
|
126
|
-
|
|
135
|
+
})
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Widget size
|
|
139
|
+
|
|
140
|
+
You can change the width of the plugin using `layout.width`:
|
|
141
|
+
```js
|
|
142
|
+
documentListWidget({
|
|
143
|
+
layout: { width: "small" }
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
MIT-licensed. See LICENSE.
|
|
150
|
+
|
|
151
|
+
## Develop & test
|
|
152
|
+
|
|
153
|
+
Make sure to run `npm run build` once, then run
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
npm run link-watch
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
In another shell, `cd` to your test studio and run:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
npx yalc add dashboard-widget-document-list --link && yarn install
|
|
127
163
|
```
|
|
164
|
+
|
|
165
|
+
Now, changes in this repo will be automatically built and pushed to the studio,
|
|
166
|
+
triggering hotreload. Yalc avoids issues with react-hooks that are typical when using yarn/npm link.
|
|
167
|
+
|
|
168
|
+
### About build & watch
|
|
169
|
+
|
|
170
|
+
This plugin uses [@sanity/plugin-sdk](https://github.com/sanity-io/plugin-sdk)
|
|
171
|
+
with default configuration for build & watch scripts.
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
var $7xGut$reactjsxruntime = require("react/jsx-runtime");
|
|
2
|
+
var $7xGut$react = require("react");
|
|
3
|
+
var $7xGut$sanity_unstable = require("sanity/_unstable");
|
|
4
|
+
var $7xGut$sanity = require("sanity");
|
|
5
|
+
var $7xGut$lodash = require("lodash");
|
|
6
|
+
var $7xGut$sanityui = require("@sanity/ui");
|
|
7
|
+
var $7xGut$sanitydashboard = require("@sanity/dashboard");
|
|
8
|
+
var $7xGut$rxjs = require("rxjs");
|
|
9
|
+
var $7xGut$rxjsoperators = require("rxjs/operators");
|
|
10
|
+
var $7xGut$lodashuniqBy = require("lodash/uniqBy");
|
|
11
|
+
|
|
12
|
+
function $parcel$export(e, n, v, s) {
|
|
13
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
14
|
+
}
|
|
15
|
+
function $parcel$interopDefault(a) {
|
|
16
|
+
return a && a.__esModule ? a.default : a;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
$parcel$export(module.exports, "documentListWidget", () => $9233cea927cb9637$export$398a1547d616c51);
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
const $91d1c422801a90a3$var$draftId = (nonDraftDoc)=>`drafts.${nonDraftDoc._id}`;
|
|
30
|
+
function $91d1c422801a90a3$var$prepareDocumentList(incoming, client) {
|
|
31
|
+
if (!incoming) return Promise.resolve([]);
|
|
32
|
+
const documents = Array.isArray(incoming) ? incoming : [
|
|
33
|
+
incoming
|
|
34
|
+
];
|
|
35
|
+
const ids = documents.filter((doc)=>!doc._id.startsWith("drafts.")).map($91d1c422801a90a3$var$draftId);
|
|
36
|
+
return client.fetch("*[_id in $ids]", {
|
|
37
|
+
ids: ids
|
|
38
|
+
}).then((drafts)=>{
|
|
39
|
+
const outgoing = documents.map((doc)=>{
|
|
40
|
+
const foundDraft = drafts.find((draft)=>draft._id === $91d1c422801a90a3$var$draftId(doc));
|
|
41
|
+
return foundDraft || doc;
|
|
42
|
+
});
|
|
43
|
+
return (0, ($parcel$interopDefault($7xGut$lodashuniqBy)))(outgoing, "_id");
|
|
44
|
+
}).catch((error)=>{
|
|
45
|
+
throw new Error(`Problems fetching docs ${ids}. Error: ${error.message}`);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function $91d1c422801a90a3$export$161e8c29756c0df4(query, params, client) {
|
|
49
|
+
return client.listen(query, params, {
|
|
50
|
+
events: [
|
|
51
|
+
"welcome",
|
|
52
|
+
"mutation"
|
|
53
|
+
],
|
|
54
|
+
includeResult: false,
|
|
55
|
+
visibility: "query"
|
|
56
|
+
}).pipe((0, $7xGut$rxjsoperators.switchMap)((event)=>{
|
|
57
|
+
return (0, $7xGut$rxjs.of)(1).pipe(event.type === "welcome" ? (0, $7xGut$rxjsoperators.tap)() : (0, $7xGut$rxjsoperators.delay)(1000), (0, $7xGut$rxjsoperators.mergeMap)(()=>client.fetch(query, params).then((incoming)=>{
|
|
58
|
+
return $91d1c422801a90a3$var$prepareDocumentList(incoming, client);
|
|
59
|
+
}).catch((error)=>{
|
|
60
|
+
if (error.message.startsWith("Problems fetching docs")) throw error;
|
|
61
|
+
throw new Error(`Query failed ${query} and ${JSON.stringify(params)}. Error: ${error.message}`);
|
|
62
|
+
})));
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
const $77d50d608d9ace00$var$defaultProps = {
|
|
70
|
+
title: "Last created",
|
|
71
|
+
order: "_createdAt desc",
|
|
72
|
+
limit: 10,
|
|
73
|
+
queryParams: {},
|
|
74
|
+
showCreateButton: true,
|
|
75
|
+
apiVersion: "v1"
|
|
76
|
+
};
|
|
77
|
+
function $77d50d608d9ace00$export$65a803c9e13a7e5b(props) {
|
|
78
|
+
const { query: query , limit: limit , apiVersion: apiVersion , queryParams: queryParams , types: types , order: order , title: title , showCreateButton: showCreateButton , createButtonText: createButtonText , } = {
|
|
79
|
+
...$77d50d608d9ace00$var$defaultProps,
|
|
80
|
+
...props
|
|
81
|
+
};
|
|
82
|
+
const [documents, setDocuments] = (0, $7xGut$react.useState)();
|
|
83
|
+
const [loading, setLoading] = (0, $7xGut$react.useState)(true);
|
|
84
|
+
const [error, setError] = (0, $7xGut$react.useState)();
|
|
85
|
+
const client = (0, $7xGut$sanity.useClient)();
|
|
86
|
+
const schema = (0, $7xGut$sanity.useSchema)();
|
|
87
|
+
const versionedClient = (0, $7xGut$react.useMemo)(()=>client.withConfig({
|
|
88
|
+
apiVersion: apiVersion
|
|
89
|
+
}), [
|
|
90
|
+
client,
|
|
91
|
+
apiVersion
|
|
92
|
+
]);
|
|
93
|
+
const { assembledQuery: assembledQuery , params: params } = (0, $7xGut$react.useMemo)(()=>{
|
|
94
|
+
if (query) return {
|
|
95
|
+
assembledQuery: query,
|
|
96
|
+
params: queryParams
|
|
97
|
+
};
|
|
98
|
+
const documentTypes = schema.getTypeNames().filter((typeName)=>{
|
|
99
|
+
const schemaType = schema.get(typeName);
|
|
100
|
+
return schemaType.type && schemaType.type.name === "document";
|
|
101
|
+
});
|
|
102
|
+
return {
|
|
103
|
+
assembledQuery: `*[_type in $types] | order(${order}) [0...${limit * 2}]`,
|
|
104
|
+
params: {
|
|
105
|
+
types: types ? (0, $7xGut$lodash.intersection)(types, documentTypes) : documentTypes
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}, [
|
|
109
|
+
schema,
|
|
110
|
+
query,
|
|
111
|
+
queryParams,
|
|
112
|
+
order,
|
|
113
|
+
limit,
|
|
114
|
+
types
|
|
115
|
+
]);
|
|
116
|
+
(0, $7xGut$react.useEffect)(()=>{
|
|
117
|
+
if (!assembledQuery) return;
|
|
118
|
+
const subscription = (0, $91d1c422801a90a3$export$161e8c29756c0df4)(assembledQuery, params, versionedClient).subscribe({
|
|
119
|
+
next: (d)=>{
|
|
120
|
+
setDocuments(d.slice(0, limit));
|
|
121
|
+
setLoading(false);
|
|
122
|
+
},
|
|
123
|
+
error: (e)=>{
|
|
124
|
+
setError(e);
|
|
125
|
+
setLoading(false);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
// eslint-disable-next-line consistent-return
|
|
129
|
+
return ()=>{
|
|
130
|
+
subscription.unsubscribe();
|
|
131
|
+
};
|
|
132
|
+
}, [
|
|
133
|
+
limit,
|
|
134
|
+
versionedClient,
|
|
135
|
+
assembledQuery,
|
|
136
|
+
params
|
|
137
|
+
]);
|
|
138
|
+
return /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanitydashboard.DashboardWidgetContainer), {
|
|
139
|
+
header: title,
|
|
140
|
+
footer: types && types.length === 1 && showCreateButton && /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanity_unstable.IntentButton), {
|
|
141
|
+
mode: "bleed",
|
|
142
|
+
style: {
|
|
143
|
+
width: "100%"
|
|
144
|
+
},
|
|
145
|
+
paddingX: 2,
|
|
146
|
+
paddingY: 4,
|
|
147
|
+
tone: "primary",
|
|
148
|
+
type: "button",
|
|
149
|
+
intent: "create",
|
|
150
|
+
params: {
|
|
151
|
+
type: types[0]
|
|
152
|
+
},
|
|
153
|
+
text: createButtonText || `Create new ${types[0]}`
|
|
154
|
+
}),
|
|
155
|
+
children: /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsxs)((0, $7xGut$sanityui.Card), {
|
|
156
|
+
children: [
|
|
157
|
+
error && /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)("div", {
|
|
158
|
+
children: error.message
|
|
159
|
+
}),
|
|
160
|
+
!error && loading && /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanityui.Card), {
|
|
161
|
+
padding: 4,
|
|
162
|
+
children: /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanityui.Flex), {
|
|
163
|
+
justify: "center",
|
|
164
|
+
children: /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanityui.Spinner), {
|
|
165
|
+
muted: true
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
}),
|
|
169
|
+
!error && !documents && !loading && /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)("div", {
|
|
170
|
+
children: "Could not locate any documents :/"
|
|
171
|
+
}),
|
|
172
|
+
/*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanityui.Stack), {
|
|
173
|
+
space: 2,
|
|
174
|
+
children: documents && documents.map((doc)=>/*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)($77d50d608d9ace00$var$MenuEntry, {
|
|
175
|
+
doc: doc
|
|
176
|
+
}, doc._id))
|
|
177
|
+
})
|
|
178
|
+
]
|
|
179
|
+
})
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
function $77d50d608d9ace00$var$MenuEntry({ doc: doc }) {
|
|
183
|
+
const schema = (0, $7xGut$sanity.useSchema)();
|
|
184
|
+
const type = schema.get(doc._type);
|
|
185
|
+
return /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanityui.Card), {
|
|
186
|
+
flex: 1,
|
|
187
|
+
children: /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanity_unstable.IntentButton), {
|
|
188
|
+
intent: "edit",
|
|
189
|
+
mode: "bleed",
|
|
190
|
+
padding: 1,
|
|
191
|
+
radius: 0,
|
|
192
|
+
params: {
|
|
193
|
+
type: doc._type,
|
|
194
|
+
id: (0, $7xGut$sanity.getPublishedId)(doc._id)
|
|
195
|
+
},
|
|
196
|
+
style: {
|
|
197
|
+
width: "100%"
|
|
198
|
+
},
|
|
199
|
+
children: /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $7xGut$sanity_unstable.SanityPreview), {
|
|
200
|
+
layout: "default",
|
|
201
|
+
type: type,
|
|
202
|
+
value: doc
|
|
203
|
+
}, doc._id)
|
|
204
|
+
})
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
var $77d50d608d9ace00$export$2e2bcd8739ae039 = $77d50d608d9ace00$export$65a803c9e13a7e5b;
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
function $9233cea927cb9637$export$398a1547d616c51(config) {
|
|
212
|
+
return {
|
|
213
|
+
name: "document-list-widget",
|
|
214
|
+
component: function component() {
|
|
215
|
+
return /*#__PURE__*/ (0, $7xGut$reactjsxruntime.jsx)((0, $77d50d608d9ace00$export$2e2bcd8739ae039), {
|
|
216
|
+
...config
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
layout: config.layout
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;AAAA;ACAA;;;;;ACAA;;;AAMA,MAAM,6BAAO,GAAG,CAAC,WAA2B,GAAK,CAAC,OAAO,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;AAE5E,SAAS,yCAAmB,CAC1B,QAA2C,EAC3C,MAAoB,EACO;IAC3B,IAAI,CAAC,QAAQ,EACX,OAAO,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IAE5B,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO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,CAAI;UAC3D;MACV,CACR;CACF;IAED,wCAA2B,GAAZ,yCAAY;;;;ADhJpB,SAAS,wCAAkB,CAAC,MAAgC,EAAmB;IACpF,OAAO;QACL,IAAI,EAAE,sBAAsB;QAC5B,SAAS,EAAE,SAAS,SAAS,GAAG;YAC9B,qBAAO,gCAAC,CAAA,GAAA,wCAAY,CAAA;gBAAE,GAAG,MAAM;cAAI,CAAA;SACpC;QACD,MAAM,EAAE,MAAM,CAAC,MAAM;KACtB,CAAA;CACF","sources":["src/index.tsx","src/DocumentList.tsx","src/sanityConnector.ts"],"sourcesContent":["import DocumentList, {DocumentListConfig} from './DocumentList'\nimport React from 'react'\nimport {LayoutConfig, DashboardWidget} from '@sanity/dashboard'\n\nexport interface DocumentListWidgetConfig extends DocumentListConfig {\n layout?: LayoutConfig\n}\n\nexport function documentListWidget(config: DocumentListWidgetConfig): DashboardWidget {\n return {\n name: 'document-list-widget',\n component: function component() {\n return <DocumentList {...config} />\n },\n layout: config.layout,\n }\n}\n","import React, {useEffect, useMemo, useState} from 'react'\nimport {IntentButton, SanityPreview} from 'sanity/_unstable'\nimport {getPublishedId, useClient, useSchema} from 'sanity'\nimport {intersection} from 'lodash'\nimport {getSubscription} from './sanityConnector'\nimport {SanityDocument} from '@sanity/types'\nimport {Card, Flex, Spinner, Stack} from '@sanity/ui'\nimport {DashboardWidgetContainer} from '@sanity/dashboard'\n\nexport interface DocumentListConfig {\n title?: string\n types?: string[]\n query?: string\n queryParams?: Record<string, any>\n order?: string\n limit?: number\n showCreateButton?: boolean\n createButtonText?: string\n apiVersion?: string\n}\n\nconst defaultProps = {\n title: 'Last created',\n order: '_createdAt desc',\n limit: 10,\n queryParams: {},\n showCreateButton: true,\n apiVersion: 'v1',\n}\n\nexport function DocumentList(props: DocumentListConfig) {\n const {\n query,\n limit,\n apiVersion,\n queryParams,\n types,\n order,\n title,\n showCreateButton,\n createButtonText,\n } = {\n ...defaultProps,\n ...props,\n }\n\n const [documents, setDocuments] = useState<SanityDocument[] | undefined>()\n const [loading, setLoading] = useState<boolean>(true)\n const [error, setError] = useState<Error | undefined>()\n\n const client = useClient()\n const schema = useSchema()\n const versionedClient = useMemo(() => client.withConfig({apiVersion}), [client, apiVersion])\n\n const {assembledQuery, params} = useMemo(() => {\n if (query) {\n return {assembledQuery: query, params: queryParams}\n }\n\n const documentTypes = schema.getTypeNames().filter((typeName) => {\n const schemaType = schema.get(typeName)\n return schemaType.type && schemaType.type.name === 'document'\n })\n\n return {\n assembledQuery: `*[_type in $types] | order(${order}) [0...${limit * 2}]`,\n params: {types: types ? intersection(types, documentTypes) : documentTypes},\n }\n }, [schema, query, queryParams, order, limit, types])\n\n useEffect(() => {\n if (!assembledQuery) {\n return\n }\n\n const subscription = getSubscription(assembledQuery, params, versionedClient).subscribe({\n next: (d) => {\n setDocuments(d.slice(0, limit))\n setLoading(false)\n },\n error: (e) => {\n setError(e)\n setLoading(false)\n },\n })\n // eslint-disable-next-line consistent-return\n return () => {\n subscription.unsubscribe()\n }\n }, [limit, versionedClient, assembledQuery, params])\n\n return (\n <DashboardWidgetContainer\n header={title}\n footer={\n types &&\n types.length === 1 &&\n showCreateButton && (\n <IntentButton\n mode=\"bleed\"\n style={{width: '100%'}}\n paddingX={2}\n paddingY={4}\n tone=\"primary\"\n type=\"button\"\n intent=\"create\"\n params={{type: types[0]}}\n text={createButtonText || `Create new ${types[0]}`}\n />\n )\n }\n >\n <Card>\n {error && <div>{error.message}</div>}\n {!error && loading && (\n <Card padding={4}>\n <Flex justify=\"center\">\n <Spinner muted />\n </Flex>\n </Card>\n )}\n {!error && !documents && !loading && <div>Could not locate any documents :/</div>}\n <Stack space={2}>\n {documents && documents.map((doc) => <MenuEntry key={doc._id} doc={doc} />)}\n </Stack>\n </Card>\n </DashboardWidgetContainer>\n )\n}\n\nfunction MenuEntry({doc}: {doc: SanityDocument}) {\n const schema = useSchema()\n const type = schema.get(doc._type)\n return (\n <Card flex={1}>\n <IntentButton\n intent=\"edit\"\n mode=\"bleed\"\n padding={1}\n radius={0}\n params={{\n type: doc._type,\n id: getPublishedId(doc._id),\n }}\n style={{width: '100%'}}\n >\n <SanityPreview layout=\"default\" type={type} value={doc} key={doc._id} />\n </IntentButton>\n </Card>\n )\n}\n\nexport default DocumentList\n","import {Observable, of as observableOf} from 'rxjs'\nimport {delay, mergeMap, switchMap, tap} from 'rxjs/operators'\nimport uniqBy from 'lodash/uniqBy'\nimport {SanityClient} from '@sanity/client'\nimport {SanityDocument} from '@sanity/types'\n\nconst draftId = (nonDraftDoc: SanityDocument) => `drafts.${nonDraftDoc._id}`\n\nfunction prepareDocumentList(\n incoming: SanityDocument | SanityDocument[],\n client: SanityClient\n): Promise<SanityDocument[]> {\n if (!incoming) {\n return Promise.resolve([])\n }\n const documents = Array.isArray(incoming) ? incoming : [incoming]\n\n const ids = documents.filter((doc) => !doc._id.startsWith('drafts.')).map(draftId)\n\n return client\n .fetch('*[_id in $ids]', {ids})\n .then((drafts) => {\n const outgoing = documents.map((doc) => {\n const foundDraft = drafts.find((draft) => draft._id === draftId(doc))\n return foundDraft || doc\n })\n return uniqBy(outgoing, '_id')\n })\n .catch((error) => {\n throw new Error(`Problems fetching docs ${ids}. Error: ${error.message}`)\n })\n}\n\nexport function getSubscription(\n query: string,\n params: Record<string, any>,\n client: SanityClient\n): Observable<SanityDocument[]> {\n return client\n .listen(query, params, {\n events: ['welcome', 'mutation'],\n includeResult: false,\n visibility: 'query',\n })\n .pipe(\n switchMap((event) => {\n return observableOf(1).pipe(\n event.type === 'welcome' ? tap() : delay(1000),\n mergeMap(() =>\n client\n .fetch(query, params)\n .then((incoming) => {\n return prepareDocumentList(incoming, client)\n })\n .catch((error) => {\n if (error.message.startsWith('Problems fetching docs')) {\n throw error\n }\n throw new Error(\n `Query failed ${query} and ${JSON.stringify(params)}. Error: ${error.message}`\n )\n })\n )\n )\n })\n )\n}\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {jsx as $5lYf0$jsx, jsxs as $5lYf0$jsxs} from "react/jsx-runtime";
|
|
2
|
+
import {useState as $5lYf0$useState, useMemo as $5lYf0$useMemo, useEffect as $5lYf0$useEffect} from "react";
|
|
3
|
+
import {IntentButton as $5lYf0$IntentButton, SanityPreview as $5lYf0$SanityPreview} from "sanity/_unstable";
|
|
4
|
+
import {useClient as $5lYf0$useClient, useSchema as $5lYf0$useSchema, getPublishedId as $5lYf0$getPublishedId} from "sanity";
|
|
5
|
+
import {intersection as $5lYf0$intersection} from "lodash";
|
|
6
|
+
import {Card as $5lYf0$Card, Flex as $5lYf0$Flex, Spinner as $5lYf0$Spinner, Stack as $5lYf0$Stack} from "@sanity/ui";
|
|
7
|
+
import {DashboardWidgetContainer as $5lYf0$DashboardWidgetContainer} from "@sanity/dashboard";
|
|
8
|
+
import {of as $5lYf0$of} from "rxjs";
|
|
9
|
+
import {switchMap as $5lYf0$switchMap, tap as $5lYf0$tap, delay as $5lYf0$delay, mergeMap as $5lYf0$mergeMap} from "rxjs/operators";
|
|
10
|
+
import $5lYf0$lodashuniqBy from "lodash/uniqBy";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
const $31d241600102c2cb$var$draftId = (nonDraftDoc)=>`drafts.${nonDraftDoc._id}`;
|
|
22
|
+
function $31d241600102c2cb$var$prepareDocumentList(incoming, client) {
|
|
23
|
+
if (!incoming) return Promise.resolve([]);
|
|
24
|
+
const documents = Array.isArray(incoming) ? incoming : [
|
|
25
|
+
incoming
|
|
26
|
+
];
|
|
27
|
+
const ids = documents.filter((doc)=>!doc._id.startsWith("drafts.")).map($31d241600102c2cb$var$draftId);
|
|
28
|
+
return client.fetch("*[_id in $ids]", {
|
|
29
|
+
ids: ids
|
|
30
|
+
}).then((drafts)=>{
|
|
31
|
+
const outgoing = documents.map((doc)=>{
|
|
32
|
+
const foundDraft = drafts.find((draft)=>draft._id === $31d241600102c2cb$var$draftId(doc));
|
|
33
|
+
return foundDraft || doc;
|
|
34
|
+
});
|
|
35
|
+
return (0, $5lYf0$lodashuniqBy)(outgoing, "_id");
|
|
36
|
+
}).catch((error)=>{
|
|
37
|
+
throw new Error(`Problems fetching docs ${ids}. Error: ${error.message}`);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
function $31d241600102c2cb$export$161e8c29756c0df4(query, params, client) {
|
|
41
|
+
return client.listen(query, params, {
|
|
42
|
+
events: [
|
|
43
|
+
"welcome",
|
|
44
|
+
"mutation"
|
|
45
|
+
],
|
|
46
|
+
includeResult: false,
|
|
47
|
+
visibility: "query"
|
|
48
|
+
}).pipe((0, $5lYf0$switchMap)((event)=>{
|
|
49
|
+
return (0, $5lYf0$of)(1).pipe(event.type === "welcome" ? (0, $5lYf0$tap)() : (0, $5lYf0$delay)(1000), (0, $5lYf0$mergeMap)(()=>client.fetch(query, params).then((incoming)=>{
|
|
50
|
+
return $31d241600102c2cb$var$prepareDocumentList(incoming, client);
|
|
51
|
+
}).catch((error)=>{
|
|
52
|
+
if (error.message.startsWith("Problems fetching docs")) throw error;
|
|
53
|
+
throw new Error(`Query failed ${query} and ${JSON.stringify(params)}. Error: ${error.message}`);
|
|
54
|
+
})));
|
|
55
|
+
}));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
const $df774c2b801774e3$var$defaultProps = {
|
|
62
|
+
title: "Last created",
|
|
63
|
+
order: "_createdAt desc",
|
|
64
|
+
limit: 10,
|
|
65
|
+
queryParams: {},
|
|
66
|
+
showCreateButton: true,
|
|
67
|
+
apiVersion: "v1"
|
|
68
|
+
};
|
|
69
|
+
function $df774c2b801774e3$export$65a803c9e13a7e5b(props) {
|
|
70
|
+
const { query: query , limit: limit , apiVersion: apiVersion , queryParams: queryParams , types: types , order: order , title: title , showCreateButton: showCreateButton , createButtonText: createButtonText , } = {
|
|
71
|
+
...$df774c2b801774e3$var$defaultProps,
|
|
72
|
+
...props
|
|
73
|
+
};
|
|
74
|
+
const [documents, setDocuments] = (0, $5lYf0$useState)();
|
|
75
|
+
const [loading, setLoading] = (0, $5lYf0$useState)(true);
|
|
76
|
+
const [error, setError] = (0, $5lYf0$useState)();
|
|
77
|
+
const client = (0, $5lYf0$useClient)();
|
|
78
|
+
const schema = (0, $5lYf0$useSchema)();
|
|
79
|
+
const versionedClient = (0, $5lYf0$useMemo)(()=>client.withConfig({
|
|
80
|
+
apiVersion: apiVersion
|
|
81
|
+
}), [
|
|
82
|
+
client,
|
|
83
|
+
apiVersion
|
|
84
|
+
]);
|
|
85
|
+
const { assembledQuery: assembledQuery , params: params } = (0, $5lYf0$useMemo)(()=>{
|
|
86
|
+
if (query) return {
|
|
87
|
+
assembledQuery: query,
|
|
88
|
+
params: queryParams
|
|
89
|
+
};
|
|
90
|
+
const documentTypes = schema.getTypeNames().filter((typeName)=>{
|
|
91
|
+
const schemaType = schema.get(typeName);
|
|
92
|
+
return schemaType.type && schemaType.type.name === "document";
|
|
93
|
+
});
|
|
94
|
+
return {
|
|
95
|
+
assembledQuery: `*[_type in $types] | order(${order}) [0...${limit * 2}]`,
|
|
96
|
+
params: {
|
|
97
|
+
types: types ? (0, $5lYf0$intersection)(types, documentTypes) : documentTypes
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}, [
|
|
101
|
+
schema,
|
|
102
|
+
query,
|
|
103
|
+
queryParams,
|
|
104
|
+
order,
|
|
105
|
+
limit,
|
|
106
|
+
types
|
|
107
|
+
]);
|
|
108
|
+
(0, $5lYf0$useEffect)(()=>{
|
|
109
|
+
if (!assembledQuery) return;
|
|
110
|
+
const subscription = (0, $31d241600102c2cb$export$161e8c29756c0df4)(assembledQuery, params, versionedClient).subscribe({
|
|
111
|
+
next: (d)=>{
|
|
112
|
+
setDocuments(d.slice(0, limit));
|
|
113
|
+
setLoading(false);
|
|
114
|
+
},
|
|
115
|
+
error: (e)=>{
|
|
116
|
+
setError(e);
|
|
117
|
+
setLoading(false);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
// eslint-disable-next-line consistent-return
|
|
121
|
+
return ()=>{
|
|
122
|
+
subscription.unsubscribe();
|
|
123
|
+
};
|
|
124
|
+
}, [
|
|
125
|
+
limit,
|
|
126
|
+
versionedClient,
|
|
127
|
+
assembledQuery,
|
|
128
|
+
params
|
|
129
|
+
]);
|
|
130
|
+
return /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$DashboardWidgetContainer), {
|
|
131
|
+
header: title,
|
|
132
|
+
footer: types && types.length === 1 && showCreateButton && /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$IntentButton), {
|
|
133
|
+
mode: "bleed",
|
|
134
|
+
style: {
|
|
135
|
+
width: "100%"
|
|
136
|
+
},
|
|
137
|
+
paddingX: 2,
|
|
138
|
+
paddingY: 4,
|
|
139
|
+
tone: "primary",
|
|
140
|
+
type: "button",
|
|
141
|
+
intent: "create",
|
|
142
|
+
params: {
|
|
143
|
+
type: types[0]
|
|
144
|
+
},
|
|
145
|
+
text: createButtonText || `Create new ${types[0]}`
|
|
146
|
+
}),
|
|
147
|
+
children: /*#__PURE__*/ (0, $5lYf0$jsxs)((0, $5lYf0$Card), {
|
|
148
|
+
children: [
|
|
149
|
+
error && /*#__PURE__*/ (0, $5lYf0$jsx)("div", {
|
|
150
|
+
children: error.message
|
|
151
|
+
}),
|
|
152
|
+
!error && loading && /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$Card), {
|
|
153
|
+
padding: 4,
|
|
154
|
+
children: /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$Flex), {
|
|
155
|
+
justify: "center",
|
|
156
|
+
children: /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$Spinner), {
|
|
157
|
+
muted: true
|
|
158
|
+
})
|
|
159
|
+
})
|
|
160
|
+
}),
|
|
161
|
+
!error && !documents && !loading && /*#__PURE__*/ (0, $5lYf0$jsx)("div", {
|
|
162
|
+
children: "Could not locate any documents :/"
|
|
163
|
+
}),
|
|
164
|
+
/*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$Stack), {
|
|
165
|
+
space: 2,
|
|
166
|
+
children: documents && documents.map((doc)=>/*#__PURE__*/ (0, $5lYf0$jsx)($df774c2b801774e3$var$MenuEntry, {
|
|
167
|
+
doc: doc
|
|
168
|
+
}, doc._id))
|
|
169
|
+
})
|
|
170
|
+
]
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function $df774c2b801774e3$var$MenuEntry({ doc: doc }) {
|
|
175
|
+
const schema = (0, $5lYf0$useSchema)();
|
|
176
|
+
const type = schema.get(doc._type);
|
|
177
|
+
return /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$Card), {
|
|
178
|
+
flex: 1,
|
|
179
|
+
children: /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$IntentButton), {
|
|
180
|
+
intent: "edit",
|
|
181
|
+
mode: "bleed",
|
|
182
|
+
padding: 1,
|
|
183
|
+
radius: 0,
|
|
184
|
+
params: {
|
|
185
|
+
type: doc._type,
|
|
186
|
+
id: (0, $5lYf0$getPublishedId)(doc._id)
|
|
187
|
+
},
|
|
188
|
+
style: {
|
|
189
|
+
width: "100%"
|
|
190
|
+
},
|
|
191
|
+
children: /*#__PURE__*/ (0, $5lYf0$jsx)((0, $5lYf0$SanityPreview), {
|
|
192
|
+
layout: "default",
|
|
193
|
+
type: type,
|
|
194
|
+
value: doc
|
|
195
|
+
}, doc._id)
|
|
196
|
+
})
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
var $df774c2b801774e3$export$2e2bcd8739ae039 = $df774c2b801774e3$export$65a803c9e13a7e5b;
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
function $090815f5086f7f29$export$398a1547d616c51(config) {
|
|
204
|
+
return {
|
|
205
|
+
name: "document-list-widget",
|
|
206
|
+
component: function component() {
|
|
207
|
+
return /*#__PURE__*/ (0, $5lYf0$jsx)((0, $df774c2b801774e3$export$2e2bcd8739ae039), {
|
|
208
|
+
...config
|
|
209
|
+
});
|
|
210
|
+
},
|
|
211
|
+
layout: config.layout
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
export {$090815f5086f7f29$export$398a1547d616c51 as documentListWidget};
|
|
217
|
+
//# sourceMappingURL=index.js.map
|