import "./CodeTheme.scss"; import { Box, ColumnLayout, Container, Header, Spinner } from "@awsui/components-react"; import classNames from "classnames"; import React, { ReactNode, useMemo, useState } from "react"; import { isLoaded, Loadable } from "../../utils/Loadable"; import { highlight } from "./Highlight"; import styles from "./SourceCodeDisplay.module.scss"; interface Props { sourceFileContent: Loadable; title: ReactNode; comments: Comment[]; isLoading: boolean; highlightMode: "csharp" | "xml"; buttons?: ReactNode; limitRows?: number; } export interface Comment { startLine: number; endLine: number; comment: ReactNode; } const SourceCodeDisplayInternal: React.FC = ({ sourceFileContent, title, comments, isLoading, highlightMode, buttons, limitRows }) => { const [isLimited, setIsLimited] = useState(true); const highlightedLines = useMemo(() => { if (isLoaded(sourceFileContent)) { return highlight(sourceFileContent.data, highlightMode); } return Array(); }, [highlightMode, sourceFileContent]); const limitedHighlightedLines = useMemo(() => { if (isLimited && limitRows !== undefined) { return highlightedLines.slice(0, limitRows); } return highlightedLines; }, [highlightedLines, isLimited, limitRows]); return ( {title} } footer={ limitRows != null && limitRows !== 0 && !isLoading && (
setIsLimited(!isLimited)}> {isLimited ? `See all ${highlightedLines.length} lines` : "See less"}
) } disableContentPaddings > {!isLoading ? ( {limitedHighlightedLines.map((line, i) => { let lineComments = Array(); lineComments.push(...comments.filter(comment => comment.startLine - 1 === i)); const standardLine = ( ); const commentSection = lineComments.length > 0 ? ( ) : null; return [standardLine, commentSection]; })} ) : ( )}
0 })} > {i + 1} 0 })} dangerouslySetInnerHTML={{ __html: `${line}` }} >
{lineComments.map(lineComment => lineComment.comment)}
); }; export const SourceCodeDisplay = React.memo(SourceCodeDisplayInternal);