import VdapApi from '../../hooks/useVdapApi.js';
import {Button, Dialog, Flex, Grid, GridItem, Spinner, Text} from "@chakra-ui/react";
import {useEffect, useMemo, useState} from 'react';
import {useDashboardStore} from "@/store/useDashboardStore.jsx";
export default function IvansAlerts({ settings, widgetID }) {
console.log("IvansAlerts");
const [activeSummaryItem, setActiveSummaryItem] = useState(false);
const [activeFileItem, setActiveFileItem] = useState(false);
const [activeImageItem, setActiveImageItem] = useState(false);
const widgets = useDashboardStore(state => state.widgets);
console.log("widgets: ", widgets);
const updateSettings = useDashboardStore(state => state.updateWidgetSettings);
const isCopy = settings?.copyToClipboard === true; // acts as a failsafe if no default setting is set
const sorts = settings?.activeSorts;
// build url once on mount
const seven_day_url = useMemo(() => {
const base_url = '/api/v2/comm_coor_ivans'
let today = new Date();
let sevenDaysAgo = new Date(today);
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
sevenDaysAgo = sevenDaysAgo.toLocaleDateString('en-CA');
today = today.toLocaleDateString('en-CA');
return base_url + '?start_date=' + sevenDaysAgo + '&end_date=' + today;
}, []);
// VdapApi hook only calls api if the url changes
const {data: apiD, isLoading, error} = VdapApi(seven_day_url);
const apiData = [
{
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"
}
]
}
];
const data = useMemo(() => {
if (apiData.length === 0) return [];
let sorted = [];
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);
}
console.log("sorted data: ", sorted);
return sorted;
}, [apiData, sorts]);
useEffect(() => {
if(isCopy) {
navigator.clipboard.writeText(data)
.then(() => {
alert('Copied to clipboard!');
})
.catch((err => {
console.error('Failed to copy: ', err);
}));
updateSettings(widgetID, {copyToClipboard: false});
}
}, [isCopy]);
if (isLoading) return (
);
if (error) return (
{error}
);
if (data.length === 0) return (
No flight level data available this week.
);
return (
{/**/}
{/* Volcano*/}
{/* Report Number*/}
{/* Report Send Time*/}
{/* Preparer*/}
{/* Summary*/}
{/* Attachments*/}
{/**/}
{data.map((item, index) => {
const context_string = item.volcano_name + ' • ' + item.send_time;
return (
{item.volcano_name}{item.report_num}{item.send_time}{item.preparer}
<>
setActiveSummaryItem(null)}
item={activeSummaryItem}
context={context_string}
/>
>
{item.files.length === 0 ? (
No Attachments
):(
item.files.map((file, index) => {
if (file.type === "pdf") return (