From 245eb39892112b8733ca07415390bc836545f596 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Wed, 18 May 2022 17:06:21 +0800 Subject: [PATCH] Add 'announcement' to v2 homepage api This requires an accompanying commit in the homepages repo (see "Add announcement support"). Using `raw-loader` to import markdown files as a string works for the app-side, but didn't work well in web-side. Falling back to copying the announcement files to `dist` and reading them via `readFileSync`. Don't really like this -- will return with an alternative. --- ui/redux/selectors/settings.js | 12 ++++++++++++ web/src/getHomepageJSON.js | 20 +++++++++++++++++++- web/webpack.config.js | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/ui/redux/selectors/settings.js b/ui/redux/selectors/settings.js index 9cf61f36e..d86d25da9 100644 --- a/ui/redux/selectors/settings.js +++ b/ui/redux/selectors/settings.js @@ -82,6 +82,18 @@ export const selectHomepageMeme = (state) => { return homepages ? homepages['en'].meme || {} : {}; }; +export const selectHomepageAnnouncement = (state) => { + const homepageCode = selectHomepageCode(state); + const homepages = window.homepages; + if (homepages) { + const news = homepages[homepageCode].announcement; + if (news) { + return news; + } + } + return homepages ? homepages['en'].announcement || '' : ''; +}; + export const selectInRegionByCode = (state, code) => { const hp = selectClientSetting(state, SETTINGS.HOMEPAGE); const lang = selectLanguage(state); diff --git a/web/src/getHomepageJSON.js b/web/src/getHomepageJSON.js index 1df48f9f8..e7480ea8e 100644 --- a/web/src/getHomepageJSON.js +++ b/web/src/getHomepageJSON.js @@ -1,9 +1,24 @@ +const path = require('path'); const memo = {}; +const loadAnnouncements = (homepageKeys) => { + const fs = require('fs'); + const announcements = {}; + + homepageKeys.forEach((key) => { + const file = path.join(__dirname, `../dist/announcement/${key.toLowerCase()}.md`); + const announcement = fs.readFileSync(file, 'utf8'); + announcements[key] = announcement ? announcement.trim() : ''; + }); + + return announcements; +}; + // this didn't seem to help. if (!memo.homepageData) { try { memo.homepageData = require('../../custom/homepages/v2'); + memo.announcements = loadAnnouncements(Object.keys(memo.homepageData)); } catch (err) { console.log('getHomepageJSON:', err); } @@ -30,7 +45,10 @@ const getHomepageJsonV2 = () => { const v2 = {}; const homepageKeys = Object.keys(memo.homepageData); homepageKeys.forEach((hp) => { - v2[hp] = memo.homepageData[hp]; + v2[hp] = { + ...memo.homepageData[hp], + announcement: memo.announcements[hp], + }; }); return v2; }; diff --git a/web/webpack.config.js b/web/webpack.config.js index ae44436e1..c66a7ed78 100644 --- a/web/webpack.config.js +++ b/web/webpack.config.js @@ -68,6 +68,10 @@ const copyWebpackCommands = [ from: `${WEB_STATIC_ROOT}/pwa/`, to: `${DIST_ROOT}/public/pwa/`, }, + { + from: `${STATIC_ROOT}/../custom/homepages/v2/announcement`, + to: `${DIST_ROOT}/announcement`, + }, ]; const CUSTOM_OG_PATH = `${CUSTOM_ROOT}/v2-og.png`;