import { AppendLoadingSymbol } from "@/seqta/ui/Loading"; import stringToHTML from "./stringToHTML"; import { delay } from "./delay"; import { settingsState } from "./listeners/SettingsState"; import browser from "webextension-polyfill"; import LogoLightOutline from "@/resources/icons/betterseqta-light-outline.png"; import { animate, stagger } from "motion"; export async function SendNewsPage() { console.info("[BetterSEQTA+] Started Loading News Page"); document.title = "News ― SEQTA Learn"; await delay(10); const element = document.querySelector("[data-key=news]"); element!.classList.add("active"); // Remove all current elements in the main div to add new elements const main = document.getElementById("main"); main!.innerHTML = ""; const displayCountry = (() => { switch (settingsState.newsSource?.toLowerCase()) { case "usa": return "the USA"; case "uk": return "the UK"; case "netherlands": return "the Netherlands"; default: return settingsState.newsSource ? settingsState.newsSource .split("_") .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(" ") : "Australia"; } })(); const html = stringToHTML(/* html */ `

Latest Headlines in ${displayCountry}

`); main!.append(html.firstChild!); const titlediv = document.getElementById("title")!.firstChild; (titlediv! as HTMLElement).innerText = "News"; AppendLoadingSymbol("newsloading", "#news-container"); const response = (await browser.runtime.sendMessage({ type: "sendNews", source: settingsState.newsSource, })) as any; const newscontainer = document.querySelector("#news-container"); document.getElementById("newsloading")?.remove(); const articles = response?.news?.articles; if (!Array.isArray(articles) || articles.length === 0) { const emptyState = document.createElement("div"); emptyState.classList.add("day-empty"); const img = document.createElement("img"); img.src = browser.runtime.getURL(LogoLightOutline); const text = document.createElement("p"); text.innerText = "No news articles available right now."; emptyState.append(img, text); newscontainer?.append(emptyState); return; } const fragment = document.createDocumentFragment(); articles.forEach((article: any) => { const newsarticle = document.createElement("a"); newsarticle.classList.add("NewsArticle"); newsarticle.href = article.url; newsarticle.target = "_blank"; const articleimage = document.createElement("div"); articleimage.classList.add("articleimage"); if (article.urlToImage == "null" || article.urlToImage == null) { articleimage.style.cssText = ` background-image: url(${browser.runtime.getURL(LogoLightOutline)}); width: 20%; margin: 0 7.5%; `; } else { articleimage.style.backgroundImage = `url(${article.urlToImage})`; } const articletext = document.createElement("div"); articletext.classList.add("ArticleText"); const title = document.createElement("a"); title.innerText = article.title; title.href = article.url; title.target = "_blank"; const description = document.createElement("p"); const articleDescription = typeof article.description === "string" ? article.description : "No description available."; description.innerHTML = articleDescription.length > 400 ? articleDescription.substring(0, 400) + "..." : articleDescription; articletext.append(title, description); newsarticle.append(articleimage, articletext); fragment.append(newsarticle); }); // Single DOM update to append all articles newscontainer?.append(fragment); if (!settingsState.animations) return; const animatedArticles = Array.from(document.querySelectorAll(".NewsArticle")); animate( animatedArticles.slice(0, 20), { opacity: [0, 1], y: [10, 0], scale: [0.99, 1] }, { delay: stagger(0.1), type: "spring", stiffness: 341, damping: 20, mass: 1, }, ); }