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.

9.2 KB
Raw Blame
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Plus, Search } from '@signozhq/icons';
import { Input } from '@signozhq/ui/input';
import { SelectSimple } from '@signozhq/ui/select';
import { Typography } from '@signozhq/ui/typography';
import { Skeleton } from 'antd';
import logEvent from 'api/common/logEvent';
import ErrorEmptyState from 'components/Alerts/ErrorEmptyState';
import NoResultsEmptyState from 'components/Alerts/NoResultsEmptyState';
import TanStackTable, { ColumnSelector } from 'components/TanStackTableView';
import { useCalculatedPageSize } from 'components/TanStackTableView/useCalculatedPageSize';
import { useTableParams } from 'components/TanStackTableView/useTableParams';
import TextToolTip from 'components/TextToolTip';
import { ChannelKind } from 'container/CreateAlertChannels/types';
import { useUrlSearchState } from 'hooks/useUrlSearchState';
import AuthZButton from 'lib/authz/components/AuthZButton/AuthZButton';
import { useNotificationChannelCollectionPermissions } from 'hooks/notificationChannels/useNotificationChannelCollectionPermissions';
import { AuthZGuardContent } from 'lib/authz/components/AuthZGuard/AuthZGuardContent';
import PermissionDeniedCallout from 'lib/authz/components/PermissionDeniedCallout/PermissionDeniedCallout';
import {
	NotificationChannelCreatePermission,
	NotificationChannelListPermission,
} from 'lib/authz/hooks/useAuthZ/permissions/notification-channel.permissions';
import { parseAsString, useQueryState } from 'nuqs';
import { useTimezone } from 'providers/Timezone';

import ChannelActionsMenu from './components/ChannelActionsMenu/ChannelActionsMenu';
import ChannelJsonDialog from './components/ChannelJsonDialog/ChannelJsonDialog';
import ChannelsEmptyState from './components/ChannelsEmptyState/ChannelsEmptyState';
import {
	CHANNEL_KIND_LABEL,
	KIND_FILTER_ALL,
	KIND_FILTER_OPTIONS,
	KIND_KEY,
	SEARCH_KEY,
} from './constants';
import { useChannelDelete } from './hooks/useChannelDelete';
import { useChannelFormView } from './hooks/useChannelFormView';
import { useChannelsData } from './hooks/useChannelsData';
import styles from './NotificationChannels.module.scss';
import { getChannelColumns } from './table.config';
import type { Channel } from './types';

const QUERY_PARAMS_CONFIG = {
	orderBy: 'orderBy',
	page: 'page',
	limit: 'limit',
} as const;

const CREATE_CHECKS = [NotificationChannelCreatePermission];
const LIST_CHECKS = [NotificationChannelListPermission];
const DEFAULT_PAGE = 1;
const DEFAULT_LIMIT = 10;

function NotificationChannels(): JSX.Element {
	const { formatTimezoneAdjustedTimestamp } = useTimezone();
	const formView = useChannelFormView();

	const { containerRef, calculatedPageSize } = useCalculatedPageSize({
		rowHeight: 46,
	});

	const { orderBy, page, limit, setLimit, setPage } = useTableParams(
		QUERY_PARAMS_CONFIG,
		{
			page: DEFAULT_PAGE,
			limit: DEFAULT_LIMIT,
			storageKey: 'notification-channels',
			calculatedPageSize,
			cleanupOnUnmount: true,
		},
	);

	const resetPageOnSearch = useCallback((): void => {
		setPage(1);
	}, [setPage]);

	const { searchText, debouncedSearch, handleSearchChange, clearSearch } =
		useUrlSearchState(SEARCH_KEY, { onDebouncedChange: resetPageOnSearch });

	const [kind, setKind] = useQueryState(
		KIND_KEY,
		parseAsString.withDefault(KIND_FILTER_ALL),
	);

	const handleKindChange = useCallback(
		(value: string | string[]): void => {
			const next = value as string;
			void setKind(next === KIND_FILTER_ALL ? null : next);
			setPage(1);
		},
		[setKind, setPage],
	);

	const { canList } = useNotificationChannelCollectionPermissions();
	const { channels, total, isFetching, isError, refetch } = useChannelsData({
		search: debouncedSearch,
		kind,
		orderBy,
		page,
		limit,
	});

	const deletion = useChannelDelete();
	const [jsonChannel, setJsonChannel] = useState<Channel | null>(null);

	useEffect(() => {
		void logEvent('Alert Channel: Channel list page visited', {});
	}, []);

	const handleEdit = useCallback(
		(channel: Channel, options?: { newTab?: boolean }): void => {
			formView.openEdit(channel.id, options);
		},
		[formView],
	);

	const handleRowClick = useCallback(
		(channel: Channel): void => formView.openEdit(channel.id),
		[formView],
	);

	const handleRowClickNewTab = useCallback(
		(channel: Channel): void => formView.openEdit(channel.id, { newTab: true }),
		[formView],
	);

	const columns = useMemo(
		() => getChannelColumns(formatTimezoneAdjustedTimestamp),
		[formatTimezoneAdjustedTimestamp],
	);

	const columnsWithActions = useMemo(
		() => [
			...columns,
			{
				id: 'actions',
				header: (): JSX.Element => (
					<span className={styles.actionsHeader}>Actions</span>
				),
				accessorKey: 'id',
				width: { fixed: '96px', ignoreLastColumnFill: true },
				enableSort: false,
				enableResize: false,
				enableRemove: false,
				enableMove: false,
				pin: 'right' as const,
				cell: ({ row }: { row: Channel }): JSX.Element => (
					<ChannelActionsMenu
						channel={row}
						onOpen={handleEdit}
						onDelete={deletion.request}
						onViewJson={setJsonChannel}
					/>
				),
			},
		],
		[columns, handleEdit, deletion.request],
	);

	const hasActiveFilters = searchText.length > 0 || kind !== KIND_FILTER_ALL;
	// With a kind picked, creating one of that kind is the likely next step, so
	// the button offers it rather than making the type be chosen again.
	const filteredKind =
		kind !== KIND_FILTER_ALL ? (kind as ChannelKind) : undefined;
	const isEmpty = !isFetching && channels.length === 0;

	const handleClearFilters = useCallback((): void => {
		clearSearch();
		void setKind(null);
	}, [clearSearch, setKind]);

	return (
		<div className={styles.container}>
			<header className={styles.headerBand}>
				<div className={styles.commandHeader}>
					<div className={styles.headingBlock}>
						<Typography.Title className={styles.title}>
							{filteredKind
								? `All ${CHANNEL_KIND_LABEL[filteredKind]} channels`
								: 'All notification channels'}
						</Typography.Title>
						<span className={styles.countPill}>{total}</span>
					</div>
					<div className={styles.grow} />
					{!isEmpty && (
						<ColumnSelector
							columns={columnsWithActions}
							storageKey="notification-channels-columns"
							testId="channels-columns"
						/>
					)}
					<AuthZButton
						checks={CREATE_CHECKS}
						variant="solid"
						color="primary"
						size="sm"
						prefix={<Plus size={14} />}
						onClick={(): void => formView.openCreate(filteredKind)}
						testId="channels-create"
					>
						{filteredKind
							? `New ${CHANNEL_KIND_LABEL[filteredKind]} Channel`
							: 'New Channel'}
					</AuthZButton>
					<TextToolTip
						text="More details on how to configure notification channels"
						url="https://signoz.io/docs/setup-alerts-notification/"
						urlText="Learn More"
					/>
				</div>

				<div className={styles.filtersRow}>
					<Input
						containerClassName={styles.searchInput}
						placeholder="Search by channel name"
						value={searchText}
						onChange={handleSearchChange}
						disabled={!canList}
						suffix={<Search size={14} className={styles.searchIcon} />}
						testId="channels-search"
					/>
					<SelectSimple
						className={styles.kindSelect}
						items={KIND_FILTER_OPTIONS}
						value={kind}
						onChange={handleKindChange}
						disabled={!canList}
						withPortal={false}
						testId="channels-kind-filter"
					/>
				</div>
			</header>

			<div ref={containerRef} className={styles.tableContainer}>
				<AuthZGuardContent
					checks={LIST_CHECKS}
					fallback={({ deniedPermissions }): JSX.Element => (
						<PermissionDeniedCallout
							deniedPermissions={deniedPermissions}
							className={styles.guardInset}
						/>
					)}
					fallbackOnLoading={
						<div className={styles.guardInset}>
							<Skeleton active paragraph={{ rows: 6 }} />
						</div>
					}
				>
					{isError ? (
						<ErrorEmptyState
							title="Failed to load notification channels"
							onRefresh={refetch}
						/>
					) : isEmpty && hasActiveFilters ? (
						<NoResultsEmptyState
							title="No matching channels"
							subtitle="No notification channels match your search. Try adjusting your search criteria."
							onClear={handleClearFilters}
							clearButtonText="Clear Search"
						/>
					) : isEmpty ? (
						<ChannelsEmptyState
							onCreate={(): void => formView.openCreate(filteredKind)}
						/>
					) : (
						<TanStackTable<Channel>
							data={channels}
							columns={columnsWithActions}
							isLoading={isFetching}
							getRowKey={(row): string => row.id}
							getItemKey={(row): string => row.id}
							columnStorageKey="notification-channels-columns"
							enableQueryParams={QUERY_PARAMS_CONFIG}
							onRowClick={handleRowClick}
							onRowClickNewTab={handleRowClickNewTab}
							pagination={{
								total,
								calculatedPageSize,
								onLimitChange: setLimit,
								showTotalCount: true,
							}}
							paginationClassname={styles.paginationContainer}
							enableAlternatingRowColors
							plainTextCellLineClamp={1}
						/>
					)}
				</AuthZGuardContent>
			</div>

			{jsonChannel && (
				<ChannelJsonDialog
					channelId={jsonChannel.id}
					channelName={jsonChannel.displayName}
					onClose={(): void => setJsonChannel(null)}
				/>
			)}

			{deletion.contextHolder}
		</div>
	);
}

export default NotificationChannels;