sanity-plugin-documents-pane 1.0.8 → 1.0.9

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/lib/Debug.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Debug.js","names":["Debug","query","params","JSON","stringify"],"sources":["../src/Debug.tsx"],"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"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEe,SAASA,KAAT,OAAgC;EAAA,IAAhBC,KAAgB,QAAhBA,KAAgB;EAAA,IAATC,MAAS,QAATA,MAAS;EAC7C,oBACE,yEACE,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,6BAAC,OAAD,qBAAK,6BAAC,SAAD,gBAAL,CADF,eAEE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOD,KAAP,CADF,CAFF,CADF,eAOE,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,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"}
1
+ {"version":3,"file":"Debug.js","names":["Debug","query","params","JSON","stringify"],"sources":["../src/Debug.tsx"],"sourcesContent":["import React from 'react'\nimport {Code, Box, Label, Stack} from '@sanity/ui'\n\nexport default function Debug({query, params}: {query: string; params: {[key: string]: string}}) {\n return (\n <>\n <Stack space={4}>\n <Box>\n <Label>Query</Label>\n </Box>\n <Box>\n <Code>{query}</Code>\n </Box>\n </Stack>\n <Stack space={4}>\n <Box>\n <Label>Params</Label>\n </Box>\n <Box>\n <Code>{JSON.stringify(params)}</Code>\n </Box>\n </Stack>\n </>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;;;AAEe,SAASA,KAAT,OAAkF;EAAA,IAAlEC,KAAkE,QAAlEA,KAAkE;EAAA,IAA3DC,MAA2D,QAA3DA,MAA2D;EAC/F,oBACE,yEACE,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,6BAAC,OAAD,qBACE,6BAAC,SAAD,gBADF,CADF,eAIE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOD,KAAP,CADF,CAJF,CADF,eASE,6BAAC,SAAD;IAAO,KAAK,EAAE;EAAd,gBACE,6BAAC,OAAD,qBACE,6BAAC,SAAD,iBADF,CADF,eAIE,6BAAC,OAAD,qBACE,6BAAC,QAAD,QAAOE,IAAI,CAACC,SAAL,CAAeF,MAAf,CAAP,CADF,CAJF,CATF,CADF;AAoBD"}
package/lib/Documents.js CHANGED
@@ -7,57 +7,48 @@ exports.default = Documents;
7
7
 
8
8
  var _react = _interopRequireWildcard(require("react"));
9
9
 
10
- var _reactQuery = require("react-query");
11
-
12
10
  var _ui = require("@sanity/ui");
13
11
 
14
- var _deskTool = require("@sanity/desk-tool");
15
-
16
12
  var _paths = require("@sanity/util/paths");
17
13
 
14
+ var _deskTool = require("@sanity/desk-tool");
15
+
18
16
  var _preview = _interopRequireDefault(require("part:@sanity/base/preview"));
19
17
 
20
18
  var _schema = _interopRequireDefault(require("part:@sanity/base/schema"));
21
19
 
22
- var _client = _interopRequireDefault(require("part:@sanity/base/client"));
23
-
24
20
  var _Debug = _interopRequireDefault(require("./Debug"));
25
21
 
26
22
  var _Feedback = _interopRequireDefault(require("./Feedback"));
27
23
 
24
+ var _useListeningQuery2 = _interopRequireDefault(require("./hooks/useListeningQuery"));
25
+
28
26
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
27
 
30
28
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
31
29
 
32
30
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
33
31
 
34
- // import {RouterContext} from '@sanity/state-router/lib/RouterContext'
35
- var client = _client.default.withConfig({
36
- apiVersion: "2021-05-19"
37
- });
38
-
39
32
  function Documents(props) {
40
- var _routerPanesState;
41
-
42
33
  var query = props.query,
43
34
  params = props.params,
44
35
  debug = props.debug;
45
36
 
46
- var _useQuery = (0, _reactQuery.useQuery)(['useDocuments', {
47
- props
48
- }], () => client.fetch(query, params)),
49
- isLoading = _useQuery.isLoading,
50
- error = _useQuery.error,
51
- data = _useQuery.data;
52
-
53
37
  var _usePaneRouter = (0, _deskTool.usePaneRouter)(),
54
38
  routerPanesState = _usePaneRouter.routerPanesState,
55
39
  groupIndex = _usePaneRouter.groupIndex,
56
40
  handleEditReference = _usePaneRouter.handleEditReference;
57
41
 
58
- var childParams = ((_routerPanesState = routerPanesState[groupIndex + 1]) === null || _routerPanesState === void 0 ? void 0 : _routerPanesState[0].params) || {};
59
- var parentRefPath = childParams.parentRefPath;
42
+ var _useListeningQuery = (0, _useListeningQuery2.default)(query, params),
43
+ loading = _useListeningQuery.loading,
44
+ error = _useListeningQuery.error,
45
+ data = _useListeningQuery.data;
46
+
60
47
  var handleClick = (0, _react.useCallback)((id, type) => {
48
+ var _routerPanesState;
49
+
50
+ var childParams = ((_routerPanesState = routerPanesState[groupIndex + 1]) === null || _routerPanesState === void 0 ? void 0 : _routerPanesState[0].params) || {};
51
+ var parentRefPath = childParams.parentRefPath;
61
52
  handleEditReference({
62
53
  id,
63
54
  type,
@@ -66,9 +57,9 @@ function Documents(props) {
66
57
  // Added this to satisfy TS
67
58
  template: type
68
59
  });
69
- }, [routerPanesState]);
60
+ }, [routerPanesState, groupIndex, handleEditReference]);
70
61
 
71
- if (isLoading) {
62
+ if (loading) {
72
63
  return /*#__PURE__*/_react.default.createElement(_ui.Box, {
73
64
  padding: 4
74
65
  }, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
@@ -103,6 +94,7 @@ function Documents(props) {
103
94
  padding: 2,
104
95
  space: 1
105
96
  }, data.map(doc => /*#__PURE__*/_react.default.createElement(_ui.Button, {
97
+ key: doc._id,
106
98
  onClick: () => handleClick(doc._id, doc._type),
107
99
  padding: 2,
108
100
  mode: "bleed"
@@ -1 +1 @@
1
- {"version":3,"file":"Documents.js","names":["client","sanityClient","withConfig","apiVersion","Documents","props","query","params","debug","fetch","isLoading","error","data","routerPanesState","groupIndex","handleEditReference","childParams","parentRefPath","handleClick","id","type","template","length","map","doc","_id","_type","schema","get"],"sources":["../src/Documents.tsx"],"sourcesContent":["import React, {useCallback} from 'react'\nimport {useQuery} from 'react-query'\nimport {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport {fromString as pathFromString} from '@sanity/util/paths'\n// import {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\nconst client = sanityClient.withConfig({apiVersion: `2021-05-19`})\n\nexport default function Documents(props) {\n const {query, params, debug} = props\n\n const {isLoading, error, data} = useQuery(['useDocuments', {props}], () =>\n client.fetch(query, params)\n )\n\n const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()\n const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}\n const {parentRefPath} = childParams\n\n const handleClick = useCallback((id, type) => {\n handleEditReference({\n id,\n type,\n // Uncertain that this works as intended\n parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],\n // Added this to satisfy TS\n template: type,\n })\n }, [routerPanesState])\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={1}>\n {data.map((doc) => (\n <Button onClick={() => handleClick(doc._id, doc._type)} padding={2} mode=\"bleed\">\n <Preview value={doc} type={schema.get(doc._type)} />\n </Button>\n ))}\n </Stack>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AAEA;;AACA;;AACA;;AAEA;;AACA;;;;;;;;AANA;AAQA,IAAMA,MAAM,GAAGC,gBAAaC,UAAb,CAAwB;EAACC,UAAU;AAAX,CAAxB,CAAf;;AAEe,SAASC,SAAT,CAAmBC,KAAnB,EAA0B;EAAA;;EACvC,IAAOC,KAAP,GAA+BD,KAA/B,CAAOC,KAAP;EAAA,IAAcC,MAAd,GAA+BF,KAA/B,CAAcE,MAAd;EAAA,IAAsBC,KAAtB,GAA+BH,KAA/B,CAAsBG,KAAtB;;EAEA,gBA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A7B,EAFZ,CADF;EAMD;;EAED,IAAI,EAACK,IAAD,aAACA,IAAD,eAACA,IAAI,CAAEU,MAAP,CAAJ,EAAmB;IACjB,oBACE,6BAAC,SAAD;MAAO,OAAO,EAAE,CAAhB;MAAmB,KAAK,EAAE;IAA1B,gBACE,6BAAC,iBAAD,6BADF,EAEGd,KAAK,iBAAI,6BAAC,cAAD;MAAO,KAAK,EAAEF,KAAd;MAAqB,MAAM,EAAEC;IAA7B,EAFZ,CADF;EAMD;;EAED,oBACE,6BAAC,SAAD;IAAO,OAAO,EAAE,CAAhB;IAAmB,KAAK,EAAE;EAA1B,GACGK,IAAI,CAACW,GAAL,CAAUC,GAAD,iBACR,6BAAC,UAAD;IAAQ,OAAO,EAAE,MAAMN,WAAW,CAACM,GAAG,CAACC,GAAL,EAAUD,GAAG,CAACE,KAAd,CAAlC;IAAwD,OAAO,EAAE,CAAjE;IAAoE,IAAI,EAAC;EAAzE,gBACE,6BAAC,gBAAD;IAAS,KAAK,EAAEF,GAAhB;IAAqB,IAAI,EAAEG,gBAAOC,GAAP,CAAWJ,GAAG,CAACE,KAAf;EAA3B,EADF,CADD,CADH,CADF;AASD"}
1
+ {"version":3,"file":"Documents.js","names":["Documents","props","query","params","debug","usePaneRouter","routerPanesState","groupIndex","handleEditReference","useListeningQuery","loading","error","data","handleClick","useCallback","id","type","childParams","parentRefPath","pathFromString","template","length","map","doc","_id","_type","schema","get"],"sources":["../src/Documents.tsx"],"sourcesContent":["import React, {useCallback} from 'react'\nimport {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'\nimport {fromString as pathFromString} from '@sanity/util/paths'\nimport {usePaneRouter} from '@sanity/desk-tool'\nimport Preview from 'part:@sanity/base/preview'\nimport schema from 'part:@sanity/base/schema'\n\nimport Debug from './Debug'\nimport Feedback from './Feedback'\nimport useListeningQuery from './hooks/useListeningQuery'\n\ntype DocumentsProps = {\n query: string\n params: {[key: string]: string}\n debug: boolean\n}\n\nexport default function Documents(props: DocumentsProps) {\n const {query, params, debug} = props\n const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()\n\n const {loading, error, data} = useListeningQuery(query, params)\n\n const handleClick = useCallback(\n (id, type) => {\n const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}\n const {parentRefPath} = childParams\n\n handleEditReference({\n id,\n type,\n // Uncertain that this works as intended\n parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],\n // Added this to satisfy TS\n template: type,\n })\n },\n [routerPanesState, groupIndex, handleEditReference]\n )\n\n if (loading) {\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={1}>\n {data.map((doc) => (\n <Button\n key={doc._id}\n onClick={() => handleClick(doc._id, doc._type)}\n padding={2}\n mode=\"bleed\"\n >\n <Preview value={doc} type={schema.get(doc._type)} />\n </Button>\n ))}\n </Stack>\n )\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAEA;;AACA;;AACA;;;;;;;;AAQe,SAASA,SAAT,CAAmBC,KAAnB,EAA0C;EACvD,IAAOC,KAAP,GAA+BD,KAA/B,CAAOC,KAAP;EAAA,IAAcC,MAAd,GAA+BF,KAA/B,CAAcE,MAAd;EAAA,IAAsBC,KAAtB,GAA+BH,KAA/B,CAAsBG,KAAtB;;EACA,qBAA4D,IAAAC,uBAAA,GAA5D;EAAA,IAAOC,gBAAP,kBAAOA,gBAAP;EAAA,IAAyBC,UAAzB,kBAAyBA,UAAzB;EAAA,IAAqCC,mBAArC,kBAAqCA,mBAArC;;EAEA,yBAA+B,IAAAC,2BAAA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mB,KAAK,EAAE;IAA1B,gBACE,6BAAC,iBAAD,6BADF,EAEGjB,KAAK,iBAAI,6BAAC,cAAD;MAAO,KAAK,EAAEF,KAAd;MAAqB,MAAM,EAAEC;IAA7B,EAFZ,CADF;EAMD;;EAED,oBACE,6BAAC,SAAD;IAAO,OAAO,EAAE,CAAhB;IAAmB,KAAK,EAAE;EAA1B,GACGS,IAAI,CAACU,GAAL,CAAUC,GAAD,iBACR,6BAAC,UAAD;IACE,GAAG,EAAEA,GAAG,CAACC,GADX;IAEE,OAAO,EAAE,MAAMX,WAAW,CAACU,GAAG,CAACC,GAAL,EAAUD,GAAG,CAACE,KAAd,CAF5B;IAGE,OAAO,EAAE,CAHX;IAIE,IAAI,EAAC;EAJP,gBAME,6BAAC,gBAAD;IAAS,KAAK,EAAEF,GAAhB;IAAqB,IAAI,EAAEG,eAAA,CAAOC,GAAP,CAAWJ,GAAG,CAACE,KAAf;EAA3B,EANF,CADD,CADH,CADF;AAcD"}
@@ -9,8 +9,6 @@ var _react = _interopRequireDefault(require("react"));
9
9
 
10
10
  var _dlv = _interopRequireDefault(require("dlv"));
11
11
 
12
- var _reactQuery = require("react-query");
13
-
14
12
  var _ui = require("@sanity/ui");
15
13
 
16
14
  var _Documents = _interopRequireDefault(require("./Documents"));
@@ -27,19 +25,17 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
27
25
 
28
26
  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; }
29
27
 
30
- var queryClient = new _reactQuery.QueryClient();
31
-
32
- function DocumentsPane(_ref) {
33
- var sanityDocument = _ref.document,
34
- options = _ref.options;
28
+ function DocumentsPane(props) {
29
+ var sanityDocument = props.document,
30
+ options = props.options;
35
31
  var query = options.query,
36
32
  params = options.params,
37
33
  useDraft = options.useDraft,
38
34
  debug = options.debug;
39
35
  var doc = useDraft ? sanityDocument.displayed : sanityDocument.published;
40
36
 
41
- var _ref2 = doc !== null && doc !== void 0 ? doc : {},
42
- _rev = _ref2._rev;
37
+ var _ref = doc !== null && doc !== void 0 ? doc : {},
38
+ _rev = _ref._rev;
43
39
 
44
40
  var paramValues = Object.keys(params).reduce((acc, key) => _objectSpread(_objectSpread({}, acc), {}, {
45
41
  [key]: (0, _dlv.default)(doc, params[key])
@@ -55,12 +51,10 @@ function DocumentsPane(_ref) {
55
51
  }));
56
52
  }
57
53
 
58
- return /*#__PURE__*/_react.default.createElement(_reactQuery.QueryClientProvider, {
59
- client: queryClient
60
- }, /*#__PURE__*/_react.default.createElement(_Documents.default, {
54
+ return /*#__PURE__*/_react.default.createElement(_Documents.default, {
61
55
  query: query,
62
56
  params: paramValues,
63
57
  debug: debug
64
- }));
58
+ });
65
59
  }
66
60
  //# sourceMappingURL=DocumentsPane.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"DocumentsPane.js","names":["queryClient","QueryClient","DocumentsPane","sanityDocument","document","options","query","params","useDraft","debug","doc","displayed","published","_rev","paramValues","Object","keys","reduce","acc","key"],"sources":["../src/DocumentsPane.tsx"],"sourcesContent":["import React from 'react'\nimport delve from 'dlv'\nimport {QueryClient, QueryClientProvider} from 'react-query'\nimport {Stack} from '@sanity/ui'\n\nimport Documents from './Documents'\nimport Feedback from './Feedback'\nimport Debug from './Debug'\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 {_rev} = doc ?? {}\n\n const paramValues = Object.keys(params).reduce(\n (acc, key) => ({...acc, [key]: delve(doc, params[key])}),\n {}\n )\n\n if (!_rev) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>Document must be Published to have References</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n return (\n <QueryClientProvider client={queryClient}>\n <Documents query={query} params={paramValues} debug={debug} />\n </QueryClientProvider>\n )\n}\n"],"mapp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
1
+ {"version":3,"file":"DocumentsPane.js","names":["DocumentsPane","props","sanityDocument","document","options","query","params","useDraft","debug","doc","displayed","published","_rev","paramValues","Object","keys","reduce","acc","key","delve"],"sources":["../src/DocumentsPane.tsx"],"sourcesContent":["import React from 'react'\nimport delve from 'dlv'\nimport {Stack} from '@sanity/ui'\nimport {SanityDocument} from '@sanity/client'\n\nimport Documents from './Documents'\nimport Feedback from './Feedback'\nimport Debug from './Debug'\n\ntype DocumentsPaneOptions = {\n query: string\n params: {[key: string]: string}\n debug: boolean\n useDraft: boolean\n}\n\ntype DocumentsPaneProps = {\n document: SanityDocument\n options: DocumentsPaneOptions\n}\n\nexport default function DocumentsPane(props: DocumentsPaneProps) {\n const {document: sanityDocument, options} = props\n const {query, params, useDraft, debug} = options\n\n const doc = useDraft ? sanityDocument.displayed : sanityDocument.published\n const {_rev} = doc ?? {}\n\n const paramValues = Object.keys(params).reduce(\n (acc, key) => ({...acc, [key]: delve(doc, params[key])}),\n {}\n )\n\n if (!_rev) {\n return (\n <Stack padding={4} space={5}>\n <Feedback>Document must be Published to have References</Feedback>\n {debug && <Debug query={query} params={params} />}\n </Stack>\n )\n }\n\n return <Documents query={query} params={paramValues} debug={debug} />\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAGA;;AACA;;AACA;;;;;;;;;;AAce,SAASA,aAAT,CAAuBC,KAAvB,EAAkD;EAC/D,IAAiBC,cAAjB,GAA4CD,KAA5C,CAAOE,QAAP;EAAA,IAAiCC,OAAjC,GAA4CH,KAA5C,CAAiCG,OAAjC;EACA,IAAOC,KAAP,GAAyCD,OAAzC,CAAOC,KAAP;EAAA,IAAcC,MAAd,GAAyCF,OAAzC,CAAcE,MAAd;EAAA,IAAsBC,QAAtB,GAAyCH,OAAzC,CAAsBG,QAAtB;EAAA,IAAgCC,KAAhC,GAAyCJ,OAAzC,CAAgCI,KAAhC;EAEA,IAAMC,GAAG,GAAGF,QAAQ,GAAGL,cAAc,CAACQ,SAAlB,GAA8BR,cAAc,CAACS,SAAjE;;EACA,WAAeF,GAAf,aAAeA,GAAf,cAAeA,GAAf,GAAsB,EAAtB;EAAA,IAAOG,IAAP,QAAOA,IAAP;;EAEA,IAAMC,WAAW,GAAGC,MAAM,CAACC,IAAP,CAAYT,MAAZ,EAAoBU,MAApB,CAClB,CAACC,GAAD,EAAMC,GAAN,qCAAmBD,GAAnB;IAAwB,CAACC,GAAD,GAAO,IAAAC,YAAA,EAAMV,GAAN,EAAWH,MAAM,CAACY,GAAD,CAAjB;EAA/B,EADkB,EAElB,EAFkB,CAApB;;EAKA,IAAI,CAACN,IAAL,EAAW;IACT,oBACE,6BAAC,SAAD;MAAO,OAAO,EAAE,CAAhB;MAAmB,KAAK,EAAE;IAA1B,gBACE,6BAAC,iBAAD,wDADF,EAEGJ,KAAK,iBAAI,6BAAC,cAAD;MAAO,KAAK,EAAEH,KAAd;MAAqB,MAAM,EAAEC;IAA7B,EAFZ,CADF;EAMD;;EAED,oBAAO,6BAAC,kBAAD;IAAW,KAAK,EAAED,KAAlB;IAAyB,MAAM,EAAEQ,WAAjC;IAA8C,KAAK,EAAEL;EAArD,EAAP;AACD"}
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = useListeningQuery;
7
+
8
+ var _react = require("react");
9
+
10
+ var _document = _interopRequireDefault(require("part:@sanity/base/datastore/document"));
11
+
12
+ var _operators = require("rxjs/operators");
13
+
14
+ var _reactFastCompare = _interopRequireDefault(require("react-fast-compare"));
15
+
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
+
18
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
19
+
20
+ 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."); }
21
+
22
+ 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); }
23
+
24
+ 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; }
25
+
26
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : 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; }
27
+
28
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
29
+
30
+ var DEFAULT_PARAMS = {};
31
+ var DEFAULT_OPTIONS = {
32
+ apiVersion: "v2022-05-09"
33
+ };
34
+
35
+ function useListeningQuery(query) {
36
+ var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_PARAMS;
37
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_OPTIONS;
38
+
39
+ var _useState = (0, _react.useState)(true),
40
+ _useState2 = _slicedToArray(_useState, 2),
41
+ loading = _useState2[0],
42
+ setLoading = _useState2[1];
43
+
44
+ var _useState3 = (0, _react.useState)(false),
45
+ _useState4 = _slicedToArray(_useState3, 2),
46
+ error = _useState4[0],
47
+ setError = _useState4[1];
48
+
49
+ var _useState5 = (0, _react.useState)(null),
50
+ _useState6 = _slicedToArray(_useState5, 2),
51
+ data = _useState6[0],
52
+ setData = _useState6[1];
53
+
54
+ var subscription = (0, _react.useRef)(null);
55
+ (0, _react.useEffect)(() => {
56
+ if (query) {
57
+ subscription.current = _document.default.listenQuery(query, params, options).pipe((0, _operators.distinctUntilChanged)(_reactFastCompare.default), (0, _operators.catchError)(err => {
58
+ console.error(err);
59
+ setError(err);
60
+ setLoading(false);
61
+ setData(null);
62
+ return err;
63
+ })).subscribe(documents => {
64
+ setData(current => (0, _reactFastCompare.default)(current, documents) ? current : documents);
65
+ setLoading(false);
66
+ setError(false);
67
+ });
68
+ }
69
+
70
+ return () => {
71
+ return subscription.current ? subscription.current.unsubscribe() : undefined;
72
+ };
73
+ }, [query, params, options]);
74
+ return {
75
+ loading,
76
+ error,
77
+ data
78
+ };
79
+ }
80
+ //# sourceMappingURL=useListeningQuery.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useListeningQuery.js","names":["DEFAULT_PARAMS","DEFAULT_OPTIONS","apiVersion","useListeningQuery","query","params","options","useState","loading","setLoading","error","setError","data","setData","subscription","useRef","useEffect","current","documentStore","listenQuery","pipe","distinctUntilChanged","isEqual","catchError","err","console","subscribe","documents","unsubscribe","undefined"],"sources":["../../src/hooks/useListeningQuery.ts"],"sourcesContent":["import React, {useEffect, useState, useRef} from 'react'\nimport documentStore from 'part:@sanity/base/datastore/document'\nimport {catchError, distinctUntilChanged} from 'rxjs/operators'\nimport isEqual from 'react-fast-compare'\n\ntype Params = Record<string, string | number | boolean | string[]>\n\ninterface ListenQueryOptions {\n tag?: string\n apiVersion?: string\n}\n\ntype ReturnShape = {\n loading: boolean\n error: boolean\n data: any\n}\n\ntype Observable = {\n unsubscribe: () => void\n}\n\nconst DEFAULT_PARAMS = {}\nconst DEFAULT_OPTIONS = {apiVersion: `v2022-05-09`}\n\nexport default function useListeningQuery(\n query: string,\n params: Params = DEFAULT_PARAMS,\n options: ListenQueryOptions = DEFAULT_OPTIONS\n): ReturnShape {\n const [loading, setLoading] = useState(true)\n const [error, setError] = useState(false)\n const [data, setData] = useState(null)\n const subscription = useRef<null | Observable>(null)\n\n useEffect(() => {\n if (query) {\n subscription.current = documentStore\n .listenQuery(query, params, options)\n .pipe(\n distinctUntilChanged(isEqual),\n catchError((err) => {\n console.error(err)\n setError(err)\n setLoading(false)\n setData(null)\n\n return err\n })\n )\n .subscribe((documents) => {\n setData((current) => (isEqual(current, documents) ? current : documents))\n setLoading(false)\n setError(false)\n })\n }\n\n return () => {\n return subscription.current ? subscription.current.unsubscribe() : undefined\n }\n }, [query, params, options])\n\n return {loading, error, data}\n}\n"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;AAmBA,IAAMA,cAAc,GAAG,EAAvB;AACA,IAAMC,eAAe,GAAG;EAACC,UAAU;AAAX,CAAxB;;AAEe,SAASC,iBAAT,CACbC,KADa,EAIA;EAAA,IAFbC,MAEa,uEAFIL,cAEJ;EAAA,IADbM,OACa,uEADiBL,eACjB;;EACb,gBAA8B,IAAAM,eAAA,EAAS,IAAT,CAA9B;EAAA;EAAA,IAAOC,OAAP;EAAA,IAAgBC,UAAhB;;EACA,iBAA0B,IAAAF,eAAA,EAAS,KAAT,CAA1B;EAAA;EAAA,IAAOG,KAAP;EAAA,IAAcC,QAAd;;EACA,iBAAwB,IAAAJ,eAAA,EAAS,IAAT,CAAxB;EAAA;EAAA,IAAOK,IAAP;EAAA,IAAaC,OAAb;;EACA,IAAMC,YAAY,GAAG,IAAAC,aAAA,EAA0B,IAA1B,CAArB;EAEA,IAAAC,gBAAA,EAAU,MAAM;IACd,IAAIZ,KAAJ,EAAW;MACTU,YAAY,CAACG,OAAb,GAAuBC,iBAAA,CACpBC,WADoB,CACRf,KADQ,EACDC,MADC,EACOC,OADP,EAEpBc,IAFoB,CAGnB,IAAAC,+BAAA,EAAqBC,yBAArB,CAHmB,EAInB,IAAAC,qBAAA,EAAYC,GAAD,IAAS;QAClBC,OAAO,CAACf,KAAR,CAAcc,GAAd;QACAb,QAAQ,CAACa,GAAD,CAAR;QACAf,UAAU,CAAC,KAAD,CAAV;QACAI,OAAO,CAAC,IAAD,CAAP;QAEA,OAAOW,GAAP;MACD,CAPD,CAJmB,EAapBE,SAboB,CAaTC,SAAD,IAAe;QACxBd,OAAO,CAAEI,OAAD,IAAc,IAAAK,yBAAA,EAAQL,OAAR,EAAiBU,SAAjB,IAA8BV,OAA9B,GAAwCU,SAAvD,CAAP;QACAlB,UAAU,CAAC,KAAD,CAAV;QACAE,QAAQ,CAAC,KAAD,CAAR;MACD,CAjBoB,CAAvB;IAkBD;;IAED,OAAO,MAAM;MACX,OAAOG,YAAY,CAACG,OAAb,GAAuBH,YAAY,CAACG,OAAb,CAAqBW,WAArB,EAAvB,GAA4DC,SAAnE;IACD,CAFD;EAGD,CAzBD,EAyBG,CAACzB,KAAD,EAAQC,MAAR,EAAgBC,OAAhB,CAzBH;EA2BA,OAAO;IAACE,OAAD;IAAUE,KAAV;IAAiBE;EAAjB,CAAP;AACD"}
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["DocumentsPane"],"sources":["../src/index.js"],"sourcesContent":["\nimport DocumentsPane from \"./DocumentsPane\";\n\nexport default DocumentsPane\n"],"mappings":";;;;;;;AACA;;;;eAEeA,sB"}
1
+ {"version":3,"file":"index.js","names":["DocumentsPane"],"sources":["../src/index.ts"],"sourcesContent":["import DocumentsPane from './DocumentsPane'\n\nexport default DocumentsPane\n"],"mappings":";;;;;;;AAAA;;;;eAEeA,sB"}
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "sanity-plugin-documents-pane",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Displays the results of a GROQ query in a View Pane",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
7
7
  "build": "sanipack build",
8
+ "verify": "sanipack verify",
8
9
  "watch": "sanipack build --watch",
9
10
  "_postinstall": "husky install",
10
11
  "prepublishOnly": "pinst --disable && sanipack build && sanipack verify",
@@ -28,43 +29,32 @@
28
29
  "author": "Sanity.io <hello@sanity.io>",
29
30
  "license": "MIT",
30
31
  "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",
35
- "@sanity/util": "^2.29.5",
32
+ "@sanity/ui": "^0.37.12",
36
33
  "dlv": "^1.1.3",
37
- "husky": "^7.0.1",
38
- "react-query": "^3.19.1"
39
- },
40
- "devDependencies": {
41
- "eslint": "7.31.0",
42
- "eslint-config-prettier": "^8.3.0",
43
- "eslint-config-sanity": "5.1.0",
44
- "eslint-plugin-react": "^7.24.0",
45
- "pinst": "^2.1.6",
46
- "prettier": "^2.3.2",
47
- "sanipack": "^2.1.0"
34
+ "react-fast-compare": "^3.2.0",
35
+ "rxjs": "^6.5.6"
48
36
  },
49
37
  "peerDependencies": {
50
- "react": "^17.0.0"
38
+ "@sanity/base": "^2.30.1",
39
+ "@sanity/desk-tool": "^2.30.1",
40
+ "@sanity/util": "^2.29.5",
41
+ "react": "^16.0.0 || ^17.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@sanity/eslint-config-studio": "^2.0.0",
45
+ "eslint": "8.19.0",
46
+ "eslint-config-prettier": "^8.5.0",
47
+ "eslint-config-sanity": "6.0.0",
48
+ "eslint-plugin-prettier": "^4.2.1",
49
+ "eslint-plugin-react": "^7.30.1",
50
+ "husky": "^8.0.1",
51
+ "pinst": "^3.0.0",
52
+ "prettier": "^2.7.1",
53
+ "sanipack": "^2.1.0",
54
+ "typescript": "^4.7.4"
51
55
  },
52
56
  "bugs": {
53
57
  "url": "https://github.com/sanity-io/sanity-plugin-document-pane/issues"
54
58
  },
55
- "homepage": "https://github.com/sanity-io/sanity-plugin-document-pane#readme",
56
- "prettier": {
57
- "semi": false,
58
- "printWidth": 100,
59
- "bracketSpacing": false,
60
- "singleQuote": true
61
- },
62
- "eslintConfig": {
63
- "parser": "sanipack/babel/eslint-parser",
64
- "extends": [
65
- "sanity",
66
- "sanity/react",
67
- "prettier"
68
- ]
69
- }
59
+ "homepage": "https://github.com/sanity-io/sanity-plugin-document-pane#readme"
70
60
  }
package/src/Debug.tsx CHANGED
@@ -1,17 +1,21 @@
1
1
  import React from 'react'
2
2
  import {Code, Box, Label, Stack} from '@sanity/ui'
3
3
 
4
- export default function Debug({query, params}) {
4
+ export default function Debug({query, params}: {query: string; params: {[key: string]: string}}) {
5
5
  return (
6
6
  <>
7
7
  <Stack space={4}>
8
- <Box><Label>Query</Label></Box>
8
+ <Box>
9
+ <Label>Query</Label>
10
+ </Box>
9
11
  <Box>
10
12
  <Code>{query}</Code>
11
13
  </Box>
12
14
  </Stack>
13
15
  <Stack space={4}>
14
- <Box><Label>Params</Label></Box>
16
+ <Box>
17
+ <Label>Params</Label>
18
+ </Box>
15
19
  <Box>
16
20
  <Code>{JSON.stringify(params)}</Code>
17
21
  </Box>
package/src/Documents.tsx CHANGED
@@ -1,41 +1,44 @@
1
1
  import React, {useCallback} from 'react'
2
- import {useQuery} from 'react-query'
3
2
  import {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'
4
- import {usePaneRouter} from '@sanity/desk-tool'
5
3
  import {fromString as pathFromString} from '@sanity/util/paths'
6
- // import {RouterContext} from '@sanity/state-router/lib/RouterContext'
4
+ import {usePaneRouter} from '@sanity/desk-tool'
7
5
  import Preview from 'part:@sanity/base/preview'
8
6
  import schema from 'part:@sanity/base/schema'
9
- import sanityClient from 'part:@sanity/base/client'
10
7
 
11
8
  import Debug from './Debug'
12
9
  import Feedback from './Feedback'
10
+ import useListeningQuery from './hooks/useListeningQuery'
13
11
 
14
- const client = sanityClient.withConfig({apiVersion: `2021-05-19`})
12
+ type DocumentsProps = {
13
+ query: string
14
+ params: {[key: string]: string}
15
+ debug: boolean
16
+ }
15
17
 
16
- export default function Documents(props) {
18
+ export default function Documents(props: DocumentsProps) {
17
19
  const {query, params, debug} = props
20
+ const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()
18
21
 
19
- const {isLoading, error, data} = useQuery(['useDocuments', {props}], () =>
20
- client.fetch(query, params)
21
- )
22
+ const {loading, error, data} = useListeningQuery(query, params)
22
23
 
23
- const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()
24
- const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}
25
- const {parentRefPath} = childParams
24
+ const handleClick = useCallback(
25
+ (id, type) => {
26
+ const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}
27
+ const {parentRefPath} = childParams
26
28
 
27
- const handleClick = useCallback((id, type) => {
28
- handleEditReference({
29
- id,
30
- type,
31
- // Uncertain that this works as intended
32
- parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],
33
- // Added this to satisfy TS
34
- template: type,
35
- })
36
- }, [routerPanesState])
29
+ handleEditReference({
30
+ id,
31
+ type,
32
+ // Uncertain that this works as intended
33
+ parentRefPath: parentRefPath ? pathFromString(parentRefPath) : [``],
34
+ // Added this to satisfy TS
35
+ template: type,
36
+ })
37
+ },
38
+ [routerPanesState, groupIndex, handleEditReference]
39
+ )
37
40
 
38
- if (isLoading) {
41
+ if (loading) {
39
42
  return (
40
43
  <Box padding={4}>
41
44
  <Flex justify="center" align="center">
@@ -66,7 +69,12 @@ export default function Documents(props) {
66
69
  return (
67
70
  <Stack padding={2} space={1}>
68
71
  {data.map((doc) => (
69
- <Button onClick={() => handleClick(doc._id, doc._type)} padding={2} mode="bleed">
72
+ <Button
73
+ key={doc._id}
74
+ onClick={() => handleClick(doc._id, doc._type)}
75
+ padding={2}
76
+ mode="bleed"
77
+ >
70
78
  <Preview value={doc} type={schema.get(doc._type)} />
71
79
  </Button>
72
80
  ))}
@@ -1,15 +1,26 @@
1
1
  import React from 'react'
2
2
  import delve from 'dlv'
3
- import {QueryClient, QueryClientProvider} from 'react-query'
4
3
  import {Stack} from '@sanity/ui'
4
+ import {SanityDocument} from '@sanity/client'
5
5
 
6
6
  import Documents from './Documents'
7
7
  import Feedback from './Feedback'
8
8
  import Debug from './Debug'
9
9
 
10
- const queryClient = new QueryClient()
10
+ type DocumentsPaneOptions = {
11
+ query: string
12
+ params: {[key: string]: string}
13
+ debug: boolean
14
+ useDraft: boolean
15
+ }
16
+
17
+ type DocumentsPaneProps = {
18
+ document: SanityDocument
19
+ options: DocumentsPaneOptions
20
+ }
11
21
 
12
- export default function DocumentsPane({document: sanityDocument, options}) {
22
+ export default function DocumentsPane(props: DocumentsPaneProps) {
23
+ const {document: sanityDocument, options} = props
13
24
  const {query, params, useDraft, debug} = options
14
25
 
15
26
  const doc = useDraft ? sanityDocument.displayed : sanityDocument.published
@@ -29,9 +40,5 @@ export default function DocumentsPane({document: sanityDocument, options}) {
29
40
  )
30
41
  }
31
42
 
32
- return (
33
- <QueryClientProvider client={queryClient}>
34
- <Documents query={query} params={paramValues} debug={debug} />
35
- </QueryClientProvider>
36
- )
43
+ return <Documents query={query} params={paramValues} debug={debug} />
37
44
  }
@@ -0,0 +1,64 @@
1
+ import React, {useEffect, useState, useRef} from 'react'
2
+ import documentStore from 'part:@sanity/base/datastore/document'
3
+ import {catchError, distinctUntilChanged} from 'rxjs/operators'
4
+ import isEqual from 'react-fast-compare'
5
+
6
+ type Params = Record<string, string | number | boolean | string[]>
7
+
8
+ interface ListenQueryOptions {
9
+ tag?: string
10
+ apiVersion?: string
11
+ }
12
+
13
+ type ReturnShape = {
14
+ loading: boolean
15
+ error: boolean
16
+ data: any
17
+ }
18
+
19
+ type Observable = {
20
+ unsubscribe: () => void
21
+ }
22
+
23
+ const DEFAULT_PARAMS = {}
24
+ const DEFAULT_OPTIONS = {apiVersion: `v2022-05-09`}
25
+
26
+ export default function useListeningQuery(
27
+ query: string,
28
+ params: Params = DEFAULT_PARAMS,
29
+ options: ListenQueryOptions = DEFAULT_OPTIONS
30
+ ): ReturnShape {
31
+ const [loading, setLoading] = useState(true)
32
+ const [error, setError] = useState(false)
33
+ const [data, setData] = useState(null)
34
+ const subscription = useRef<null | Observable>(null)
35
+
36
+ useEffect(() => {
37
+ if (query) {
38
+ subscription.current = documentStore
39
+ .listenQuery(query, params, options)
40
+ .pipe(
41
+ distinctUntilChanged(isEqual),
42
+ catchError((err) => {
43
+ console.error(err)
44
+ setError(err)
45
+ setLoading(false)
46
+ setData(null)
47
+
48
+ return err
49
+ })
50
+ )
51
+ .subscribe((documents) => {
52
+ setData((current) => (isEqual(current, documents) ? current : documents))
53
+ setLoading(false)
54
+ setError(false)
55
+ })
56
+ }
57
+
58
+ return () => {
59
+ return subscription.current ? subscription.current.unsubscribe() : undefined
60
+ }
61
+ }, [query, params, options])
62
+
63
+ return {loading, error, data}
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import DocumentsPane from './DocumentsPane'
2
+
3
+ export default DocumentsPane
package/.babelrc DELETED
@@ -1,3 +0,0 @@
1
- {
2
- "extends": "sanipack/babel"
3
- }
package/.eslintignore DELETED
@@ -1 +0,0 @@
1
- lib
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
-
2
- import DocumentsPane from "./DocumentsPane";
3
-
4
- export default DocumentsPane