@datawheel/bespoke 0.5.4 → 0.5.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/dist/index.js CHANGED
@@ -1267,7 +1267,9 @@ var init_selectorQueryToVariable = __esm({
1267
1267
  init_cms();
1268
1268
  selectorQueryToVariable = (id, query, config) => {
1269
1269
  const accessor = `selector${id}id`;
1270
- const queryObject = new URLSearchParams(query);
1270
+ const queryObject = new URLSearchParams(
1271
+ typeof location !== "undefined" && location?.search ? location.search : query
1272
+ );
1271
1273
  if (config.type === SELECTOR_TYPES.MULTI) {
1272
1274
  const queryIds = queryObject.get(accessor) ? queryObject.get(accessor).split(",") : config.defaultValue;
1273
1275
  const options = config.options.filter((d2) => queryIds.includes(d2.id));
@@ -4984,9 +4986,9 @@ function Stat({
4984
4986
  }
4985
4987
  ),
4986
4988
  /* @__PURE__ */ jsxs(Stack, { spacing: 0, children: [
4987
- /* @__PURE__ */ jsx(RichText, { component: "div", size: "md", html: title, style: settings }),
4988
- /* @__PURE__ */ jsx(RichText, { component: "div", size: "xl", html: value, style: settings }),
4989
- /* @__PURE__ */ jsx(RichText, { component: "div", size: "sm", html: subtitle, style: settings })
4989
+ title && /* @__PURE__ */ jsx(RichText, { component: "div", size: "md", html: title, style: settings }),
4990
+ value && /* @__PURE__ */ jsx(RichText, { component: "div", size: "xl", html: value, style: settings }),
4991
+ subtitle && /* @__PURE__ */ jsx(RichText, { component: "div", size: "sm", html: subtitle, style: settings })
4990
4992
  ] }, "stat"),
4991
4993
  tooltip && /* @__PURE__ */ jsx(InnerTooltip, { content: tooltip })
4992
4994
  ] });
@@ -12666,6 +12668,63 @@ var init_UnlocalizedFieldLabel = __esm({
12666
12668
  UnlocalizedFieldLabel_default = UnlocalizedFieldLabel;
12667
12669
  }
12668
12670
  });
12671
+
12672
+ // components/blocks/types/selector.ts
12673
+ var SELECTOR_TYPE, SELECTOR_COMPONENT, OPTION_TYPE, isValidSelectorOptionsArray, isPrimitiveTypeVariable, isValidSelectorState, deriveLogicOptions;
12674
+ var init_selector = __esm({
12675
+ "components/blocks/types/selector.ts"() {
12676
+ init_esm_shims();
12677
+ SELECTOR_TYPE = {
12678
+ SINGLE: "single",
12679
+ MULTI: "multi"
12680
+ };
12681
+ SELECTOR_COMPONENT = {
12682
+ SEGMENTED_CONTROL: "segmented control",
12683
+ SELECTOR: "selector"
12684
+ };
12685
+ OPTION_TYPE = {
12686
+ STATIC: "static",
12687
+ DYNAMIC: "dynamic"
12688
+ };
12689
+ isValidSelectorOptionsArray = (variable) => variable && Array.isArray(variable) && variable.length > 0;
12690
+ isPrimitiveTypeVariable = (variable) => isValidSelectorOptionsArray(variable) && ["string"].includes(typeof variable[0]);
12691
+ isValidSelectorState = (simpleState) => {
12692
+ const {
12693
+ name,
12694
+ type,
12695
+ optionsType,
12696
+ options
12697
+ } = simpleState;
12698
+ if (![name, type, optionsType, options].every(Boolean))
12699
+ return false;
12700
+ if (optionsType === OPTION_TYPE.STATIC)
12701
+ return options.static?.length > 0 && options.static?.every((o2) => o2.id);
12702
+ if (!options.dynamic?.isPrimitiveType)
12703
+ return !!options.dynamic?.idKey;
12704
+ return true;
12705
+ };
12706
+ deriveLogicOptions = (selectorType, options, optionsType) => {
12707
+ let defaultValue;
12708
+ let logicOptions;
12709
+ if (optionsType === OPTION_TYPE.STATIC) {
12710
+ defaultValue = selectorType === SELECTOR_TYPE.SINGLE ? options.static.find((o2) => o2.isDefault)?.id : options.static.filter((o2) => o2.isDefault).map((o2) => o2.id);
12711
+ logicOptions = options.static.map(({ id, label }) => ({ id, label: label || id }));
12712
+ } else {
12713
+ const {
12714
+ optionsVar,
12715
+ idKey,
12716
+ labelKey,
12717
+ defaultVar,
12718
+ isPrimitiveType
12719
+ } = options.dynamic;
12720
+ if (defaultVar)
12721
+ defaultValue = `variables["${defaultVar}"]`;
12722
+ logicOptions = isPrimitiveType ? `variables["${optionsVar}"]` : `(variables["${optionsVar}"] || []).map(d => ({id: String(d["${idKey}"]), label: String(d["${labelKey || idKey}"])}))`;
12723
+ }
12724
+ return { logicOptions, defaultValue };
12725
+ };
12726
+ }
12727
+ });
12669
12728
  function StaticSelectorOptionRow({ option, editOption, deleteOption }) {
12670
12729
  return /* @__PURE__ */ jsxs("tr", { className: "reports-selector-static-option", children: [
12671
12730
  /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(
@@ -12904,23 +12963,7 @@ function SelectorUI(props) {
12904
12963
  component: selectorComponent,
12905
12964
  type: selectorType
12906
12965
  };
12907
- let defaultValue;
12908
- let logicOptions;
12909
- if (optionsType === OPTION_TYPE.STATIC) {
12910
- defaultValue = selectorType === SELECTOR_TYPE.SINGLE ? options.static.find((o2) => o2.isDefault)?.id : options.static.filter((o2) => o2.isDefault).map((o2) => o2.id);
12911
- logicOptions = options.static.map(({ id: id2, label }) => ({ id: id2, label: label || id2 }));
12912
- } else {
12913
- const {
12914
- optionsVar,
12915
- idKey,
12916
- labelKey,
12917
- defaultVar,
12918
- isPrimitiveType
12919
- } = options.dynamic;
12920
- if (defaultVar)
12921
- defaultValue = `variables["${defaultVar}"]`;
12922
- logicOptions = isPrimitiveType ? `variables["${optionsVar}"]` : `(variables["${optionsVar}"] || []).map(d => ({id: String(d["${idKey}"]), label: String(d["${labelKey || idKey}"])}))`;
12923
- }
12966
+ const { logicOptions, defaultValue } = deriveLogicOptions(selectorType, options, optionsType);
12924
12967
  if (defaultValue)
12925
12968
  logicObj.defaultValue = defaultValue;
12926
12969
  logicObj.options = logicOptions;
@@ -13009,7 +13052,7 @@ function SelectorUI(props) {
13009
13052
  executeButton
13010
13053
  ] });
13011
13054
  }
13012
- var SELECTOR_TYPE, SELECTOR_COMPONENT, OPTION_TYPE, isValidSelectorOptionsArray, isPrimitiveTypeVariable, isValidSelectorState, SelectorUI_default;
13055
+ var SelectorUI_default;
13013
13056
  var init_SelectorUI = __esm({
13014
13057
  "components/blocks/types/simpleEditors/SelectorUI.tsx"() {
13015
13058
  init_esm_shims();
@@ -13019,35 +13062,7 @@ var init_SelectorUI = __esm({
13019
13062
  init_RichTextEditor();
13020
13063
  init_UnlocalizedFieldLabel();
13021
13064
  init_cms();
13022
- SELECTOR_TYPE = {
13023
- SINGLE: "single",
13024
- MULTI: "multi"
13025
- };
13026
- SELECTOR_COMPONENT = {
13027
- SEGMENTED_CONTROL: "segmented control",
13028
- SELECTOR: "selector"
13029
- };
13030
- OPTION_TYPE = {
13031
- STATIC: "static",
13032
- DYNAMIC: "dynamic"
13033
- };
13034
- isValidSelectorOptionsArray = (variable) => variable && Array.isArray(variable) && variable.length > 0;
13035
- isPrimitiveTypeVariable = (variable) => isValidSelectorOptionsArray(variable) && ["string"].includes(typeof variable[0]);
13036
- isValidSelectorState = (simpleState) => {
13037
- const {
13038
- name,
13039
- type,
13040
- optionsType,
13041
- options
13042
- } = simpleState;
13043
- if (![name, type, optionsType, options].every(Boolean))
13044
- return false;
13045
- if (optionsType === OPTION_TYPE.STATIC)
13046
- return options.static?.length > 0 && options.static?.every((o2) => o2.id);
13047
- if (!options.dynamic?.isPrimitiveType)
13048
- return !!options.dynamic?.idKey;
13049
- return true;
13050
- };
13065
+ init_selector();
13051
13066
  SelectorUI_default = SelectorUI;
13052
13067
  }
13053
13068
  });
@@ -18280,12 +18295,12 @@ function ReportSelector({
18280
18295
  }) {
18281
18296
  const [slugReady, setSlugReady] = useState(false);
18282
18297
  const dispatch = useAppDispatch();
18283
- const location = useManagerLocation();
18298
+ const location2 = useManagerLocation();
18284
18299
  const resource = useContext(ResourceContext);
18285
18300
  const dimensions = report && report.dimensions ? report.dimensions : [];
18286
18301
  const previews = useAppSelector((state) => state.status.previews);
18287
18302
  const currentReport = previews.map((p2) => ({ variantId: p2.variant_id, memberId: p2.content_id }));
18288
- const param = location.query.preview;
18303
+ const param = location2.query.preview;
18289
18304
  const slugs = Array.isArray(param) ? param.join("!") : param || "";
18290
18305
  const onSelect = (previewMember) => {
18291
18306
  const newPreviews = report.dimensions.map((dId) => {
@@ -18322,7 +18337,7 @@ function ReportSelector({
18322
18337
  const changeLocation = previewsAsReadMember.filter((p2) => p2.content_id).length === dimensions.length;
18323
18338
  dispatch(actions_exports.setPreviews(previewsAsReadMember));
18324
18339
  if (changeLocation) {
18325
- location.setSearchParam("preview", nextParam);
18340
+ location2.setSearchParam("preview", nextParam);
18326
18341
  dispatch(actions_exports.recalculateVariables(resource, {
18327
18342
  previews: previewsAsReadMember
18328
18343
  }));
@@ -19666,8 +19681,8 @@ function CMSHeader(props) {
19666
19681
  const resource = useContext(ResourceContext);
19667
19682
  const dispatch = useAppDispatch();
19668
19683
  const previews = useAppSelector((state) => state.status.previews);
19669
- const location = useManagerLocation();
19670
- const reportId = Number.parseInt(location.params[0]);
19684
+ const location2 = useManagerLocation();
19685
+ const reportId = Number.parseInt(location2.params[0]);
19671
19686
  const reportRef = useReportRef(reportId);
19672
19687
  const profilePrefix = useProfilePrefix();
19673
19688
  const currentLocale = useCurrentLocale2();
@@ -19683,7 +19698,7 @@ function CMSHeader(props) {
19683
19698
  const localePrefix = currentLocale === localeDefault10 ? "" : `/${currentLocale}`;
19684
19699
  const realPath = previewPath.length ? `${localePrefix}${profilePrefix}/${previewPath.join("/")}` : "";
19685
19700
  const onSelectLocale = (previewLocale) => {
19686
- location.setSearchParam("locale", previewLocale);
19701
+ location2.setSearchParam("locale", previewLocale);
19687
19702
  dispatch(actions_exports.readMember({
19688
19703
  mode: "content_ids",
19689
19704
  variant: previews.map((m) => m.variant_id),
@@ -21019,6 +21034,7 @@ var scaffoldEmptyBlock = (block) => {
21019
21034
  // components/blocks/Block.tsx
21020
21035
  init_stringifyObject();
21021
21036
  init_BlockEditorProvider();
21037
+ init_selector();
21022
21038
  var UNLOCALIZED_ICON_KEYS = [
21023
21039
  "_iconVariant",
21024
21040
  "_iconSize",
@@ -21144,7 +21160,11 @@ function Block2({ id, modified, callbacks, inline }) {
21144
21160
  });
21145
21161
  const logicObj = { ...newSimple };
21146
21162
  if (block.type === BLOCK_TYPES.SELECTOR) {
21147
- logicObj.options = logicObj.options[logicObj.optionsType];
21163
+ const { selectorType, options, optionsType } = contentByLocale[currentLocale2].content.simple;
21164
+ const { logicOptions, defaultValue } = deriveLogicOptions(selectorType, options, optionsType);
21165
+ if (defaultValue)
21166
+ logicObj.defaultValue = defaultValue;
21167
+ logicObj.options = logicOptions;
21148
21168
  }
21149
21169
  newContentByLocale[locale].content = {
21150
21170
  ...newContentByLocale[locale].content,
@@ -21160,10 +21180,11 @@ function Block2({ id, modified, callbacks, inline }) {
21160
21180
  const { settings } = blockState;
21161
21181
  const contentByLocale = Object.values(blockState.contentByLocale).reduce(reduceContent, {});
21162
21182
  const properties = ["id", "api", "shared", "type"].reduce((acc, d2) => ({ ...acc, [d2]: blockState[d2] }), {});
21183
+ const newContent = spreadUnlocalizedContent(contentByLocale, currentLocale);
21163
21184
  return {
21164
21185
  ...properties,
21165
21186
  id: blockState.id,
21166
- contentByLocale: spreadUnlocalizedContent(contentByLocale, currentLocale),
21187
+ contentByLocale: newContent,
21167
21188
  settings
21168
21189
  };
21169
21190
  };
@@ -22728,8 +22749,8 @@ init_hooks();
22728
22749
  init_use_entity_settings();
22729
22750
  init_hooks3();
22730
22751
  function ReportSettingsPanel() {
22731
- const location = useManagerLocation();
22732
- const reportId = Number.parseInt(location.params[0]);
22752
+ const location2 = useManagerLocation();
22753
+ const reportId = Number.parseInt(location2.params[0]);
22733
22754
  const report = useReportRef(reportId).data;
22734
22755
  const { settings, updateSettings } = useReportSettings(reportId);
22735
22756
  const { handlers } = useSidebar("reportEditor");
@@ -24592,7 +24613,7 @@ init_EntityDeleteButton();
24592
24613
  function ReportCard(props) {
24593
24614
  const { for: report, enabled } = props;
24594
24615
  const theme = useMantineTheme();
24595
- const location = useManagerLocation();
24616
+ const location2 = useManagerLocation();
24596
24617
  const secondaryColor = theme.colorScheme === "dark" ? theme.colors.dark[1] : theme.colors.gray[7];
24597
24618
  return /* @__PURE__ */ jsxs(Card, { shadow: "xs", p: "sm", w: "100%", children: [
24598
24619
  /* @__PURE__ */ jsxs(Group, { position: "apart", children: [
@@ -24605,7 +24626,7 @@ function ReportCard(props) {
24605
24626
  /* @__PURE__ */ jsx(Text, { size: "sm", style: { color: secondaryColor, lineHeight: 2 }, children: "Description of report will go here" }),
24606
24627
  /* @__PURE__ */ jsx(Space, { w: "xs" }),
24607
24628
  /* @__PURE__ */ jsxs(Group, { position: "apart", children: [
24608
- /* @__PURE__ */ jsx(Link, { href: location.href("reports", report.id), passHref: true, children: /* @__PURE__ */ jsx(Button, { leftIcon: /* @__PURE__ */ jsx(IconPencil, {}), compact: true, children: "Edit" }) }),
24629
+ /* @__PURE__ */ jsx(Link, { href: location2.href("reports", report.id), passHref: true, children: /* @__PURE__ */ jsx(Button, { leftIcon: /* @__PURE__ */ jsx(IconPencil, {}), compact: true, children: "Edit" }) }),
24609
24630
  /* @__PURE__ */ jsx(EntityDeleteButton, { type: "report", id: report.id, disabled: !enabled })
24610
24631
  ] })
24611
24632
  ] });
@@ -25467,7 +25488,7 @@ function BespokeManagerShell(props) {
25467
25488
  const action = useRef(null);
25468
25489
  const [opened, { open, close }] = useDisclosure(false);
25469
25490
  const { user, error, isLoading } = useBespokeUser_default();
25470
- const location = useManagerLocation();
25491
+ const location2 = useManagerLocation();
25471
25492
  const buildLinks = useCallback((user2) => {
25472
25493
  const menuLinks = [];
25473
25494
  links.forEach((link) => {
@@ -25486,7 +25507,7 @@ function BespokeManagerShell(props) {
25486
25507
  }, []);
25487
25508
  const navbar = useMemo(() => {
25488
25509
  const linkButtons = buildLinks(user);
25489
- const expand = location.page === "welcome" || opened;
25510
+ const expand = location2.page === "welcome" || opened;
25490
25511
  return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsx(
25491
25512
  Box,
25492
25513
  {
@@ -25532,7 +25553,7 @@ function BespokeManagerShell(props) {
25532
25553
  },
25533
25554
  withBorder: false,
25534
25555
  children: [
25535
- /* @__PURE__ */ jsx(Navbar.Section, { style: { position: "relative" }, children: /* @__PURE__ */ jsx(BespokeLogo, { href: location.href(""), onlyIcon: !expand, version }) }),
25556
+ /* @__PURE__ */ jsx(Navbar.Section, { style: { position: "relative" }, children: /* @__PURE__ */ jsx(BespokeLogo, { href: location2.href(""), onlyIcon: !expand, version }) }),
25536
25557
  /* @__PURE__ */ jsx(Navbar.Section, { grow: true, component: ScrollArea, children: /* @__PURE__ */ jsx(Stack, { spacing: 0, children: linkButtons }) }),
25537
25558
  /* @__PURE__ */ jsx(Navbar.Section, { children: user && !error && !isLoading && /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs(Stack, { px: 13, children: [
25538
25559
  /* @__PURE__ */ jsxs(Group, { align: "flex-start", children: [
@@ -25580,34 +25601,34 @@ function BespokeManagerShell(props) {
25580
25601
  )
25581
25602
  }
25582
25603
  ) });
25583
- }, [location, opened, user]);
25604
+ }, [location2, opened, user]);
25584
25605
  const route = useMemo(() => {
25585
- const item = links.find((page) => page.href.includes(location.page));
25586
- if (location.page === "welcome") {
25606
+ const item = links.find((page) => page.href.includes(location2.page));
25607
+ if (location2.page === "welcome") {
25587
25608
  return /* @__PURE__ */ jsx(HelloView, {});
25588
25609
  }
25589
25610
  if (user && Array.isArray(user.bespoke_roles) && user.bespoke_roles.includes(item?.roleRequired)) {
25590
- if (location.page === "metadata") {
25611
+ if (location2.page === "metadata") {
25591
25612
  return /* @__PURE__ */ jsx(MetadataEditor, {});
25592
25613
  }
25593
- if (location.page === "formatters") {
25614
+ if (location2.page === "formatters") {
25594
25615
  return /* @__PURE__ */ jsx(FormatterEditor, {});
25595
25616
  }
25596
- if (location.page === "reports") {
25597
- if (location.params.length > 0) {
25598
- const reportId = Number.parseInt(location.params[0]);
25617
+ if (location2.page === "reports") {
25618
+ if (location2.params.length > 0) {
25619
+ const reportId = Number.parseInt(location2.params[0]);
25599
25620
  return /* @__PURE__ */ jsx(BuilderEditor, { id: reportId, locale });
25600
25621
  }
25601
25622
  return /* @__PURE__ */ jsx(ReportPicker, {});
25602
25623
  }
25603
- if (location.page === "users") {
25624
+ if (location2.page === "users") {
25604
25625
  return /* @__PURE__ */ jsx(UsersEditor, {});
25605
25626
  }
25606
25627
  } else {
25607
25628
  return /* @__PURE__ */ jsx(UnauthorizeView, {});
25608
25629
  }
25609
25630
  return /* @__PURE__ */ jsx(NotFoundView, {});
25610
- }, [location, user]);
25631
+ }, [location2, user]);
25611
25632
  const dispatch = useAppDispatch();
25612
25633
  useEffect(() => {
25613
25634
  dispatch(statusActions.setStatus({ isCMS: true }));
package/dist/server.js CHANGED
@@ -4881,7 +4881,9 @@ function runSelector(id, logic, formatterFunctions, blockContext, replaceQuery =
4881
4881
  // libs/selectors/selectorQueryToVariable.js
4882
4882
  var selectorQueryToVariable = (id, query, config) => {
4883
4883
  const accessor = `selector${id}id`;
4884
- const queryObject = new URLSearchParams(query);
4884
+ const queryObject = new URLSearchParams(
4885
+ typeof location !== "undefined" && location?.search ? location.search : query
4886
+ );
4885
4887
  if (config.type === SELECTOR_TYPES.MULTI) {
4886
4888
  const queryIds = queryObject.get(accessor) ? queryObject.get(accessor).split(",") : config.defaultValue;
4887
4889
  const options = config.options.filter((d2) => queryIds.includes(d2.id));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datawheel/bespoke",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "description": "Content management system for creating automated data reports",
5
5
  "exports": {
6
6
  ".": {
@@ -141,4 +141,4 @@
141
141
  "peerDependencies": {
142
142
  "next": "^14.2.3"
143
143
  }
144
- }
144
+ }