SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
import type { VariableFormModel } from '../../DashboardSettings/Variables/variableFormModel';
import type {
SelectedVariableValue,
VariableSelection,
} from '../selectionTypes';
import { ALL_SELECTED } from './variablesUrlState';
// One default resolver, shared by the seed, the post-fetch reconcile and the
// payload fallback, so the bar, the fetch gate and the query payload can never
// disagree about a variable's default.
const ALL_SELECTION: VariableSelection = { value: null, allSelected: true };
/**
* A configured `defaultValue` reduced to its non-empty form (array as-is, string
* as-is), or undefined when unset — the one place the raw shapes are normalized.
*/
function configuredDefault(
defaultValue: VariableFormModel['defaultValue'],
): Exclude<SelectedVariableValue, null> | undefined {
if (Array.isArray(defaultValue)) {
return defaultValue.length > 0 ? defaultValue : undefined;
}
return defaultValue || undefined;
}
/**
* The configured default as a single value (first of an array). undefined means
* "no configured default", so callers fall through to the first option / ALL.
*/
function firstConfiguredDefault(model: VariableFormModel): string | undefined {
const value = configuredDefault(model.defaultValue);
if (value === undefined) {
return undefined;
}
return Array.isArray(value) ? String(value[0]) : String(value);
}
/** A TEXT variable's default: its configured default, else its textValue (always a string). */
export function textDefault(model: VariableFormModel): string {
return firstConfiguredDefault(model) ?? model.textValue;
}
/** Whether the configured default marks the ALL sentinel. */
function isAllDefault(
defaultValue: VariableFormModel['defaultValue'],
): boolean {
return (
defaultValue === ALL_SELECTED ||
(Array.isArray(defaultValue) &&
defaultValue.length === 1 &&
defaultValue[0] === ALL_SELECTED)
);
}
/**
* The default selection for a variable with nothing usable selected: its configured
* default, else ALL for an ALL-enabled multi-select, else the first option.
*/
function fillDefault(
model: VariableFormModel,
options: string[],
): VariableSelection {
const fallback = firstConfiguredDefault(model);
if (fallback && options.includes(fallback)) {
return {
value: model.multiSelect ? [fallback] : fallback,
allSelected: false,
};
}
// No usable configured default: an ALL-enabled multi-select defaults to ALL, not
// to an arbitrary first option — the same default the seed applies (keep in sync
// with resolveDefaultSelection).
if (model.multiSelect && model.showAllOption) {
return materializeAll(model, options, null) ?? ALL_SELECTION;
}
return {
value: model.multiSelect ? [options[0]] : options[0],
allSelected: false,
};
}
/**
* For an ALL selection, the value to materialize (or null when unchanged).
* Dynamic ALL travels as the `__all__` wire sentinel and renders ALL from the
* flag, so it needs no materialized value. Query/custom ALL must carry the full
* option array (the payload builder cannot expand it) — keep it in sync.
*/
function materializeAll(
model: VariableFormModel,
options: string[],
current: SelectedVariableValue,
): VariableSelection | null {
if (!model.multiSelect || model.type === 'DYNAMIC') {
return null;
}
const alreadyFull =
Array.isArray(current) &&
current.length === options.length &&
current.every((c) => options.includes(String(c)));
return alreadyFull ? null : { value: options, allSelected: true };
}
/**
* The seed-time default for a variable, before any options are fetched.
* - TEXT: the configured default (`defaultValue` → `textValue`), else empty.
* - CUSTOM/QUERY/DYNAMIC: the configured default; else ALL when allowAll is on;
* else a placeholder that {@link reconcileWithOptions} fills with the first
* option once the options resolve.
*/
export function resolveDefaultSelection(
model: VariableFormModel,
): VariableSelection {
if (model.type === 'TEXT') {
return { value: textDefault(model), allSelected: false };
}
if (isAllDefault(model.defaultValue)) {
return ALL_SELECTION;
}
const configured = configuredDefaultValue(model);
if (configured !== undefined) {
return {
value:
Array.isArray(configured) || !model.multiSelect ? configured : [configured],
allSelected: false,
};
}
if (model.multiSelect && model.showAllOption) {
return ALL_SELECTION;
}
return { value: model.multiSelect ? [] : '', allSelected: false };
}
interface ReconcileOptions {
/**
* Set when no other variable caused this refetch (time-range change, reload): the
* selection then outranks the options and is kept as-is. Leave false for a
* dependency cascade, where a selection that no longer applies must give way.
*/
preserveSelection?: boolean;
}
/**
* Reconciles a variable's current selection against its freshly-fetched options.
* Returns the next selection, or null when nothing should change (a valid pick is
* left untouched — local-first). Behaviour, in order:
* - materialize ALL to the full option set (query/custom);
* - keep a multi-select selection outright when `preserveSelection` is set;
* - keep a still-valid multi-select subset, dropping only entries the list no longer
* offers and the user did not type in (`customValues`);
* - otherwise auto-pick the default (or first option) so dependent variables and
* panels always resolve against a usable value.
*/
export function reconcileWithOptions(
model: VariableFormModel,
current: VariableSelection,
options: string[],
{ preserveSelection = false }: ReconcileOptions = {},
): VariableSelection | null {
if (options.length === 0) {
return null;
}
if (current.allSelected) {
return materializeAll(model, options, current.value);
}
if (
model.multiSelect &&
Array.isArray(current.value) &&
current.value.length > 0
) {
// A pick this window has no data for is still the user's filter; re-defaulting it
// here is what widened a single pick to ALL on every time-range change.
if (preserveSelection) {
return null;
}
// A typed value is in no option list, so it is never "no longer offered".
const custom = new Set(current.customValues ?? []);
const valid = current.value
.map(String)
.filter((c) => options.includes(c) || custom.has(c));
if (valid.length === current.value.length) {
return null;
}
if (valid.length === 0) {
return fillDefault(model, options);
}
const customValues = valid.filter((v) => custom.has(v));
return {
value: valid,
allSelected: false,
...(customValues.length > 0 && { customValues }),
};
}
if (!model.multiSelect) {
// Preserve any non-empty single value across a refetch — including a user-typed
// value that isn't in the fetched options (freeform). Only fall back to the
// default/first option when there is no value yet, so e.g. a time-range change
// (which refetches options) never wipes a value the user didn't change.
const hasValue =
!Array.isArray(current.value) &&
current.value !== '' &&
current.value !== null &&
current.value !== undefined;
return hasValue ? null : fillDefault(model, options);
}
return fillDefault(model, options);
}
/**
* The value to send for a variable when the user has made no selection yet
* (the payload fallback). Mirrors the configured default only — an ALL-by-default
* list variable resolves to `undefined` here (its concrete values are carried by
* the materialized selection once options are known), so it is omitted until then
* rather than sent wrong.
*/
export function configuredDefaultValue(
model: VariableFormModel,
): Exclude<SelectedVariableValue, null> | undefined {
if (model.type === 'TEXT') {
return textDefault(model);
}
return configuredDefault(model.defaultValue);
}
/** Normalize a selection's value to an order-independent key of its members. */
function valueSetKey(value: SelectedVariableValue): string {
const list = Array.isArray(value) ? value : value == null ? [] : [value];
return list.map(String).sort().join('||');
}
/** True when two selections carry the same ALL flag and the same value set. */
export function areSelectionsEqual(
a: VariableSelection,
b: VariableSelection,
): boolean {
return (
!!a.allSelected === !!b.allSelected &&
valueSetKey(a.value) === valueSetKey(b.value)
);
}