Skip to content

🎥 Make videos programmatically with React

2.7 KB
Raw Blame
import {useHistory, useLocation} from '@docusaurus/router';
import type {WrapperProps} from '@docusaurus/types';
import '@remotion/promo-pages/dist/tailwind.css';
import {isInsideStudio} from '@remotion/studio-protocol';
import Layout from '@theme-original/Layout';
import type LayoutType from '@theme/Layout';
import {useCrawlChatSidePanel} from 'crawlchat-client';
import React, {
	useEffect,
	useLayoutEffect,
	useState,
	type ReactNode,
} from 'react';

type Props = WrapperProps<typeof LayoutType>;

const CRAWLCHAT_SCRIPT_ID = 'crawlchat-script';

// The script must be injected imperatively rather than rendered as JSX:
// embed.js declares a top-level `class CrawlChatEmbed` and mutates the DOM
// on load, so having it in the server-rendered HTML breaks hydration
// (React error #418) and any re-render of the <script> element executes it
// a second time ("Identifier 'CrawlChatEmbed' has already been declared").
const useCrawlChatScript = () => {
	useEffect(() => {
		if (
			document.body.classList.contains('studio-elements-embed') ||
			document.getElementById(CRAWLCHAT_SCRIPT_ID)
		) {
			return;
		}

		const script = document.createElement('script');
		script.src = 'https://crawlchat.app/embed.js';
		script.async = true;
		script.id = CRAWLCHAT_SCRIPT_ID;
		script.dataset.id = '67c0a28c5b075f0bb35e5366';
		script.dataset.sidepanel = 'true';
		script.dataset.small = 'true';
		document.body.appendChild(script);
		// No cleanup: removing the tag would not undo the global declaration,
		// and re-inserting it later would throw.
	}, []);
};

const LayoutWithCrawlChat = (props: Props): ReactNode => {
	useCrawlChatSidePanel({history: useHistory()});
	useCrawlChatScript();

	return <Layout {...props} />;
};

const LayoutWrapper = (props: Props): ReactNode => {
	const {pathname, search} = useLocation();
	const [isStudioElementsEmbed, setIsStudioElementsEmbed] = useState(false);

	useLayoutEffect(() => {
		// Client-side navigation drops the iframe's ?remotion-studio=true parameter.
		const isEmbedded =
			isInsideStudio() &&
			(pathname === '/elements' || pathname.startsWith('/elements/'));
		const isCaptionPicker =
			isEmbedded &&
			new URLSearchParams(search).get('remotion-studio-context') === 'captions';
		document.body.classList.toggle('studio-elements-embed', isEmbedded);
		document.body.classList.toggle('studio-caption-picker', isCaptionPicker);
		setIsStudioElementsEmbed(isEmbedded);

		return () => {
			document.body.classList.remove(
				'studio-elements-embed',
				'studio-caption-picker',
			);
		};
	}, [pathname, search]);

	if (isStudioElementsEmbed) {
		return <Layout {...props} noFooter />;
	}

	return <LayoutWithCrawlChat {...props} />;
};

export default LayoutWrapper;