Skip to content

Commit b4bdf37

Browse files
authored
🔀 Merge pull request #320 from ajnart/ajnart/issue316
🔧 Dashdot module changes
2 parents ff5a334 + 1fa2060 commit b4bdf37

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

src/modules/dashdot/DashdotModule.tsx

+28-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const DashdotModule = asModule({
1313
icon: CalendarIcon,
1414
component: DashdotComponent,
1515
options: {
16+
url: {
17+
name: 'Dash. URL',
18+
value: '',
19+
},
1620
cpuMultiView: {
1721
name: 'CPU Multi-Core View',
1822
value: false,
@@ -88,23 +92,23 @@ const bytePrettyPrint = (byte: number): string =>
8892
? `${(byte / 1024).toFixed(1)} KiB`
8993
: `${byte.toFixed(1)} B`;
9094

91-
const useJson = (service: serviceItem | undefined, url: string) => {
95+
const useJson = (targetUrl: string, url: string) => {
9296
const [data, setData] = useState<any | undefined>();
9397

9498
const doRequest = async () => {
9599
try {
96-
const resp = await axios.get(url, { baseURL: service?.url });
100+
const resp = await axios.get(`/api/modules/dashdot?url=${url}&base=${targetUrl}`);
97101

98102
setData(resp.data);
99103
// eslint-disable-next-line no-empty
100104
} catch (e) {}
101105
};
102106

103107
useEffect(() => {
104-
if (service?.url) {
108+
if (targetUrl) {
105109
doRequest();
106110
}
107-
}, [service?.url]);
111+
}, [targetUrl]);
108112

109113
return data;
110114
};
@@ -118,17 +122,19 @@ export function DashdotComponent() {
118122
const dashConfig = config.modules?.[DashdotModule.title]
119123
.options as typeof DashdotModule['options'];
120124
const isCompact = dashConfig?.useCompactView?.value ?? false;
121-
const dashdotService = config.services.filter((service) => service.type === 'Dash.')[0];
122-
125+
const dashdotService: serviceItem | undefined = config.services.filter(
126+
(service) => service.type === 'Dash.'
127+
)[0];
128+
const dashdotUrl = dashdotService?.url ?? dashConfig?.url?.value ?? '';
123129
const enabledGraphs = dashConfig?.graphs?.value ?? ['CPU', 'RAM', 'Storage', 'Network'];
124130
const cpuEnabled = enabledGraphs.includes('CPU');
125131
const storageEnabled = enabledGraphs.includes('Storage');
126132
const ramEnabled = enabledGraphs.includes('RAM');
127133
const networkEnabled = enabledGraphs.includes('Network');
128134
const gpuEnabled = enabledGraphs.includes('GPU');
129135

130-
const info = useJson(dashdotService, '/info');
131-
const storageLoad = useJson(dashdotService, '/load/storage');
136+
const info = useJson(dashdotUrl, '/info');
137+
const storageLoad = useJson(dashdotUrl, '/load/storage');
132138

133139
const totalUsed =
134140
(storageLoad?.layout as any[])?.reduce((acc, curr) => (curr.load ?? 0) + acc, 0) ?? 0;
@@ -166,13 +172,23 @@ export function DashdotComponent() {
166172
},
167173
].filter((g) => g.enabled);
168174

175+
if (dashdotUrl === '') {
176+
return (
177+
<div>
178+
<h2 className={classes.heading}>Dash.</h2>
179+
<p>
180+
No dash. service found. Please add one to your Homarr dashboard or set a dashdot URL in
181+
the module options
182+
</p>
183+
</div>
184+
);
185+
}
186+
169187
return (
170188
<div>
171189
<h2 className={classes.heading}>Dash.</h2>
172190

173-
{!dashdotService ? (
174-
<p>No dash. service found. Please add one to your Homarr dashboard.</p>
175-
) : !info ? (
191+
{!info ? (
176192
<p>Cannot acquire information from dash. - are you running the latest version?</p>
177193
) : (
178194
<div className={classes.graphsContainer}>
@@ -209,9 +225,7 @@ export function DashdotComponent() {
209225
}
210226
key={graph.name}
211227
title={graph.name}
212-
src={`${
213-
dashdotService.url
214-
}?singleGraphMode=true&graph=${graph.name.toLowerCase()}&theme=${colorScheme}&surface=${(colorScheme ===
228+
src={`${dashdotUrl}?singleGraphMode=true&graph=${graph.name.toLowerCase()}&theme=${colorScheme}&surface=${(colorScheme ===
215229
'dark'
216230
? theme.colors.dark[7]
217231
: theme.colors.gray[0]

src/pages/api/modules/dashdot.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import axios from 'axios';
2+
import { NextApiRequest, NextApiResponse } from 'next';
3+
4+
async function Get(req: NextApiRequest, res: NextApiResponse) {
5+
// Extract url from req.query as string
6+
const { url, base } = req.query;
7+
8+
// If no url is provided, return an error
9+
if (!url || !base) {
10+
return res.status(400).json({
11+
message: 'Missing required parameter in url',
12+
});
13+
}
14+
// Get the origin URL
15+
const response = await axios.get(url as string, { baseURL: base as string });
16+
// Return the response
17+
return res.status(200).json(response.data);
18+
}
19+
20+
export default async (req: NextApiRequest, res: NextApiResponse) => {
21+
// Filter out if the reuqest is a POST or a GET
22+
if (req.method === 'GET') {
23+
return Get(req, res);
24+
}
25+
return res.status(405).json({
26+
statusCode: 405,
27+
message: 'Method not allowed',
28+
});
29+
};

0 commit comments

Comments
 (0)