diff --git a/app/assets/javascripts/index/history.js b/app/assets/javascripts/index/history.js index 8b64a4432cb..905ed21b46a 100644 --- a/app/assets/javascripts/index/history.js +++ b/app/assets/javascripts/index/history.js @@ -32,6 +32,83 @@ OSM.History = function (map) { let changesetIntersectionObserver; + function safeHistoryT(key, options) { + try { + return OSM.i18n.t(key, options); + } catch (e) { + return key; + } + } + + function buildFetchErrorMessage(response) { + const status = response?.status; + const statusText = response?.statusText; + const parts = []; + if (status) parts.push(status); + if (statusText) parts.push(statusText); + return parts.join(" ") || safeHistoryT("javascripts.history.load_failed_unknown"); + } + + function fetchHtmlOrThrow(url) { + return fetch(url).then(response => { + if (response.ok) return response.text(); + throw new Error(buildFetchErrorMessage(response)); + }); + } + + function removeHistoryLoadError() { + $("#sidebar_content .history-load-error").remove(); + } + + function showHistoryLoadError(options) { + const { message, retry } = options; + const detail = String(message ?? ""); + // Always resolve targets from the live DOM (Turbo may replace sidebar HTML after fetch starts). + const $changesets = $("#sidebar_content .changesets"); + const $target = $changesets.length ? $changesets : $("#sidebar_content"); + + removeHistoryLoadError(); + + try { + const $alert = $("