From 17179e170850abc423f3f4323e0da535d2d653f2 Mon Sep 17 00:00:00 2001 From: Yogesh Ojha Date: Sat, 31 Aug 2024 17:10:37 +0530 Subject: [PATCH] seperate update related js functions to a file called update.js, show popup about latest releases --- web/static/custom/update.js | 201 +++++++++++++++++++++++++++++++++++ web/templates/base/base.html | 156 +-------------------------- 2 files changed, 205 insertions(+), 152 deletions(-) create mode 100644 web/static/custom/update.js diff --git a/web/static/custom/update.js b/web/static/custom/update.js new file mode 100644 index 000000000..ee03ac99a --- /dev/null +++ b/web/static/custom/update.js @@ -0,0 +1,201 @@ +// all the functions related to reNgine update, including showing up modal, notification etc will be here + +// Source : https://stackoverflow.com/a/32428268 +function checkDailyUpdate() { + if (!hasOneDayPassed()) return false; + console.log("Checking Daily Update..."); + fetch("/api/rengine/update/") + .then((response) => response.json()) + .then(function (response) { + if (response["update_available"]) { + window.localStorage.setItem("update_available", true); + $(".rengine_update_available").show(); + update_available(response["latest_version"], response["changelog"]); + } else { + window.localStorage.setItem("update_available", false); + $(".rengine_update_available").hide(); + } + }); +} + +function check_rengine_update() { + if ( + window.localStorage.getItem("update_available") && + window.localStorage.getItem("update_available") === "true" + ) { + // redirect to github release page + window.open("https://github.com/yogeshojha/rengine/releases", "_blank"); + } else { + Swal.fire({ + title: "Checking reNgine latest version...", + allowOutsideClick: false, + }); + swal.showLoading(); + fetch("/api/rengine/update/") + .then((response) => response.json()) + .then(function (response) { + console.log(response); + swal.close(); + if (response["description"] == "RateLimited") { + Swal.fire({ + title: "Oops!", + text: "Github rate limit exceeded, please try again in an hour!", + icon: "error", + }); + window.localStorage.setItem("update_available", false); + $(".rengine_update_available").hide(); + } else if (response["update_available"]) { + window.localStorage.setItem("update_available", true); + $(".rengine_update_available").show(); + update_available(response["latest_version"], response["changelog"]); + } else { + window.localStorage.setItem("update_available", false); + $(".rengine_update_available").hide(); + Swal.fire({ + title: "Update not available", + text: "You are running the latest version of reNgine!", + icon: "info", + }); + } + }); + } +} + +function update_available(latest_version_number, changelog) { + // Ensure marked and highlight.js are loaded, to render the changelog + Promise.all([ + loadScript("https://cdn.jsdelivr.net/npm/marked/marked.min.js"), + loadScript( + "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js" + ), + loadCSS( + "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/github.min.css" + ), + ]).then(() => { + marked.setOptions({ + highlight: function (code, lang) { + const language = hljs.getLanguage(lang) ? lang : "plaintext"; + return hljs.highlight(code, { language }).value; + }, + langPrefix: "hljs language-", + }); + + const parsedChangelog = marked.parse(changelog); + + const changelogStyle = ` + + `; + + Swal.fire({ + title: "Update Available!", + html: ` + ${changelogStyle} +
reNgine's new update ${latest_version_number} is available, please follow the update instructions.
+
+ ${parsedChangelog} +
+ `, + icon: "info", + confirmButtonText: "Update Instructions", + showCancelButton: true, + cancelButtonText: "Dismiss", + width: "70%", + didOpen: () => { + document.querySelectorAll("pre code").forEach((block) => { + hljs.highlightBlock(block); + }); + }, + }).then((result) => { + if (result.isConfirmed) { + window.open("https://www.rengine.wiki/update", "_blank"); + } + }); + }); +} + +// Source: https://stackoverflow.com/a/32428268 +function hasOneDayPassed() { + var date = new Date().toLocaleDateString(); + if (window.localStorage.getItem("last_update_checked") == date) { + return false; + } + window.localStorage.setItem("last_update_checked", date); + return true; +} + +function showAfterUpdatePopup() { + // this function will show a popup after the update is done to tell user about the new features + const currentVersion = document.body.getAttribute("data-rengine-version"); + const lastShownVersion = localStorage.getItem("lastShownUpdateVersion"); + + if (lastShownVersion !== currentVersion) { + // const isFirstRun = lastShownVersion === null; + // we will use this once videos are made for features + // Swal.fire({ + // title: isFirstRun ? "Welcome to reNgine!" : "Thanks for updating!", + // text: `Would you like to see ${ + // isFirstRun ? "the features" : "what's changed" + // } in this version?`, + // icon: "info", + // showCancelButton: true, + // confirmButtonText: "Yes, show me", + // cancelButtonText: "No, thanks", + // }).then((result) => { + // if (result.isConfirmed) { + // window.open("https://rengine.wiki/changelog/latest", "_blank"); + // } + // localStorage.setItem("lastShownUpdateVersion", currentVersion); + // }); + Swal.fire({ + title: "Thanks for using reNgine!", + text: `Would you like to see what's new in this version?`, + icon: "info", + showCancelButton: true, + confirmButtonText: "Yes, show me", + cancelButtonText: "No, thanks", + }).then((result) => { + if (result.isConfirmed) { + window.open(`https://rengine.wiki/whatisnew/${currentVersion}`, "_blank"); + } + localStorage.setItem("lastShownUpdateVersion", currentVersion); + }); + } +} + +$(document).ready(function () { + // show popup after update + showAfterUpdatePopup(); + // hide badge if update does not exists + if ( + window.localStorage.getItem("update_available") && + window.localStorage.getItem("update_available") === "true" + ) { + $(".rengine_update_available").show(); + } else { + $(".rengine_update_available").hide(); + } +}); diff --git a/web/templates/base/base.html b/web/templates/base/base.html index af82b9d3d..fc94c33c2 100644 --- a/web/templates/base/base.html +++ b/web/templates/base/base.html @@ -57,7 +57,7 @@ {% endblock custom_js_css_link %} - + {% include 'base/_items/top_bar.html' %} {# topbar ends #} @@ -100,6 +100,7 @@

{% block page_title %}{% endblock page_title %}

+ @@ -125,13 +126,7 @@

{% block page_title %}{% endblock page_title %}

checkDailyUpdate(); getScanStatusSidebar(project='{{current_project.slug}}', reload=false); - // hide badge if update does not exists - if (window.localStorage.getItem('update_available') && window.localStorage.getItem('update_available') === 'true') { - $('.rengine_update_available').show(); - } - else{ - $('.rengine_update_available').hide(); - } + // tippy for notification or scan acitivity tippy('.top-activity-counter', { content: 'Scan Activity', @@ -166,149 +161,6 @@

{% block page_title %}{% endblock page_title %}

}); } - function check_rengine_update(){ - if (window.localStorage.getItem('update_available') && window.localStorage.getItem('update_available') === 'true') { - // redirect to github release page - window.open("https://github.com/yogeshojha/rengine/releases","_blank"); - } - else{ - Swal.fire({ - title: 'Checking reNgine latest version...', - allowOutsideClick: false - }); - swal.showLoading(); - fetch('/api/rengine/update/') - .then(response => response.json()) - .then(function (response) { - console.log(response); - swal.close(); - if (response['description'] == 'RateLimited') { - Swal.fire({ - title: 'Oops!', - text: 'Github rate limit exceeded, please try again in an hour!', - icon: 'error' - }); - window.localStorage.setItem('update_available', false); - $('.rengine_update_available').hide(); - } - else if (response['update_available']){ - window.localStorage.setItem('update_available', true); - $('.rengine_update_available').show(); - update_available(response['latest_version'], response['changelog']); - } - else{ - window.localStorage.setItem('update_available', false); - $('.rengine_update_available').hide(); - Swal.fire({ - title: 'Update not available', - text: 'You are running the latest version of reNgine!', - icon: 'info' - }); - } - }); - } - } - - function update_available(latest_version_number, changelog) { - // Ensure marked and highlight.js are loaded, to render the changelog - Promise.all([ - loadScript('https://cdn.jsdelivr.net/npm/marked/marked.min.js'), - loadScript('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js'), - loadCSS('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/github.min.css') - ]).then(() => { - marked.setOptions({ - highlight: function(code, lang) { - const language = hljs.getLanguage(lang) ? lang : 'plaintext'; - return hljs.highlight(code, { language }).value; - }, - langPrefix: 'hljs language-' - }); - - const parsedChangelog = marked.parse(changelog); - - const changelogStyle = ` - - `; - - Swal.fire({ - title: 'Update Available!', - html: ` - ${changelogStyle} -
reNgine's new update ${latest_version_number} is available, please follow the update instructions.
-
- ${parsedChangelog} -
- `, - icon: 'info', - confirmButtonText: 'Update Instructions', - showCancelButton: true, - cancelButtonText: 'Dismiss', - width: '70%', - didOpen: () => { - document.querySelectorAll('pre code').forEach((block) => { - hljs.highlightBlock(block); - }); - } - }).then((result) => { - if (result.isConfirmed) { - window.open("https://www.rengine.wiki/update","_blank"); - } - }); - }); - } - - // Source : https://stackoverflow.com/a/32428268 - function checkDailyUpdate(){ - if( !hasOneDayPassed() ) return false; - console.log('Checking Daily Update...') - fetch('/api/rengine/update/') - .then(response => response.json()) - .then(function (response) { - if (response['update_available']){ - window.localStorage.setItem('update_available', true); - $('.rengine_update_available').show(); - update_available(response['latest_version'], response['changelog']) - } - else{ - window.localStorage.setItem('update_available', false); - $('.rengine_update_available').hide(); - } - }); - } - - // Source: https://stackoverflow.com/a/32428268 - function hasOneDayPassed(){ - var date = new Date().toLocaleDateString(); - if(window.localStorage.getItem('last_update_checked') == date){ - return false; - } - window.localStorage.setItem('last_update_checked', date); - return true; - } {% if messages %} {% for message in messages %} @@ -330,4 +182,4 @@
reNgine's new update ${latest_version_number} is available, please follow th {% block page_level_script %} {% endblock page_level_script%} - + \ No newline at end of file