aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-docs/src/layouts/components/Sidebar.js
blob: cf68d376f42feb940b713bba8c3730282c86191b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * SonarQube
 * Copyright (C) 2009-2018 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 React from 'react';
import Link from 'gatsby-link';
import { fromPairs } from 'lodash';
import { sortNodes } from '../utils';
import CategoryLink from './CategoryLink';
import VersionSelect from './VersionSelect';
import Search from './Search';
import SearchEntryResult from './SearchEntryResult';

export default class Sidebar extends React.PureComponent {
  state = { loaded: false, results: [], versions: [] };

  componentDidMount() {
    this.loadVersions();
  }

  loadVersions() {
    fetch('/DocsVersions.json').then(response =>
      response.json().then(json => {
        this.setState({ loaded: true, versions: json });
      })
    );
  }

  getPagesHierarchy() {
    const categories = sortNodes(
      this.props.pages.filter(p => p.fields.slug.split('/').length === 3)
    );
    const pages = this.props.pages.filter(p => p.fields.slug.split('/').length > 3);
    const categoriesObject = fromPairs(categories.map(c => [c.fields.slug, { ...c, pages: [] }]));
    pages.forEach(page => {
      const parentSlug = page.fields.slug
        .split('/')
        .slice(0, 2)
        .join('/');
      categoriesObject[parentSlug + '/'].pages.push(page);
    });
    return categoriesObject;
  }

  renderResults = () => {
    return (
      <div>
        {this.state.results.map(result => (
          <SearchEntryResult
            active={
              (this.props.location.pathname === result.page.slug && result.page.slug === '/') ||
              (result.page.slug !== '/' && this.props.location.pathname.endsWith(result.page.slug))
            }
            key={result.page.id}
            result={result}
          />
        ))}
      </div>
    );
  };

  handleSearch = results => {
    this.setState({ results });
  };

  render() {
    const nodes = this.getPagesHierarchy();
    const isOnCurrentVersion =
      this.state.versions.find(v => v.value === this.props.version) !== undefined;
    return (
      <div className="page-sidebar">
        <div className="sidebar-header">
          <Link to="/">
            <img
              alt="Continuous Code Quality"
              css={{ verticalAlign: 'top', margin: 0 }}
              width="160"
              src="/images/SonarQubeIcon.svg"
              title="Continuous Code Quality"
            />
          </Link>
          <VersionSelect
            location={this.props.location}
            version={this.props.version}
            versions={this.state.versions}
          />

          {this.state.loaded &&
            !isOnCurrentVersion && (
              <div className="alert alert-warning">
                This is an archived version of the doc for{' '}
                <b>SonarQube version {this.props.version}</b>. <a href="/">See Documentation</a> for
                current functionnality.
              </div>
            )}
        </div>
        <div className="page-indexes">
          <Search pages={this.props.pages} onResultsChange={this.handleSearch} />
          {this.state.results.length > 0 && this.renderResults()}
          {this.state.results.length === 0 &&
            Object.keys(nodes).map(key => (
              <CategoryLink
                key={key}
                headers={this.props.headers}
                node={nodes[key]}
                location={this.props.location}
              />
            ))}
        </div>
      </div>
    );
  }
}