From ea6a668a84604d62e8218c9afe71e600d924b214 Mon Sep 17 00:00:00 2001 From: Ben Phelps Date: Sat, 24 Sep 2022 01:18:37 +0300 Subject: [PATCH] add version information --- Dockerfile | 6 ++++- package.json | 1 + pnpm-lock.yaml | 6 +++++ src/components/version.jsx | 47 ++++++++++++++++++++++++++++++++++++++ src/pages/index.jsx | 16 ++++++++++--- 5 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/components/version.jsx diff --git a/Dockerfile b/Dockerfile index b1193813c..da7d290c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,6 +22,10 @@ RUN --mount=type=cache,id=pnpm-store,target=/root/.local/share/pnpm/store pnpm i FROM node:current-alpine AS builder WORKDIR /app +ARG BUILDTIME +ARG VERSION +ARG REVISION + COPY --link --from=deps /app/node_modules ./node_modules/ COPY . . @@ -29,7 +33,7 @@ RUN < config/settings.yaml - npm run build + NEXT_PUBLIC_BUILDTIME=$BUILDTIME NEXT_PUBLIC_VERSION=$VERSION NEXT_PUBLIC_REVISION=$REVISION npm run build EOF # Production image, copy all the files and run next diff --git a/package.json b/package.json index cf9c4131b..239d68dc7 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@headlessui/react": "^1.7.0", "@tailwindcss/forms": "^0.5.3", "classnames": "^2.3.1", + "compare-versions": "^5.0.1", "dockerode": "^3.3.4", "follow-redirects": "^1.15.2", "i18next": "^21.9.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e2625a903..2c9e167b3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,7 @@ specifiers: '@tailwindcss/forms': ^0.5.3 autoprefixer: ^10.4.9 classnames: ^2.3.1 + compare-versions: ^5.0.1 dockerode: ^3.3.4 eslint: ^8.23.1 eslint-config-airbnb: ^19.0.4 @@ -45,6 +46,7 @@ dependencies: '@headlessui/react': 1.7.0_biqbaboplfbrettd7655fr4n2y '@tailwindcss/forms': 0.5.3_tailwindcss@3.1.8 classnames: 2.3.1 + compare-versions: 5.0.1 dockerode: 3.3.4 follow-redirects: 1.15.2 i18next: 21.9.1 @@ -715,6 +717,10 @@ packages: delayed-stream: 1.0.0 dev: false + /compare-versions/5.0.1: + resolution: {integrity: sha512-v8Au3l0b+Nwkp4G142JcgJFh1/TUhdxut7wzD1Nq1dyp5oa3tXaqb03EXOAB6jS4gMlalkjAUPZBMiAfKUixHQ==} + dev: false + /concat-map/0.0.1: resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==} dev: true diff --git a/src/components/version.jsx b/src/components/version.jsx new file mode 100644 index 000000000..187ad3273 --- /dev/null +++ b/src/components/version.jsx @@ -0,0 +1,47 @@ +import { useTranslation } from "react-i18next"; +import useSWR from "swr"; +import { compareVersions } from "compare-versions"; +import { MdNewReleases } from "react-icons/md"; + +export default function Version() { + const { t, i18n } = useTranslation(); + + const buildTime = process.env.NEXT_PUBLIC_BUILDTIME ?? new Date().toISOString(); + const revision = process.env.NEXT_PUBLIC_REVISION ?? "dev"; + const version = process.env.NEXT_PUBLIC_VERSION ?? "dev"; + + const { data: releaseData } = useSWR("https://api.github.com/repos/benphelps/homepage/releases"); + + // use Intl.DateTimeFormat to format the date + const formatDate = (date) => { + const options = { + year: "numeric", + month: "short", + day: "numeric", + }; + return new Intl.DateTimeFormat(i18n.language, options).format(new Date(date)); + }; + + const latestRelease = releaseData?.[0]; + + return ( +
+ + {version} ({revision.substring(0, 7)}, {formatDate(buildTime)}) + + {version === "main" || version === "dev" + ? null + : releaseData && + compareVersions(latestRelease.tag_name, version) > 0 && ( + + {t("Update Available")} + + )} +
+ ); +} diff --git a/src/pages/index.jsx b/src/pages/index.jsx index 16cb836f6..c083d3379 100644 --- a/src/pages/index.jsx +++ b/src/pages/index.jsx @@ -24,12 +24,16 @@ const ColorToggle = dynamic(() => import("components/color-toggle"), { ssr: false, }); +const Version = dynamic(() => import("components/version"), { + ssr: false, +}); + const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"]; export function getStaticProps() { let logger; try { - logger = createLogger('index'); + logger = createLogger("index"); const { providers, ...settings } = getSettings(); return { @@ -38,7 +42,9 @@ export function getStaticProps() { }, }; } catch (e) { - if (logger) { logger.error(e); } + if (logger) { + logger.error(e); + } return { props: { initialSettings: {}, @@ -157,11 +163,15 @@ function Home({ initialSettings }) { )} -
+
{!settings?.color && } {!settings?.theme && }
+ +
+ +
);