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
|
@@ -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,CAAC,QAAQ,CAAC,GAAG,QAAQ,GAAG;QAAC,QAAQ;KAAC;IAEjE,MAAM,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,GAAK,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,6BAAO,CAAC;IAElF,OAAO,MAAM,CACV,KAAK,CAAC,gBAAgB,EAAE;aAAC,GAAG;KAAC,CAAC,CAC9B,IAAI,CAAC,CAAC,MAAM,GAAK;QACh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Connector.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"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LayoutConfig, DashboardWidget } 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
|
+
export interface DocumentListWidgetConfig extends DocumentListConfig {
|
|
14
|
+
layout?: LayoutConfig;
|
|
15
|
+
}
|
|
16
|
+
export function documentListWidget(config: DocumentListWidgetConfig): DashboardWidget;
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";ACSA;IACE,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,EAAE,CAAA;IAChB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACjC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;ACfD,yCAA0C,SAAQ,kBAAkB;IAClE,MAAM,CAAC,EAAE,YAAY,CAAA;CACtB;AAED,mCAAmC,MAAM,EAAE,wBAAwB,GAAG,eAAe,CAQpF","sources":["src/src/sanityConnector.ts","src/src/DocumentList.tsx","src/src/index.tsx","src/index.tsx"],"sourcesContent":[null,null,null,"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":[],"version":3,"file":"index.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"public": true,
|
|
3
3
|
"name": "sanity-plugin-dashboard-widget-document-list",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "3.0.0-studio-v3.2",
|
|
5
5
|
"author": "Sanity.io <hello@sanity.io>",
|
|
6
6
|
"description": "Example dashboard widget for Sanity Content Studio",
|
|
7
7
|
"homepage": "https://github.com/sanity-io/dashboard-widget-document-list#readme",
|
|
@@ -13,37 +13,68 @@
|
|
|
13
13
|
"widget",
|
|
14
14
|
"documents"
|
|
15
15
|
],
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
"source": "./src/index.tsx",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"require": "./lib/cjs/index.js",
|
|
20
|
+
"default": "./lib/esm/index.js"
|
|
21
|
+
}
|
|
20
22
|
},
|
|
23
|
+
"main": "./lib/cjs/index.js",
|
|
24
|
+
"module": "./lib/esm/index.js",
|
|
25
|
+
"types": "./lib/types/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"lib"
|
|
29
|
+
],
|
|
21
30
|
"scripts": {
|
|
22
|
-
"build": "sanipack build",
|
|
23
|
-
"test": "sanipack verify",
|
|
24
31
|
"clean": "rimraf lib",
|
|
25
|
-
"prebuild": "npm run clean",
|
|
26
|
-
"
|
|
32
|
+
"prebuild": "npm run clean && tsc --noEmit",
|
|
33
|
+
"build": "parcel build",
|
|
34
|
+
"watch": "parcel watch",
|
|
35
|
+
"prepublishOnly": "npm run build",
|
|
36
|
+
"compile": "tsc --noEmit",
|
|
37
|
+
"lint": "eslint ."
|
|
38
|
+
},
|
|
39
|
+
"sanityPlugin": {},
|
|
40
|
+
"license": "MIT",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/sanity-io/dashboard-widget-document-list/issues"
|
|
27
43
|
},
|
|
28
44
|
"repository": {
|
|
29
45
|
"type": "git",
|
|
30
46
|
"url": "git+https://github.com/sanity-io/dashboard-widget-document-list.git"
|
|
31
47
|
},
|
|
32
48
|
"devDependencies": {
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
49
|
+
"@commitlint/cli": "^16.2.4",
|
|
50
|
+
"@commitlint/config-conventional": "^16.2.4",
|
|
51
|
+
"@parcel/packager-ts": "^2.6.0",
|
|
52
|
+
"@parcel/transformer-typescript-types": "^2.6.0",
|
|
53
|
+
"@types/react": "^17.0.0 || ^18.0.0",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^5.12.0",
|
|
55
|
+
"@typescript-eslint/parser": "^5.12.0",
|
|
56
|
+
"eslint": "^8.7.0",
|
|
57
|
+
"eslint-config-prettier": "^8.3.0",
|
|
58
|
+
"eslint-config-sanity": "^5.1.0",
|
|
59
|
+
"eslint-plugin-prettier": "4.0.0",
|
|
60
|
+
"eslint-plugin-react": "^7.28.0",
|
|
61
|
+
"eslint-plugin-react-hooks": "^4.5.0",
|
|
62
|
+
"husky": "^7.0.0",
|
|
63
|
+
"parcel": "^2.6.0",
|
|
64
|
+
"prettier": "^2.6.2",
|
|
65
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
66
|
+
"rimraf": "^3.0.2",
|
|
67
|
+
"sanity": "purple-unicorn",
|
|
68
|
+
"typescript": "^4.7.3"
|
|
39
69
|
},
|
|
40
70
|
"peerDependencies": {
|
|
41
|
-
"
|
|
42
|
-
"
|
|
71
|
+
"react": "^17.0.0 || ^18.0.0",
|
|
72
|
+
"sanity": "purple-unicorn"
|
|
43
73
|
},
|
|
44
74
|
"dependencies": {
|
|
75
|
+
"@sanity/ui": "^0.37.9",
|
|
76
|
+
"@sanity/dashboard": "studio-v3",
|
|
45
77
|
"lodash": "^4.17.15",
|
|
46
|
-
"prop-types": "^15.7.2",
|
|
47
78
|
"rxjs": "^6.0.0"
|
|
48
79
|
}
|
|
49
80
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import React, {useEffect, useMemo, useState} from 'react'
|
|
2
|
+
import {IntentButton, SanityPreview} from 'sanity/_unstable'
|
|
3
|
+
import {getPublishedId, useClient, useSchema} from 'sanity'
|
|
4
|
+
import {intersection} from 'lodash'
|
|
5
|
+
import {getSubscription} from './sanityConnector'
|
|
6
|
+
import {SanityDocument} from '@sanity/types'
|
|
7
|
+
import {Card, Flex, Spinner, Stack} from '@sanity/ui'
|
|
8
|
+
import {DashboardWidgetContainer} from '@sanity/dashboard'
|
|
9
|
+
|
|
10
|
+
export interface DocumentListConfig {
|
|
11
|
+
title?: string
|
|
12
|
+
types?: string[]
|
|
13
|
+
query?: string
|
|
14
|
+
queryParams?: Record<string, any>
|
|
15
|
+
order?: string
|
|
16
|
+
limit?: number
|
|
17
|
+
showCreateButton?: boolean
|
|
18
|
+
createButtonText?: string
|
|
19
|
+
apiVersion?: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const defaultProps = {
|
|
23
|
+
title: 'Last created',
|
|
24
|
+
order: '_createdAt desc',
|
|
25
|
+
limit: 10,
|
|
26
|
+
queryParams: {},
|
|
27
|
+
showCreateButton: true,
|
|
28
|
+
apiVersion: 'v1',
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function DocumentList(props: DocumentListConfig) {
|
|
32
|
+
const {
|
|
33
|
+
query,
|
|
34
|
+
limit,
|
|
35
|
+
apiVersion,
|
|
36
|
+
queryParams,
|
|
37
|
+
types,
|
|
38
|
+
order,
|
|
39
|
+
title,
|
|
40
|
+
showCreateButton,
|
|
41
|
+
createButtonText,
|
|
42
|
+
} = {
|
|
43
|
+
...defaultProps,
|
|
44
|
+
...props,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const [documents, setDocuments] = useState<SanityDocument[] | undefined>()
|
|
48
|
+
const [loading, setLoading] = useState<boolean>(true)
|
|
49
|
+
const [error, setError] = useState<Error | undefined>()
|
|
50
|
+
|
|
51
|
+
const client = useClient()
|
|
52
|
+
const schema = useSchema()
|
|
53
|
+
const versionedClient = useMemo(() => client.withConfig({apiVersion}), [client, apiVersion])
|
|
54
|
+
|
|
55
|
+
const {assembledQuery, params} = useMemo(() => {
|
|
56
|
+
if (query) {
|
|
57
|
+
return {assembledQuery: query, params: queryParams}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const documentTypes = schema.getTypeNames().filter((typeName) => {
|
|
61
|
+
const schemaType = schema.get(typeName)
|
|
62
|
+
return schemaType.type && schemaType.type.name === 'document'
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
assembledQuery: `*[_type in $types] | order(${order}) [0...${limit * 2}]`,
|
|
67
|
+
params: {types: types ? intersection(types, documentTypes) : documentTypes},
|
|
68
|
+
}
|
|
69
|
+
}, [schema, query, queryParams, order, limit, types])
|
|
70
|
+
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
if (!assembledQuery) {
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const subscription = getSubscription(assembledQuery, params, versionedClient).subscribe({
|
|
77
|
+
next: (d) => {
|
|
78
|
+
setDocuments(d.slice(0, limit))
|
|
79
|
+
setLoading(false)
|
|
80
|
+
},
|
|
81
|
+
error: (e) => {
|
|
82
|
+
setError(e)
|
|
83
|
+
setLoading(false)
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
// eslint-disable-next-line consistent-return
|
|
87
|
+
return () => {
|
|
88
|
+
subscription.unsubscribe()
|
|
89
|
+
}
|
|
90
|
+
}, [limit, versionedClient, assembledQuery, params])
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<DashboardWidgetContainer
|
|
94
|
+
header={title}
|
|
95
|
+
footer={
|
|
96
|
+
types &&
|
|
97
|
+
types.length === 1 &&
|
|
98
|
+
showCreateButton && (
|
|
99
|
+
<IntentButton
|
|
100
|
+
mode="bleed"
|
|
101
|
+
style={{width: '100%'}}
|
|
102
|
+
paddingX={2}
|
|
103
|
+
paddingY={4}
|
|
104
|
+
tone="primary"
|
|
105
|
+
type="button"
|
|
106
|
+
intent="create"
|
|
107
|
+
params={{type: types[0]}}
|
|
108
|
+
text={createButtonText || `Create new ${types[0]}`}
|
|
109
|
+
/>
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
>
|
|
113
|
+
<Card>
|
|
114
|
+
{error && <div>{error.message}</div>}
|
|
115
|
+
{!error && loading && (
|
|
116
|
+
<Card padding={4}>
|
|
117
|
+
<Flex justify="center">
|
|
118
|
+
<Spinner muted />
|
|
119
|
+
</Flex>
|
|
120
|
+
</Card>
|
|
121
|
+
)}
|
|
122
|
+
{!error && !documents && !loading && <div>Could not locate any documents :/</div>}
|
|
123
|
+
<Stack space={2}>
|
|
124
|
+
{documents && documents.map((doc) => <MenuEntry key={doc._id} doc={doc} />)}
|
|
125
|
+
</Stack>
|
|
126
|
+
</Card>
|
|
127
|
+
</DashboardWidgetContainer>
|
|
128
|
+
)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function MenuEntry({doc}: {doc: SanityDocument}) {
|
|
132
|
+
const schema = useSchema()
|
|
133
|
+
const type = schema.get(doc._type)
|
|
134
|
+
return (
|
|
135
|
+
<Card flex={1}>
|
|
136
|
+
<IntentButton
|
|
137
|
+
intent="edit"
|
|
138
|
+
mode="bleed"
|
|
139
|
+
padding={1}
|
|
140
|
+
radius={0}
|
|
141
|
+
params={{
|
|
142
|
+
type: doc._type,
|
|
143
|
+
id: getPublishedId(doc._id),
|
|
144
|
+
}}
|
|
145
|
+
style={{width: '100%'}}
|
|
146
|
+
>
|
|
147
|
+
<SanityPreview layout="default" type={type} value={doc} key={doc._id} />
|
|
148
|
+
</IntentButton>
|
|
149
|
+
</Card>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export default DocumentList
|
package/src/index.tsx
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
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/types'
|
|
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('*[_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/lib/DocumentList.css
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
@import 'part:@sanity/base/theme/variables-style';
|
|
2
|
-
|
|
3
|
-
.container {
|
|
4
|
-
composes: container from "part:@sanity/dashboard/widget-styles";
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
.header {
|
|
8
|
-
composes: header from "part:@sanity/dashboard/widget-styles";
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
.title {
|
|
12
|
-
composes: title from "part:@sanity/dashboard/widget-styles";
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
.content {
|
|
16
|
-
composes: content from 'part:@sanity/dashboard/widget-styles';
|
|
17
|
-
border-top: 1px solid var(--hairline-color);
|
|
18
|
-
padding: var(--small-padding) 0;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
.link {
|
|
22
|
-
composes: item from 'part:@sanity/base/theme/layout/selectable-style';
|
|
23
|
-
display: block;
|
|
24
|
-
color: inherit;
|
|
25
|
-
text-decoration: inherit;
|
|
26
|
-
outline: none;
|
|
27
|
-
padding: var(--small-padding) var(--medium-padding);
|
|
28
|
-
height: 100%;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
.footer {
|
|
32
|
-
composes: footer from 'part:@sanity/dashboard/widget-styles';
|
|
33
|
-
border-top: 1px solid var(--hairline-color);
|
|
34
|
-
}
|
package/lib/DocumentList.js
DELETED
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _react = _interopRequireDefault(require("react"));
|
|
9
|
-
|
|
10
|
-
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
|
-
|
|
12
|
-
var _router = require("part:@sanity/base/router");
|
|
13
|
-
|
|
14
|
-
var _preview = _interopRequireDefault(require("part:@sanity/base/preview"));
|
|
15
|
-
|
|
16
|
-
var _spinner = _interopRequireDefault(require("part:@sanity/components/loading/spinner"));
|
|
17
|
-
|
|
18
|
-
var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
|
|
19
|
-
|
|
20
|
-
var _intent = _interopRequireDefault(require("part:@sanity/components/buttons/intent"));
|
|
21
|
-
|
|
22
|
-
var _default2 = require("part:@sanity/components/lists/default");
|
|
23
|
-
|
|
24
|
-
var _draftUtils = require("part:@sanity/base/util/draft-utils");
|
|
25
|
-
|
|
26
|
-
var _lodash = require("lodash");
|
|
27
|
-
|
|
28
|
-
var _sanityConnector = require("./sanityConnector");
|
|
29
|
-
|
|
30
|
-
var _DocumentList = _interopRequireDefault(require("./DocumentList.css"));
|
|
31
|
-
|
|
32
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
33
|
-
|
|
34
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
35
|
-
|
|
36
|
-
var schemaTypeNames = _schema.default.getTypeNames();
|
|
37
|
-
|
|
38
|
-
class DocumentList extends _react.default.Component {
|
|
39
|
-
constructor() {
|
|
40
|
-
super(...arguments);
|
|
41
|
-
|
|
42
|
-
_defineProperty(this, "state", {
|
|
43
|
-
documents: null,
|
|
44
|
-
loading: true,
|
|
45
|
-
error: null
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
_defineProperty(this, "componentDidMount", () => {
|
|
49
|
-
var _this$props = this.props,
|
|
50
|
-
query = _this$props.query,
|
|
51
|
-
limit = _this$props.limit,
|
|
52
|
-
apiVersion = _this$props.apiVersion;
|
|
53
|
-
|
|
54
|
-
var _this$assembleQuery = this.assembleQuery(),
|
|
55
|
-
assembledQuery = _this$assembleQuery.assembledQuery,
|
|
56
|
-
params = _this$assembleQuery.params;
|
|
57
|
-
|
|
58
|
-
if (!assembledQuery) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
this.unsubscribe();
|
|
63
|
-
this.subscription = (0, _sanityConnector.getSubscription)(assembledQuery, params, apiVersion).subscribe({
|
|
64
|
-
next: documents => this.setState({
|
|
65
|
-
documents: documents.slice(0, limit),
|
|
66
|
-
loading: false
|
|
67
|
-
}),
|
|
68
|
-
error: _error => this.setState({
|
|
69
|
-
error: _error,
|
|
70
|
-
query,
|
|
71
|
-
loading: false
|
|
72
|
-
})
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
_defineProperty(this, "assembleQuery", () => {
|
|
77
|
-
var _this$props2 = this.props,
|
|
78
|
-
query = _this$props2.query,
|
|
79
|
-
queryParams = _this$props2.queryParams,
|
|
80
|
-
types = _this$props2.types,
|
|
81
|
-
order = _this$props2.order,
|
|
82
|
-
limit = _this$props2.limit;
|
|
83
|
-
|
|
84
|
-
if (query) {
|
|
85
|
-
return {
|
|
86
|
-
assembledQuery: query,
|
|
87
|
-
params: queryParams
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
var documentTypes = schemaTypeNames.filter(typeName => {
|
|
92
|
-
var schemaType = _schema.default.get(typeName);
|
|
93
|
-
|
|
94
|
-
return schemaType.type && schemaType.type.name === 'document';
|
|
95
|
-
});
|
|
96
|
-
return {
|
|
97
|
-
assembledQuery: "*[_type in $types] | order(".concat(order, ") [0...").concat(limit * 2, "]"),
|
|
98
|
-
params: {
|
|
99
|
-
types: types ? (0, _lodash.intersection)(types, documentTypes) : documentTypes
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
componentWillUnmount() {
|
|
106
|
-
this.unsubscribe();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
unsubscribe() {
|
|
110
|
-
if (this.subscription) {
|
|
111
|
-
this.subscription.unsubscribe();
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
render() {
|
|
116
|
-
var _this$props3 = this.props,
|
|
117
|
-
title = _this$props3.title,
|
|
118
|
-
types = _this$props3.types,
|
|
119
|
-
showCreateButton = _this$props3.showCreateButton,
|
|
120
|
-
createButtonText = _this$props3.createButtonText;
|
|
121
|
-
var _this$state = this.state,
|
|
122
|
-
documents = _this$state.documents,
|
|
123
|
-
loading = _this$state.loading,
|
|
124
|
-
error = _this$state.error;
|
|
125
|
-
return /*#__PURE__*/_react.default.createElement("div", {
|
|
126
|
-
className: _DocumentList.default.container
|
|
127
|
-
}, /*#__PURE__*/_react.default.createElement("header", {
|
|
128
|
-
className: _DocumentList.default.header
|
|
129
|
-
}, /*#__PURE__*/_react.default.createElement("h2", {
|
|
130
|
-
className: _DocumentList.default.title
|
|
131
|
-
}, title)), /*#__PURE__*/_react.default.createElement("div", {
|
|
132
|
-
className: _DocumentList.default.content
|
|
133
|
-
}, error && /*#__PURE__*/_react.default.createElement("div", null, error.message), !error && loading && /*#__PURE__*/_react.default.createElement(_spinner.default, {
|
|
134
|
-
center: true,
|
|
135
|
-
message: "Loading..."
|
|
136
|
-
}), !error && !documents && !loading && /*#__PURE__*/_react.default.createElement("div", null, "Could not locate any documents :/"), /*#__PURE__*/_react.default.createElement(_default2.List, null, documents && documents.map(doc => {
|
|
137
|
-
var type = _schema.default.get(doc._type);
|
|
138
|
-
|
|
139
|
-
return /*#__PURE__*/_react.default.createElement(_default2.Item, {
|
|
140
|
-
key: doc._id
|
|
141
|
-
}, /*#__PURE__*/_react.default.createElement(_router.IntentLink, {
|
|
142
|
-
intent: "edit",
|
|
143
|
-
params: {
|
|
144
|
-
type: doc._type,
|
|
145
|
-
id: (0, _draftUtils.getPublishedId)(doc._id)
|
|
146
|
-
},
|
|
147
|
-
className: _DocumentList.default.link
|
|
148
|
-
}, /*#__PURE__*/_react.default.createElement(_preview.default, {
|
|
149
|
-
layout: "default",
|
|
150
|
-
type: type,
|
|
151
|
-
value: doc,
|
|
152
|
-
key: doc._id
|
|
153
|
-
})));
|
|
154
|
-
}))), types && types.length === 1 && showCreateButton && /*#__PURE__*/_react.default.createElement("div", {
|
|
155
|
-
className: _DocumentList.default.footer
|
|
156
|
-
}, /*#__PURE__*/_react.default.createElement(_intent.default, {
|
|
157
|
-
bleed: true,
|
|
158
|
-
color: "primary",
|
|
159
|
-
kind: "simple",
|
|
160
|
-
intent: "create",
|
|
161
|
-
params: {
|
|
162
|
-
type: types[0]
|
|
163
|
-
}
|
|
164
|
-
}, createButtonText || "Create new ".concat(types[0]))));
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
_defineProperty(DocumentList, "propTypes", {
|
|
170
|
-
title: _propTypes.default.string,
|
|
171
|
-
types: _propTypes.default.arrayOf(_propTypes.default.string),
|
|
172
|
-
query: _propTypes.default.string,
|
|
173
|
-
queryParams: _propTypes.default.object,
|
|
174
|
-
// eslint-disable-line react/forbid-prop-types
|
|
175
|
-
order: _propTypes.default.string,
|
|
176
|
-
limit: _propTypes.default.number,
|
|
177
|
-
showCreateButton: _propTypes.default.bool,
|
|
178
|
-
createButtonText: _propTypes.default.string,
|
|
179
|
-
apiVersion: _propTypes.default.string
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
_defineProperty(DocumentList, "defaultProps", {
|
|
183
|
-
title: 'Last created',
|
|
184
|
-
order: '_createdAt desc',
|
|
185
|
-
limit: 10,
|
|
186
|
-
types: null,
|
|
187
|
-
query: null,
|
|
188
|
-
queryParams: {},
|
|
189
|
-
showCreateButton: true,
|
|
190
|
-
createButtonText: null,
|
|
191
|
-
apiVersion: 'v1'
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
var _default = DocumentList;
|
|
195
|
-
exports.default = _default;
|
|
196
|
-
//# sourceMappingURL=DocumentList.js.map
|
package/lib/DocumentList.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/DocumentList.js"],"names":["schemaTypeNames","schema","getTypeNames","DocumentList","React","Component","documents","loading","error","props","query","limit","apiVersion","assembleQuery","assembledQuery","params","unsubscribe","subscription","subscribe","next","setState","slice","queryParams","types","order","documentTypes","filter","typeName","schemaType","get","type","name","componentWillUnmount","render","title","showCreateButton","createButtonText","state","styles","container","header","content","message","map","doc","_type","_id","id","link","length","footer","PropTypes","string","arrayOf","object","number","bool"],"mappings":";;;;;;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;AAEA,IAAMA,eAAe,GAAGC,gBAAOC,YAAP,EAAxB;;AAEA,MAAMC,YAAN,SAA2BC,eAAMC,SAAjC,CAA2C;AAAA;AAAA;;AAAA,mCAEjC;AACNC,MAAAA,SAAS,EAAE,IADL;AAENC,MAAAA,OAAO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B,mBAAUC,MADA;AAEjB7B,EAAAA,KAAK,EAAE4B,mBAAUE,OAAV,CAAkBF,mBAAUC,MAA5B,CAFU;AAGjB1C,EAAAA,KAAK,EAAEyC,mBAAUC,MAHA;AAIjB9B,EAAAA,WAAW,EAAE6B,mBAAUG,MAJN;AAIc;AAC/B9B,EAAAA,KAAK,EAAE2B,mBAAUC,MALA;AAMjBzC,EAAAA,KAAK,EAAEwC,mBAAUI,MANA;AAOjBpB,EAAAA,gBAAgB,EAAEgB,mBAAUK,IAPX;AAQjBpB,EAAAA,gBAAgB,EAAEe,mBAAUC,MARX;AASjBxC,EAAAA,UAAU,EAAEuC,mBAAUC;AATL,C;;gBARfjD,Y,kBAoBkB;AACpB+B,EAAAA,KAAK,EAAE,cADa;AAEpBV,EAAAA,KAAK,EAAE,iBAFa;AAGpBb,EAAAA,KAAK,EAAE,EAHa;AAIpBY,EAAAA,KAAK,EAAE,IAJa;AAKpBb,EAAAA,KAAK,EAAE,IALa;AAMpBY,EAAAA,WAAW,EAAE,EANO;AAOpBa,EAAAA,gBAAgB,EAAE,IAPE;AAQpBC,EAAAA,gBAAgB,EAAE,IARE;AASpBxB,EAAAA,UAAU,EAAE;AATQ,C;;eAuGTT,Y","sourcesContent":["/* eslint-disable complexity */\nimport React from 'react'\nimport PropTypes from 'prop-types'\nimport {IntentLink} from 'part:@sanity/base/router'\nimport SanityPreview from 'part:@sanity/base/preview'\nimport Spinner from 'part:@sanity/components/loading/spinner'\nimport schema from 'part:@sanity/base/schema'\nimport IntentButton from 'part:@sanity/components/buttons/intent'\nimport {List, Item} from 'part:@sanity/components/lists/default'\nimport {getPublishedId} from 'part:@sanity/base/util/draft-utils'\nimport {intersection} from 'lodash'\nimport {getSubscription} from './sanityConnector'\nimport styles from './DocumentList.css'\n\nconst schemaTypeNames = schema.getTypeNames()\n\nclass DocumentList extends React.Component {\n\n state = {\n documents: null,\n loading: true,\n error: null\n }\n\n static propTypes = {\n title: PropTypes.string,\n types: PropTypes.arrayOf(PropTypes.string),\n query: PropTypes.string,\n queryParams: PropTypes.object, // eslint-disable-line react/forbid-prop-types\n order: PropTypes.string,\n limit: PropTypes.number,\n showCreateButton: PropTypes.bool,\n createButtonText: PropTypes.string,\n apiVersion: PropTypes.string\n }\n\n static defaultProps = {\n title: 'Last created',\n order: '_createdAt desc',\n limit: 10,\n types: null,\n query: null,\n queryParams: {},\n showCreateButton: true,\n createButtonText: null,\n apiVersion: 'v1'\n }\n\n componentDidMount = () => {\n const {query, limit, apiVersion} = this.props\n const {assembledQuery, params} = this.assembleQuery()\n if (!assembledQuery) {\n return\n }\n\n this.unsubscribe()\n this.subscription = getSubscription(assembledQuery, params, apiVersion)\n .subscribe({\n next: documents =>\n this.setState({documents: documents.slice(0, limit), loading: false}),\n error: error =>\n this.setState({error, query, loading: false})\n })\n }\n\n componentWillUnmount() {\n this.unsubscribe()\n }\n\n unsubscribe() {\n if (this.subscription) {\n this.subscription.unsubscribe()\n }\n }\n\n assembleQuery = () => {\n const {query, queryParams, types, order, limit} = this.props\n if (query) {\n return {assembledQuery: query, params: queryParams}\n }\n\n const documentTypes = schemaTypeNames.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 }\n\n\n render() {\n const {title, types, showCreateButton, createButtonText} = this.props\n const {documents, loading, error} = this.state\n\n return (\n <div className={styles.container}>\n <header className={styles.header}>\n <h2 className={styles.title}>{title}</h2>\n </header>\n <div className={styles.content}>\n {error && <div>{error.message}</div>}\n {!error && loading && <Spinner center message=\"Loading...\" />}\n {(!error && !documents && !loading) && <div>Could not locate any documents :/</div>}\n <List>\n {documents && documents.map(doc => {\n const type = schema.get(doc._type)\n return (\n <Item key={doc._id}>\n <IntentLink\n intent=\"edit\"\n params={{\n type: doc._type,\n id: getPublishedId(doc._id)\n }}\n className={styles.link}\n >\n <SanityPreview layout=\"default\" type={type} value={doc} key={doc._id} />\n </IntentLink>\n </Item>\n )\n\n })}\n </List>\n </div>\n {types && types.length === 1 && showCreateButton && (\n <div className={styles.footer}>\n <IntentButton bleed color=\"primary\" kind=\"simple\" intent=\"create\" params={{type: types[0]}}>\n {createButtonText || `Create new ${types[0]}`}\n </IntentButton>\n </div>\n )}\n </div>\n )\n }\n}\n\nexport default DocumentList\n"],"file":"DocumentList.js"}
|
package/lib/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _DocumentList = _interopRequireDefault(require("./DocumentList"));
|
|
9
|
-
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
-
|
|
12
|
-
var _default = {
|
|
13
|
-
name: 'document-list',
|
|
14
|
-
component: _DocumentList.default
|
|
15
|
-
};
|
|
16
|
-
exports.default = _default;
|
|
17
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"names":["name","component","DocumentList"],"mappings":";;;;;;;AAAA;;;;eAEe;AACbA,EAAAA,IAAI,EAAE,eADO;AAEbC,EAAAA,SAAS,EAAEC;AAFE,C","sourcesContent":["import DocumentList from './DocumentList'\n\nexport default {\n name: 'document-list',\n component: DocumentList\n}\n"],"file":"index.js"}
|