Files
Web-Frontend/client/src/components/Widgets/IvansAlerts.jsx
T

251 lines
10 KiB
React
Raw Normal View History

import {Button, Flex, Spinner, Text, Box, Collapsible} from "@chakra-ui/react";
import {useEffect, useMemo } from 'react';
2026-07-30 09:18:47 -07:00
import {useDashboardStore} from "@/store/useDashboardStore.jsx";
import VdapApi from '../../hooks/useVdapApi.js';
import useBuildUrl from "@/hooks/useBuildUrl.js";
2026-07-30 09:18:47 -07:00
export default function IvansAlerts({ settings, widgetID }) {
console.log("IvansAlerts widget ID: ", widgetID);
2026-07-30 09:18:47 -07:00
const updateSettings = useDashboardStore(state => state.updateWidgetSettings);
const isCopy = settings?.copyToClipboard === true; // acts as a fail-safe if no default setting is set
2026-07-30 09:18:47 -07:00
const sorts = settings?.activeSorts;
// build url, default is 7 days past
const url = useBuildUrl('/api/v2/comm_coor_ivans', settings?.startDate, settings?.endDate);
2026-07-30 09:18:47 -07:00
// VdapApi hook only calls api if the url changes and apiData remains stable since vdapApi uses state to track the data
const {data: apiData, isLoading, error} = VdapApi(url);
2026-07-30 09:18:47 -07:00
// TEST DATA
// const apiData = useMemo(() => {
// return ([
// {
// id: 277,
// volcano_number: 268010,
// report_num: "2026-01",
// report_date: "2026-05-08T22:05:44Z",
// preparer: "hwright",
// reviewer: "dramsey",
// send_time: "2026-05-08T22:05:52Z",
// volcano_name: "Dukono",
// summary: "At 07:41 local time on 8 May 2026, an explosion of persistently active Dukono volcano occurred, producing an ash plume that rose over 15 km (52,000 feet) above sea level and ejecting ballistic blocks of lava beyond the crater rim. The eruption killed three tourists who were hiking within the official exclusion zone; other hikers were rescued and returned to safety.",
// files: [
// {
// name: "277-268010-Dukono-2026-01.pdf",
// url: "https://vdap.org/ivans/sitrep/277-268010-Dukono-2026-01.pdf",
// type: "pdf"
// }
// ]
// },
// {
// id: 279,
// volcano_number: 250030,
// report_num: "2026-01",
// report_date: "2026-05-15T05:11:53Z",
// preparer: "charpel",
// reviewer: "jlwnstrn",
// send_time: "2026-05-15T05:12:01Z",
// volcano_name: "Central Bismarck Sea",
// summary: "A M5.4 earthquake occurred on 14 May 2026 at 17:13 (UTC) during the ongoing eruption of the submarine Central Bismarck Sea volcano that began 8 May 2026. The volcano's current activity does not threaten any populated areas, but today’s earthquake indicates that eruptive activity will continue or increase.",
// files: [
// {
// name: "279-250030-Central Bismarck Sea-2026-01.pdf",
// url: "https://vdap.org/ivans/sitrep/279-250030-Central Bismarck Sea-2026-01.pdf",
// type: "pdf"
// }
// ]
// },
// {
// id: 275,
// volcano_number: 273030,
// report_num: "2026-02",
// report_date: "2026-05-03T05:28:12Z",
// preparer: "dramsey",
// reviewer: "hwright",
// send_time: "2026-05-03T05:28:20Z",
// volcano_name: "Mayon",
// summary: "At around 5:30 PM local time on May 2, 2026, a pyroclastic flow (cloud of searing ash and rock particles) from erupting Mayon Volcano traveled around 4 km (~2.5 miles) down the southwestern slope of the volcano and produced an ash plume that drifted to the west-southwest. At least 52 villages in various towns in Albay province were heavily affected by thick ash fall, reducing visibility to near zero and making travel extremely difficult. There were no reports of casualties and the volcano alert level remained at Level 3 (on a 0-5 level scale).",
// files: [
// {
// name: "275-273030-Mayon-2026-02.pdf",
// url: "https://vdap.org/ivans/sitrep/275-273030-Mayon-2026-02.pdf",
// type: "pdf"
// },
// {
// name: "275-273030-Mayon-2026-02.pdf",
// url: "https://vdap.org/ivans/sitrep/275-273030-Mayon-2026-02.pdf",
// type: "pdf"
// }
// ]
// }
// ])
// }, []);
2026-07-30 09:18:47 -07:00
const data = useMemo(() => {
if (apiData?.length === 0) return [];
let sorted;
2026-07-30 09:18:47 -07:00
if (sorts?.volcanoSort && sorts?.timeSort === "asc") {
sorted = [...apiData].sort(ascTimeVolcanoSort);
} else if (sorts?.volcanoSort && sorts?.timeSort === "desc") {
sorted = [...apiData].sort(descTimeVolcanoSort);
} else if (sorts?.timeSort === "asc") {
sorted = [...apiData].sort(ascTimeSort);
} else {
sorted = [...apiData].sort(descTimeSort);
}
return sorted;
}, [apiData, sorts]);
useEffect(() => {
if(isCopy) {
const dataText = data.map((item) => {
return `${item.volcano_name}\n` +
`Report Number: ${item.report_num}\n` +
`Send Time: ${item.send_time}\n` +
`Preparer: ${item.preparer}\n` +
`Summary:\n${item.summary}\n`;
}).join('\n\n');
navigator.clipboard.writeText(dataText)
2026-07-30 09:18:47 -07:00
.then(() => {
alert('Copied to clipboard!');
})
.catch((err => {
console.error('Failed to copy: ', err);
}));
updateSettings(widgetID, {copyToClipboard: false});
}
}, [isCopy, data, widgetID, updateSettings]);
2026-07-30 09:18:47 -07:00
if (isLoading) return (
<Flex w="100%" h="100%" align="center" justify="center" borderRadius="lg" bg="gray.800">
2026-07-30 09:18:47 -07:00
<Spinner size="xl" color="blue.400" thickness="4px"/>
</Flex>
);
if (error) return (
<Flex w="100%" h="100%" align="center" justify="center" borderRadius="lg" bg="gray.800">
2026-07-30 09:18:47 -07:00
<Text color="red.400">{error}</Text>
</Flex>
);
if (data.length === 0) return (
<Flex w="100%" h="100%" align="center" justify="center" borderRadius="lg" bg="gray.800">
<Text textAlign="center" color="gray.400">No flight level data available for the selected dates.</Text>
2026-07-30 09:18:47 -07:00
</Flex>
);
return (
<Flex direction="column"
w="100%" h="100%"
pos="relative"
justify="top" align="center"
p={2} gap={2}
2026-08-10 07:53:26 -07:00
overflow="auto"
borderRadius="xl"
bg="gray.800"
>
2026-07-30 09:18:47 -07:00
{data.map((item, index) => {
return (
<Collapsible.Root key={index} w="100%">
2026-08-10 07:53:26 -07:00
{/* Using context to dynamically change the border radii of the triggers */}
<Collapsible.Context>
{(collapsible) => (
<>
<Collapsible.Trigger w="100%">
<Flex direction="row"
w="100%" h="100%"
justify="space-between" align="center"
p={1} pl={3} pr={3}
gap={2}
borderTopRadius="lg"
borderBottomRadius={collapsible.open ? "0" : "lg"}
2026-08-19 07:47:48 -07:00
bg="gray.600"
>
<Flex direction="column" w="100%" minW={0} textAlign="left" color="gray.100">
<Text truncate>{item.volcano_name}</Text>
<Text truncate fontSize="xs" color="gray.300/90">{item.send_time}</Text>
</Flex>
<Flex w="100%" minW={0} justify="right">
2026-08-19 07:47:48 -07:00
<Text truncate pl={2} pr={2} borderRadius="lg" bg="gray.800/50" color="gray.100" fontWeight="semibold">{item.report_num}</Text>
</Flex>
</Flex>
</Collapsible.Trigger>
2026-08-10 07:53:26 -07:00
<Collapsible.Content w="100%" h="auto">
{/* This box needs to be at h="100%" for the pdf toolbar to appear */}
2026-08-12 15:00:24 -07:00
<Box w="100%" h="100%" p={1} borderBottomRadius="lg" bg="gray.700/45">
<Flex direction="column" gap={2} bg="gray.800/75" borderRadius="lg" p={2} align="end">
<Text w="100%" h="100%" fontSize="sm" color="gray.100" textAlign="left" whiteSpace="pre-line">{item?.summary}</Text>
<Text w="fit" fontSize="sm" color="gray.200" pl={1} pr={1} bg="gray.700/50" borderRadius="md">Preparer: {item?.preparer}</Text>
</Flex>
{item?.files?.map((file, index) => {
return (
2026-08-10 07:53:26 -07:00
<Box w="100%" h="100%" key={index} p={1}>
<br />
2026-08-18 14:50:37 -07:00
<Flex mb={2} justify="space-between">
<Text pl={1} pr={1} bg="gray.600/50" borderRadius="md" fontSize="xs" color="gray.300">{file?.name}</Text>
2026-08-18 14:50:37 -07:00
<Box borderRadius="lg" bg="gray.700" color="gray.300" pl={1} pr={1} fontSize={13} asChild>
<a href={`${file?.url}#view=FitV&toolbar=1`} target="_blank" rel="noopener noreferrer">
Open
</a>
</Box>
</Flex>
2026-08-10 07:53:26 -07:00
<object
data={`${file?.url}#view=FitH&toolbar=1`}
type="application/pdf"
style={{ width: '100%', height: '100%', display: 'block' }}
>
<Flex w="100%" h="100%" p={4} color="gray.400">
<Text mb={2} color="fg.muted">
Your browser cannot display this PDF directly.
</Text>
<a href={file?.url} target="_blank">
Download PDF
</a>
</Flex>
</object>
2026-08-10 07:53:26 -07:00
</Box>
)})}
</Box>
</Collapsible.Content>
</>
)}
</Collapsible.Context>
</Collapsible.Root>
2026-07-30 09:18:47 -07:00
)
})}
</Flex>
);
}
function ascTimeVolcanoSort(a, b) {
if (a.volcano_name < b.volcano_name) return -1
else if (a.volcano_name > b.volcano_name) return 1
else return ascTimeSort(a, b);
}
function descTimeVolcanoSort(a, b) {
if (a.volcano_name < b.volcano_name) return -1
else if (a.volcano_name > b.volcano_name) return 1
else return descTimeSort(a, b);
}
function ascTimeSort(a, b) {
const aTime = new Date(a.send_time);
const bTime = new Date(b.send_time);
if (aTime > bTime) return 1;
if (aTime < bTime) return -1;
return 0;
}
function descTimeSort(a, b) {
const aTime = new Date(a.send_time);
const bTime = new Date(b.send_time);
if (aTime > bTime) return -1;
if (aTime < bTime) return 1;
return 0;
}