Skip to content

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.

2.6 KB
Raw Blame
import { Dispatch, SetStateAction } from 'react';
import getLocalStorageKey from 'api/browser/localstorage/get';
import setLocalStorageKey from 'api/browser/localstorage/set';
import { TelemetryFieldKey } from 'api/v5/v5';
import { LOCALSTORAGE } from 'constants/localStorage';
import {
	defaultOptionsQuery,
	ensureLogsRequiredColumns,
} from 'container/OptionsMenu/constants';
import { FontSize, OptionsQuery } from 'container/OptionsMenu/types';

import { FormattingOptions, PreferenceMode, Preferences } from '../types';

// --- LOGS preferences updater config ---
const getLogsUpdaterConfig = (
	preferences: Preferences | null,
	redirectWithOptionsData: (options: OptionsQuery) => void,
	setSavedViewPreferences: Dispatch<SetStateAction<Preferences | null>>,
): {
	updateColumns: (newColumns: TelemetryFieldKey[], mode: string) => void;
	updateFormatting: (newFormatting: FormattingOptions, mode: string) => void;
} => ({
	updateColumns: (newColumns: TelemetryFieldKey[], mode: string): void => {
		const guardedColumns = ensureLogsRequiredColumns(newColumns);
		if (mode === PreferenceMode.SAVED_VIEW) {
			setSavedViewPreferences((prev) => {
				if (!prev) {
					return {
						columns: guardedColumns,
						formatting: {
							maxLines: 1,
							format: 'table',
							fontSize: 'small' as FontSize,
							version: 1,
						},
					};
				}

				return {
					...prev,
					columns: guardedColumns,
				};
			});
		}

		if (mode === PreferenceMode.DIRECT) {
			// just need to update the columns see for remove props
			redirectWithOptionsData({
				...defaultOptionsQuery,
				...preferences?.formatting,
				selectColumns: guardedColumns,
			});

			// Also update local storage
			const local = JSON.parse(
				getLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',
			);
			local.selectColumns = guardedColumns;
			setLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS, JSON.stringify(local));
		}
	},
	updateFormatting: (newFormatting: FormattingOptions, mode: string): void => {
		if (mode === PreferenceMode.SAVED_VIEW) {
			setSavedViewPreferences((prev) => {
				if (!prev) {
					return { columns: [], formatting: newFormatting };
				}
				return {
					...prev,
					formatting: newFormatting,
				};
			});
		}

		if (mode === PreferenceMode.DIRECT) {
			redirectWithOptionsData({
				...defaultOptionsQuery,
				...preferences?.formatting,
				...newFormatting,
			});

			// Also update local storage
			const local = JSON.parse(
				getLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS) || '{}',
			);
			Object.assign(local, newFormatting);
			setLocalStorageKey(LOCALSTORAGE.LOGS_LIST_OPTIONS, JSON.stringify(local));
		}
	},
});

export default getLogsUpdaterConfig;