sanity-plugin-dashboard-widget-document-list 3.0.0-v3-studio.7 → 3.0.1
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/LICENSE +1 -1
- package/README.md +36 -54
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +180 -0
- package/dist/index.js.map +1 -0
- package/package.json +42 -74
- package/lib/index.esm.js +0 -2
- package/lib/index.esm.js.map +0 -1
- package/lib/index.js +0 -2
- package/lib/index.js.map +0 -1
- package/lib/src/index.d.ts +0 -24
- package/sanity.json +0 -8
- package/src/DocumentList.tsx +0 -155
- package/src/index.ts +0 -1
- package/src/plugin.tsx +0 -17
- package/src/sanityConnector.ts +0 -67
- package/v2-incompatible.js +0 -11
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
# sanity-plugin-dashboard-widget-document-list
|
|
2
2
|
|
|
3
|
-
> **NOTE**
|
|
4
|
-
>
|
|
5
|
-
> This is the **Sanity Studio v3 version** of dashboard-widget-document-list.
|
|
6
|
-
>
|
|
7
|
-
> For the v2 version, please refer to the [v2-branch](https://github.com/sanity-io/dashboard-widget-document-list).
|
|
8
|
-
|
|
9
3
|
## What is it?
|
|
10
4
|
|
|
11
5
|
Dashboard widget for the Sanity Content Studio which displays a list of documents.
|
|
@@ -13,13 +7,13 @@ Dashboard widget for the Sanity Content Studio which displays a list of document
|
|
|
13
7
|
## Install
|
|
14
8
|
|
|
15
9
|
```
|
|
16
|
-
npm install --save sanity-plugin-dashboard-widget-document-list
|
|
10
|
+
npm install --save sanity-plugin-dashboard-widget-document-list
|
|
17
11
|
```
|
|
18
12
|
|
|
19
13
|
or
|
|
20
14
|
|
|
21
15
|
```
|
|
22
|
-
yarn add sanity-plugin-dashboard-widget-document-list
|
|
16
|
+
yarn add sanity-plugin-dashboard-widget-document-list
|
|
23
17
|
```
|
|
24
18
|
|
|
25
19
|
Ensure that you have followed install and usage instructions for @sanity/dashboard.
|
|
@@ -29,86 +23,88 @@ Ensure that you have followed install and usage instructions for @sanity/dashboa
|
|
|
29
23
|
Add dashboard-widget-document-list as a widget to @sanity/dashboard plugin in sanity.config.ts (or .js):
|
|
30
24
|
|
|
31
25
|
```js
|
|
32
|
-
import {
|
|
33
|
-
import {
|
|
26
|
+
import {dashboardTool} from '@sanity/dashboard'
|
|
27
|
+
import {documentListWidget} from 'sanity-plugin-dashboard-widget-document-list'
|
|
34
28
|
|
|
35
29
|
export default defineConfig({
|
|
36
30
|
// ...
|
|
37
31
|
plugins: [
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
),
|
|
44
|
-
]
|
|
32
|
+
dashboardTool({
|
|
33
|
+
widgets: [documentListWidget()],
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
45
36
|
})
|
|
46
37
|
```
|
|
47
38
|
|
|
48
|
-
|
|
39
|
+
_Note_: If a document in the result (as returned by the backend) has a draft, that draft is rendered instead of the published document.
|
|
49
40
|
|
|
50
41
|
## Options
|
|
51
42
|
|
|
52
43
|
There are some options available, as specified by [DocumentListConfig](src/DocumentList.tsx):
|
|
53
44
|
|
|
54
45
|
### `title` (string)
|
|
46
|
+
|
|
55
47
|
Widget title
|
|
56
48
|
|
|
57
49
|
```js
|
|
58
50
|
documentListWidget({
|
|
59
|
-
|
|
51
|
+
title: 'Some documents',
|
|
60
52
|
})
|
|
61
53
|
```
|
|
62
54
|
|
|
63
55
|
### `order` (string)
|
|
56
|
+
|
|
64
57
|
Field and direction to order by when docs are rendered
|
|
65
58
|
|
|
66
59
|
```js
|
|
67
60
|
documentListWidget({
|
|
68
|
-
|
|
69
|
-
|
|
61
|
+
title: 'Last edited',
|
|
62
|
+
order: '_updatedAt desc',
|
|
70
63
|
})
|
|
71
64
|
```
|
|
72
65
|
|
|
73
66
|
### `limit` (number)
|
|
67
|
+
|
|
74
68
|
Number of docs rendered
|
|
75
69
|
|
|
76
70
|
```js
|
|
77
71
|
documentListWidget({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
72
|
+
title: 'Last edited',
|
|
73
|
+
order: '_updatedAt desc',
|
|
74
|
+
limit: 3,
|
|
81
75
|
})
|
|
82
76
|
```
|
|
83
77
|
|
|
84
78
|
### `types` (array)
|
|
79
|
+
|
|
85
80
|
Array of strings signifying which document (schema) types are fetched
|
|
86
81
|
|
|
87
82
|
```js
|
|
88
83
|
documentListWidget({
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
title: 'Last edited',
|
|
85
|
+
order: '_updatedAt desc',
|
|
86
|
+
types: ['book', 'author'],
|
|
92
87
|
})
|
|
93
88
|
```
|
|
94
89
|
|
|
95
90
|
### `query` (string) and `params` (object)
|
|
91
|
+
|
|
96
92
|
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.
|
|
97
93
|
|
|
98
94
|
```js
|
|
99
95
|
documentListWidget({
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
title: 'Published books by title',
|
|
97
|
+
query: '*[_type == "book" && published == true] | order(title asc) [0...10]',
|
|
102
98
|
})
|
|
103
99
|
```
|
|
104
100
|
|
|
105
101
|
```js
|
|
106
102
|
documentListWidget({
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
103
|
+
title: 'My favorite documents',
|
|
104
|
+
query: '*[_id in $ids]',
|
|
105
|
+
params: {
|
|
106
|
+
ids: ['ab2', 'c5z', '654'],
|
|
107
|
+
},
|
|
112
108
|
})
|
|
113
109
|
```
|
|
114
110
|
|
|
@@ -118,9 +114,9 @@ You can override the button default button text (`Create new ${types[0]}`) by se
|
|
|
118
114
|
|
|
119
115
|
```js
|
|
120
116
|
documentListWidget({
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
117
|
+
title: 'Blog posts',
|
|
118
|
+
query: '*[_type == "post"]',
|
|
119
|
+
createButtonText: 'Create new blog post',
|
|
124
120
|
})
|
|
125
121
|
```
|
|
126
122
|
|
|
@@ -130,34 +126,20 @@ You can disable the create button altogether by passing a `showCreateButton` boo
|
|
|
130
126
|
|
|
131
127
|
```js
|
|
132
128
|
documentListWidget({
|
|
133
|
-
|
|
129
|
+
showCreateButton: false,
|
|
134
130
|
})
|
|
135
131
|
```
|
|
136
132
|
|
|
137
133
|
### Widget size
|
|
138
134
|
|
|
139
135
|
You can change the width of the plugin using `layout.width`:
|
|
136
|
+
|
|
140
137
|
```js
|
|
141
138
|
documentListWidget({
|
|
142
|
-
|
|
139
|
+
layout: {width: 'small'},
|
|
143
140
|
})
|
|
144
141
|
```
|
|
145
142
|
|
|
146
143
|
## License
|
|
147
144
|
|
|
148
145
|
MIT-licensed. See LICENSE.
|
|
149
|
-
|
|
150
|
-
## Develop & test
|
|
151
|
-
|
|
152
|
-
This plugin uses [@sanity/plugin-kit](https://github.com/sanity-io/plugin-kit)
|
|
153
|
-
with default configuration for build & watch scripts.
|
|
154
|
-
|
|
155
|
-
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
|
|
156
|
-
on how to run this plugin with hotreload in the studio.
|
|
157
|
-
|
|
158
|
-
### Release new version
|
|
159
|
-
|
|
160
|
-
Run ["CI & Release" workflow](https://github.com/sanity-io/dashboard-widget-document-list/actions/workflows/main.yml).
|
|
161
|
-
Make sure to select the main branch and check "Release new version".
|
|
162
|
-
|
|
163
|
-
Semantic release will only release on configured branches, so it is safe to run release on any branch.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DashboardWidget, LayoutConfig } from "@sanity/dashboard";
|
|
2
|
+
interface DocumentListConfig {
|
|
3
|
+
title?: string;
|
|
4
|
+
types?: string[];
|
|
5
|
+
query?: string;
|
|
6
|
+
queryParams?: Record<string, any>;
|
|
7
|
+
order?: string;
|
|
8
|
+
limit?: number;
|
|
9
|
+
showCreateButton?: boolean;
|
|
10
|
+
createButtonText?: string;
|
|
11
|
+
apiVersion?: string;
|
|
12
|
+
}
|
|
13
|
+
interface DocumentListWidgetConfig extends DocumentListConfig {
|
|
14
|
+
layout?: LayoutConfig;
|
|
15
|
+
}
|
|
16
|
+
declare function documentListWidget(config: DocumentListWidgetConfig): DashboardWidget;
|
|
17
|
+
export { type DocumentListWidgetConfig, documentListWidget };
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/DocumentList.tsx","../src/plugin.tsx"],"mappings":";UAeiB,kBAAA;EACf,KAAA;EACA,KAAA;EACA,KAAA;EACA,WAAA,GAAc,MAAM;EACpB,KAAA;EACA,KAAA;EACA,gBAAA;EACA,gBAAA;EACA,UAAA;AAAA;AAAA,UCpBe,wBAAA,SAAiC,kBAAkB;EAClE,MAAA,GAAS,YAAA;AAAA;AAAA,iBAGK,kBAAA,CAAmB,MAAA,EAAQ,wBAAA,GAA2B,eAAe"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { c } from "react/compiler-runtime";
|
|
3
|
+
import { DashboardWidgetContainer } from "@sanity/dashboard";
|
|
4
|
+
import { Card, Flex, Spinner, Stack } from "@sanity/ui";
|
|
5
|
+
import intersection from "lodash-es/intersection.js";
|
|
6
|
+
import { useState, useEffect } from "react";
|
|
7
|
+
import { useClient, useSchema, getPublishedId, IntentButton, Preview } from "sanity";
|
|
8
|
+
import uniqBy from "lodash-es/uniqBy.js";
|
|
9
|
+
import { of } from "rxjs";
|
|
10
|
+
import { switchMap, tap, delay, mergeMap } from "rxjs/operators";
|
|
11
|
+
const draftId = (nonDraftDoc) => `drafts.${nonDraftDoc._id}`;
|
|
12
|
+
function prepareDocumentList(incoming, client) {
|
|
13
|
+
if (!incoming)
|
|
14
|
+
return Promise.resolve([]);
|
|
15
|
+
const documents = Array.isArray(incoming) ? incoming : [incoming], ids = documents.filter((doc) => !doc._id.startsWith("drafts.")).map(draftId);
|
|
16
|
+
return client.fetch("*[_id in $ids]", {
|
|
17
|
+
ids
|
|
18
|
+
}).then((drafts) => {
|
|
19
|
+
const outgoing = documents.map((doc) => drafts.find((draft) => draft._id === draftId(doc)) || doc);
|
|
20
|
+
return uniqBy(outgoing, "_id");
|
|
21
|
+
}).catch((error) => {
|
|
22
|
+
throw new Error(`Problems fetching docs ${ids.join(", ")}. Error: ${error.message}`);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function getSubscription(query, params, client) {
|
|
26
|
+
return client.listen(query, params, {
|
|
27
|
+
events: ["welcome", "mutation"],
|
|
28
|
+
includeResult: !1,
|
|
29
|
+
visibility: "query"
|
|
30
|
+
}).pipe(switchMap((event) => of(1).pipe(event.type === "welcome" ? tap() : delay(1e3), mergeMap(() => client.fetch(query, params).then((incoming) => prepareDocumentList(incoming, client)).catch((error) => {
|
|
31
|
+
throw error.message.startsWith("Problems fetching docs") ? error : new Error(`Query failed ${query} and ${JSON.stringify(params)}. Error: ${error.message}`);
|
|
32
|
+
})))));
|
|
33
|
+
}
|
|
34
|
+
const defaultProps = {
|
|
35
|
+
title: "Last created",
|
|
36
|
+
order: "_createdAt desc",
|
|
37
|
+
limit: 10,
|
|
38
|
+
queryParams: {},
|
|
39
|
+
showCreateButton: !0,
|
|
40
|
+
apiVersion: "v1"
|
|
41
|
+
};
|
|
42
|
+
function DocumentList(props) {
|
|
43
|
+
const $ = c(52);
|
|
44
|
+
let t0;
|
|
45
|
+
$[0] !== props ? (t0 = {
|
|
46
|
+
...defaultProps,
|
|
47
|
+
...props
|
|
48
|
+
}, $[0] = props, $[1] = t0) : t0 = $[1];
|
|
49
|
+
const {
|
|
50
|
+
query,
|
|
51
|
+
limit,
|
|
52
|
+
apiVersion,
|
|
53
|
+
queryParams,
|
|
54
|
+
types,
|
|
55
|
+
order,
|
|
56
|
+
title,
|
|
57
|
+
showCreateButton,
|
|
58
|
+
createButtonText
|
|
59
|
+
} = t0, [documents, setDocuments] = useState(), [loading, setLoading] = useState(!0), [error, setError] = useState();
|
|
60
|
+
let t1;
|
|
61
|
+
$[2] !== apiVersion ? (t1 = {
|
|
62
|
+
apiVersion
|
|
63
|
+
}, $[2] = apiVersion, $[3] = t1) : t1 = $[3];
|
|
64
|
+
const versionedClient = useClient(t1), schema = useSchema();
|
|
65
|
+
let t2;
|
|
66
|
+
bb0: {
|
|
67
|
+
if (query) {
|
|
68
|
+
let t33;
|
|
69
|
+
$[4] !== query || $[5] !== queryParams ? (t33 = {
|
|
70
|
+
assembledQuery: query,
|
|
71
|
+
params: queryParams
|
|
72
|
+
}, $[4] = query, $[5] = queryParams, $[6] = t33) : t33 = $[6], t2 = t33;
|
|
73
|
+
break bb0;
|
|
74
|
+
}
|
|
75
|
+
let t32, t42;
|
|
76
|
+
if ($[7] !== limit || $[8] !== order || $[9] !== schema || $[10] !== types) {
|
|
77
|
+
let t53;
|
|
78
|
+
$[13] !== schema ? (t53 = (typeName) => schema.get(typeName)?.type?.name === "document", $[13] = schema, $[14] = t53) : t53 = $[14];
|
|
79
|
+
const documentTypes = schema.getTypeNames().filter(t53);
|
|
80
|
+
t42 = `*[_type in $types] | order(${order}) [0...${limit * 2}]`, t32 = types ? intersection(types, documentTypes) : documentTypes, $[7] = limit, $[8] = order, $[9] = schema, $[10] = types, $[11] = t32, $[12] = t42;
|
|
81
|
+
} else
|
|
82
|
+
t32 = $[11], t42 = $[12];
|
|
83
|
+
let t52;
|
|
84
|
+
$[15] !== t32 ? (t52 = {
|
|
85
|
+
types: t32
|
|
86
|
+
}, $[15] = t32, $[16] = t52) : t52 = $[16];
|
|
87
|
+
let t62;
|
|
88
|
+
$[17] !== t42 || $[18] !== t52 ? (t62 = {
|
|
89
|
+
assembledQuery: t42,
|
|
90
|
+
params: t52
|
|
91
|
+
}, $[17] = t42, $[18] = t52, $[19] = t62) : t62 = $[19], t2 = t62;
|
|
92
|
+
}
|
|
93
|
+
const {
|
|
94
|
+
assembledQuery,
|
|
95
|
+
params
|
|
96
|
+
} = t2;
|
|
97
|
+
let t3, t4;
|
|
98
|
+
$[20] !== assembledQuery || $[21] !== limit || $[22] !== params || $[23] !== versionedClient ? (t3 = () => {
|
|
99
|
+
if (!assembledQuery)
|
|
100
|
+
return;
|
|
101
|
+
const subscription = getSubscription(assembledQuery, params, versionedClient).subscribe({
|
|
102
|
+
next: (d) => {
|
|
103
|
+
setDocuments(d.slice(0, limit)), setLoading(!1);
|
|
104
|
+
},
|
|
105
|
+
error: (e) => {
|
|
106
|
+
setError(e), setLoading(!1);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return () => {
|
|
110
|
+
subscription.unsubscribe();
|
|
111
|
+
};
|
|
112
|
+
}, t4 = [limit, versionedClient, assembledQuery, params], $[20] = assembledQuery, $[21] = limit, $[22] = params, $[23] = versionedClient, $[24] = t3, $[25] = t4) : (t3 = $[24], t4 = $[25]), useEffect(t3, t4);
|
|
113
|
+
let t5;
|
|
114
|
+
$[26] !== createButtonText || $[27] !== showCreateButton || $[28] !== types ? (t5 = types && types.length === 1 && showCreateButton && /* @__PURE__ */ jsx(IntentButton, { mode: "bleed", style: {
|
|
115
|
+
width: "100%"
|
|
116
|
+
}, paddingY: 4, tone: "primary", type: "button", intent: "create", params: {
|
|
117
|
+
type: types[0]
|
|
118
|
+
}, text: createButtonText || `Create new ${types[0]}` }), $[26] = createButtonText, $[27] = showCreateButton, $[28] = types, $[29] = t5) : t5 = $[29];
|
|
119
|
+
let t6;
|
|
120
|
+
$[30] !== error ? (t6 = error && /* @__PURE__ */ jsx("div", { children: error.message }), $[30] = error, $[31] = t6) : t6 = $[31];
|
|
121
|
+
let t7;
|
|
122
|
+
$[32] !== error || $[33] !== loading ? (t7 = !error && loading && /* @__PURE__ */ jsx(Card, { padding: 4, children: /* @__PURE__ */ jsx(Flex, { justify: "center", children: /* @__PURE__ */ jsx(Spinner, { muted: !0 }) }) }), $[32] = error, $[33] = loading, $[34] = t7) : t7 = $[34];
|
|
123
|
+
let t8;
|
|
124
|
+
$[35] !== documents || $[36] !== error || $[37] !== loading ? (t8 = !error && !documents && !loading && /* @__PURE__ */ jsx("div", { children: "Could not locate any documents :/" }), $[35] = documents, $[36] = error, $[37] = loading, $[38] = t8) : t8 = $[38];
|
|
125
|
+
let t9;
|
|
126
|
+
$[39] !== documents ? (t9 = documents && documents.map(_temp), $[39] = documents, $[40] = t9) : t9 = $[40];
|
|
127
|
+
let t10;
|
|
128
|
+
$[41] !== t9 ? (t10 = /* @__PURE__ */ jsx(Stack, { gap: 2, children: t9 }), $[41] = t9, $[42] = t10) : t10 = $[42];
|
|
129
|
+
let t11;
|
|
130
|
+
$[43] !== t10 || $[44] !== t6 || $[45] !== t7 || $[46] !== t8 ? (t11 = /* @__PURE__ */ jsxs(Card, { children: [
|
|
131
|
+
t6,
|
|
132
|
+
t7,
|
|
133
|
+
t8,
|
|
134
|
+
t10
|
|
135
|
+
] }), $[43] = t10, $[44] = t6, $[45] = t7, $[46] = t8, $[47] = t11) : t11 = $[47];
|
|
136
|
+
let t12;
|
|
137
|
+
return $[48] !== t11 || $[49] !== t5 || $[50] !== title ? (t12 = /* @__PURE__ */ jsx(DashboardWidgetContainer, { header: title, footer: t5, children: t11 }), $[48] = t11, $[49] = t5, $[50] = title, $[51] = t12) : t12 = $[51], t12;
|
|
138
|
+
}
|
|
139
|
+
function _temp(doc) {
|
|
140
|
+
return /* @__PURE__ */ jsx(MenuEntry, { doc }, doc._id);
|
|
141
|
+
}
|
|
142
|
+
function MenuEntry(t0) {
|
|
143
|
+
const $ = c(16), {
|
|
144
|
+
doc
|
|
145
|
+
} = t0, schema = useSchema();
|
|
146
|
+
let t1;
|
|
147
|
+
$[0] !== doc._type || $[1] !== schema ? (t1 = schema.get(doc._type), $[0] = doc._type, $[1] = schema, $[2] = t1) : t1 = $[2];
|
|
148
|
+
const type = t1;
|
|
149
|
+
let t2;
|
|
150
|
+
$[3] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t2 = {}, $[3] = t2) : t2 = $[3];
|
|
151
|
+
const t3 = doc._type;
|
|
152
|
+
let t4;
|
|
153
|
+
$[4] !== doc._id ? (t4 = getPublishedId(doc._id), $[4] = doc._id, $[5] = t4) : t4 = $[5];
|
|
154
|
+
let t5;
|
|
155
|
+
$[6] !== doc._type || $[7] !== t4 ? (t5 = {
|
|
156
|
+
type: t3,
|
|
157
|
+
id: t4
|
|
158
|
+
}, $[6] = doc._type, $[7] = t4, $[8] = t5) : t5 = $[8];
|
|
159
|
+
let t6;
|
|
160
|
+
$[9] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (t6 = {
|
|
161
|
+
width: "100%"
|
|
162
|
+
}, $[9] = t6) : t6 = $[9];
|
|
163
|
+
let t7;
|
|
164
|
+
$[10] !== doc || $[11] !== type ? (t7 = type ? /* @__PURE__ */ jsx(Preview, { layout: "default", schemaType: type, value: doc }, doc._id) : "Schema-type missing", $[10] = doc, $[11] = type, $[12] = t7) : t7 = $[12];
|
|
165
|
+
let t8;
|
|
166
|
+
return $[13] !== t5 || $[14] !== t7 ? (t8 = /* @__PURE__ */ jsx(Card, { flex: 1, children: /* @__PURE__ */ jsx(IntentButton, { intent: "edit", mode: "bleed", tooltipProps: t2, params: t5, style: t6, children: t7 }) }), $[13] = t5, $[14] = t7, $[15] = t8) : t8 = $[15], t8;
|
|
167
|
+
}
|
|
168
|
+
function documentListWidget(config) {
|
|
169
|
+
return {
|
|
170
|
+
name: "document-list-widget",
|
|
171
|
+
component: function() {
|
|
172
|
+
return /* @__PURE__ */ jsx(DocumentList, { ...config });
|
|
173
|
+
},
|
|
174
|
+
layout: config.layout
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
export {
|
|
178
|
+
documentListWidget
|
|
179
|
+
};
|
|
180
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/sanityConnector.ts","../src/DocumentList.tsx","../src/plugin.tsx"],"sourcesContent":["import uniqBy from 'lodash-es/uniqBy.js'\nimport {type Observable, of as observableOf} from 'rxjs'\nimport {delay, mergeMap, switchMap, tap} from 'rxjs/operators'\nimport type {SanityClient, SanityDocument} from 'sanity'\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<SanityDocument[]>('*[_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.join(', ')}. 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","import {DashboardWidgetContainer} from '@sanity/dashboard'\nimport {Card, Flex, Spinner, Stack} from '@sanity/ui'\nimport intersection from 'lodash-es/intersection.js'\nimport {type ReactNode, useEffect, useMemo, useState} from 'react'\nimport {\n getPublishedId,\n IntentButton,\n Preview,\n type SanityDocument,\n useClient,\n useSchema,\n} from 'sanity'\n\nimport {getSubscription} from './sanityConnector'\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\nfunction DocumentList(props: DocumentListConfig): ReactNode {\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 versionedClient = useClient({apiVersion})\n const schema = useSchema()\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?.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 gap={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 tooltipProps={{}}\n // padding={1}\n // radius={0}\n params={{\n type: doc._type,\n id: getPublishedId(doc._id),\n }}\n style={{width: '100%'}}\n >\n {type ? (\n <Preview layout=\"default\" schemaType={type} value={doc} key={doc._id} />\n ) : (\n 'Schema-type missing'\n )}\n </IntentButton>\n </Card>\n )\n}\n\nexport default DocumentList\n","import type {DashboardWidget, LayoutConfig} from '@sanity/dashboard'\n\nimport DocumentList, {type DocumentListConfig} from './DocumentList'\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"],"names":["draftId","nonDraftDoc","_id","prepareDocumentList","incoming","client","Promise","resolve","documents","Array","isArray","ids","filter","doc","startsWith","map","fetch","then","drafts","outgoing","find","draft","uniqBy","catch","error","Error","join","message","getSubscription","query","params","listen","events","includeResult","visibility","pipe","switchMap","event","observableOf","type","tap","delay","mergeMap","JSON","stringify","defaultProps","title","order","limit","queryParams","showCreateButton","apiVersion","DocumentList","props","$","_c","t0","types","createButtonText","setDocuments","useState","loading","setLoading","setError","t1","versionedClient","useClient","schema","useSchema","t2","bb0","t3","assembledQuery","t4","t5","typeName","get","name","documentTypes","getTypeNames","intersection","t6","subscription","subscribe","next","d","slice","e","unsubscribe","useEffect","length","width","t7","t8","t9","_temp","t10","t11","t12","MenuEntry","_type","Symbol","for","getPublishedId","id","documentListWidget","config","component","layout"],"mappings":";;;;;;;;;;AAKA,MAAMA,UAAWC,CAAAA,gBAAgC,UAAUA,YAAYC,GAAG;AAE1E,SAASC,oBACPC,UACAC,QAC2B;AAC3B,MAAI,CAACD;AACH,WAAOE,QAAQC,QAAQ,EAAE;AAE3B,QAAMC,YAAYC,MAAMC,QAAQN,QAAQ,IAAIA,WAAW,CAACA,QAAQ,GAE1DO,MAAMH,UAAUI,OAAQC,CAAAA,QAAQ,CAACA,IAAIX,IAAIY,WAAW,SAAS,CAAC,EAAEC,IAAIf,OAAO;AAEjF,SAAOK,OACJW,MAAwB,kBAAkB;AAAA,IAACL;AAAAA,EAAAA,CAAI,EAC/CM,KAAMC,CAAAA,WAAW;AAChB,UAAMC,WAAWX,UAAUO,IAAKF,CAAAA,QACXK,OAAOE,KAAMC,CAAAA,UAAUA,MAAMnB,QAAQF,QAAQa,GAAG,CAAC,KAC/CA,GACtB;AACD,WAAOS,OAAOH,UAAU,KAAK;AAAA,EAC/B,CAAC,EACAI,MAAOC,CAAAA,UAAU;AAChB,UAAM,IAAIC,MAAM,0BAA0Bd,IAAIe,KAAK,IAAI,CAAC,YAAYF,MAAMG,OAAO,EAAE;AAAA,EACrF,CAAC;AACL;AAEO,SAASC,gBACdC,OACAC,QACAzB,QAC8B;AAC9B,SAAOA,OACJ0B,OAAOF,OAAOC,QAAQ;AAAA,IACrBE,QAAQ,CAAC,WAAW,UAAU;AAAA,IAC9BC,eAAe;AAAA,IACfC,YAAY;AAAA,EAAA,CACb,EACAC,KACCC,UAAWC,WACFC,GAAa,CAAC,EAAEH,KACrBE,MAAME,SAAS,YAAYC,IAAAA,IAAQC,MAAM,GAAI,GAC7CC,SAAS,MACPrC,O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
+
"description": "Dashboard widget for the Sanity Content Studio which displays a list of documents.",
|
|
5
5
|
"keywords": [
|
|
6
|
-
"sanity",
|
|
7
|
-
"plugin",
|
|
8
|
-
"part",
|
|
9
6
|
"dashboard",
|
|
10
|
-
"
|
|
11
|
-
"
|
|
7
|
+
"documents",
|
|
8
|
+
"part",
|
|
9
|
+
"plugin",
|
|
10
|
+
"sanity",
|
|
11
|
+
"widget"
|
|
12
12
|
],
|
|
13
|
-
"homepage": "https://github.com/sanity-io/dashboard-widget-document-list#readme",
|
|
13
|
+
"homepage": "https://github.com/sanity-io/plugins/tree/main/plugins/sanity-plugin-dashboard-widget-document-list#readme",
|
|
14
14
|
"bugs": {
|
|
15
|
-
"url": "https://github.com/sanity-io/
|
|
16
|
-
},
|
|
17
|
-
"repository": {
|
|
18
|
-
"type": "git",
|
|
19
|
-
"url": "git@github.com:sanity-io/dashboard-widget-document-list.git"
|
|
15
|
+
"url": "https://github.com/sanity-io/plugins/issues"
|
|
20
16
|
},
|
|
21
17
|
"license": "MIT",
|
|
22
18
|
"author": "Sanity.io <hello@sanity.io>",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"import": "./lib/index.esm.js",
|
|
28
|
-
"require": "./lib/index.js",
|
|
29
|
-
"default": "./lib/index.esm.js"
|
|
30
|
-
},
|
|
31
|
-
"./package.json": "./package.json"
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+ssh://git@github.com/sanity-io/plugins.git",
|
|
22
|
+
"directory": "plugins/sanity-plugin-dashboard-widget-document-list"
|
|
32
23
|
},
|
|
33
|
-
"main": "./lib/index.js",
|
|
34
|
-
"module": "./lib/index.esm.js",
|
|
35
|
-
"source": "./src/index.ts",
|
|
36
|
-
"types": "./lib/src/index.d.ts",
|
|
37
24
|
"files": [
|
|
38
|
-
"
|
|
39
|
-
"lib",
|
|
40
|
-
"v2-incompatible.js",
|
|
41
|
-
"sanity.json"
|
|
25
|
+
"dist"
|
|
42
26
|
],
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"link-watch": "plugin-kit link-watch",
|
|
49
|
-
"lint": "eslint .",
|
|
50
|
-
"prepare": "husky install",
|
|
51
|
-
"prepublishOnly": "npm run build",
|
|
52
|
-
"watch": "pkg-utils watch"
|
|
27
|
+
"type": "module",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./dist/index.js",
|
|
31
|
+
"./package.json": "./package.json"
|
|
53
32
|
},
|
|
54
33
|
"dependencies": {
|
|
55
|
-
"@sanity/
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"rxjs": "^6.0.0"
|
|
34
|
+
"@sanity/ui": "^3.2.0",
|
|
35
|
+
"lodash-es": "^4.17.23",
|
|
36
|
+
"rxjs": "^7.8.2"
|
|
59
37
|
},
|
|
60
38
|
"devDependencies": {
|
|
61
|
-
"@
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"@
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"@
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"eslint-config-sanity": "^6.0.0",
|
|
73
|
-
"eslint-plugin-prettier": "^4.2.1",
|
|
74
|
-
"eslint-plugin-react": "^7.31.10",
|
|
75
|
-
"eslint-plugin-react-hooks": "^4.6.0",
|
|
76
|
-
"husky": "^8.0.1",
|
|
77
|
-
"lint-staged": "^13.0.3",
|
|
78
|
-
"prettier": "^2.7.1",
|
|
79
|
-
"prettier-plugin-packagejson": "^2.3.0",
|
|
80
|
-
"react": "^18",
|
|
81
|
-
"rimraf": "^3.0.2",
|
|
82
|
-
"sanity": "3.0.0-rc.0",
|
|
83
|
-
"typescript": "^4.8.4"
|
|
39
|
+
"@sanity/pkg-utils": "^10.5.5",
|
|
40
|
+
"@types/lodash-es": "^4.17.12",
|
|
41
|
+
"@types/react": "^19.2.17",
|
|
42
|
+
"@types/react-dom": "^19.2.3",
|
|
43
|
+
"babel-plugin-react-compiler": "^1.0.0",
|
|
44
|
+
"react": "^19.2.7",
|
|
45
|
+
"react-dom": "^19.2.7",
|
|
46
|
+
"sanity": "^6.0.0",
|
|
47
|
+
"@repo/package.config": "0.0.0",
|
|
48
|
+
"@repo/tsconfig": "0.0.0",
|
|
49
|
+
"@sanity/dashboard": "6.0.1"
|
|
84
50
|
},
|
|
85
51
|
"peerDependencies": {
|
|
86
|
-
"
|
|
87
|
-
"react": "^
|
|
88
|
-
"sanity": "
|
|
52
|
+
"react": "^19.2",
|
|
53
|
+
"react-dom": "^19.2",
|
|
54
|
+
"sanity": "^5 || ^6.0.0-0",
|
|
55
|
+
"@sanity/dashboard": "^6.0.1"
|
|
89
56
|
},
|
|
90
57
|
"engines": {
|
|
91
|
-
"node": ">=
|
|
58
|
+
"node": ">=20.19 <22 || >=22.12"
|
|
92
59
|
},
|
|
93
|
-
"
|
|
94
|
-
|
|
95
|
-
}
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "pkg build --strict --check --clean"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/lib/index.esm.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
function e(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function t(t){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?e(Object(i),!0).forEach((function(e){r(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):e(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}import{jsx as n,jsxs as i}from"react/jsx-runtime";import{useState as o,useMemo as c,useEffect as a}from"react";import{useClient as s,useSchema as d,IntentButton as u,getPublishedId as p,SanityPreview as l}from"sanity";import{intersection as m}from"lodash";import{of as y}from"rxjs";import{switchMap as f,tap as h,delay as b,mergeMap as g}from"rxjs/operators";import w from"lodash/uniqBy";import{Card as O,Flex as j,Spinner as v,Stack as P}from"@sanity/ui";import{DashboardWidgetContainer as _}from"@sanity/dashboard";const x=e=>"drafts.".concat(e._id);function E(e,t,r){return r.listen(e,t,{events:["welcome","mutation"],includeResult:!1,visibility:"query"}).pipe(f((n=>y(1).pipe("welcome"===n.type?h():b(1e3),g((()=>r.fetch(e,t).then((e=>function(e,t){if(!e)return Promise.resolve([]);const r=Array.isArray(e)?e:[e],n=r.filter((e=>!e._id.startsWith("drafts."))).map(x);return t.fetch("*[_id in $ids]",{ids:n}).then((e=>{const t=r.map((t=>e.find((e=>e._id===x(t)))||t));return w(t,"_id")})).catch((e=>{throw new Error("Problems fetching docs ".concat(n,". Error: ").concat(e.message))}))}(e,r))).catch((r=>{if(r.message.startsWith("Problems fetching docs"))throw r;throw new Error("Query failed ".concat(e," and ").concat(JSON.stringify(t),". Error: ").concat(r.message))}))))))))}const q={title:"Last created",order:"_createdAt desc",limit:10,queryParams:{},showCreateButton:!0,apiVersion:"v1"};function B(e){const{query:r,limit:p,apiVersion:l,queryParams:y,types:f,order:h,title:b,showCreateButton:g,createButtonText:w}=t(t({},q),e),[x,B]=o(),[D,Q]=o(!0),[S,A]=o(),T=s({apiVersion:l}),V=d(),{assembledQuery:N,params:W}=c((()=>{if(r)return{assembledQuery:r,params:y};const e=V.getTypeNames().filter((e=>{var t;const r=V.get(e);return"document"===(null==(t=null==r?void 0:r.type)?void 0:t.name)}));return{assembledQuery:"*[_type in $types] | order(".concat(h,") [0...").concat(2*p,"]"),params:{types:f?m(f,e):e}}}),[V,r,y,h,p,f]);return a((()=>{if(!N)return;const e=E(N,W,T).subscribe({next:e=>{B(e.slice(0,p)),Q(!1)},error:e=>{A(e),Q(!1)}});return()=>{e.unsubscribe()}}),[p,T,N,W]),n(_,{header:b,footer:f&&1===f.length&&g&&n(u,{mode:"bleed",style:{width:"100%"},paddingX:2,paddingY:4,tone:"primary",type:"button",intent:"create",params:{type:f[0]},text:w||"Create new ".concat(f[0])}),children:i(O,{children:[S&&n("div",{children:S.message}),!S&&D&&n(O,{padding:4,children:n(j,{justify:"center",children:n(v,{muted:!0})})}),!S&&!x&&!D&&n("div",{children:"Could not locate any documents :/"}),n(P,{space:2,children:x&&x.map((e=>n(C,{doc:e},e._id)))})]})})}function C(e){let{doc:t}=e;const r=d().get(t._type);return n(O,{flex:1,children:n(u,{intent:"edit",mode:"bleed",padding:1,radius:0,params:{type:t._type,id:p(t._id)},style:{width:"100%"},children:r?n(l,{layout:"default",schemaType:r,value:t},t._id):"Schema-type missing"})})}function D(e){return{name:"document-list-widget",component:function(){return n(B,t({},e))},layout:e.layout}}export{D as documentListWidget};
|
|
2
|
-
//# sourceMappingURL=index.esm.js.map
|
package/lib/index.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/sanityConnector.ts","../src/DocumentList.tsx","../src/plugin.tsx"],"sourcesContent":["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'\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<SanityDocument[]>('*[_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","import React, {useEffect, useMemo, useState} from 'react'\nimport {getPublishedId, useClient, useSchema} from 'sanity'\nimport {intersection} from 'lodash'\nimport {getSubscription} from './sanityConnector'\nimport {SanityDocument, IntentButton, SanityPreview} from 'sanity'\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 versionedClient = useClient({apiVersion})\n const schema = useSchema()\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?.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 {type ? (\n <SanityPreview layout=\"default\" schemaType={type} value={doc} key={doc._id} />\n ) : (\n 'Schema-type missing'\n )}\n </IntentButton>\n </Card>\n )\n}\n\nexport default DocumentList\n","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"],"names":["draftId","nonDraftDoc","_id","getSubscription","query","params","client","listen","events","includeResult","visibility","pipe","switchMap","event","observableOf","type","tap","delay","mergeMap","fetch","then","incoming","Promise","resolve","documents","Array","isArray","ids","filter","doc","startsWith","map","drafts","outgoing","find","draft","uniqBy","catch","error","Error","message","prepareDocumentList","JSON","stringify","defaultProps","title","order","limit","queryParams","showCreateButton","apiVersion","DocumentList","props","types","createButtonText","setDocuments","useState","loading","setLoading","setError","versionedClient","useClient","schema","useSchema","assembledQuery","useMemo","documentTypes","getTypeNames","typeName","_a","schemaType","get","name","concat","intersection","useEffect","subscription","subscribe","next","d","slice","e","unsubscribe","jsx","DashboardWidgetContainer","header","footer","length","IntentButton","mode","style","width","paddingX","paddingY","tone","intent","text","children","jsxs","Card","padding","Flex","justify","Spinner","muted","Stack","space","MenuEntry","_ref","_type","flex","radius","id","getPublishedId","SanityPreview","layout","value","documentListWidget","config","component","_objectSpread"],"mappings":"4rCAMA,MAAMA,EAAWC,oBAA0CA,EAAYC,KA2BvD,SAAAC,EACdC,EACAC,EACAC,GAEO,OAAAA,EACJC,OAAOH,EAAOC,EAAQ,CACrBG,OAAQ,CAAC,UAAW,YACpBC,eAAe,EACfC,WAAY,UAEbC,KACCC,GAAWC,GACFC,EAAa,GAAGH,KACN,YAAfE,EAAME,KAAqBC,IAAQC,EAAM,KACzCC,GAAS,IACPZ,EACGa,MAAMf,EAAOC,GACbe,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T,SAAazE,GAAAA,EAAUO,KAAKF,GAASsD,EAAAwB,EAAA,CAAwB9E,OAATA,EAAI3B,aAKnE,CAEA,SAASyG,EAAwCC,GAAA,IAA9B/E,IAACA,GAA6B+E,EAC/C,MACM7F,EADSgD,IACKQ,IAAI1C,EAAIgF,OAC5B,OACG1B,EAAAgB,EAAA,CAAKW,KAAM,EACVb,SAACd,EAAAK,EAAA,CACCO,OAAO,OACPN,KAAK,QACLW,QAAS,EACTW,OAAQ,EACR1G,OAAQ,CACNU,KAAMc,EAAIgF,MACVG,GAAIC,EAAepF,EAAI3B,MAEzBwF,MAAO,CAACC,MAAO,QAEdM,WACEd,EAAA+B,EAAA,CAAcC,OAAO,UAAU7C,WAAYvD,EAAMqG,MAAOvF,GAAUA,EAAI3B,KAEvE,yBAKV,CChJO,SAASmH,EAAmBC,GAC1B,MAAA,CACL9C,KAAM,uBACN+C,UAAW,WACT,OAAQpC,EAAAhC,EAAAqE,EAAA,CAAA,EAAiBF,GAC3B,EACAH,OAAQG,EAAOH,OAEnB"}
|
package/lib/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";function e(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function t(t){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?e(Object(i),!0).forEach((function(e){r(t,e,i[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):e(Object(i)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))}))}return t}function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}Object.defineProperty(exports,"__esModule",{value:!0});var n=require("react/jsx-runtime"),i=require("react"),s=require("sanity"),a=require("lodash"),o=require("rxjs"),c=require("rxjs/operators"),u=require("lodash/uniqBy"),d=require("@sanity/ui"),l=require("@sanity/dashboard");function p(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var y=p(u);const f=e=>"drafts.".concat(e._id);function m(e,t,r){return r.listen(e,t,{events:["welcome","mutation"],includeResult:!1,visibility:"query"}).pipe(c.switchMap((n=>o.of(1).pipe("welcome"===n.type?c.tap():c.delay(1e3),c.mergeMap((()=>r.fetch(e,t).then((e=>function(e,t){if(!e)return Promise.resolve([]);const r=Array.isArray(e)?e:[e],n=r.filter((e=>!e._id.startsWith("drafts."))).map(f);return t.fetch("*[_id in $ids]",{ids:n}).then((e=>{const t=r.map((t=>e.find((e=>e._id===f(t)))||t));return y.default(t,"_id")})).catch((e=>{throw new Error("Problems fetching docs ".concat(n,". Error: ").concat(e.message))}))}(e,r))).catch((r=>{if(r.message.startsWith("Problems fetching docs"))throw r;throw new Error("Query failed ".concat(e," and ").concat(JSON.stringify(t),". Error: ").concat(r.message))}))))))))}const h={title:"Last created",order:"_createdAt desc",limit:10,queryParams:{},showCreateButton:!0,apiVersion:"v1"};function b(e){const{query:r,limit:o,apiVersion:c,queryParams:u,types:p,order:y,title:f,showCreateButton:b,createButtonText:g}=t(t({},h),e),[x,w]=i.useState(),[O,v]=i.useState(!0),[P,q]=i.useState(),_=s.useClient({apiVersion:c}),S=s.useSchema(),{assembledQuery:C,params:E}=i.useMemo((()=>{if(r)return{assembledQuery:r,params:u};const e=S.getTypeNames().filter((e=>{var t;const r=S.get(e);return"document"===(null==(t=null==r?void 0:r.type)?void 0:t.name)}));return{assembledQuery:"*[_type in $types] | order(".concat(y,") [0...").concat(2*o,"]"),params:{types:p?a.intersection(p,e):e}}}),[S,r,u,y,o,p]);return i.useEffect((()=>{if(!C)return;const e=m(C,E,_).subscribe({next:e=>{w(e.slice(0,o)),v(!1)},error:e=>{q(e),v(!1)}});return()=>{e.unsubscribe()}}),[o,_,C,E]),n.jsx(l.DashboardWidgetContainer,{header:f,footer:p&&1===p.length&&b&&n.jsx(s.IntentButton,{mode:"bleed",style:{width:"100%"},paddingX:2,paddingY:4,tone:"primary",type:"button",intent:"create",params:{type:p[0]},text:g||"Create new ".concat(p[0])}),children:n.jsxs(d.Card,{children:[P&&n.jsx("div",{children:P.message}),!P&&O&&n.jsx(d.Card,{padding:4,children:n.jsx(d.Flex,{justify:"center",children:n.jsx(d.Spinner,{muted:!0})})}),!P&&!x&&!O&&n.jsx("div",{children:"Could not locate any documents :/"}),n.jsx(d.Stack,{space:2,children:x&&x.map((e=>n.jsx(j,{doc:e},e._id)))})]})})}function j(e){let{doc:t}=e;const r=s.useSchema().get(t._type);return n.jsx(d.Card,{flex:1,children:n.jsx(s.IntentButton,{intent:"edit",mode:"bleed",padding:1,radius:0,params:{type:t._type,id:s.getPublishedId(t._id)},style:{width:"100%"},children:r?n.jsx(s.SanityPreview,{layout:"default",schemaType:r,value:t},t._id):"Schema-type missing"})})}exports.documentListWidget=function(e){return{name:"document-list-widget",component:function(){return n.jsx(b,t({},e))},layout:e.layout}};
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/sanityConnector.ts","../src/DocumentList.tsx","../src/plugin.tsx"],"sourcesContent":["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'\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<SanityDocument[]>('*[_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","import React, {useEffect, useMemo, useState} from 'react'\nimport {getPublishedId, useClient, useSchema} from 'sanity'\nimport {intersection} from 'lodash'\nimport {getSubscription} from './sanityConnector'\nimport {SanityDocument, IntentButton, SanityPreview} from 'sanity'\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 versionedClient = useClient({apiVersion})\n const schema = useSchema()\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?.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 {type ? (\n <SanityPreview layout=\"default\" schemaType={type} value={doc} key={doc._id} />\n ) : (\n 'Schema-type missing'\n )}\n </IntentButton>\n </Card>\n )\n}\n\nexport default DocumentList\n","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"],"names":["draftId","nonDraftDoc","_id","getSubscription","query","params","client","listen","events","includeResult","visibility","pipe","switchMap","event","observableOf","of","type","tap","delay","mergeMap","fetch","then","incoming","Promise","resolve","documents","Array","isArray","ids","filter","doc","startsWith","map","drafts","outgoing","find","draft","uniqBy","catch","error","Error","message","prepareDocumentList","JSON","stringify","defaultProps","title","order","limit","queryParams","showCreateButton","apiVersion","DocumentList","props","types","createButtonText","setDocuments","useState","loading","setLoading","setError","versionedClient","useClient","schema","useSchema","assembledQuery","useMemo","documentTypes","getTypeNames","typeName","_a","schemaType","get","name","concat","intersection","useEffect","subscription","subscribe","next","d","slice","e","unsubscribe","jsx","DashboardWidgetContainer","header","footer","length","IntentButton","mode","style","width","paddingX","paddingY","tone","intent","text","children","jsxs","Card","padding","Flex","justify","Spinner","muted","Stack","space","MenuEntry","_ref","_type","flex","radius","id","getPublishedId","SanityPreview","layout","value","config","component","_objectSpread"],"mappings":"4iCAMA,MAAMA,EAAWC,oBAA0CA,EAAYC,KA2BvD,SAAAC,EACdC,EACAC,EACAC,GAEO,OAAAA,EACJC,OAAOH,EAAOC,EAAQ,CACrBG,OAAQ,CAAC,UAAW,YACpBC,eAAe,EACfC,WAAY,UAEbC,KACCC,EAAAA,WAAWC,GACFC,EAAAC,GAAa,GAAGJ,KACN,YAAfE,EAAMG,KAAqBC,EAAIA,MAAIC,EAAAA,MAAM,KACzCC,EAAAA,UAAS,I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L,SAACd,EAAAA,IAAAoB,UAAA,CAAQC,OAAK,SAIlBlE,IAAUd,IAAciC,GAAY0B,EAAAA,IAAA,MAAA,CAAIc,SAAA,sCACzCd,EAAAA,IAAAsB,EAAAA,MAAA,CAAMC,MAAO,EACXT,SAAazE,GAAAA,EAAUO,KAAKF,GAASsD,EAAAA,IAAAwB,EAAA,CAAwB9E,OAATA,EAAI5B,aAKnE,CAEA,SAAS0G,EAAwCC,GAAA,IAA9B/E,IAACA,GAA6B+E,EAC/C,MACM7F,EADSgD,EAAAA,YACKQ,IAAI1C,EAAIgF,OAC5B,OACG1B,EAAAA,IAAAgB,EAAAA,KAAA,CAAKW,KAAM,EACVb,SAACd,EAAAA,IAAAK,eAAA,CACCO,OAAO,OACPN,KAAK,QACLW,QAAS,EACTW,OAAQ,EACR3G,OAAQ,CACNW,KAAMc,EAAIgF,MACVG,GAAIC,EAAAA,eAAepF,EAAI5B,MAEzByF,MAAO,CAACC,MAAO,QAEdM,WACEd,EAAAA,IAAA+B,gBAAA,CAAcC,OAAO,UAAU7C,WAAYvD,EAAMqG,MAAOvF,GAAUA,EAAI5B,KAEvE,yBAKV,4BChJO,SAA4BoH,GAC1B,MAAA,CACL7C,KAAM,uBACN8C,UAAW,WACT,OAAQnC,EAAAA,IAAAhC,EAAAoE,EAAA,CAAA,EAAiBF,GAC3B,EACAF,OAAQE,EAAOF,OAEnB"}
|
package/lib/src/index.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
|
|
3
|
-
import {DashboardWidget} from '@sanity/dashboard'
|
|
4
|
-
import {LayoutConfig} from '@sanity/dashboard'
|
|
5
|
-
|
|
6
|
-
declare interface DocumentListConfig {
|
|
7
|
-
title?: string
|
|
8
|
-
types?: string[]
|
|
9
|
-
query?: string
|
|
10
|
-
queryParams?: Record<string, any>
|
|
11
|
-
order?: string
|
|
12
|
-
limit?: number
|
|
13
|
-
showCreateButton?: boolean
|
|
14
|
-
createButtonText?: string
|
|
15
|
-
apiVersion?: string
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export declare function documentListWidget(config: DocumentListWidgetConfig): DashboardWidget
|
|
19
|
-
|
|
20
|
-
export declare interface DocumentListWidgetConfig extends DocumentListConfig {
|
|
21
|
-
layout?: LayoutConfig
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export {}
|
package/sanity.json
DELETED
package/src/DocumentList.tsx
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import React, {useEffect, useMemo, useState} from 'react'
|
|
2
|
-
import {getPublishedId, useClient, useSchema} from 'sanity'
|
|
3
|
-
import {intersection} from 'lodash'
|
|
4
|
-
import {getSubscription} from './sanityConnector'
|
|
5
|
-
import {SanityDocument, IntentButton, SanityPreview} from 'sanity'
|
|
6
|
-
import {Card, Flex, Spinner, Stack} from '@sanity/ui'
|
|
7
|
-
import {DashboardWidgetContainer} from '@sanity/dashboard'
|
|
8
|
-
|
|
9
|
-
export interface DocumentListConfig {
|
|
10
|
-
title?: string
|
|
11
|
-
types?: string[]
|
|
12
|
-
query?: string
|
|
13
|
-
queryParams?: Record<string, any>
|
|
14
|
-
order?: string
|
|
15
|
-
limit?: number
|
|
16
|
-
showCreateButton?: boolean
|
|
17
|
-
createButtonText?: string
|
|
18
|
-
apiVersion?: string
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const defaultProps = {
|
|
22
|
-
title: 'Last created',
|
|
23
|
-
order: '_createdAt desc',
|
|
24
|
-
limit: 10,
|
|
25
|
-
queryParams: {},
|
|
26
|
-
showCreateButton: true,
|
|
27
|
-
apiVersion: 'v1',
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function DocumentList(props: DocumentListConfig) {
|
|
31
|
-
const {
|
|
32
|
-
query,
|
|
33
|
-
limit,
|
|
34
|
-
apiVersion,
|
|
35
|
-
queryParams,
|
|
36
|
-
types,
|
|
37
|
-
order,
|
|
38
|
-
title,
|
|
39
|
-
showCreateButton,
|
|
40
|
-
createButtonText,
|
|
41
|
-
} = {
|
|
42
|
-
...defaultProps,
|
|
43
|
-
...props,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const [documents, setDocuments] = useState<SanityDocument[] | undefined>()
|
|
47
|
-
const [loading, setLoading] = useState<boolean>(true)
|
|
48
|
-
const [error, setError] = useState<Error | undefined>()
|
|
49
|
-
|
|
50
|
-
const versionedClient = useClient({apiVersion})
|
|
51
|
-
const schema = useSchema()
|
|
52
|
-
|
|
53
|
-
const {assembledQuery, params} = useMemo(() => {
|
|
54
|
-
if (query) {
|
|
55
|
-
return {assembledQuery: query, params: queryParams}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const documentTypes = schema.getTypeNames().filter((typeName) => {
|
|
59
|
-
const schemaType = schema.get(typeName)
|
|
60
|
-
return schemaType?.type?.name === 'document'
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
assembledQuery: `*[_type in $types] | order(${order}) [0...${limit * 2}]`,
|
|
65
|
-
params: {types: types ? intersection(types, documentTypes) : documentTypes},
|
|
66
|
-
}
|
|
67
|
-
}, [schema, query, queryParams, order, limit, types])
|
|
68
|
-
|
|
69
|
-
useEffect(() => {
|
|
70
|
-
if (!assembledQuery) {
|
|
71
|
-
return
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const subscription = getSubscription(assembledQuery, params, versionedClient).subscribe({
|
|
75
|
-
next: (d) => {
|
|
76
|
-
setDocuments(d.slice(0, limit))
|
|
77
|
-
setLoading(false)
|
|
78
|
-
},
|
|
79
|
-
error: (e) => {
|
|
80
|
-
setError(e)
|
|
81
|
-
setLoading(false)
|
|
82
|
-
},
|
|
83
|
-
})
|
|
84
|
-
// eslint-disable-next-line consistent-return
|
|
85
|
-
return () => {
|
|
86
|
-
subscription.unsubscribe()
|
|
87
|
-
}
|
|
88
|
-
}, [limit, versionedClient, assembledQuery, params])
|
|
89
|
-
|
|
90
|
-
return (
|
|
91
|
-
<DashboardWidgetContainer
|
|
92
|
-
header={title}
|
|
93
|
-
footer={
|
|
94
|
-
types &&
|
|
95
|
-
types.length === 1 &&
|
|
96
|
-
showCreateButton && (
|
|
97
|
-
<IntentButton
|
|
98
|
-
mode="bleed"
|
|
99
|
-
style={{width: '100%'}}
|
|
100
|
-
paddingX={2}
|
|
101
|
-
paddingY={4}
|
|
102
|
-
tone="primary"
|
|
103
|
-
type="button"
|
|
104
|
-
intent="create"
|
|
105
|
-
params={{type: types[0]}}
|
|
106
|
-
text={createButtonText || `Create new ${types[0]}`}
|
|
107
|
-
/>
|
|
108
|
-
)
|
|
109
|
-
}
|
|
110
|
-
>
|
|
111
|
-
<Card>
|
|
112
|
-
{error && <div>{error.message}</div>}
|
|
113
|
-
{!error && loading && (
|
|
114
|
-
<Card padding={4}>
|
|
115
|
-
<Flex justify="center">
|
|
116
|
-
<Spinner muted />
|
|
117
|
-
</Flex>
|
|
118
|
-
</Card>
|
|
119
|
-
)}
|
|
120
|
-
{!error && !documents && !loading && <div>Could not locate any documents :/</div>}
|
|
121
|
-
<Stack space={2}>
|
|
122
|
-
{documents && documents.map((doc) => <MenuEntry key={doc._id} doc={doc} />)}
|
|
123
|
-
</Stack>
|
|
124
|
-
</Card>
|
|
125
|
-
</DashboardWidgetContainer>
|
|
126
|
-
)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function MenuEntry({doc}: {doc: SanityDocument}) {
|
|
130
|
-
const schema = useSchema()
|
|
131
|
-
const type = schema.get(doc._type)
|
|
132
|
-
return (
|
|
133
|
-
<Card flex={1}>
|
|
134
|
-
<IntentButton
|
|
135
|
-
intent="edit"
|
|
136
|
-
mode="bleed"
|
|
137
|
-
padding={1}
|
|
138
|
-
radius={0}
|
|
139
|
-
params={{
|
|
140
|
-
type: doc._type,
|
|
141
|
-
id: getPublishedId(doc._id),
|
|
142
|
-
}}
|
|
143
|
-
style={{width: '100%'}}
|
|
144
|
-
>
|
|
145
|
-
{type ? (
|
|
146
|
-
<SanityPreview layout="default" schemaType={type} value={doc} key={doc._id} />
|
|
147
|
-
) : (
|
|
148
|
-
'Schema-type missing'
|
|
149
|
-
)}
|
|
150
|
-
</IntentButton>
|
|
151
|
-
</Card>
|
|
152
|
-
)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export default DocumentList
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {documentListWidget, type DocumentListWidgetConfig} from './plugin'
|
package/src/plugin.tsx
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import DocumentList, {DocumentListConfig} from './DocumentList'
|
|
2
|
-
import React from 'react'
|
|
3
|
-
import {LayoutConfig, DashboardWidget} from '@sanity/dashboard'
|
|
4
|
-
|
|
5
|
-
export interface DocumentListWidgetConfig extends DocumentListConfig {
|
|
6
|
-
layout?: LayoutConfig
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export function documentListWidget(config: DocumentListWidgetConfig): DashboardWidget {
|
|
10
|
-
return {
|
|
11
|
-
name: 'document-list-widget',
|
|
12
|
-
component: function component() {
|
|
13
|
-
return <DocumentList {...config} />
|
|
14
|
-
},
|
|
15
|
-
layout: config.layout,
|
|
16
|
-
}
|
|
17
|
-
}
|
package/src/sanityConnector.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import {Observable, of as observableOf} from 'rxjs'
|
|
2
|
-
import {delay, mergeMap, switchMap, tap} from 'rxjs/operators'
|
|
3
|
-
import uniqBy from 'lodash/uniqBy'
|
|
4
|
-
import {SanityClient} from '@sanity/client'
|
|
5
|
-
import {SanityDocument} from 'sanity'
|
|
6
|
-
|
|
7
|
-
const draftId = (nonDraftDoc: SanityDocument) => `drafts.${nonDraftDoc._id}`
|
|
8
|
-
|
|
9
|
-
function prepareDocumentList(
|
|
10
|
-
incoming: SanityDocument | SanityDocument[],
|
|
11
|
-
client: SanityClient
|
|
12
|
-
): Promise<SanityDocument[]> {
|
|
13
|
-
if (!incoming) {
|
|
14
|
-
return Promise.resolve([])
|
|
15
|
-
}
|
|
16
|
-
const documents = Array.isArray(incoming) ? incoming : [incoming]
|
|
17
|
-
|
|
18
|
-
const ids = documents.filter((doc) => !doc._id.startsWith('drafts.')).map(draftId)
|
|
19
|
-
|
|
20
|
-
return client
|
|
21
|
-
.fetch<SanityDocument[]>('*[_id in $ids]', {ids})
|
|
22
|
-
.then((drafts) => {
|
|
23
|
-
const outgoing = documents.map((doc) => {
|
|
24
|
-
const foundDraft = drafts.find((draft) => draft._id === draftId(doc))
|
|
25
|
-
return foundDraft || doc
|
|
26
|
-
})
|
|
27
|
-
return uniqBy(outgoing, '_id')
|
|
28
|
-
})
|
|
29
|
-
.catch((error) => {
|
|
30
|
-
throw new Error(`Problems fetching docs ${ids}. Error: ${error.message}`)
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getSubscription(
|
|
35
|
-
query: string,
|
|
36
|
-
params: Record<string, any>,
|
|
37
|
-
client: SanityClient
|
|
38
|
-
): Observable<SanityDocument[]> {
|
|
39
|
-
return client
|
|
40
|
-
.listen(query, params, {
|
|
41
|
-
events: ['welcome', 'mutation'],
|
|
42
|
-
includeResult: false,
|
|
43
|
-
visibility: 'query',
|
|
44
|
-
})
|
|
45
|
-
.pipe(
|
|
46
|
-
switchMap((event) => {
|
|
47
|
-
return observableOf(1).pipe(
|
|
48
|
-
event.type === 'welcome' ? tap() : delay(1000),
|
|
49
|
-
mergeMap(() =>
|
|
50
|
-
client
|
|
51
|
-
.fetch(query, params)
|
|
52
|
-
.then((incoming) => {
|
|
53
|
-
return prepareDocumentList(incoming, client)
|
|
54
|
-
})
|
|
55
|
-
.catch((error) => {
|
|
56
|
-
if (error.message.startsWith('Problems fetching docs')) {
|
|
57
|
-
throw error
|
|
58
|
-
}
|
|
59
|
-
throw new Error(
|
|
60
|
-
`Query failed ${query} and ${JSON.stringify(params)}. Error: ${error.message}`
|
|
61
|
-
)
|
|
62
|
-
})
|
|
63
|
-
)
|
|
64
|
-
)
|
|
65
|
-
})
|
|
66
|
-
)
|
|
67
|
-
}
|
package/v2-incompatible.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
const { showIncompatiblePluginDialog } = require('@sanity/incompatible-plugin')
|
|
2
|
-
const { name, version, sanityExchangeUrl } = require('./package.json')
|
|
3
|
-
|
|
4
|
-
export default showIncompatiblePluginDialog({
|
|
5
|
-
name: name,
|
|
6
|
-
versions: {
|
|
7
|
-
v3: version,
|
|
8
|
-
v2: '^0.2.1',
|
|
9
|
-
},
|
|
10
|
-
sanityExchangeUrl,
|
|
11
|
-
})
|