@grafana/scenes 6.12.0 → 6.13.0--canary.1122.15166261616.0

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
@@ -2955,6 +2955,9 @@ class GroupByVariableUrlSyncHandler {
2955
2955
  this._sceneObject = _sceneObject;
2956
2956
  this._nextChangeShouldAddHistoryStep = false;
2957
2957
  }
2958
+ getRestorableKey() {
2959
+ return `${this._sceneObject.state.name}-restorable`;
2960
+ }
2958
2961
  getKey() {
2959
2962
  return `var-${this._sceneObject.state.name}`;
2960
2963
  }
@@ -2962,21 +2965,36 @@ class GroupByVariableUrlSyncHandler {
2962
2965
  if (this._sceneObject.state.skipUrlSync) {
2963
2966
  return [];
2964
2967
  }
2965
- return [this.getKey()];
2968
+ return [this.getKey(), ...this._sceneObject.state.defaultValue ? [this.getRestorableKey()] : []];
2966
2969
  }
2967
2970
  getUrlState() {
2968
2971
  if (this._sceneObject.state.skipUrlSync) {
2969
2972
  return {};
2970
2973
  }
2971
- return { [this.getKey()]: toUrlValues(this._sceneObject.state.value, this._sceneObject.state.text) };
2974
+ return {
2975
+ [this.getKey()]: toUrlValues(this._sceneObject.state.value, this._sceneObject.state.text),
2976
+ ...this._sceneObject.state.defaultValue ? {
2977
+ [this.getRestorableKey()]: this._sceneObject.state.restorable ? "true" : void 0
2978
+ } : {}
2979
+ };
2972
2980
  }
2973
2981
  updateFromUrl(values) {
2982
+ var _a, _b;
2974
2983
  let urlValue = values[this.getKey()];
2984
+ let restorableValue = values[this.getRestorableKey()];
2975
2985
  if (urlValue != null) {
2976
2986
  if (!this._sceneObject.isActive) {
2977
2987
  this._sceneObject.skipNextValidation = true;
2978
2988
  }
2979
2989
  const { values: values2, texts } = fromUrlValues(urlValue);
2990
+ if (this._sceneObject.state.defaultValue && !restorableValue) {
2991
+ this._sceneObject.changeValueTo(
2992
+ (_a = this._sceneObject.state.defaultValue) == null ? void 0 : _a.value,
2993
+ (_b = this._sceneObject.state.defaultValue) == null ? void 0 : _b.text,
2994
+ false
2995
+ );
2996
+ return;
2997
+ }
2980
2998
  this._sceneObject.changeValueTo(values2, texts);
2981
2999
  }
2982
3000
  }
@@ -3077,6 +3095,98 @@ function wrapInSafeSerializableSceneObject(sceneObject) {
3077
3095
  return { value: sceneObject, text: "__sceneObject" };
3078
3096
  }
3079
3097
 
3098
+ function DefaultGroupByCustomIndicatorContainer(props) {
3099
+ const { model } = props;
3100
+ const theme = ui.useTheme2();
3101
+ const styles = getStyles$h(theme);
3102
+ const inputStyles = ui.getInputStyles({ theme, invalid: false });
3103
+ const value = lodash.isArray(model.state.value) ? model.state.value : model.state.value ? [model.state.value] : [];
3104
+ let buttons = [];
3105
+ if (value && value.length) {
3106
+ buttons.push(
3107
+ /* @__PURE__ */ React__default.default.createElement(
3108
+ ui.IconButton,
3109
+ {
3110
+ "aria-label": "clear",
3111
+ key: "clear",
3112
+ name: "times",
3113
+ size: "md",
3114
+ className: styles.clearIcon,
3115
+ onClick: (e) => {
3116
+ model.changeValueTo([], void 0, true);
3117
+ if (model.checkIfRestorable([])) {
3118
+ model.setState({ restorable: true });
3119
+ }
3120
+ }
3121
+ }
3122
+ )
3123
+ );
3124
+ }
3125
+ if (model.state.restorable) {
3126
+ buttons.push(
3127
+ /* @__PURE__ */ React__default.default.createElement(
3128
+ ui.IconButton,
3129
+ {
3130
+ onClick: (e) => {
3131
+ props.model.restoreDefaultValues();
3132
+ },
3133
+ onKeyDownCapture: (e) => {
3134
+ if (e.key === "Enter") {
3135
+ props.model.restoreDefaultValues();
3136
+ }
3137
+ },
3138
+ key: "restore",
3139
+ name: "history",
3140
+ size: "md",
3141
+ className: styles.clearIcon,
3142
+ tooltip: "Restore groupby set by this dashboard."
3143
+ }
3144
+ )
3145
+ );
3146
+ }
3147
+ if (!model.state.restorable) {
3148
+ buttons.push(
3149
+ /* @__PURE__ */ React__default.default.createElement(
3150
+ ui.Tooltip,
3151
+ {
3152
+ key: "tooltip",
3153
+ content: "Applied by default in this dashboard. If edited, it carries over to other dashboards.",
3154
+ placement: "bottom"
3155
+ },
3156
+ /* @__PURE__ */ React__default.default.createElement(ui.Icon, { name: "info-circle", size: "md" })
3157
+ )
3158
+ );
3159
+ }
3160
+ return /* @__PURE__ */ React__default.default.createElement(
3161
+ "div",
3162
+ {
3163
+ onMouseDown: (e) => {
3164
+ e.preventDefault();
3165
+ e.stopPropagation();
3166
+ },
3167
+ className: css.cx(
3168
+ inputStyles.suffix,
3169
+ css.css({
3170
+ position: "relative"
3171
+ })
3172
+ )
3173
+ },
3174
+ buttons
3175
+ );
3176
+ }
3177
+ const getStyles$h = (theme) => ({
3178
+ clearIcon: css.css({
3179
+ color: theme.colors.action.disabledText,
3180
+ cursor: "pointer",
3181
+ "&:hover:before": {
3182
+ backgroundColor: "transparent"
3183
+ },
3184
+ "&:hover": {
3185
+ color: theme.colors.text.primary
3186
+ }
3187
+ })
3188
+ });
3189
+
3080
3190
  class GroupByVariable extends MultiValueVariable {
3081
3191
  constructor(initialState) {
3082
3192
  super({
@@ -3095,6 +3205,21 @@ class GroupByVariable extends MultiValueVariable {
3095
3205
  });
3096
3206
  this.isLazy = true;
3097
3207
  this._urlSync = new GroupByVariableUrlSyncHandler(this);
3208
+ this._activationHandler = () => {
3209
+ if (this.state.defaultValue && (lodash.isArray(this.state.value) && !this.state.value.length || !this.state.value)) {
3210
+ this.setState({
3211
+ value: this.state.defaultValue.value,
3212
+ text: this.state.defaultValue.text,
3213
+ restorable: false
3214
+ });
3215
+ }
3216
+ if (this.state.defaultValue && this.checkIfRestorable(this.state.value)) {
3217
+ this.setState({ restorable: true });
3218
+ }
3219
+ return () => {
3220
+ this.restoreDefaultValues();
3221
+ };
3222
+ };
3098
3223
  /**
3099
3224
  * Get possible keys given current filters. Do not call from plugins directly
3100
3225
  */
@@ -3139,6 +3264,7 @@ class GroupByVariable extends MultiValueVariable {
3139
3264
  return () => allActiveGroupByVariables.delete(this);
3140
3265
  });
3141
3266
  }
3267
+ this.addActivationHandler(this._activationHandler);
3142
3268
  }
3143
3269
  validateAndUpdate() {
3144
3270
  return this.getValueOptions({}).pipe(
@@ -3197,6 +3323,25 @@ class GroupByVariable extends MultiValueVariable {
3197
3323
  })
3198
3324
  );
3199
3325
  }
3326
+ // This method is related to the defaultValue property. We check if the current value
3327
+ // is different from the default value. If it is, the groupBy will show a button
3328
+ // allowing the user to restore the default values.
3329
+ checkIfRestorable(values) {
3330
+ var _a, _b, _c, _d;
3331
+ const originalValues = lodash.isArray((_a = this.state.defaultValue) == null ? void 0 : _a.value) ? (_b = this.state.defaultValue) == null ? void 0 : _b.value : ((_c = this.state.defaultValue) == null ? void 0 : _c.value) ? [(_d = this.state.defaultValue) == null ? void 0 : _d.value] : [];
3332
+ const vals = lodash.isArray(values) ? values : [values];
3333
+ if (vals.length !== originalValues.length) {
3334
+ return true;
3335
+ }
3336
+ return !lodash.isEqual(vals, originalValues);
3337
+ }
3338
+ restoreDefaultValues() {
3339
+ this.setState({ restorable: false });
3340
+ if (!this.state.defaultValue) {
3341
+ return;
3342
+ }
3343
+ this.changeValueTo(this.state.defaultValue.value, this.state.defaultValue.text, true);
3344
+ }
3200
3345
  /**
3201
3346
  * Allows clearing the value of the variable to an empty value. Overrides default behavior of a MultiValueVariable
3202
3347
  */
@@ -3215,7 +3360,8 @@ function GroupByVariableRenderer({ model }) {
3215
3360
  noValueOnClear,
3216
3361
  options,
3217
3362
  includeAll,
3218
- allowCustomValue = true
3363
+ allowCustomValue = true,
3364
+ defaultValue
3219
3365
  } = model.useState();
3220
3366
  const values = React.useMemo(() => {
3221
3367
  const arrayValue = lodash.isArray(value) ? value : [value];
@@ -3233,6 +3379,7 @@ function GroupByVariableRenderer({ model }) {
3233
3379
  const [inputValue, setInputValue] = React.useState("");
3234
3380
  const [uncommittedValue, setUncommittedValue] = React.useState(values);
3235
3381
  const optionSearcher = React.useMemo(() => getOptionSearcher(options, includeAll), [options, includeAll]);
3382
+ const hasDefaultValue = defaultValue !== void 0;
3236
3383
  React.useEffect(() => {
3237
3384
  setUncommittedValue(values);
3238
3385
  }, [values]);
@@ -3260,7 +3407,7 @@ function GroupByVariableRenderer({ model }) {
3260
3407
  "aria-label": "Group by selector",
3261
3408
  "data-testid": `GroupBySelect-${key}`,
3262
3409
  id: key,
3263
- placeholder: "Select value",
3410
+ placeholder: "Group by label",
3264
3411
  width: "auto",
3265
3412
  allowCustomValue,
3266
3413
  inputValue,
@@ -3276,7 +3423,12 @@ function GroupByVariableRenderer({ model }) {
3276
3423
  isClearable: true,
3277
3424
  hideSelectedOptions: false,
3278
3425
  isLoading: isFetchingOptions,
3279
- components: { Option: OptionWithCheckbox },
3426
+ components: {
3427
+ Option: OptionWithCheckbox,
3428
+ ...hasDefaultValue ? {
3429
+ IndicatorsContainer: () => /* @__PURE__ */ React__default.default.createElement(DefaultGroupByCustomIndicatorContainer, { model })
3430
+ } : {}
3431
+ },
3280
3432
  onInputChange,
3281
3433
  onBlur: () => {
3282
3434
  model.changeValueTo(
@@ -3284,6 +3436,10 @@ function GroupByVariableRenderer({ model }) {
3284
3436
  uncommittedValue.map((x) => x.label),
3285
3437
  true
3286
3438
  );
3439
+ const restorable = model.checkIfRestorable(uncommittedValue.map((v) => v.value));
3440
+ if (restorable !== model.state.restorable) {
3441
+ model.setState({ restorable });
3442
+ }
3287
3443
  },
3288
3444
  onChange: (newValue, action) => {
3289
3445
  if (action.action === "clear" && noValueOnClear) {
@@ -3307,7 +3463,7 @@ function GroupByVariableRenderer({ model }) {
3307
3463
  "aria-label": "Group by selector",
3308
3464
  "data-testid": `GroupBySelect-${key}`,
3309
3465
  id: key,
3310
- placeholder: "Select value",
3466
+ placeholder: "Group by label",
3311
3467
  width: "auto",
3312
3468
  inputValue,
3313
3469
  value: uncommittedValue && uncommittedValue.length > 0 ? uncommittedValue : null,