/* * SonarQube * Copyright (C) 2009-2023 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { debounce } from 'lodash'; import * as React from 'react'; import { getParents } from '../../api/components'; import withBranchStatusActions from '../../app/components/branch-status/withBranchStatusActions'; import { isPullRequest } from '../../helpers/branch-like'; import { BranchLike } from '../../types/branch-like'; import { Issue, SourceViewerFile } from '../../types/types'; import SourceViewer from '../SourceViewer/SourceViewer'; import WorkspaceComponentTitle from './WorkspaceComponentTitle'; import WorkspaceHeader, { Props as WorkspaceHeaderProps } from './WorkspaceHeader'; import { ComponentDescriptor } from './context'; export interface Props extends Omit { component: ComponentDescriptor; fetchBranchStatus: (branchLike: BranchLike, projectKey: string) => Promise; height: number; onClose: (componentKey: string) => void; onLoad: (details: { key: string; name: string; qualifier: string }) => void; } export class WorkspaceComponentViewer extends React.PureComponent { container?: HTMLElement | null; constructor(props: Props) { super(props); this.refreshBranchStatus = debounce(this.refreshBranchStatus, 1000); } componentDidMount() { if (document.documentElement) { document.documentElement.classList.add('with-workspace'); } } componentWillUnmount() { if (document.documentElement) { document.documentElement.classList.remove('with-workspace'); } } handleClose = () => { this.props.onClose(this.props.component.key); }; handleIssueChange = (_: Issue) => { this.refreshBranchStatus(); }; handleLoaded = (component: SourceViewerFile) => { this.props.onLoad({ key: this.props.component.key, name: component.path, qualifier: component.q, }); if (this.container && this.props.component.line) { const row = this.container.querySelector( `.it__source-line[data-line-number="${this.props.component.line}"]` ); if (row) { row.scrollIntoView({ block: 'center' }); } } }; refreshBranchStatus = () => { const { component } = this.props; const { branchLike } = component; if (branchLike && isPullRequest(branchLike)) { getParents(component.key).then( (parents?: any[]) => { if (parents && parents.length > 0) { this.props.fetchBranchStatus(branchLike, parents.pop().key); } }, () => {} ); } }; render() { const { component } = this.props; return (
(this.container = node)} style={{ height: this.props.height }} >
); } } export default withBranchStatusActions(WorkspaceComponentViewer);