From 0fc63fa49cd21c2f0e325e15ffbf24fb61a16801 Mon Sep 17 00:00:00 2001 From: ItsNik <106100177+Its4Nik@users.noreply.github.com> Date: Tue, 27 Aug 2024 07:35:20 +0200 Subject: [PATCH] Added: Sorting for hosts and Better Dropdowns with icons (#7) --- src/App.js | 30 +++++++++++++++-- src/components/Controls.js | 33 +++++++++---------- src/components/DataFetcher.js | 17 ++++++++-- src/components/ThemeSwitcher.js | 4 +-- src/components/dropdowns/SortDropdown.js | 28 ++++++++++++++++ src/components/dropdowns/ThemeDropdown.js | 39 +++++++++++++++++++++++ 6 files changed, 127 insertions(+), 24 deletions(-) create mode 100644 src/components/dropdowns/SortDropdown.js create mode 100644 src/components/dropdowns/ThemeDropdown.js diff --git a/src/App.js b/src/App.js index 37a7db2..7f02d8d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,4 +1,4 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { toast, ToastContainer } from 'react-toastify'; import HostStats from './components/HostStats'; import ConfigFetcher from './components/ConfigFetcher'; @@ -10,14 +10,15 @@ import Loading from './components/Loading'; function App() { const [data, setData] = useState({}); const [isInitialLoad, setIsInitialLoad] = useState(true); - const [intervalTime, setIntervalTime] = useState(10000); const [theme, setTheme] = useState(''); + const [intervalTime, setIntervalTime] = useState(10000); const [loadingTheme, setLoadingTheme] = useState(false); const [apihost, setApihost] = useState(''); const [apiKey, setApiKey] = useState(''); const [logoSize, setLogoSize] = useState(''); const [darkModeLogoColor, setDarkModeLogoColor] = useState(''); const [lightModeLogoColor, setLightModeLogoColor] = useState(''); + const [sortOption, setSortOption] = useState('name-asc'); const handleConfigLoaded = (configData) => { setApihost(configData.API_URL); @@ -27,6 +28,27 @@ function App() { setLightModeLogoColor(configData.LIGHT_MODE_LOGO_COLOR); }; + const sortHosts = (hostsData, option) => { + const sortedHosts = [...Object.keys(hostsData)].sort((a, b) => { + const aData = hostsData[a]; + const bData = hostsData[b]; + + switch (option) { + case 'name-asc': + return a.localeCompare(b); + case 'name-desc': + return b.localeCompare(a); + case 'containers-asc': + return aData.length - bData.length; + case 'containers-desc': + return bData.length - aData.length; + default: + return 0; + } + }); + return sortedHosts; + }; + return (
@@ -37,6 +59,8 @@ function App() { setIntervalTime={setIntervalTime} theme={theme} setTheme={setTheme} + sortOption={sortOption} + setSortOption={setSortOption} />
@@ -55,7 +79,7 @@ function App() {

If this screen persists, please check the browser console.

) : ( - Object.keys(data).map((host) => { + sortHosts(data, sortOption).map((host) => { return ( { +const Controls = ({ intervalTime, setIntervalTime, theme, setTheme, sortOption, setSortOption }) => { const [isModalOpen, setIsModalOpen] = useState(false); const handleThemeChange = (e) => { @@ -48,25 +50,15 @@ const Controls = ({ intervalTime, setIntervalTime, theme, setTheme }) => { {/* Theme Selector */}

Select Theme

- + />
{/* Refresh Rate Selector */} -
+

Select Refresh Rate

+ {/* Sort Selector */} +
+

Sort Hosts By

+ setSortOption(e.target.value)} + /> +
+