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.

1.1 KB
Raw Blame
import { useEffect, useState } from 'react';

import { isLanguageRegistered, loadLanguage } from '../utils/syntaxLanguages';

/**
 * Registers `language` with Prism on demand, reporting when it is ready to
 * highlight with. Already-loaded languages report ready on the first render, so a
 * second fence of the same language never flashes unhighlighted.
 */
export function usePrismLanguage(language: string | null): boolean {
	const [isReady, setIsReady] = useState(
		() => !!language && isLanguageRegistered(language),
	);

	useEffect(() => {
		if (!language) {
			setIsReady(false);
			return undefined;
		}
		if (isLanguageRegistered(language)) {
			setIsReady(true);
			return undefined;
		}

		setIsReady(false);
		let isStale = false;
		// `loadLanguage` resolves false rather than rejecting, so there is no failure
		// path here beyond leaving the block unhighlighted.
		void loadLanguage(language).then((loaded): boolean => {
			if (!isStale && loaded) {
				setIsReady(true);
			}
			return loaded;
		});

		return (): void => {
			isStale = true;
		};
	}, [language]);

	return isReady;
}