Skip to content

Commit 1f0685b

Browse files
authored
Merge pull request #128 from headlamp-k8s/add-official-switch-for-plugins
plugin-catalog: Add a switch to show/hide non-official plugins quickly
2 parents baf2414 + 92726aa commit 1f0685b

File tree

1 file changed

+88
-3
lines changed
  • plugin-catalog/src/components/plugins

1 file changed

+88
-3
lines changed

plugin-catalog/src/components/plugins/List.tsx

+88-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { PluginManager } from '@kinvolk/headlamp-plugin/lib';
22
import { ConfigStore } from '@kinvolk/headlamp-plugin/lib';
3-
import { SectionHeader } from '@kinvolk/headlamp-plugin/lib/CommonComponents';
3+
import { ConfirmDialog, SectionHeader } from '@kinvolk/headlamp-plugin/lib/CommonComponents';
44
import { Loader } from '@kinvolk/headlamp-plugin/lib/components/common';
55
import { Box, Pagination, TextField } from '@mui/material';
66
import { Typography } from '@mui/material';
77
import React, { useEffect, useMemo, useState } from 'react';
88
import { PluginCard } from './PluginCard';
9+
import { Switch } from '@mui/material';
10+
import { FormControlLabel } from '@mui/material';
11+
import { isEqual } from 'lodash';
912

1013
const PAGE_SIZE = 60; // Maximum allowed by the API
1114
const ARTIFACTHUB_HEADLAMP_PLUGIN_KIND = '21';
@@ -151,6 +154,50 @@ export interface PurePluginListProps {
151154
search: string;
152155
onSearchChange: (event: React.ChangeEvent<HTMLInputElement>) => void;
153156
onPageChange: (event: React.ChangeEvent<unknown>, page: number) => void;
157+
isOfficialSwitchChecked: boolean;
158+
onOfficialSwitchChange: (isChecked: boolean) => void;
159+
}
160+
161+
interface OfficialSwitchProps {
162+
isChecked: boolean;
163+
onChange: (isChecked: boolean) => void;
164+
}
165+
166+
function OfficialSwitch(props: OfficialSwitchProps) {
167+
const { onChange : onOfficialSwitchChange, isChecked} = props;
168+
const [isConfirmOpen, setIsConfirmOpen] = useState(false);
169+
return (
170+
<>
171+
<FormControlLabel
172+
control={
173+
<Switch defaultChecked size="small" />
174+
}
175+
label={
176+
<Typography>
177+
Only Official
178+
</Typography>
179+
}
180+
checked={isChecked}
181+
onChange={() => {
182+
if (isChecked) {
183+
setIsConfirmOpen(true);
184+
} else {
185+
onOfficialSwitchChange(false);
186+
}
187+
}}
188+
/>
189+
<ConfirmDialog
190+
// @ts-ignore
191+
open={isConfirmOpen}
192+
title="Do you want to show non-official plugins?"
193+
description="Important: Non-official plugins may not be published by the actual projects they are related to, nor by Headlamp's maintainers. Are you sure you want to show them?"
194+
handleClose={() => setIsConfirmOpen(false)}
195+
onConfirm={() => {
196+
onOfficialSwitchChange(true);
197+
}}
198+
/>
199+
</>
200+
);
154201
}
155202

156203
export function PurePluginList({
@@ -160,11 +207,21 @@ export function PurePluginList({
160207
search,
161208
onSearchChange,
162209
onPageChange,
210+
isOfficialSwitchChecked,
211+
onOfficialSwitchChange,
163212
}: PurePluginListProps) {
164213
return (
165214
<>
166215
<SectionHeader
167216
title="Plugins"
217+
titleSideActions={[
218+
<Box pl={2}>
219+
<OfficialSwitch
220+
isChecked={isOfficialSwitchChecked}
221+
onChange={onOfficialSwitchChange}
222+
/>
223+
</Box>
224+
]}
168225
actions={[
169226
<TextField
170227
key="search"
@@ -218,6 +275,11 @@ export function PluginList() {
218275
const [allPlugins, setAllPlugins] = useState<PluginPackage[] | null>(null);
219276
const [page, setPage] = useState(1);
220277
const [totalPages, setTotalPages] = useState(0);
278+
const conf = configStore.useConfig()();
279+
const [fetchSettings, setFetchSettings] = useState<conf | null>({
280+
displayOnlyOfficialPlugins: true,
281+
displayOnlyVerifiedPlugins: true,
282+
});
221283

222284
useEffect(() => {
223285
const fetchAndProcessPlugins = async () => {
@@ -226,9 +288,28 @@ export function PluginList() {
226288
setTotalPages(totalPages);
227289
console.log(plugins, totalPages);
228290
};
229-
230291
fetchAndProcessPlugins();
231-
}, []);
292+
}, [fetchSettings]);
293+
294+
useEffect(() => {
295+
if (!conf) {
296+
return;
297+
}
298+
299+
setFetchSettings(oldSettings => {
300+
const newSettings = {
301+
...oldSettings,
302+
...conf,
303+
};
304+
305+
if (!isEqual(oldSettings, newSettings)) {
306+
return newSettings;
307+
}
308+
309+
return oldSettings;
310+
});
311+
},
312+
[conf]);
232313

233314
const filteredPlugins = useMemo(() => {
234315
if (!allPlugins) return null;
@@ -262,6 +343,10 @@ export function PluginList() {
262343
search={search}
263344
onSearchChange={handleSearchChange}
264345
onPageChange={handlePageChange}
346+
isOfficialSwitchChecked={conf?.displayOnlyOfficialPlugins ?? true}
347+
onOfficialSwitchChange={() => {
348+
configStore.update({ displayOnlyOfficialPlugins: !conf?.displayOnlyOfficialPlugins });
349+
}}
265350
/>
266351
);
267352
}

0 commit comments

Comments
 (0)