From 60aaf2a8dc84a177dc0021c02ca8ae3cce73dc60 Mon Sep 17 00:00:00 2001 From: Sarahtonin Date: Thu, 10 Sep 2026 09:34:25 +0200 Subject: [PATCH] Added a connection status in case the connection to database fails. --- Pages/Bits/Head.php | 2 ++ Scripts/ConnectionStatus.js | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Scripts/ConnectionStatus.js diff --git a/Pages/Bits/Head.php b/Pages/Bits/Head.php index c2d8a9e..92f10e1 100644 --- a/Pages/Bits/Head.php +++ b/Pages/Bits/Head.php @@ -23,4 +23,6 @@ rel="stylesheet" href="/Styles/Style.css" > + + diff --git a/Scripts/ConnectionStatus.js b/Scripts/ConnectionStatus.js new file mode 100644 index 0000000..e27c80a --- /dev/null +++ b/Scripts/ConnectionStatus.js @@ -0,0 +1,61 @@ +const GardenApiHost = "api.srhtnin.garden"; +let GardenConnectionMessageShown = false; + +function ShowGardenConnectionMessage() { + if (GardenConnectionMessageShown) { + return; + } + + const MainContainer = + document.querySelector(".MainContainer"); + + if (MainContainer === null) { + return; + } + + GardenConnectionMessageShown = true; + + const Message = document.createElement("p"); + Message.className = "PageMessage"; + Message.textContent = + "Garden's server is currently unavailable. " + + "Some features may not work until the connection is restored."; + + MainContainer.prepend(Message); +} + +const OriginalFetch = window.fetch.bind(window); + +window.fetch = async function(...Arguments) { + const RequestUrl = + typeof Arguments[0] === "string" + ? Arguments[0] + : Arguments[0]?.url; + + const IsGardenApiRequest = + typeof RequestUrl === "string" && + new URL( + RequestUrl, + window.location.href + ).hostname === GardenApiHost; + + try { + const Response = + await OriginalFetch(...Arguments); + + if ( + IsGardenApiRequest && + Response.status >= 500 + ) { + ShowGardenConnectionMessage(); + } + + return Response; + } catch (Error) { + if (IsGardenApiRequest) { + ShowGardenConnectionMessage(); + } + + throw Error; + } +};