Skip to content

Commit f96409c

Browse files
committed
switch statement for icons
1 parent 9fed0da commit f96409c

File tree

7 files changed

+314
-20
lines changed

7 files changed

+314
-20
lines changed

src/components/SimplePanel.tsx

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import React from 'react';
22
import { PanelProps } from '@grafana/data';
33
import { SimpleOptions } from 'types';
44
import './SimplePanel.css';
5+
import ushahidiIcon from '../img/ushahidi.svg';
6+
import facebookIcon from '../img/facebook.svg';
7+
import twitterIcon from '../img/twitter.svg';
8+
import youtubeIcon from '../img/youtube.svg';
59

610
interface Props extends PanelProps<SimpleOptions> {}
711

@@ -15,12 +19,29 @@ export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) =
1519
fieldCategory,
1620
fieldMentionsPercent,
1721
fieldMentionsCount,
18-
icon,
19-
iconColor,
2022
statColor
2123
} = options;
2224

23-
// Handle no data case
25+
const getCategoryStyle = (categoryName: string) => {
26+
const normalized = categoryName?.toLowerCase().trim() || '';
27+
switch (normalized) {
28+
case 'platform':
29+
return { icon: ushahidiIcon, color: '#EFC44C' };
30+
31+
case 'facebook':
32+
return { icon: facebookIcon, color: '#8B9DC3' };
33+
34+
case 'twitter':
35+
return { icon: twitterIcon, color: '#1DA1F2' };
36+
37+
case 'youtube':
38+
return { icon: youtubeIcon, color: '#CC181E' };
39+
40+
default:
41+
return { icon: '', color: '#8e8e8e' };
42+
}
43+
};
44+
2445
if (!data.series.length) {
2546
return <div className="panel-container">No data to display</div>
2647
}
@@ -44,19 +65,21 @@ export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) =
4465
socialCount: getValue(socialVoicesCount, 0),
4566
};
4667

47-
// extract category rows; filter out empty categories
68+
// extract platform Rows and apply icon logic
4869
const platformRows = [];
4970
for (let i = 0; i < length; i++) {
5071
const category = getValue(fieldCategory, i);
5172

5273
// only add to list if category exists
5374
if (category) {
75+
const style = getCategoryStyle(category);
76+
5477
platformRows.push({
5578
category: category,
5679
mentionsPercent: getValue(fieldMentionsPercent, i),
5780
mentionsCount: getValue(fieldMentionsCount, i),
58-
icon: getValue(icon, i),
59-
iconColor: getValue(iconColor, i),
81+
icon: style.icon,
82+
iconColor: style.color,
6083
});
6184
}
6285
}
@@ -110,9 +133,10 @@ export const SimplePanel: React.FC<Props> = ({ options, data, width, height }) =
110133
<div className={`panel-card-inner ${isNarrow ? 'is-narrow' : ''}`}>
111134
<div
112135
className="panel-card-icon-wrapper"
113-
style={{ background: row.iconColor ?? '#ddd' }}
136+
style={{ background: row.iconColor }}
114137
>
115-
{row.icon?.startsWith('http') ? (
138+
{/* if it looks like a url, render img, else text */}
139+
{row.icon?.startsWith('http') || row.icon?.startsWith('/') ? (
116140
<img src={row.icon} alt="" className="panel-card-icon-img" />
117141
) : (
118142
row.icon ?? '↓'

src/img/facebook.svg

Lines changed: 58 additions & 0 deletions
Loading

src/img/twitter.svg

Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)