sanity-plugin-documents-pane 1.0.6 → 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 CHANGED
@@ -20,13 +20,4 @@ function Debug(_ref) {
20
20
  space: 4
21
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
22
  }
23
-
24
- Debug.propTypes = {
25
- params: PropTypes.object,
26
- query: PropTypes.string
27
- };
28
- Debug.defaultProps = {
29
- params: {},
30
- query: ""
31
- };
32
23
  //# sourceMappingURL=Debug.js.map
package/lib/Debug.js.map CHANGED
@@ -1 +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"}
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
@@ -5,76 +5,61 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = Documents;
7
7
 
8
- var _react = _interopRequireDefault(require("react"));
9
-
10
- var _reactQuery = require("react-query");
8
+ var _react = _interopRequireWildcard(require("react"));
11
9
 
12
10
  var _ui = require("@sanity/ui");
13
11
 
14
- var _deskTool = require("@sanity/desk-tool");
12
+ var _paths = require("@sanity/util/paths");
15
13
 
16
- var _RouterContext = require("@sanity/state-router/lib/RouterContext");
14
+ var _deskTool = require("@sanity/desk-tool");
17
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
- var client = _client.default.withConfig({
31
- apiVersion: "2021-05-19"
32
- });
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); }
29
+
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
32
  function Documents(props) {
35
33
  var query = props.query,
36
34
  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);
35
+ debug = props.debug;
48
36
 
49
37
  var _usePaneRouter = (0, _deskTool.usePaneRouter)(),
50
38
  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
39
+ groupIndex = _usePaneRouter.groupIndex,
40
+ handleEditReference = _usePaneRouter.handleEditReference;
41
+
42
+ var _useListeningQuery = (0, _useListeningQuery2.default)(query, params),
43
+ loading = _useListeningQuery.loading,
44
+ error = _useListeningQuery.error,
45
+ data = _useListeningQuery.data;
46
+
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;
52
+ handleEditReference({
53
+ id,
54
+ type,
55
+ // Uncertain that this works as intended
56
+ parentRefPath: parentRefPath ? (0, _paths.fromString)(parentRefPath) : [""],
57
+ // Added this to satisfy TS
58
+ template: type
63
59
  });
64
- routerContext.navigateUrl(href);
65
- }, [routerContext, routerPanesState, groupIndex]);
60
+ }, [routerPanesState, groupIndex, handleEditReference]);
66
61
 
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) {
62
+ if (loading) {
78
63
  return /*#__PURE__*/_react.default.createElement(_ui.Box, {
79
64
  padding: 4
80
65
  }, /*#__PURE__*/_react.default.createElement(_ui.Flex, {
@@ -107,13 +92,12 @@ function Documents(props) {
107
92
 
108
93
  return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
109
94
  padding: 2,
110
- space: 2
95
+ space: 1
111
96
  }, data.map(doc => /*#__PURE__*/_react.default.createElement(_ui.Button, {
112
97
  key: doc._id,
113
- onClick: () => openDocumentInSidePane(doc._id, doc._type),
114
- mode: "bleed",
115
- radius: 2,
116
- padding: 2
98
+ onClick: () => handleClick(doc._id, doc._type),
99
+ padding: 2,
100
+ mode: "bleed"
117
101
  }, /*#__PURE__*/_react.default.createElement(_preview.default, {
118
102
  value: doc,
119
103
  type: _schema.default.get(doc._type)
@@ -1 +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","doc","_id","_type","schema","get"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AACA;;AAEA;;AACA;;;;AAEE,IAAMA,MAAM,GA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C,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"}
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,EAAkBP,KAAlB,EAAyBC,MAAzB,CAA/B;EAAA,IAAOO,OAAP,sBAAOA,OAAP;EAAA,IAAgBC,KAAhB,sBAAgBA,KAAhB;EAAA,IAAuBC,IAAvB,sBAAuBA,IAAvB;;EAEA,IAAMC,WAAW,GAAG,IAAAC,kBAAA,EAClB,CAACC,EAAD,EAAKC,IAAL,KAAc;IAAA;;IACZ,IAAMC,WAAW,GAAG,sBAAAX,gBAAgB,CAACC,UAAU,GAAG,CAAd,CA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A,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,10 +9,14 @@ var _react = _interopRequireDefault(require("react"));
9
9
 
10
10
  var _dlv = _interopRequireDefault(require("dlv"));
11
11
 
12
- var _reactQuery = require("react-query");
12
+ var _ui = require("@sanity/ui");
13
13
 
14
14
  var _Documents = _interopRequireDefault(require("./Documents"));
15
15
 
16
+ var _Feedback = _interopRequireDefault(require("./Feedback"));
17
+
18
+ var _Debug = _interopRequireDefault(require("./Debug"));
19
+
16
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
21
 
18
22
  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; }
@@ -21,31 +25,36 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
21
25
 
22
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; }
23
27
 
24
- var queryClient = new _reactQuery.QueryClient();
25
-
26
- function DocumentsPane(_ref) {
27
- var sanityDocument = _ref.document,
28
- options = _ref.options;
28
+ function DocumentsPane(props) {
29
+ var sanityDocument = props.document,
30
+ options = props.options;
29
31
  var query = options.query,
30
32
  params = options.params,
31
33
  useDraft = options.useDraft,
32
34
  debug = options.debug;
33
35
  var doc = useDraft ? sanityDocument.displayed : sanityDocument.published;
34
36
 
35
- var _ref2 = doc !== null && doc !== void 0 ? doc : {},
36
- _id = _ref2._id,
37
- _rev = _ref2._rev;
37
+ var _ref = doc !== null && doc !== void 0 ? doc : {},
38
+ _rev = _ref._rev;
38
39
 
39
40
  var paramValues = Object.keys(params).reduce((acc, key) => _objectSpread(_objectSpread({}, acc), {}, {
40
41
  [key]: (0, _dlv.default)(doc, params[key])
41
42
  }), {});
42
- return /*#__PURE__*/_react.default.createElement(_reactQuery.QueryClientProvider, {
43
- client: queryClient
44
- }, /*#__PURE__*/_react.default.createElement(_Documents.default, {
45
- _rev: _rev,
43
+
44
+ if (!_rev) {
45
+ return /*#__PURE__*/_react.default.createElement(_ui.Stack, {
46
+ padding: 4,
47
+ space: 5
48
+ }, /*#__PURE__*/_react.default.createElement(_Feedback.default, null, "Document must be Published to have References"), debug && /*#__PURE__*/_react.default.createElement(_Debug.default, {
49
+ query: query,
50
+ params: params
51
+ }));
52
+ }
53
+
54
+ return /*#__PURE__*/_react.default.createElement(_Documents.default, {
46
55
  query: query,
47
56
  params: paramValues,
48
57
  debug: debug
49
- }));
58
+ });
50
59
  }
51
60
  //# sourceMappingURL=DocumentsPane.js.map
@@ -1 +1 @@
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"}
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"}
@@ -1 +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"}
1
+ {"version":3,"file":"Feedback.js","names":["Feedback","props","children","tone"],"sources":["../src/Feedback.tsx"],"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"],"mappings":";;;;;;;AAAA;;AACA;;;;AAQe,SAASA,QAAT,CAAkBC,KAAlB,EAAwC;EACrD,IAAOC,QAAP,GAAqCD,KAArC,CAAOC,QAAP;EAAA,kBAAqCD,KAArC,CAAiBE,IAAjB;EAAA,IAAiBA,IAAjB;EAEA,oBACE,6BAAC,QAAD;IAAM,OAAO,EAAE,CAAf;IAAkB,MAAM,EAAE,CAA1B;IAA6B,MAAM,EAAE,CAArC;IAAwC,IAAI,EAAEA;EAA9C,gBACE,6BAAC,QAAD;IAAM,IAAI,EAAE;EAAZ,GAAgBD,QAAhB,CADF,CADF;AAKD"}
@@ -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,"sources":["../src/index.js"],"names":["DocumentsPane"],"mappings":";;;;;;;AACA;;;;eAEeA,sB","sourcesContent":["\nimport DocumentsPane from \"./DocumentsPane\";\n\nexport default DocumentsPane\n"],"file":"index.js"}
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.6",
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,42 +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",
32
+ "@sanity/ui": "^0.37.12",
35
33
  "dlv": "^1.1.3",
36
- "react-query": "^3.19.1",
37
- "husky": "^7.0.1"
38
- },
39
- "devDependencies": {
40
- "eslint": "7.31.0",
41
- "eslint-config-prettier": "^8.3.0",
42
- "eslint-config-sanity": "5.1.0",
43
- "eslint-plugin-react": "^7.24.0",
44
- "pinst": "^2.1.6",
45
- "prettier": "^2.3.2",
46
- "sanipack": "^2.1.0"
34
+ "react-fast-compare": "^3.2.0",
35
+ "rxjs": "^6.5.6"
47
36
  },
48
37
  "peerDependencies": {
49
- "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"
50
55
  },
51
56
  "bugs": {
52
57
  "url": "https://github.com/sanity-io/sanity-plugin-document-pane/issues"
53
58
  },
54
- "homepage": "https://github.com/sanity-io/sanity-plugin-document-pane#readme",
55
- "prettier": {
56
- "semi": false,
57
- "printWidth": 100,
58
- "bracketSpacing": false,
59
- "singleQuote": true
60
- },
61
- "eslintConfig": {
62
- "parser": "sanipack/babel/eslint-parser",
63
- "extends": [
64
- "sanity",
65
- "sanity/react",
66
- "prettier"
67
- ]
68
- }
59
+ "homepage": "https://github.com/sanity-io/sanity-plugin-document-pane#readme"
69
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>
@@ -19,13 +23,3 @@ export default function Debug({query, params}) {
19
23
  </>
20
24
  )
21
25
  }
22
-
23
- Debug.propTypes = {
24
- params: PropTypes.object,
25
- query: PropTypes.string,
26
- }
27
-
28
- Debug.defaultProps = {
29
- params: {},
30
- query: ``,
31
- }
package/src/Documents.tsx CHANGED
@@ -1,53 +1,44 @@
1
- import React from 'react'
2
- import {useQuery} from 'react-query'
3
- import {Button, Box, Stack, Flex, Spinner} from '@sanity/ui'
1
+ import React, {useCallback} from 'react'
2
+ import {Box, Button, Stack, Flex, Spinner} from '@sanity/ui'
3
+ import {fromString as pathFromString} from '@sanity/util/paths'
4
4
  import {usePaneRouter} from '@sanity/desk-tool'
5
- import {RouterContext} from '@sanity/state-router/lib/RouterContext'
6
5
  import Preview from 'part:@sanity/base/preview'
7
6
  import schema from 'part:@sanity/base/schema'
8
- import sanityClient from 'part:@sanity/base/client'
9
7
 
10
8
  import Debug from './Debug'
11
9
  import Feedback from './Feedback'
10
+ import useListeningQuery from './hooks/useListeningQuery'
12
11
 
13
- const client = sanityClient.withConfig({apiVersion: `2021-05-19`})
14
-
15
- export default function Documents(props) {
16
- const {query, params, debug, _rev} = props
12
+ type DocumentsProps = {
13
+ query: string
14
+ params: {[key: string]: string}
15
+ debug: boolean
16
+ }
17
17
 
18
- const {isLoading, error, data} = useQuery(['useDocuments', {props}], () =>
19
- client.fetch(query, params)
20
- )
18
+ export default function Documents(props: DocumentsProps) {
19
+ const {query, params, debug} = props
20
+ const {routerPanesState, groupIndex, handleEditReference} = usePaneRouter()
21
21
 
22
- const routerContext = React.useContext(RouterContext)
23
- const {routerPanesState, groupIndex} = usePaneRouter()
22
+ const {loading, error, data} = useListeningQuery(query, params)
24
23
 
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]
24
+ const handleClick = useCallback(
25
+ (id, type) => {
26
+ const childParams = routerPanesState[groupIndex + 1]?.[0].params || {}
27
+ const {parentRefPath} = childParams
34
28
 
35
- const href = routerContext.resolvePathFromState({panes})
36
- routerContext.navigateUrl(href)
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
37
  },
38
- [routerContext, routerPanesState, groupIndex]
38
+ [routerPanesState, groupIndex, handleEditReference]
39
39
  )
40
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) {
41
+ if (loading) {
51
42
  return (
52
43
  <Box padding={4}>
53
44
  <Flex justify="center" align="center">
@@ -76,14 +67,13 @@ export default function Documents(props) {
76
67
  }
77
68
 
78
69
  return (
79
- <Stack padding={2} space={2}>
70
+ <Stack padding={2} space={1}>
80
71
  {data.map((doc) => (
81
72
  <Button
82
73
  key={doc._id}
83
- onClick={() => openDocumentInSidePane(doc._id, doc._type)}
84
- mode="bleed"
85
- radius={2}
74
+ onClick={() => handleClick(doc._id, doc._type)}
86
75
  padding={2}
76
+ mode="bleed"
87
77
  >
88
78
  <Preview value={doc} type={schema.get(doc._type)} />
89
79
  </Button>
@@ -1,25 +1,44 @@
1
1
  import React from 'react'
2
2
  import delve from 'dlv'
3
- import {QueryClient, QueryClientProvider} from 'react-query'
3
+ import {Stack} from '@sanity/ui'
4
+ import {SanityDocument} from '@sanity/client'
4
5
 
5
6
  import Documents from './Documents'
7
+ import Feedback from './Feedback'
8
+ import Debug from './Debug'
6
9
 
7
- 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
+ }
8
21
 
9
- export default function DocumentsPane({document: sanityDocument, options}) {
22
+ export default function DocumentsPane(props: DocumentsPaneProps) {
23
+ const {document: sanityDocument, options} = props
10
24
  const {query, params, useDraft, debug} = options
11
25
 
12
26
  const doc = useDraft ? sanityDocument.displayed : sanityDocument.published
13
- const {_id, _rev} = doc ?? {}
27
+ const {_rev} = doc ?? {}
14
28
 
15
29
  const paramValues = Object.keys(params).reduce(
16
30
  (acc, key) => ({...acc, [key]: delve(doc, params[key])}),
17
31
  {}
18
32
  )
19
33
 
20
- return (
21
- <QueryClientProvider client={queryClient}>
22
- <Documents _rev={_rev} query={query} params={paramValues} debug={debug} />
23
- </QueryClientProvider>
24
- )
34
+ if (!_rev) {
35
+ return (
36
+ <Stack padding={4} space={5}>
37
+ <Feedback>Document must be Published to have References</Feedback>
38
+ {debug && <Debug query={query} params={params} />}
39
+ </Stack>
40
+ )
41
+ }
42
+
43
+ return <Documents query={query} params={paramValues} debug={debug} />
25
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