sanity-plugin-documents-pane 1.0.3 → 1.0.6

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/.eslintignore ADDED
@@ -0,0 +1 @@
1
+ lib
package/README.md CHANGED
@@ -1,17 +1,16 @@
1
1
  # sanity-plugin-documents-pane
2
2
 
3
- Display a GROQ-queried list of Documents in a View Pane.
3
+ Displays the results of a GROQ query in a View Pane. With the ability to use field values in the current document as query parameters.
4
4
 
5
5
  ![Incoming References](https://user-images.githubusercontent.com/9684022/121202594-52bc6180-c86d-11eb-897d-f418047b3d22.png)
6
6
 
7
-
8
7
  ## Installation
9
8
 
10
9
  ```
11
10
  sanity install documents-pane
12
11
  ```
13
12
 
14
- This plugin is designed to be used as a [Component inside of a View](https://www.sanity.io/docs/structure-builder-reference#c0c8284844b7).
13
+ This plugin is designed to be used as a [Component inside of a View](https://www.sanity.io/docs/structure-builder-reference#c0c8284844b7).
15
14
 
16
15
  The example below illustrates using the current Document being used to query for all Documents that reference it.
17
16
 
@@ -25,19 +24,25 @@ S.view
25
24
  .component(DocumentsPane)
26
25
  .options({
27
26
  query: `*[!(_id in path("drafts.**")) && references($id)]`,
28
- params: { id: `_id` },
29
- useDraft: false
27
+ params: {id: `_id`},
28
+ useDraft: false,
29
+ debug: true,
30
30
  })
31
31
  .title('Incoming References')
32
32
  ```
33
33
 
34
34
  The `.options()` configuration works as follows:
35
35
 
36
- - `query` (string, required)
37
- - `params` (object, optional) A [dot-notated string](https://www.npmjs.com/package/dlv) from the document object to a field, to use as variables in the query.
38
- - `useDraft` (bool, optional, default: `false`) When populating the `params` values, it will use the `published` version of the document by default.
36
+ - `query` (string, required)
37
+ - `params` (object, optional) A [dot-notated string](https://www.npmjs.com/package/dlv) from the document object to a field, to use as variables in the query.
38
+ - `useDraft` (bool, optional, default: `false`) When populating the `params` values, it will use the `published` version of the document by default.
39
+ - `debug` (bool, optional, default: `false`) In case of an error or the query returning no documents, setting to `true` will display the query and params that were used.
40
+
41
+ ## Thanks!
42
+
43
+ This plugin is based on [Incoming References](https://github.com/sanity-io/sanity/tree/victoria/incoming-refs-preview/packages/test-studio/src/previews/incoming-refs) originally written by Victoria Bergquist.
39
44
 
40
45
  ## License
41
46
 
42
47
  MIT © Simeon Griggs
43
- See LICENSE
48
+ See LICENSE
package/lib/Debug.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = Debug;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _ui = require("@sanity/ui");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ function Debug(_ref) {
15
+ var query = _ref.query,
16
+ params = _ref.params;
17
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_ui.Stack, {
18
+ space: 4
19
+ }, /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Label, null, "Query")), /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Code, null, query))), /*#__PURE__*/_react.default.createElement(_ui.Stack, {
20
+ space: 4
21
+ }, /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Label, null, "Params")), /*#__PURE__*/_react.default.createElement(_ui.Box, null, /*#__PURE__*/_react.default.createElement(_ui.Code, null, JSON.stringify(params)))));
22
+ }
23
+
24
+ Debug.propTypes = {
25
+ params: PropTypes.object,
26
+ query: PropTypes.string
27
+ };
28
+ Debug.defaultProps = {
29
+ params: {},
30
+ query: ""
31
+ };
32
+ //# sourceMappingURL=Debug.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Debug.tsx"],"names":["Debug","query","params","JSON","stringify","propTypes","PropTypes","object","string","defaultProps"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEe,SAASA,KAAT,OAAgC;AAAA,MAAhBC,KAAgB,QAAhBA,KAAgB;AAAA,MAATC,MAAS,QAATA,MAAS;AAC7C,sBACE,yEACE,6BAAC,SAAD;AAAO,IAAA,KAAK,EAAE;AAAd,kBACE,6BAAC,OAAD,qBAAK,6BAAC,SAAD,gBAAL,CADF,eAEE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOD,KAAP,CADF,CAFF,CADF,eAOE,6BAAC,SAAD;AAAO,IAAA,KAAK,EAAE;AAAd,kBACE,6BAAC,OAAD,qBAAK,6BAAC,SAAD,iBAAL,CADF,eAEE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOE,IAAI,CAACC,SAAL,CAAeF,MAAf,CAAP,CADF,CAFF,CAPF,CADF;AAgBD;;AAEDF,KAAK,CAACK,SAAN,GAAkB;AAChBH,EAAAA,MAAM,EAAEI,SAAS,CAACC,MADF;AAEhBN,EAAAA,KAAK,EAAEK,SAAS,CAACE;AAFD,CAAlB;AAKAR,KAAK,CAACS,YAAN,GAAqB;AACnBP,EAAAA,MAAM,EAAE,EADW;AAEnBD,EAAAA,KAAK;AAFc,CAArB","sourcesContent":["import React from 'react'\nimport {Code, Box, Label, Stack} from '@sanity/ui'\n\nexport default function Debug({query, params}) {\n return (\n <>\n <Stack space={4}>\n <Box><Label>Query</Label></Box>\n <Box>\n <Code>{query}</Code>\n </Box>\n </Stack>\n <Stack space={4}>\n <Box><Label>Params</Label></Box>\n <Box>\n <Code>{JSON.stringify(params)}</Code>\n </Box>\n </Stack>\n </>\n )\n}\n\nDebug.propTypes = {\n params: PropTypes.object,\n query: PropTypes.string,\n}\n\nDebug.defaultProps = {\n params: {},\n query: ``,\n}\n"],"file":"Debug.js"}
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = Documents;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _reactQuery = require("react-query");
11
+
12
+ var _ui = require("@sanity/ui");
13
+
14
+ var _deskTool = require("@sanity/desk-tool");
15
+
16
+ var _RouterContext = require("@sanity/state-router/lib/RouterContext");
17
+
18
+ var _preview = _interopRequireDefault(require("part:@sanity/base/preview"));
19
+
20
+ var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
21
+
22
+ var _client = _interopRequireDefault(require("part:@sanity/base/client"));
23
+
24
+ var _Debug = _interopRequireDefault(require("./Debug"));
25
+
26
+ var _Feedback = _interopRequireDefault(require("./Feedback"));
27
+
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
+
30
+ var client = _client.default.withConfig({
31
+ apiVersion: "2021-05-19"
32
+ });
33
+
34
+ function Documents(props) {
35
+ var query = props.query,
36
+ params = props.params,
37
+ debug = props.debug,
38
+ _rev = props._rev;
39
+
40
+ var _useQuery = (0, _reactQuery.useQuery)(['useDocuments', {
41
+ props
42
+ }], () => client.fetch(query, params)),
43
+ isLoading = _useQuery.isLoading,
44
+ error = _useQuery.error,
45
+ data = _useQuery.data;
46
+
47
+ var routerContext = _react.default.useContext(_RouterContext.RouterContext);
48
+
49
+ var _usePaneRouter = (0, _deskTool.usePaneRouter)(),
50
+ routerPanesState = _usePaneRouter.routerPanesState,
51
+ groupIndex = _usePaneRouter.groupIndex;
52
+
53
+ var openDocumentInSidePane = _react.default.useCallback((id, type) => {
54
+ var newPane = [{
55
+ id: id,
56
+ params: {
57
+ type
58
+ }
59
+ }];
60
+ var panes = [routerPanesState[0], routerPanesState[1], newPane];
61
+ var href = routerContext.resolvePathFromState({
62
+ panes
63
+ });
64
+ routerContext.navigateUrl(href);
65
+ }, [routerContext, routerPanesState, groupIndex]);
66
+
67
+ if (!_rev) {
68
+ return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
69
+ padding: 4,
70
+ space: 5
71
+ }, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "Document must be Published"), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
72
+ query: query,
73
+ params: params
74
+ }));
75
+ }
76
+
77
+ if (isLoading) {
78
+ return /*#__PURE__*/_react.default.createElement(_ui.Box, {
79
+ padding: 4
80
+ }, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
81
+ justify: "center",
82
+ align: "center"
83
+ }, /*#__PURE__*/_react.default.createElement(_ui.Spinner, {
84
+ muted: true
85
+ })));
86
+ }
87
+
88
+ if (error) {
89
+ return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
90
+ padding: 4,
91
+ space: 5
92
+ }, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "There was en error performing this query"), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
93
+ query: query,
94
+ params: params
95
+ }));
96
+ }
97
+
98
+ if (!(data !== null && data !== void 0 && data.length)) {
99
+ return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
100
+ padding: 4,
101
+ space: 5
102
+ }, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "No Documents found"), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
103
+ query: query,
104
+ params: params
105
+ }));
106
+ }
107
+
108
+ return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
109
+ padding: 2,
110
+ space: 2
111
+ }, data.map(doc => /*#__PURE__*/_react.default.createElement(_ui.Button, {
112
+ key: doc._id,
113
+ onClick: () => openDocumentInSidePane(doc._id, doc._type),
114
+ mode: "bleed",
115
+ radius: 2,
116
+ padding: 2
117
+ }, /*#__PURE__*/_react.default.createElement(_preview.default, {
118
+ value: doc,
119
+ type: _schema.default.get(doc._type)
120
+ }))));
121
+ }
122
+ //# sourceMappingURL=Documents.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Documents.tsx"],"names":["client","sanityClient","withConfig","apiVersion","Documents","props","query","params","debug","_rev","fetch","isLoading","error","data","routerContext","React","useContext","RouterContext","routerPanesState","groupIndex","openDocumentInSidePane","useCallback","id","type","newPane","panes","href","resolvePathFromState","navigateUrl","length","map","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SAAD;AAAO,IAAA,OAAO,EAAE,CAAhB;AAAmB,IAAA,KAAK,EAAE;AAA1B,KACGM,IAAI,CAACiB,GAAL,CAAUC,GAAD,iBACR,6BAAC,UAAD;AACE,IAAA,GAAG,EAAEA,GAAG,CAACC,GADX;AAEE,IAAA,OAAO,EAAE,MAAMZ,sBAAsB,CAACW,GAAG,CAACC,GAAL,EAAUD,GAAG,CAACE,KAAd,CAFvC;AAGE,IAAA,IAAI,EAAC,OAHP;AAIE,IAAA,MAAM,EAAE,CAJV;AAKE,IAAA,OAAO,EAAE;AALX,kBAOE,6BAAC,gBAAD;AAAS,IAAA,KAAK,EAAEF,GAAhB;AAAqB,IAAA,IAAI,EAAEG,gBAAOC,GAAP,CAAWJ,GAAG,CAACE,KAAf;AAA3B,IAPF,CADD,CADH,CADF;AAeD","sourcesContent":["import React from 'react'\nimport {useQuery} from 'react-query'\nimport {Button, Box, Stack, Flex, Spinner} from '@sanity/ui'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport {RouterContext} from '@sanity/state-router/lib/RouterContext'\nimport Preview from 'part:@sanity/base/preview'\nimport schema from 'part:@sanity/base/schema'\nimport sanityClient from 'part:@sanity/base/client'\n\nimport Debug from './Debug'\nimport Feedback from './Feedback'\n\n const client = sanityClient.withConfig({apiVersion: `2021-05-19`})\n\nexport default function Documents(props) {\n const {query, params, debug, _rev} = props\n\n const {isLoading, error, data} = useQuery(['useDocuments', {props}], () =>\n client.fetch(query, params)\n )\n\n const routerContext = React.useContext(RouterContext)\n const {routerPanesState, groupIndex} = usePaneRouter()\n\n const openDocumentInSidePane = React.useCallback(\n (id: string, type: string) => {\n const newPane = [\n {\n id: id,\n params: {type},\n },\n ]\n let panes = [routerPanesState[0], routerPanesState[1], newPane]\n\n const href = routerContext.resolvePathFromState({panes})\n routerContext.navigateUrl(href)\n },\n [routerContext, routerPanesState, groupIndex]\n )\n\n if (!_rev) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>Document must be Published</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n if (isLoading) {\n return (\n <Box padding={4}>\n <Flex justify=\"center\" align=\"center\">\n <Spinner muted />\n </Flex>\n </Box>\n )\n }\n\n if (error) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>There was en error performing this query</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n if (!data?.length) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>No Documents found</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n return (\n <Stack padding={2} space={2}>\n {data.map((doc) => (\n <Button\n key={doc._id}\n onClick={() => openDocumentInSidePane(doc._id, doc._type)}\n mode=\"bleed\"\n radius={2}\n padding={2}\n >\n <Preview value={doc} type={schema.get(doc._type)} />\n </Button>\n ))}\n </Stack>\n )\n}\n"],"file":"Documents.js"}
@@ -3,75 +3,49 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.default = DocumentsPane;
7
7
 
8
8
  var _react = _interopRequireDefault(require("react"));
9
9
 
10
- var _propTypes = _interopRequireDefault(require("prop-types"));
11
-
12
10
  var _dlv = _interopRequireDefault(require("dlv"));
13
11
 
14
- var _router = require("part:@sanity/base/router");
15
-
16
- var _preview = _interopRequireDefault(require("part:@sanity/base/preview"));
17
-
18
- var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
19
-
20
- var _hooks = require("./lib/hooks");
12
+ var _reactQuery = require("react-query");
21
13
 
22
- var _DocumentsPaneModule = _interopRequireDefault(require("./DocumentsPane.module.css"));
14
+ var _Documents = _interopRequireDefault(require("./Documents"));
23
15
 
24
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
17
 
26
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
18
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
27
19
 
28
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
29
21
 
30
22
  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; }
31
23
 
32
- var DocumentsPane = _ref => {
24
+ var queryClient = new _reactQuery.QueryClient();
25
+
26
+ function DocumentsPane(_ref) {
33
27
  var sanityDocument = _ref.document,
34
28
  options = _ref.options;
35
29
  var query = options.query,
36
30
  params = options.params,
37
- useDraft = options.useDraft;
31
+ useDraft = options.useDraft,
32
+ debug = options.debug;
38
33
  var doc = useDraft ? sanityDocument.displayed : sanityDocument.published;
34
+
35
+ var _ref2 = doc !== null && doc !== void 0 ? doc : {},
36
+ _id = _ref2._id,
37
+ _rev = _ref2._rev;
38
+
39
39
  var paramValues = Object.keys(params).reduce((acc, key) => _objectSpread(_objectSpread({}, acc), {}, {
40
40
  [key]: (0, _dlv.default)(doc, params[key])
41
41
  }), {});
42
- var documents = (0, _hooks.useDocuments)(query, paramValues);
43
- return /*#__PURE__*/_react.default.createElement("div", {
44
- className: _DocumentsPaneModule.default.root
45
- }, documents.length > 0 ? documents.map(doc => /*#__PURE__*/_react.default.createElement(_router.IntentLink, {
46
- key: doc._id,
47
- className: _DocumentsPaneModule.default.item,
48
- intent: "edit",
49
- params: {
50
- id: doc._id,
51
- type: doc._type
52
- }
53
- }, /*#__PURE__*/_react.default.createElement(_preview.default, {
54
- value: doc,
55
- type: _schema.default.get(doc._type)
56
- }))) : /*#__PURE__*/_react.default.createElement("div", {
57
- className: _DocumentsPaneModule.default.noDocs
58
- }, "No Documents match this query: ", /*#__PURE__*/_react.default.createElement("code", null, query)));
59
- };
60
-
61
- DocumentsPane.propTypes = {
62
- options: _propTypes.default.shape({
63
- params: _propTypes.default.string,
64
- query: _propTypes.default.object,
65
- useDraft: _propTypes.default.bool
66
- })
67
- };
68
- DocumentsPane.defaultProps = {
69
- options: {
70
- params: {},
71
- query: "",
72
- useDraft: false
73
- }
74
- };
75
- var _default = DocumentsPane;
76
- exports.default = _default;
42
+ return /*#__PURE__*/_react.default.createElement(_reactQuery.QueryClientProvider, {
43
+ client: queryClient
44
+ }, /*#__PURE__*/_react.default.createElement(_Documents.default, {
45
+ _rev: _rev,
46
+ query: query,
47
+ params: paramValues,
48
+ debug: debug
49
+ }));
50
+ }
77
51
  //# sourceMappingURL=DocumentsPane.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/DocumentsPane.js"],"names":["DocumentsPane","sanityDocument","document","options","query","params","useDraft","doc","displayed","published","paramValues","Object","keys","reduce","acc","key","documents","styles","root","length","map","_id","item","id","type","_type","schema","get","noDocs","propTypes","PropTypes","shape","string","object","bool","defaultProps"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;A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AOW;AAAvB,qDACiC,2CAAOxB,KAAP,CADjC,CAbJ,CADF;AAoBD,CA/BD;;AAiCAJ,aAAa,CAAC6B,SAAd,GAA0B;AACxB1B,EAAAA,OAAO,EAAE2B,mBAAUC,KAAV,CAAgB;AACvB1B,IAAAA,MAAM,EAAEyB,mBAAUE,MADK;AAEvB5B,IAAAA,KAAK,EAAE0B,mBAAUG,MAFM;AAGvB3B,IAAAA,QAAQ,EAAEwB,mBAAUI;AAHG,GAAhB;AADe,CAA1B;AAQAlC,aAAa,CAACmC,YAAd,GAA6B;AAC3BhC,EAAAA,OAAO,EAAE;AACPE,IAAAA,MAAM,EAAE,EADD;AAEPD,IAAAA,KAAK,IAFE;AAGPE,IAAAA,QAAQ,EAAE;AAHH;AADkB,CAA7B;eAQeN,a","sourcesContent":["import React from 'react'\nimport PropTypes from 'prop-types'\nimport delve from 'dlv'\nimport {IntentLink} from 'part:@sanity/base/router'\nimport Preview from 'part:@sanity/base/preview'\nimport schema from 'part:@sanity/base/schema'\n\nimport {useDocuments} from './lib/hooks'\nimport styles from './DocumentsPane.module.css'\n\nconst DocumentsPane = ({document: sanityDocument, options}) => {\n const {query, params, useDraft} = options\n const doc = useDraft ? sanityDocument.displayed : sanityDocument.published\n\n const paramValues = Object.keys(params).reduce(\n (acc, key) => ({...acc, [key]: delve(doc, params[key])}),\n {}\n )\n\n const documents = useDocuments(query, paramValues)\n\n return (\n <div className={styles.root}>\n {documents.length > 0 ? (\n documents.map((doc) => (\n <IntentLink\n key={doc._id}\n className={styles.item}\n intent=\"edit\"\n params={{id: doc._id, type: doc._type}}\n >\n <Preview value={doc} type={schema.get(doc._type)} />\n </IntentLink>\n ))\n ) : (\n <div className={styles.noDocs}>\n No Documents match this query: <code>{query}</code>\n </div>\n )}\n </div>\n )\n}\n\nDocumentsPane.propTypes = {\n options: PropTypes.shape({\n params: PropTypes.string,\n query: PropTypes.object,\n useDraft: PropTypes.bool,\n }),\n}\n\nDocumentsPane.defaultProps = {\n options: {\n params: {},\n query: ``,\n useDraft: false,\n },\n}\n\nexport default DocumentsPane\n"],"file":"DocumentsPane.js"}
1
+ {"version":3,"sources":["../src/DocumentsPane.tsx"],"names":["queryClient","QueryClient","DocumentsPane","sanityDocument","document","options","query","params","useDraft","debug","doc","displayed","published","_id","_rev","paramValues","Object","keys","reduce","acc","key"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAEA;;;;;;;;;;AAEA,IAAMA,WAAW,GAAG,IAAIC,uBAAJ,EAApB;;AAEe,SAASC,aAAT,OAA4D;AAAA,MAA1BC,cAA0B,QAApCC,QAAoC;AAAA,MAAVC,OAAU,QAAVA,OAAU;AACzE,MAAOC,KAAP,GAAyCD,OAAzC,CAAOC,KAAP;AAAA,MAAcC,MAAd,GAAyCF,OAAzC,CAAcE,MAAd;AAAA,MAAsBC,QAAtB,GAAyCH,OAAzC,CAAsBG,QAAtB;AAAA,MAAgCC,KAAhC,GAAyCJ,OAAzC,CAAgCI,KAAhC;AAEA,MAAMC,GAAG,GAAGF,QAAQ,GAAGL,cAAc,CAACQ,SAAlB,GAA8BR,cAAc,CAACS,SAAjE;;AACA,cAAoBF,GAApB,aAAoBA,GAApB,cAAoBA,GAApB,GAA2B,EAA3B;AAAA,MAAOG,GAAP,SAAOA,GAAP;AAAA,MAAYC,IAAZ,SAAYA,IAAZ;;AAEA,MAAMC,WAAW,GAAGC,MAAM,CAACC,IAAP,CAAYV,MAAZ,EAAoBW,MAApB,CAClB,CAACC,GAAD,EAAMC,GAAN,qCAAmBD,GAAnB;AAAwB,KAACC,GAAD,GAAO,kBAAMV,GAAN,EAAWH,MAAM,CAACa,GAAD,CAAjB;AAA/B,IADkB,EAElB,EAFkB,CAApB;AAKA,sBACE,6BAAC,+BAAD;AAAqB,IAAA,MAAM,EAAEpB;AAA7B,kBACE,6BAAC,kBAAD;AAAW,IAAA,IAAI,EAAEc,IAAjB;AAAuB,IAAA,KAAK,EAAER,KAA9B;AAAqC,IAAA,MAAM,EAAES,WAA7C;AAA0D,IAAA,KAAK,EAAEN;AAAjE,IADF,CADF;AAKD","sourcesContent":["import React from 'react'\nimport delve from 'dlv'\nimport {QueryClient, QueryClientProvider} from 'react-query'\n\nimport Documents from './Documents'\n\nconst queryClient = new QueryClient()\n\nexport default function DocumentsPane({document: sanityDocument, options}) {\n const {query, params, useDraft, debug} = options\n\n const doc = useDraft ? sanityDocument.displayed : sanityDocument.published\n const {_id, _rev} = doc ?? {}\n\n const paramValues = Object.keys(params).reduce(\n (acc, key) => ({...acc, [key]: delve(doc, params[key])}),\n {}\n )\n\n return (\n <QueryClientProvider client={queryClient}>\n <Documents _rev={_rev} query={query} params={paramValues} debug={debug} />\n </QueryClientProvider>\n )\n}\n"],"file":"DocumentsPane.js"}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = Feedback;
7
+
8
+ var _react = _interopRequireDefault(require("react"));
9
+
10
+ var _ui = require("@sanity/ui");
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ function Feedback(props) {
15
+ var children = props.children,
16
+ _props$tone = props.tone,
17
+ tone = _props$tone === void 0 ? "caution" : _props$tone;
18
+ return /*#__PURE__*/_react.default.createElement(_ui.Card, {
19
+ padding: 3,
20
+ radius: 2,
21
+ shadow: 1,
22
+ tone: tone
23
+ }, /*#__PURE__*/_react.default.createElement(_ui.Text, {
24
+ size: 1
25
+ }, children));
26
+ }
27
+ //# sourceMappingURL=Feedback.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/Feedback.tsx"],"names":["Feedback","props","children","tone"],"mappings":";;;;;;;AAAA;;AACA;;;;AAQe,SAASA,QAAT,CAAkBC,KAAlB,EAAwC;AACrD,MAAOC,QAAP,GAAqCD,KAArC,CAAOC,QAAP;AAAA,oBAAqCD,KAArC,CAAiBE,IAAjB;AAAA,MAAiBA,IAAjB;AAEA,sBACE,6BAAC,QAAD;AAAM,IAAA,OAAO,EAAE,CAAf;AAAkB,IAAA,MAAM,EAAE,CAA1B;AAA6B,IAAA,MAAM,EAAE,CAArC;AAAwC,IAAA,IAAI,EAAEA;AAA9C,kBACE,6BAAC,QAAD;AAAM,IAAA,IAAI,EAAE;AAAZ,KAAgBD,QAAhB,CADF,CADF;AAKD","sourcesContent":["import React from 'react'\nimport {Card, Text} from '@sanity/ui'\nimport type {BadgeTone} from '@sanity/ui'\n\ntype FeedbackProps = {\n children?: React.ReactNode\n tone?: BadgeTone\n}\n\nexport default function Feedback(props: FeedbackProps) {\n const {children, tone = `caution`} = props\n\n return (\n <Card padding={3} radius={2} shadow={1} tone={tone}>\n <Text size={1}>{children}</Text>\n </Card>\n )\n}\n"],"file":"Feedback.js"}
package/lib/index.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _DocumentsPane = _interopRequireDefault(require("./DocumentsPane"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ var _default = _DocumentsPane.default;
13
+ exports.default = _default;
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.js"],"names":["DocumentsPane"],"mappings":";;;;;;;AACA;;;;eAEeA,sB","sourcesContent":["\nimport DocumentsPane from \"./DocumentsPane\";\n\nexport default DocumentsPane\n"],"file":"index.js"}
package/package.json CHANGED
@@ -1,42 +1,57 @@
1
1
  {
2
2
  "name": "sanity-plugin-documents-pane",
3
- "version": "1.0.3",
4
- "description": "Display a GROQ-queried list of Documents in a View Pane",
5
- "main": "lib/DocumentsPane.js",
3
+ "version": "1.0.6",
4
+ "description": "Displays the results of a GROQ query in a View Pane",
5
+ "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "build": "sanipack build",
8
8
  "watch": "sanipack build --watch",
9
- "prepublishOnly": "sanipack build && sanipack verify"
9
+ "_postinstall": "husky install",
10
+ "prepublishOnly": "pinst --disable && sanipack build && sanipack verify",
11
+ "postpublish": "pinst --enable",
12
+ "lint": "eslint .",
13
+ "lint:fix": "eslint . --fix"
14
+ },
15
+ "husky": {
16
+ "hooks": {
17
+ "pre-commit": "npm run lint:fix"
18
+ }
10
19
  },
11
20
  "repository": {
12
21
  "type": "git",
13
- "url": "git+ssh://git@github.com/SimeonGriggs/sanity-plugin-documents-pane.git"
22
+ "url": "git+ssh://git@github.com/sanity-io/sanity-plugin-documents-pane.git"
14
23
  },
15
24
  "keywords": [
16
25
  "sanity",
17
26
  "sanity-plugin"
18
27
  ],
19
- "author": "Simeon Griggs <simeon@sanity.io>",
28
+ "author": "Sanity.io <hello@sanity.io>",
20
29
  "license": "MIT",
21
30
  "dependencies": {
31
+ "@sanity/base": "^2.29.5",
32
+ "@sanity/desk-tool": "^2.29.5",
33
+ "@sanity/state-router": "^2.29.3",
34
+ "@sanity/ui": "^0.34.6",
22
35
  "dlv": "^1.1.3",
23
- "prop-types": "15.7.2"
36
+ "react-query": "^3.19.1",
37
+ "husky": "^7.0.1"
24
38
  },
25
39
  "devDependencies": {
26
- "eslint": "7.28.0",
27
- "eslint-config-prettier": "8.3.0",
40
+ "eslint": "7.31.0",
41
+ "eslint-config-prettier": "^8.3.0",
28
42
  "eslint-config-sanity": "5.1.0",
29
- "eslint-plugin-react": "7.24.0",
30
- "prettier": "2.3.1",
31
- "sanipack": "2.0.1"
43
+ "eslint-plugin-react": "^7.24.0",
44
+ "pinst": "^2.1.6",
45
+ "prettier": "^2.3.2",
46
+ "sanipack": "^2.1.0"
32
47
  },
33
48
  "peerDependencies": {
34
49
  "react": "^17.0.0"
35
50
  },
36
51
  "bugs": {
37
- "url": "https://github.com/SimeonGriggs/sanity-plugin-document-pane/issues"
52
+ "url": "https://github.com/sanity-io/sanity-plugin-document-pane/issues"
38
53
  },
39
- "homepage": "https://github.com/SimeonGriggs/sanity-plugin-document-pane#readme",
54
+ "homepage": "https://github.com/sanity-io/sanity-plugin-document-pane#readme",
40
55
  "prettier": {
41
56
  "semi": false,
42
57
  "printWidth": 100,
@@ -48,8 +63,7 @@
48
63
  "extends": [
49
64
  "sanity",
50
65
  "sanity/react",
51
- "prettier",
52
- "prettier/react"
66
+ "prettier"
53
67
  ]
54
68
  }
55
69
  }
package/src/Debug.tsx ADDED
@@ -0,0 +1,31 @@
1
+ import React from 'react'
2
+ import {Code, Box, Label, Stack} from '@sanity/ui'
3
+
4
+ export default function Debug({query, params}) {
5
+ return (
6
+ <>
7
+ <Stack space={4}>
8
+ <Box><Label>Query</Label></Box>
9
+ <Box>
10
+ <Code>{query}</Code>
11
+ </Box>
12
+ </Stack>
13
+ <Stack space={4}>
14
+ <Box><Label>Params</Label></Box>
15
+ <Box>
16
+ <Code>{JSON.stringify(params)}</Code>
17
+ </Box>
18
+ </Stack>
19
+ </>
20
+ )
21
+ }
22
+
23
+ Debug.propTypes = {
24
+ params: PropTypes.object,
25
+ query: PropTypes.string,
26
+ }
27
+
28
+ Debug.defaultProps = {
29
+ params: {},
30
+ query: ``,
31
+ }
@@ -0,0 +1,93 @@
1
+ import React from 'react'
2
+ import {useQuery} from 'react-query'
3
+ import {Button, Box, Stack, Flex, Spinner} from '@sanity/ui'
4
+ import {usePaneRouter} from '@sanity/desk-tool'
5
+ import {RouterContext} from '@sanity/state-router/lib/RouterContext'
6
+ import Preview from 'part:@sanity/base/preview'
7
+ import schema from 'part:@sanity/base/schema'
8
+ import sanityClient from 'part:@sanity/base/client'
9
+
10
+ import Debug from './Debug'
11
+ import Feedback from './Feedback'
12
+
13
+ const client = sanityClient.withConfig({apiVersion: `2021-05-19`})
14
+
15
+ export default function Documents(props) {
16
+ const {query, params, debug, _rev} = props
17
+
18
+ const {isLoading, error, data} = useQuery(['useDocuments', {props}], () =>
19
+ client.fetch(query, params)
20
+ )
21
+
22
+ const routerContext = React.useContext(RouterContext)
23
+ const {routerPanesState, groupIndex} = usePaneRouter()
24
+
25
+ const openDocumentInSidePane = React.useCallback(
26
+ (id: string, type: string) => {
27
+ const newPane = [
28
+ {
29
+ id: id,
30
+ params: {type},
31
+ },
32
+ ]
33
+ let panes = [routerPanesState[0], routerPanesState[1], newPane]
34
+
35
+ const href = routerContext.resolvePathFromState({panes})
36
+ routerContext.navigateUrl(href)
37
+ },
38
+ [routerContext, routerPanesState, groupIndex]
39
+ )
40
+
41
+ if (!_rev) {
42
+ return (
43
+ <Stack padding={4} space={5}>
44
+ <Feedback>Document must be Published</Feedback>
45
+ {debug && <Debug query={query} params={params} />}
46
+ </Stack>
47
+ )
48
+ }
49
+
50
+ if (isLoading) {
51
+ return (
52
+ <Box padding={4}>
53
+ <Flex justify="center" align="center">
54
+ <Spinner muted />
55
+ </Flex>
56
+ </Box>
57
+ )
58
+ }
59
+
60
+ if (error) {
61
+ return (
62
+ <Stack padding={4} space={5}>
63
+ <Feedback>There was en error performing this query</Feedback>
64
+ {debug && <Debug query={query} params={params} />}
65
+ </Stack>
66
+ )
67
+ }
68
+
69
+ if (!data?.length) {
70
+ return (
71
+ <Stack padding={4} space={5}>
72
+ <Feedback>No Documents found</Feedback>
73
+ {debug && <Debug query={query} params={params} />}
74
+ </Stack>
75
+ )
76
+ }
77
+
78
+ return (
79
+ <Stack padding={2} space={2}>
80
+ {data.map((doc) => (
81
+ <Button
82
+ key={doc._id}
83
+ onClick={() => openDocumentInSidePane(doc._id, doc._type)}
84
+ mode="bleed"
85
+ radius={2}
86
+ padding={2}
87
+ >
88
+ <Preview value={doc} type={schema.get(doc._type)} />
89
+ </Button>
90
+ ))}
91
+ </Stack>
92
+ )
93
+ }
@@ -0,0 +1,25 @@
1
+ import React from 'react'
2
+ import delve from 'dlv'
3
+ import {QueryClient, QueryClientProvider} from 'react-query'
4
+
5
+ import Documents from './Documents'
6
+
7
+ const queryClient = new QueryClient()
8
+
9
+ export default function DocumentsPane({document: sanityDocument, options}) {
10
+ const {query, params, useDraft, debug} = options
11
+
12
+ const doc = useDraft ? sanityDocument.displayed : sanityDocument.published
13
+ const {_id, _rev} = doc ?? {}
14
+
15
+ const paramValues = Object.keys(params).reduce(
16
+ (acc, key) => ({...acc, [key]: delve(doc, params[key])}),
17
+ {}
18
+ )
19
+
20
+ return (
21
+ <QueryClientProvider client={queryClient}>
22
+ <Documents _rev={_rev} query={query} params={paramValues} debug={debug} />
23
+ </QueryClientProvider>
24
+ )
25
+ }
@@ -0,0 +1,18 @@
1
+ import React from 'react'
2
+ import {Card, Text} from '@sanity/ui'
3
+ import type {BadgeTone} from '@sanity/ui'
4
+
5
+ type FeedbackProps = {
6
+ children?: React.ReactNode
7
+ tone?: BadgeTone
8
+ }
9
+
10
+ export default function Feedback(props: FeedbackProps) {
11
+ const {children, tone = `caution`} = props
12
+
13
+ return (
14
+ <Card padding={3} radius={2} shadow={1} tone={tone}>
15
+ <Text size={1}>{children}</Text>
16
+ </Card>
17
+ )
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+
2
+ import DocumentsPane from "./DocumentsPane";
3
+
4
+ export default DocumentsPane
@@ -1,18 +0,0 @@
1
- @import "part:@sanity/base/theme/variables-style";
2
-
3
- .root {
4
- /* */
5
- }
6
-
7
- .noDocs {
8
- padding: var(--medium-padding);
9
- }
10
-
11
- .item {
12
- composes: item from "part:@sanity/base/theme/layout/selectable-style";
13
- display: block;
14
- text-decoration: inherit;
15
- outline: none;
16
- margin: 0;
17
- padding: var(--small-padding) var(--medium-padding);
18
- }
package/lib/lib/hooks.js DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useDocuments = void 0;
7
-
8
- var _react = require("react");
9
-
10
- var _client = _interopRequireDefault(require("part:@sanity/base/client"));
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
15
-
16
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
17
-
18
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
19
-
20
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
21
-
22
- function _iterableToArrayLimit(arr, i) { var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
23
-
24
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
25
-
26
- var client = _client.default.withConfig({
27
- apiVersion: "2021-05-19"
28
- });
29
-
30
- var useDocuments = function useDocuments() {
31
- var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
32
- var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
33
-
34
- var _useState = (0, _react.useState)([]),
35
- _useState2 = _slicedToArray(_useState, 2),
36
- documents = _useState2[0],
37
- setDocuments = _useState2[1];
38
-
39
- var fetchDocuments = () => {
40
- client.fetch(query, params).then(res => setDocuments(res));
41
- };
42
-
43
- (0, _react.useEffect)(() => {
44
- if (query) {
45
- fetchDocuments();
46
- }
47
-
48
- var subscription = client.observable.listen(query, params).subscribe(() => {
49
- setTimeout(() => {
50
- fetchDocuments();
51
- }, 2500);
52
- });
53
- return () => {
54
- subscription.unsubscribe();
55
- };
56
- }, []);
57
- return documents || [];
58
- };
59
-
60
- exports.useDocuments = useDocuments;
61
- //# sourceMappingURL=hooks.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lib/hooks.js"],"names":["client","sanityClient","withConfig","apiVersion","useDocuments","query","params","documents","setDocuments","fetchDocuments","fetch","then","res","subscription","observable","listen","subscribe","setTimeout","unsubscribe"],"mappings":";;;;;;;AAAA;;AACA;;;;;;;;;;;;;;;;AAEA,IAAMA,MAAM,GAAGC,gBAAaC,UAAb,CAAwB;AAACC,EAAAA,UAAU;AAAX,CAAxB,CAAf;;AAEO,IAAMC,YAAY,GAAG,SAAfA,YAAe,GAA6B;AAAA,MAA5BC,KAA4B;AAAA,MAAhBC,MAAgB,uEAAP,EAAO;;AACvD,kBAAkC,qBAAS,EAAT,CAAlC;AAAA;AAAA,MAAOC,SAAP;AAAA,MAAkBC,YAAlB;;AAEA,MAAMC,cAAc,GAAG,MAAM;AAC3BT,IAAAA,MAAM,CAACU,KAAP,CAAaL,KAAb,EAAoBC,MAApB,EAA4BK,IAA5B,CAAkCC,GAAD,IAASJ,YAAY,CAACI,GAAD,CAAtD;AACD,GAFD;;AAIA,wBAAU,MAAM;AACd,QAAIP,KAAJ,EAAW;AACTI,MAAAA,cAAc;AACf;;AAED,QAAMI,YAAY,GAAGb,MAAM,CAACc,UAAP,CAAkBC,MAAlB,CAAyBV,KAAzB,EAAgCC,MAAhC,EAAwCU,SAAxC,CAAkD,MAAM;AAC3EC,MAAAA,UAAU,CAAC,MAAM;AACfR,QAAAA,cAAc;AACf,OAFS,EAEP,IAFO,CAAV;AAGD,KAJoB,CAArB;AAMA,WAAO,MAAM;AACXI,MAAAA,YAAY,CAACK,WAAb;AACD,KAFD;AAGD,GAdD,EAcG,EAdH;AAgBA,SAAOX,SAAS,IAAI,EAApB;AACD,CAxBM","sourcesContent":["import {useEffect, useState} from 'react'\nimport sanityClient from 'part:@sanity/base/client'\n\nconst client = sanityClient.withConfig({apiVersion: `2021-05-19`})\n\nexport const useDocuments = (query = ``, params = {}) => {\n const [documents, setDocuments] = useState([])\n\n const fetchDocuments = () => {\n client.fetch(query, params).then((res) => setDocuments(res))\n }\n\n useEffect(() => {\n if (query) {\n fetchDocuments()\n }\n\n const subscription = client.observable.listen(query, params).subscribe(() => {\n setTimeout(() => {\n fetchDocuments()\n }, 2500)\n })\n\n return () => {\n subscription.unsubscribe()\n }\n }, [])\n\n return documents || []\n}\n"],"file":"hooks.js"}
@@ -1,60 +0,0 @@
1
- import React from 'react'
2
- import PropTypes from 'prop-types'
3
- import delve from 'dlv'
4
- import {IntentLink} from 'part:@sanity/base/router'
5
- import Preview from 'part:@sanity/base/preview'
6
- import schema from 'part:@sanity/base/schema'
7
-
8
- import {useDocuments} from './lib/hooks'
9
- import styles from './DocumentsPane.module.css'
10
-
11
- const DocumentsPane = ({document: sanityDocument, options}) => {
12
- const {query, params, useDraft} = options
13
- const doc = useDraft ? sanityDocument.displayed : sanityDocument.published
14
-
15
- const paramValues = Object.keys(params).reduce(
16
- (acc, key) => ({...acc, [key]: delve(doc, params[key])}),
17
- {}
18
- )
19
-
20
- const documents = useDocuments(query, paramValues)
21
-
22
- return (
23
- <div className={styles.root}>
24
- {documents.length > 0 ? (
25
- documents.map((doc) => (
26
- <IntentLink
27
- key={doc._id}
28
- className={styles.item}
29
- intent="edit"
30
- params={{id: doc._id, type: doc._type}}
31
- >
32
- <Preview value={doc} type={schema.get(doc._type)} />
33
- </IntentLink>
34
- ))
35
- ) : (
36
- <div className={styles.noDocs}>
37
- No Documents match this query: <code>{query}</code>
38
- </div>
39
- )}
40
- </div>
41
- )
42
- }
43
-
44
- DocumentsPane.propTypes = {
45
- options: PropTypes.shape({
46
- params: PropTypes.string,
47
- query: PropTypes.object,
48
- useDraft: PropTypes.bool,
49
- }),
50
- }
51
-
52
- DocumentsPane.defaultProps = {
53
- options: {
54
- params: {},
55
- query: ``,
56
- useDraft: false,
57
- },
58
- }
59
-
60
- export default DocumentsPane
@@ -1,18 +0,0 @@
1
- @import "part:@sanity/base/theme/variables-style";
2
-
3
- .root {
4
- /* */
5
- }
6
-
7
- .noDocs {
8
- padding: var(--medium-padding);
9
- }
10
-
11
- .item {
12
- composes: item from "part:@sanity/base/theme/layout/selectable-style";
13
- display: block;
14
- text-decoration: inherit;
15
- outline: none;
16
- margin: 0;
17
- padding: var(--small-padding) var(--medium-padding);
18
- }
package/src/lib/hooks.js DELETED
@@ -1,30 +0,0 @@
1
- import {useEffect, useState} from 'react'
2
- import sanityClient from 'part:@sanity/base/client'
3
-
4
- const client = sanityClient.withConfig({apiVersion: `2021-05-19`})
5
-
6
- export const useDocuments = (query = ``, params = {}) => {
7
- const [documents, setDocuments] = useState([])
8
-
9
- const fetchDocuments = () => {
10
- client.fetch(query, params).then((res) => setDocuments(res))
11
- }
12
-
13
- useEffect(() => {
14
- if (query) {
15
- fetchDocuments()
16
- }
17
-
18
- const subscription = client.observable.listen(query, params).subscribe(() => {
19
- setTimeout(() => {
20
- fetchDocuments()
21
- }, 2500)
22
- })
23
-
24
- return () => {
25
- subscription.unsubscribe()
26
- }
27
- }, [])
28
-
29
- return documents || []
30
- }