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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
* SonarQube
* Copyright (C) 2009-2024 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 { Checkbox } from '@sonarsource/echoes-react';
import {
Badge,
BasicSeparator,
HelperHintIcon,
InputSearch,
SubnavigationAccordion,
SubnavigationItem,
SubnavigationSubheading,
} from 'design-system';
import { sortBy } from 'lodash';
import { OpenAPIV3 } from 'openapi-types';
import React, { Fragment, useContext, useMemo, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { translate } from '../../../helpers/l10n';
import HelpTooltip from '../../../sonar-aligned/components/controls/HelpTooltip';
import { Dict } from '../../../types/types';
import { InternalExtension } from '../types';
import { URL_DIVIDER, getApiEndpointKey } from '../utils';
import ApiFilterContext from './ApiFilterContext';
import RestMethodPill from './RestMethodPill';
interface Api {
info: OpenAPIV3.OperationObject<InternalExtension>;
method: string;
name: string;
}
interface Props {
apisList: Api[];
docInfo: OpenAPIV3.InfoObject;
}
const METHOD_ORDER: Dict<number> = {
post: 0,
get: 1,
patch: 2,
delete: 3,
};
export default function ApiSidebar({ apisList, docInfo }: Readonly<Props>) {
const [search, setSearch] = useState('');
const navigate = useNavigate();
const location = useLocation();
const { showInternal, setShowInternal } = useContext(ApiFilterContext);
const activeApi = location.hash.replace('#', '').split(URL_DIVIDER);
const handleApiClick = (value: string) => {
navigate(`.#${value}`, { replace: true });
};
const lowerCaseSearch = search.toLowerCase();
const groupedList = useMemo(
() =>
apisList
.filter(
(api) =>
api.name.toLowerCase().includes(lowerCaseSearch) ||
api.method.toLowerCase().includes(lowerCaseSearch) ||
api.info.summary?.toLowerCase().includes(lowerCaseSearch),
)
.filter((api) => showInternal || !api.info['x-internal'])
.reduce<Record<string, Api[]>>((acc, api) => {
const subgroup = api.name.split('/')[1];
return {
...acc,
[subgroup]: [...(acc[subgroup] ?? []), api],
};
}, {}),
[lowerCaseSearch, apisList, showInternal],
);
return (
<>
<h1 className="sw-mb-2">{docInfo.title}</h1>
<InputSearch
className="sw-w-full"
onChange={setSearch}
placeholder={translate('api_documentation.v2.search')}
value={search}
/>
<div className="sw-mt-4 sw-flex sw-items-center">
<Checkbox
checked={showInternal}
label={translate('api_documentation.show_internal_v2')}
onCheck={() => setShowInternal((prev) => !prev)}
/>
<HelpTooltip
className="sw-ml-2"
overlay={translate('api_documentation.internal_tooltip_v2')}
>
<HelperHintIcon aria-label="help-tooltip" />
</HelpTooltip>
</div>
{Object.entries(groupedList).map(([group, apis]) => (
<SubnavigationAccordion
className="sw-mt-2"
header={group}
id={`web-api-${group}`}
initExpanded={apis.some(
({ name, method }) => name === activeApi[0] && method === activeApi[1],
)}
key={group}
>
{sortBy(apis, (a) => [a.name, METHOD_ORDER[a.method]]).map(
({ method, name, info }, index, sorted) => {
const resourceName = getResourceFromName(name);
const previousResourceName =
index > 0 ? getResourceFromName(sorted[index - 1].name) : undefined;
const isNewResource = resourceName !== previousResourceName;
return (
<Fragment key={getApiEndpointKey(name, method)}>
{index > 0 && isNewResource && <BasicSeparator />}
{(index === 0 || isNewResource) && (
<SubnavigationSubheading>{resourceName}</SubnavigationSubheading>
)}
<SubnavigationItem
active={name === activeApi[0] && method === activeApi[1]}
onClick={handleApiClick}
value={getApiEndpointKey(name, method)}
>
<div className="sw-flex sw-gap-2 sw-w-full sw-justify-between">
<div className="sw-flex sw-gap-2">
<RestMethodPill method={method} />
<div>{info.summary ?? name}</div>
</div>
{(info['x-internal'] || info.deprecated) && (
<div className="sw-flex sw-flex-col sw-justify-center sw-gap-2">
{info['x-internal'] && (
<Badge variant="new" className="sw-self-center">
{translate('internal')}
</Badge>
)}
{info.deprecated && (
<Badge variant="deleted" className="sw-self-center">
{translate('deprecated')}
</Badge>
)}
</div>
)}
</div>
</SubnavigationItem>
</Fragment>
);
},
)}
</SubnavigationAccordion>
))}
</>
);
}
function getResourceFromName(name: string) {
const parts = name.split('/').slice(2); // remove domain + pre-slash empty string
if (name.endsWith('}')) {
parts.pop(); // remove the resource id
}
return parts.join('/');
}
|