addons/isl/src/CommitInfoView/RenderMarkup.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {useAtomValue} from 'jotai';
b69ab319import {cached} from 'shared/LRU';
b69ab3110import clientToServerAPI from '../ClientToServerAPI';
b69ab3111import {codeReviewProvider} from '../codeReview/CodeReviewInfo';
b69ab3112import {atomFamilyWeak, lazyAtom} from '../jotaiUtils';
b69ab3113
b69ab3114import './RenderedMarkup.css';
b69ab3115
b69ab3116const renderedMarkup = atomFamilyWeak((markup: string) => {
b69ab3117 // This is an atom to trigger re-render when the server returns.
b69ab3118 return lazyAtom(get => {
b69ab3119 const provider = get(codeReviewProvider);
b69ab3120 if (provider?.enableMessageSyncing !== true) {
b69ab3121 return null;
b69ab3122 }
b69ab3123 return renderMarkupToHTML(markup);
b69ab3124 }, null);
b69ab3125});
b69ab3126
b69ab3127let requestId = 0;
b69ab3128
b69ab3129const renderMarkupToHTML = cached((markup: string): Promise<string> | string => {
b69ab3130 requestId += 1;
b69ab3131 const id = requestId;
b69ab3132 clientToServerAPI.postMessage({type: 'renderMarkup', markup, id});
b69ab3133 return new Promise(resolve => {
b69ab3134 clientToServerAPI
b69ab3135 .nextMessageMatching('renderedMarkup', message => message.id === id)
b69ab3136 .then(message => resolve(message.html));
b69ab3137 });
b69ab3138});
b69ab3139
b69ab3140export function RenderMarkup({children}: {children: string}) {
b69ab3141 const renderedHtml = useAtomValue(renderedMarkup(children));
b69ab3142 // TODO: We could consider using DOM purify to sanitize this HTML,
b69ab3143 // though this html is coming directly from a trusted server.
b69ab3144 return renderedHtml != null ? (
b69ab3145 <div className="rendered-markup" dangerouslySetInnerHTML={{__html: renderedHtml}} />
b69ab3146 ) : (
b69ab3147 <div>{children}</div>
b69ab3148 );
b69ab3149}