Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 16x 16x 3x 8x 3x 8x 4x 4x 4x 2x 2x 2x 4x 4x 1x 2x 4x 4x 2x 2x 1x 1x 2x 2x 8x 8x 8x 8x | // src/SpaceWeatherWidget.tsx
import { useMemo } from 'react';
import {
DataLoadFailed,
STATE_COLORS_HEX,
STATE_COLORS_TEXT,
type StateColorKey,
useWidgetRefreshRate
} from '@ska-octopus-widget-sdk/widget-sdk';
import { useQuery } from '@apollo/client';
import { OP_POLL } from './graphql/polling';
import styles from './SpaceWeatherWidget.module.css';
const PALETTE = STATE_COLORS_HEX;
const SCALE_STATE_KEYS: Record<string, StateColorKey> = {
'0': 'OK',
'1': 'WARNING',
'2': 'MAINTENANCE',
'3': 'FAILED',
'4': 'ALARM',
'5': 'ERROR'
};
const resolveScaleStateKey = (scale: string): StateColorKey => {
const key = scale.match(/\d/)?.[0] ?? '0';
return SCALE_STATE_KEYS[key] ?? 'OK';
};
const resolveScaleColor = (scale: string): string => {
return PALETTE[resolveScaleStateKey(scale)];
};
const resolveScaleTextColor = (scale: string): string => {
return STATE_COLORS_TEXT[resolveScaleStateKey(scale)] ?? STATE_COLORS_TEXT.UNKNOWN;
};
/** Stateless — no Redux, no instanceId required */
export type SpaceWeatherWidgetProps = Record<string, never>;
export default function SpaceWeatherWidget(_: SpaceWeatherWidgetProps) {
// Use a stable key for refresh rate (no prop needed)
const refresh = useWidgetRefreshRate('spaceWeather') ?? 10;
// Apollo handles polling; we stay stateless
const { data, loading, error, refetch } = useQuery(OP_POLL, {
variables: {},
fetchPolicy: 'network-only',
notifyOnNetworkStatusChange: true,
pollInterval: refresh * 1000
});
// Format day labels
const fmtDay = (raw: string) => {
const iso = raw.replace(/\//g, '-');
const d = new Date(iso);
return Number.isNaN(d.getTime())
? '—'
: d.toLocaleDateString('en-GB', { day: '2-digit', month: 'short' });
};
// Derive view model
const view = useMemo(() => {
if (!data?.AusSpaceWeather) return null;
const { reportDate, timeZone, noaaScales } = data.AusSpaceWeather as {
reportDate: string;
timeZone: string;
noaaScales: {
prev: { g: string; g_text: string; r: string; r_text: string; s: string; s_text: string };
curr: { g: string; g_text: string; r: string; r_text: string; s: string; s_text: string };
fcast: {
day: Array<{
time: string;
g: string;
g_text: string;
r12_prob: string;
r35_prob: string;
s1_prob: string;
}>;
};
};
};
const days = (noaaScales?.fcast?.day ?? []).slice(0, 3).map((d) => ({
...d,
label: fmtDay(d.time)
}));
return {
reportDate,
timeZone,
prev: noaaScales.prev,
curr: noaaScales.curr,
days
};
}, [data]);
// Loading
if (loading) {
return (
<div className={styles.center}>
<div className={styles.spinner} />
</div>
);
}
// Error
if (error || !view) {
return (
<div className={styles.error}>
<DataLoadFailed
message="Failed to load space weather"
error={error?.message ?? 'No data'}
handleRetry={() => void refetch()}
/>
</div>
);
}
// Render
return (
<div className={styles.host}>
<header className={styles.head}>
Space Weather Conditions
<small>
({view.reportDate} • {view.timeZone})
</small>
</header>
<div className={styles.grid}>
<Section title="Past 24 h" record={view.prev} />
<Section title="Current" record={view.curr} />
{view.days.map((d) => (
<section className={styles.section} key={d.time}>
<h3 className={styles.h3}>{d.label}</h3>
<div className={styles.probBlock}>
<strong>R1 – R2:</strong> {d.r12_prob}%<br />
<strong>R3 – R5:</strong> {d.r35_prob}%
</div>
<div className={styles.probBlock}>
<strong>S1 or >:</strong> {d.s1_prob}%
</div>
<Item title="Geomagnetic storms" scale={d.g} text={d.g_text} />
</section>
))}
</div>
</div>
);
function Section({
title,
record
}: {
title: string;
record: { g: string; g_text: string; r: string; r_text: string; s: string; s_text: string };
}) {
return (
<section className={styles.section}>
<h3 className={styles.h3}>{title}</h3>
<Item title="Radio blackouts" scale={record.r} text={record.r_text} />
<Item title="Radiation storms" scale={record.s} text={record.s_text} />
<Item title="Geomagnetic storms" scale={record.g} text={record.g_text} />
</section>
);
}
function Item({ title, scale, text }: { title: string; scale: string; text: string }) {
const label = scale === '0' ? 'G0' : /^[RS]/.test(scale) ? scale : scale;
const badgeColor = resolveScaleColor(scale);
const badgeText = resolveScaleTextColor(scale);
return (
<div className={styles.item}>
<span
className={styles.badgeBase}
style={{ backgroundColor: badgeColor, color: badgeText }}
>
{label}
</span>
<div className={styles.itemTxt}>
<div className={styles.itemTitle}>{title}</div>
<div className={styles.itemSub}>{text}</div>
</div>
</div>
);
}
}
|