sanity-plugin-utils 1.6.4 → 1.6.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/lib/index.js CHANGED
@@ -1,26 +1,13 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', {
4
- value: true
5
- });
6
- var React = require('react');
7
- var sanity = require('sanity');
8
- var operators = require('rxjs/operators');
9
- var isEqual = require('react-fast-compare');
10
- var structure = require('sanity/structure');
11
- var router = require('sanity/router');
12
- var jsxRuntime = require('react/jsx-runtime');
13
- var ui = require('@sanity/ui');
14
- var styled = require('styled-components');
15
- var icons = require('@sanity/icons');
16
- function _interopDefaultCompat(e) {
17
- return e && typeof e === 'object' && 'default' in e ? e : {
18
- default: e
19
- };
20
- }
21
- var React__default = /*#__PURE__*/_interopDefaultCompat(React);
22
- var isEqual__default = /*#__PURE__*/_interopDefaultCompat(isEqual);
23
- var styled__default = /*#__PURE__*/_interopDefaultCompat(styled);
1
+ import React, { useMemo, useState, useRef, useEffect } from 'react';
2
+ import { useClient, useDocumentStore, useWorkspace, UserAvatar } from 'sanity';
3
+ import isEqual from 'react-fast-compare';
4
+ import { distinctUntilChanged, catchError } from 'rxjs/operators';
5
+ import { usePaneRouter } from 'sanity/structure';
6
+ import { RouterContext } from 'sanity/router';
7
+ import { jsx, jsxs } from 'react/jsx-runtime';
8
+ import { Card, Flex, Box, Stack, Text, Menu, MenuItem, TextInput, Badge } from '@sanity/ui';
9
+ import styled, { css } from 'styled-components';
10
+ import { RemoveCircleIcon, AddCircleIcon, RestoreIcon } from '@sanity/icons';
24
11
  var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
25
12
  function getDefaultExportFromCjs(x) {
26
13
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
@@ -671,13 +658,13 @@ var builder_1 = __importDefault(builder);
671
658
  var node = builder_1.default;
672
659
  var createImageUrlBuilder = /*@__PURE__*/getDefaultExportFromCjs(node);
673
660
  function useImageUrlBuilder(clientOptions) {
674
- const client = sanity.useClient(clientOptions);
675
- const builder = React.useMemo(() => createImageUrlBuilder(client), [client]);
661
+ const client = useClient(clientOptions);
662
+ const builder = useMemo(() => createImageUrlBuilder(client), [client]);
676
663
  return builder;
677
664
  }
678
665
  function useImageUrlBuilderImage(source, clientOptions) {
679
666
  const builder = useImageUrlBuilder(clientOptions);
680
- const image = React.useMemo(() => source && builder ? builder.image(source) : null, [builder, source]);
667
+ const image = useMemo(() => source && builder ? builder.image(source) : null, [builder, source]);
681
668
  return image;
682
669
  }
683
670
  const DEFAULT_PARAMS = {};
@@ -686,8 +673,8 @@ const DEFAULT_OPTIONS = {
686
673
  };
687
674
  const DEFAULT_INITIAL_VALUE = null;
688
675
  function useParams(params) {
689
- const stringifiedParams = React.useMemo(() => JSON.stringify(params || {}), [params]);
690
- return React.useMemo(() => JSON.parse(stringifiedParams), [stringifiedParams]);
676
+ const stringifiedParams = useMemo(() => JSON.stringify(params || {}), [params]);
677
+ return useMemo(() => JSON.parse(stringifiedParams), [stringifiedParams]);
691
678
  }
692
679
  function useListeningQuery(query, _ref) {
693
680
  let {
@@ -695,24 +682,24 @@ function useListeningQuery(query, _ref) {
695
682
  options = DEFAULT_OPTIONS,
696
683
  initialValue = DEFAULT_INITIAL_VALUE
697
684
  } = _ref;
698
- const [loading, setLoading] = React.useState(true);
699
- const [error, setError] = React.useState(false);
700
- const [data, setData] = React.useState(initialValue);
685
+ const [loading, setLoading] = useState(true);
686
+ const [error, setError] = useState(false);
687
+ const [data, setData] = useState(initialValue);
701
688
  const memoParams = useParams(params);
702
689
  const memoOptions = useParams(options);
703
- const subscription = React.useRef(null);
704
- const documentStore = sanity.useDocumentStore();
705
- React.useEffect(() => {
690
+ const subscription = useRef(null);
691
+ const documentStore = useDocumentStore();
692
+ useEffect(() => {
706
693
  if (query && !error && !subscription.current) {
707
694
  try {
708
- subscription.current = documentStore.listenQuery(query, memoParams, memoOptions).pipe(operators.distinctUntilChanged(isEqual__default.default), operators.catchError(err => {
695
+ subscription.current = documentStore.listenQuery(query, memoParams, memoOptions).pipe(distinctUntilChanged(isEqual), catchError(err => {
709
696
  console.error(err);
710
697
  setError(err);
711
698
  setLoading(false);
712
699
  setData(null);
713
700
  return err;
714
701
  })).subscribe(documents => {
715
- setData(current => isEqual__default.default(current, documents) ? current : documents);
702
+ setData(current => isEqual(current, documents) ? current : documents);
716
703
  setLoading(false);
717
704
  setError(false);
718
705
  });
@@ -739,35 +726,56 @@ function useListeningQuery(query, _ref) {
739
726
  error
740
727
  };
741
728
  }
729
+ function chunkArray(array, size) {
730
+ const chunks = [];
731
+ for (let i = 0; i < array.length; i += size) {
732
+ chunks.push(array.slice(i, i + size));
733
+ }
734
+ return chunks;
735
+ }
742
736
  function useProjectUsers(_ref2) {
743
737
  let {
744
738
  apiVersion
745
739
  } = _ref2;
746
740
  const {
747
741
  currentUser
748
- } = sanity.useWorkspace();
749
- const client = sanity.useClient({
742
+ } = useWorkspace();
743
+ const client = useClient({
750
744
  apiVersion: apiVersion != null ? apiVersion : "2023-01-01"
751
745
  });
752
- const [users, setUsers] = React.useState([]);
753
- React.useEffect(() => {
746
+ const [users, setUsers] = useState([]);
747
+ useEffect(() => {
754
748
  const {
755
749
  projectId
756
750
  } = client.config();
757
- async function getUser(id) {
758
- const userDetails = await client.request({
759
- url: "/projects/".concat(projectId, "/users/").concat(id)
760
- });
761
- return userDetails;
762
- }
763
751
  async function getUsersWithRoles() {
764
- const userRoles = await client.request({
765
- url: "/projects/".concat(projectId, "/acl")
766
- }).then(async res => Promise.all(res.map(async user => ({
767
- isCurrentUser: user.projectUserId === (currentUser == null ? void 0 : currentUser.id),
768
- ...(await getUser(user.projectUserId))
769
- })))).catch(err => err);
770
- setUsers(userRoles);
752
+ try {
753
+ const aclData = await client.request({
754
+ url: "/projects/".concat(projectId, "/acl")
755
+ });
756
+ const userIds = aclData.map(user => user.projectUserId);
757
+ const userIdChunks = chunkArray(userIds, 200);
758
+ let usersData = [];
759
+ for (const chunk of userIdChunks) {
760
+ const chunkedUserIds = chunk.join(",");
761
+ const response = await client.request({
762
+ url: "/projects/".concat(projectId, "/users/").concat(chunkedUserIds)
763
+ });
764
+ usersData = [...usersData, ...response];
765
+ }
766
+ const usersWithRoles = usersData.map(user => {
767
+ var _a;
768
+ const userRoles = ((_a = aclData.find(aclUser => aclUser.projectUserId === user.id)) == null ? void 0 : _a.roles) || [];
769
+ return {
770
+ ...user,
771
+ isCurrentUser: user.id === (currentUser == null ? void 0 : currentUser.id),
772
+ roles: userRoles
773
+ };
774
+ });
775
+ setUsers(usersWithRoles);
776
+ } catch (err) {
777
+ console.error("Failed to fetch users:", err);
778
+ }
771
779
  }
772
780
  if (!users.length) {
773
781
  getUsersWithRoles();
@@ -776,12 +784,12 @@ function useProjectUsers(_ref2) {
776
784
  return users;
777
785
  }
778
786
  function useOpenInNewPane(id, type) {
779
- const routerContext = React__default.default.useContext(router.RouterContext);
787
+ const routerContext = React.useContext(RouterContext);
780
788
  const {
781
789
  routerPanesState,
782
790
  groupIndex
783
- } = structure.usePaneRouter();
784
- const openInNewPane = React__default.default.useCallback(() => {
791
+ } = usePaneRouter();
792
+ const openInNewPane = React.useCallback(() => {
785
793
  if (!routerContext || !id || !type) {
786
794
  return;
787
795
  }
@@ -815,20 +823,20 @@ function Feedback(props) {
815
823
  ...DEFAULT_PROPS,
816
824
  ...props
817
825
  };
818
- return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
826
+ return /* @__PURE__ */jsx(Card, {
819
827
  tone,
820
828
  padding: 4,
821
829
  radius: 3,
822
830
  border: true,
823
- children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
824
- children: [icon ? "display icon" : null, children ? children : /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
831
+ children: /* @__PURE__ */jsxs(Flex, {
832
+ children: [icon ? "display icon" : null, children ? children : /* @__PURE__ */jsx(Box, {
825
833
  flex: 1,
826
- children: /* @__PURE__ */jsxRuntime.jsxs(ui.Stack, {
834
+ children: /* @__PURE__ */jsxs(Stack, {
827
835
  space: 4,
828
- children: [title ? /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
836
+ children: [title ? /* @__PURE__ */jsx(Text, {
829
837
  weight: "semibold",
830
838
  children: title
831
- }) : null, description ? /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
839
+ }) : null, description ? /* @__PURE__ */jsx(Text, {
832
840
  size: 2,
833
841
  children: description
834
842
  }) : null]
@@ -840,59 +848,59 @@ function Feedback(props) {
840
848
  var __freeze = Object.freeze;
841
849
  var __defProp = Object.defineProperty;
842
850
  var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", {
843
- value: __freeze(raw || cooked.slice())
851
+ value: __freeze(cooked.slice())
844
852
  }));
845
853
  var _a, _b, _c;
846
854
  const TableWrapper = function () {
847
855
  let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
848
- return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
856
+ return /* @__PURE__ */jsx(Card, {
849
857
  as: "table",
850
858
  ...props
851
859
  });
852
860
  };
853
- const StyledTable = styled__default.default(TableWrapper)(() => styled.css(_a || (_a = __template(["\n display: table;\n width: 100%;\n border-collapse: collapse;\n\n &:not([hidden]) {\n display: table;\n border-collapse: collapse;\n }\n "]))));
861
+ const StyledTable = styled(TableWrapper)(() => css(_a || (_a = __template(["\n display: table;\n width: 100%;\n border-collapse: collapse;\n\n &:not([hidden]) {\n display: table;\n border-collapse: collapse;\n }\n "]))));
854
862
  function Table(props) {
855
863
  const {
856
864
  children,
857
865
  ...rest
858
866
  } = props;
859
- return /* @__PURE__ */jsxRuntime.jsx(StyledTable, {
867
+ return /* @__PURE__ */jsx(StyledTable, {
860
868
  ...rest,
861
869
  children
862
870
  });
863
871
  }
864
872
  const RowWrapper = function () {
865
873
  let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
866
- return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
874
+ return /* @__PURE__ */jsx(Card, {
867
875
  as: "tr",
868
876
  ...props
869
877
  });
870
878
  };
871
- const StyledRow = styled__default.default(RowWrapper)(() => styled.css(_b || (_b = __template(["\n display: table-row;\n\n &:not([hidden]) {\n display: table-row;\n }\n "]))));
879
+ const StyledRow = styled(RowWrapper)(() => css(_b || (_b = __template(["\n display: table-row;\n\n &:not([hidden]) {\n display: table-row;\n }\n "]))));
872
880
  function Row(props) {
873
881
  const {
874
882
  children,
875
883
  ...rest
876
884
  } = props;
877
- return /* @__PURE__ */jsxRuntime.jsx(StyledRow, {
885
+ return /* @__PURE__ */jsx(StyledRow, {
878
886
  ...rest,
879
887
  children
880
888
  });
881
889
  }
882
890
  const CellWrapper = function () {
883
891
  let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
884
- return /* @__PURE__ */jsxRuntime.jsx(ui.Card, {
892
+ return /* @__PURE__ */jsx(Card, {
885
893
  as: "td",
886
894
  ...props
887
895
  });
888
896
  };
889
- const StyledCell = styled__default.default(CellWrapper)(() => styled.css(_c || (_c = __template(["\n display: table-cell;\n\n &:not([hidden]) {\n display: table-cell;\n }\n "]))));
897
+ const StyledCell = styled(CellWrapper)(() => css(_c || (_c = __template(["\n display: table-cell;\n\n &:not([hidden]) {\n display: table-cell;\n }\n "]))));
890
898
  function Cell(props) {
891
899
  const {
892
900
  children,
893
901
  ...rest
894
902
  } = props;
895
- return /* @__PURE__ */jsxRuntime.jsx(StyledCell, {
903
+ return /* @__PURE__ */jsx(StyledCell, {
896
904
  ...rest,
897
905
  children
898
906
  });
@@ -930,11 +938,11 @@ function UserSelectMenu(props) {
930
938
  ...LABELS,
931
939
  ...props.labels
932
940
  } : LABELS;
933
- const [searchString, setSearchString] = React__default.default.useState("");
941
+ const [searchString, setSearchString] = React.useState("");
934
942
  const searchResults = searchUsers(userList || [], searchString);
935
943
  const me = userList.find(u => u.isCurrentUser);
936
944
  const meAssigned = me && value.includes(me.id);
937
- const input = React.useRef(null);
945
+ const input = useRef(null);
938
946
  const handleSearchChange = event => {
939
947
  setSearchString(event.target.value);
940
948
  };
@@ -952,51 +960,51 @@ function UserSelectMenu(props) {
952
960
  const handleClearAssigneesClick = () => {
953
961
  if (onClear) onClear();
954
962
  };
955
- return /* @__PURE__ */jsxRuntime.jsxs(ui.Menu, {
963
+ return /* @__PURE__ */jsxs(Menu, {
956
964
  style,
957
- children: [meAssigned ? /* @__PURE__ */jsxRuntime.jsx(ui.MenuItem, {
965
+ children: [meAssigned ? /* @__PURE__ */jsx(MenuItem, {
958
966
  tone: "caution",
959
967
  disabled: !me,
960
968
  onClick: handleUnassignMyself,
961
- icon: icons.RemoveCircleIcon,
969
+ icon: RemoveCircleIcon,
962
970
  text: labels.removeMe
963
- }) : /* @__PURE__ */jsxRuntime.jsx(ui.MenuItem, {
971
+ }) : /* @__PURE__ */jsx(MenuItem, {
964
972
  tone: "positive",
965
973
  onClick: handleAssignMyself,
966
- icon: icons.AddCircleIcon,
974
+ icon: AddCircleIcon,
967
975
  text: labels.addMe
968
- }), /* @__PURE__ */jsxRuntime.jsx(ui.MenuItem, {
976
+ }), /* @__PURE__ */jsx(MenuItem, {
969
977
  tone: "critical",
970
978
  disabled: value.length === 0,
971
979
  onClick: handleClearAssigneesClick,
972
- icon: icons.RestoreIcon,
980
+ icon: RestoreIcon,
973
981
  text: labels.clear
974
- }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
982
+ }), /* @__PURE__ */jsx(Box, {
975
983
  padding: 1,
976
- children: /* @__PURE__ */jsxRuntime.jsx(ui.TextInput, {
984
+ children: /* @__PURE__ */jsx(TextInput, {
977
985
  ref: input,
978
986
  onChange: handleSearchChange,
979
987
  placeholder: labels.searchPlaceholder,
980
988
  value: searchString
981
989
  })
982
- }), searchString && (searchResults == null ? void 0 : searchResults.length) === 0 && /* @__PURE__ */jsxRuntime.jsx(ui.MenuItem, {
990
+ }), searchString && (searchResults == null ? void 0 : searchResults.length) === 0 && /* @__PURE__ */jsx(MenuItem, {
983
991
  disabled: true,
984
992
  text: labels.notFound
985
- }), searchResults && searchResults.map(user => /* @__PURE__ */jsxRuntime.jsx(ui.MenuItem, {
993
+ }), searchResults && searchResults.map(user => /* @__PURE__ */jsx(MenuItem, {
986
994
  pressed: value.includes(user.id),
987
995
  onClick: () => handleSelect(value.indexOf(user.id) > -1, user),
988
- children: /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
996
+ children: /* @__PURE__ */jsxs(Flex, {
989
997
  align: "center",
990
- children: [/* @__PURE__ */jsxRuntime.jsx(sanity.UserAvatar, {
998
+ children: [/* @__PURE__ */jsx(UserAvatar, {
991
999
  user,
992
1000
  size: 1
993
- }), /* @__PURE__ */jsxRuntime.jsx(ui.Box, {
1001
+ }), /* @__PURE__ */jsx(Box, {
994
1002
  paddingX: 2,
995
1003
  flex: 1,
996
- children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
1004
+ children: /* @__PURE__ */jsx(Text, {
997
1005
  children: user.displayName
998
1006
  })
999
- }), user.isCurrentUser && /* @__PURE__ */jsxRuntime.jsx(ui.Badge, {
1007
+ }), user.isCurrentUser && /* @__PURE__ */jsx(Badge, {
1000
1008
  fontSize: 1,
1001
1009
  tone: "positive",
1002
1010
  mode: "outline",
@@ -1006,14 +1014,5 @@ function UserSelectMenu(props) {
1006
1014
  }, user.id))]
1007
1015
  });
1008
1016
  }
1009
- exports.Cell = Cell;
1010
- exports.Feedback = Feedback;
1011
- exports.Row = Row;
1012
- exports.Table = Table;
1013
- exports.UserSelectMenu = UserSelectMenu;
1014
- exports.useImageUrlBuilder = useImageUrlBuilder;
1015
- exports.useImageUrlBuilderImage = useImageUrlBuilderImage;
1016
- exports.useListeningQuery = useListeningQuery;
1017
- exports.useOpenInNewPane = useOpenInNewPane;
1018
- exports.useProjectUsers = useProjectUsers;
1017
+ export { Cell, Feedback, Row, Table, UserSelectMenu, useImageUrlBuilder, useImageUrlBuilderImage, useListeningQuery, useOpenInNewPane, useProjectUsers };
1019
1018
  //# sourceMappingURL=index.js.map