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.

6.2 KB
Raw Blame
import { useMemo, useState } from 'react';
// eslint-disable-next-line no-restricted-imports
import { useSelector } from 'react-redux';
import { useAppLocation } from 'lib/router/useAppLocation';
import { matchRoute } from 'lib/router/matchRoute';
import { useCopyToClipboard } from 'react-use';
import { Color } from '@signozhq/design-tokens';
import { Button } from 'antd';
import { Switch } from '@signozhq/ui/switch';
import { Typography } from '@signozhq/ui/typography';
import logEvent from 'api/common/logEvent';
import { QueryParams } from 'constants/query';
import ROUTES from 'constants/routes';
import useUrlQuery from 'hooks/useUrlQuery';
import GetMinMax from 'lib/getMinMax';
import { Check, Info, Link2 } from '@signozhq/icons';
import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime';
import { getAbsoluteUrl } from 'utils/basePath';

const routesToBeSharedWithTime = [
	ROUTES.LOGS_EXPLORER,
	ROUTES.TRACES_EXPLORER,
	ROUTES.METRICS_EXPLORER_EXPLORER,
	ROUTES.METER_EXPLORER,
];

/**
 * An optional, page-specific toggle in the share dialog (e.g. a dashboard's
 * "Include variables"). When enabled, `apply` mutates the URL params that go into
 * the shared link. Keeps this shared modal generic — the page owns what it adds.
 */
export interface ShareURLExtraOption {
	label: string;
	defaultEnabled?: boolean;
	apply: (params: URLSearchParams) => void;
}

interface ShareURLModalProps {
	extraOption?: ShareURLExtraOption;
}

function ShareURLModal({ extraOption }: ShareURLModalProps): JSX.Element {
	const urlQuery = useUrlQuery();
	const location = useAppLocation();
	const { selectedTime } = useSelector<AppState, GlobalReducer>(
		(state) => state.globalTime,
	);

	const [enableAbsoluteTime, setEnableAbsoluteTime] = useState(
		selectedTime !== 'custom',
	);
	const [enableExtraOption, setEnableExtraOption] = useState(
		extraOption?.defaultEnabled ?? false,
	);

	const startTime = urlQuery.get(QueryParams.startTime);
	const endTime = urlQuery.get(QueryParams.endTime);
	const relativeTime = urlQuery.get(QueryParams.relativeTime);

	const [isURLCopied, setIsURLCopied] = useState(false);
	const [, handleCopyToClipboard] = useCopyToClipboard();

	const isValidateRelativeTime = useMemo(
		() =>
			selectedTime !== 'custom' ||
			(startTime && endTime && selectedTime === 'custom'),
		[startTime, endTime, selectedTime],
	);

	const shareURLWithTime = useMemo(
		() => relativeTime || (startTime && endTime),
		[relativeTime, startTime, endTime],
	);

	const isRouteToBeSharedWithTime = useMemo(
		() =>
			routesToBeSharedWithTime.some((route) =>
				matchRoute(location.pathname, route, { exact: true }),
			),
		[location.pathname],
	);

	// eslint-disable-next-line sonarjs/cognitive-complexity
	const processURL = (): string => {
		let currentUrl = window.location.href;
		const isCustomTime = !!(startTime && endTime && selectedTime === 'custom');

		if (shareURLWithTime || isRouteToBeSharedWithTime) {
			if (enableAbsoluteTime || isCustomTime) {
				if (selectedTime === 'custom') {
					if (startTime && endTime) {
						urlQuery.set(QueryParams.startTime, startTime.toString());
						urlQuery.set(QueryParams.endTime, endTime.toString());
					}
				} else {
					const { minTime, maxTime } = GetMinMax(selectedTime);

					urlQuery.set(QueryParams.startTime, minTime.toString());
					urlQuery.set(QueryParams.endTime, maxTime.toString());
				}

				urlQuery.delete(QueryParams.relativeTime);

				currentUrl = getAbsoluteUrl(`${location.pathname}?${urlQuery.toString()}`);
			} else {
				urlQuery.delete(QueryParams.startTime);
				urlQuery.delete(QueryParams.endTime);

				urlQuery.set(QueryParams.relativeTime, selectedTime);
				currentUrl = getAbsoluteUrl(`${location.pathname}?${urlQuery.toString()}`);
			}
		}

		if (extraOption && enableExtraOption) {
			extraOption.apply(urlQuery);
			currentUrl = getAbsoluteUrl(`${location.pathname}?${urlQuery.toString()}`);
		}

		return currentUrl;
	};

	const handleCopyURL = (): void => {
		const URL = processURL();

		handleCopyToClipboard(URL);
		setIsURLCopied(true);

		logEvent('Share: Copy link clicked', {
			page: location.pathname,
			URL,
		});

		setTimeout(() => {
			setIsURLCopied(false);
		}, 1000);
	};

	return (
		<div className="share-modal-content">
			{(shareURLWithTime || isRouteToBeSharedWithTime) && (
				<>
					<div className="absolute-relative-time-toggler-container">
						<Typography.Text className="absolute-relative-time-toggler-label">
							Enable absolute time
						</Typography.Text>

						<div className="absolute-relative-time-toggler">
							{!isValidateRelativeTime && (
								<Info size={14} color={Color.BG_AMBER_600} />
							)}
							<Switch
								color="primary"
								textPlacement="right"
								disabledTooltip="Please select / enter valid relative time to toggle."
								value={enableAbsoluteTime}
								disabled={!isValidateRelativeTime}
								onChange={(): void => {
									setEnableAbsoluteTime((prev) => !prev);
								}}
							/>
						</div>
					</div>

					{!isValidateRelativeTime && (
						<div className="absolute-relative-time-error">
							Please select / enter valid relative time to toggle.
						</div>
					)}
				</>
			)}

			{extraOption && (
				<div className="absolute-relative-time-toggler-container">
					<Typography.Text className="absolute-relative-time-toggler-label">
						{extraOption.label}
					</Typography.Text>
					<div className="absolute-relative-time-toggler">
						<Switch
							color="primary"
							textPlacement="right"
							value={enableExtraOption}
							onChange={(): void => setEnableExtraOption((prev) => !prev)}
						/>
					</div>
				</div>
			)}

			<div className="share-link">
				<div className="url-share-container">
					<div className="url-share-container-header">
						<Typography.Text className="url-share-title">
							Share page link
						</Typography.Text>
						<Typography.Text className="url-share-sub-title">
							Share the current page link with your team member
						</Typography.Text>
					</div>

					<Button
						className="periscope-btn secondary"
						onClick={handleCopyURL}
						icon={isURLCopied ? <Check size={14} /> : <Link2 size={14} />}
					>
						Copy page link
					</Button>
				</div>
			</div>
		</div>
	);
}

export default ShareURLModal;