Skip to content

Commit

Permalink
Merge pull request #87 from UnsignedArduino/staging
Browse files Browse the repository at this point in the history
Environment page update
  • Loading branch information
UnsignedArduino authored Mar 17, 2024
2 parents d19a072 + 4a68127 commit 1f86867
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 101 deletions.
2 changes: 1 addition & 1 deletion src/components/Adsense/adsense.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GoogleAdSense } from "nextjs-google-adsense";
import React from "react";
import { getEnvironment } from "../WithAppProps";
import { getEnvironment } from "@/scripts/Utils/Environment";

export function Adsense(): JSX.Element {
React.useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Analytics/analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { NextWebVitalsMetric } from "next/app";
import { event, GoogleAnalytics } from "nextjs-google-analytics";
import React from "react";
import { getEnvironment } from "@/components/WithAppProps";
import { getEnvironment } from "@/scripts/Utils/Environment";

export function getAdStorageConsent(): string {
return window.localStorage.getItem("adStorageConsent") || "denied";
Expand Down
2 changes: 1 addition & 1 deletion src/components/FeatureFlags/featureFlags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GrowthBook } from "@growthbook/growthbook-react";
import { getEnvironment } from "@/components/WithAppProps";
import { AnalyticEvents } from "@/components/Analytics";
import { getEnvironment } from "@/scripts/Utils/Environment";

export const growthbook = new GrowthBook({
apiHost: process.env.NEXT_PUBLIC_GROWTHBOOK_API_HOST,
Expand Down
24 changes: 23 additions & 1 deletion src/components/Footer/footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import Image from "next/image";
import { appName } from "@/components/Layout/layout";
import icon from "../../../public/android-chrome-512x512.png";
import { AppProps } from "@/components/WithAppProps";
import { formatDateLong } from "@/scripts/Utils/DateAndTime/Format";
import {
formatDateAndTime,
formatDateLong,
} from "@/scripts/Utils/DateAndTime/Format";

export const DEVELOPERS = ["UnsignedArduino"];
export const CREATION_DATE = new Date("2023-04-15T02:00:20Z");
Expand Down Expand Up @@ -113,6 +116,25 @@ function Footer({ appProps }: { appProps: AppProps }): JSX.Element {
owner of MakeCode Arcade. <br />
Microsoft and MakeCode Arcade are trademarks of the Microsoft group of
companies.
<br />
<br />
Build{" "}
<a
href={`https://github.com/UnsignedArduino/Awesome-Arcade/commit/${appProps.buildHash}`}
target="_blank"
rel="noopener noreferrer"
>
<code>{appProps.buildHash}</code>
</a>{" "}
(branch{" "}
<a
href={`https://github.com/UnsignedArduino/Awesome-Arcade/tree/${appProps.buildBranch}`}
target="_blank"
rel="noopener noreferrer"
>
<code>{appProps.buildBranch}</code>
</a>
) on {formatDateAndTime(new Date(appProps.buildTime))}.
</>
);

Expand Down
19 changes: 19 additions & 0 deletions src/components/TextCountFromDate/Static.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { formatDuration } from "@/scripts/Utils/DateAndTime/Format";
import React from "react";

export function StaticLowPrecisionTextCountup({
date,
}: {
date: Date;
}): React.ReactNode {
const [show, setShow] = React.useState(false);
const timeSince = Date.now() - date.getTime();

React.useEffect(() => {
setShow(true);
}, []);

return (
<>{show ? (timeSince > 0 ? formatDuration(timeSince) : null) : null}</>
);
}
67 changes: 67 additions & 0 deletions src/components/TextCountFromDate/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { formatDuration } from "@/scripts/Utils/DateAndTime/Format";
import React from "react";

export function TextCountdown({
date,
updatePeriod,
onEnd,
}: {
date: Date;
updatePeriod?: number;
onEnd?: () => void;
}): React.ReactNode {
const [timeLeft, setTimeLeft] = React.useState<number>(
date.getTime() - Date.now(),
);

React.useEffect(() => {
const interval = setInterval(() => {
setTimeLeft(date.getTime() - Date.now());
if (timeLeft <= 0) {
clearInterval(interval);
if (onEnd) {
onEnd();
}
}
}, updatePeriod ?? 100);

return () => {
clearInterval(interval);
};
}, [date, onEnd, timeLeft, updatePeriod]);

return <>{timeLeft > 0 ? formatDuration(timeLeft) : null}</>;
}

export function TextCountup({
date,
updatePeriod,
serverRender = true,
}: {
date: Date;
updatePeriod?: number;
serverRender?: boolean;
}): React.ReactNode {
const [show, setShow] = React.useState(serverRender);
const [timeSince, setTimeSince] = React.useState<number>(
Date.now() - date.getTime(),
);

React.useEffect(() => {
const interval = setInterval(() => {
setTimeSince(Date.now() - date.getTime());
}, updatePeriod ?? 100);

return () => {
clearInterval(interval);
};
}, [date, updatePeriod]);

React.useEffect(() => {
setShow(true);
}, []);

return (
<>{show ? (timeSince > 0 ? formatDuration(timeSince) : null) : null}</>
);
}
44 changes: 15 additions & 29 deletions src/components/WithAppProps/appProps.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,30 @@
import { parseExtensionXML, parseToolXML } from "../../scripts/ParseListXML";
import { parseExtensionXML, parseToolXML } from "@/scripts/ParseListXML";
import { promises as fs } from "fs";
import path from "path";

type Environment = "development" | "preview" | "production";

export function getEnvironment(): Environment {
return process.env.VERCEL_ENV != undefined
? (process.env.VERCEL_ENV as Environment)
: process.env.NODE_ENV != undefined
? (process.env.NODE_ENV as Environment)
: "production";
}

export function getBaseURL(): string {
switch (getEnvironment()) {
case "production": {
return "https://awesome-arcade.vercel.app";
}
case "preview": {
return "https://awesome-arcade-beta.vercel.app";
}
case "development": {
return "http://localhost:3000";
}
}
}

export function getBranch(): "main" | "staging" {
return getEnvironment() === "production" ? "main" : "staging";
}
import { Environment, getEnvironment } from "@/scripts/Utils/Environment";
import { execSync } from "node:child_process";

export interface AppProps {
environment: Environment;
buildHash: string;
buildBranch: string;
buildTime: string;
extensionsListed: number;
toolsListed: number;
}

export async function getAppProps(): Promise<AppProps> {
return {
environment: getEnvironment(),
buildHash:
process.env.VERCEL_GIT_COMMIT_SHA != undefined
? process.env.VERCEL_GIT_COMMIT_SHA
: execSync("git rev-parse HEAD").toString().trim(),
buildBranch:
process.env.VERCEL_GIT_COMMIT_REF != undefined
? process.env.VERCEL_GIT_COMMIT_REF
: execSync("git rev-parse --abbrev-ref HEAD").toString().trim(),
buildTime: new Date().toISOString(),
extensionsListed: (
await parseExtensionXML(
(
Expand Down
2 changes: 1 addition & 1 deletion src/components/WithAppProps/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default, getEnvironment, getBranch } from "./appProps";
export { default } from "./appProps";
export type { AppProps } from "./appProps";
86 changes: 86 additions & 0 deletions src/pages/_environment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React from "react";
import Layout from "../components/Layout";
import getAppProps, { AppProps } from "../components/WithAppProps";
import { formatDateAndTimeAndSecond } from "@/scripts/Utils/DateAndTime/Format";
import { TextCountup } from "@/components/TextCountFromDate";

const pageName = "Environment";

interface AboutProps {
envVars: string[][];
appProps: AppProps;
}

export function About({ envVars, appProps }: AboutProps): JSX.Element {
return (
<Layout title={pageName} currentPage={pageName} appProps={appProps}>
<h1>Environment</h1>
<p>
Build hash:{" "}
<a
href={`https://github.com/UnsignedArduino/Awesome-Arcade/commit/${appProps.buildHash}`}
target="_blank"
rel="noopener noreferrer"
>
<code>{appProps.buildHash}</code>
</a>
<br />
Build branch:{" "}
<a
href={`https://github.com/UnsignedArduino/Awesome-Arcade/tree/${appProps.buildBranch}`}
target="_blank"
rel="noopener noreferrer"
>
<code>{appProps.buildBranch}</code>
</a>
<br />
Build time: <code>{appProps.buildTime}</code> (
{formatDateAndTimeAndSecond(new Date(appProps.buildTime))} -{" "}
<TextCountup date={new Date(appProps.buildTime)} serverRender={false} />{" "}
ago)
</p>
<h2>Build variables</h2>
<code>
{envVars.map((value: string[]) => {
return (
<React.Fragment key={value[0]}>
{value[0]}: {value[1]}
<br />
</React.Fragment>
);
})}
</code>
</Layout>
);
}

export async function getStaticProps(): Promise<{ props: AboutProps }> {
const vercelEnvs = [
"NODE_ENV",
"VERCEL_ENV",
"VERCEL_URL",
"VERCEL_GIT_PROVIDER",
"VERCEL_GIT_REPO_SLUG",
"VERCEL_GIT_REPO_OWNER",
"VERCEL_GIT_REPO_ID",
"VERCEL_GIT_COMMIT_REF",
"VERCEL_GIT_COMMIT_SHA",
"VERCEL_GIT_COMMIT_MESSAGE",
"VERCEL_GIT_COMMIT_AUTHOR_LOGIN",
"VERCEL_GIT_COMMIT_AUTHOR_NAME",
];

return {
props: {
envVars: vercelEnvs.map((envVar: string) => {
return [
envVar,
process.env[envVar] != undefined ? process.env[envVar] : "undefined",
];
}) as string[][],
appProps: await getAppProps(),
},
};
}

export default About;
59 changes: 0 additions & 59 deletions src/pages/_environment/environment.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/pages/_environment/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/scripts/Database/database.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Pool, QueryConfig, QueryResult } from "pg";
import { getEnvironment } from "@/components/WithAppProps";
import { getEnvironment } from "@/scripts/Utils/Environment";

export default function queryDb(
queryTextOrConfig: string | QueryConfig<any[]>,
values?: any[] | undefined
values?: any[] | undefined,
): Promise<QueryResult<any>> {
const pool = new Pool({
host: process.env.POSTGRES_HOST,
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/RSS/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Feed } from "feed";
import { getBaseURL, getEnvironment } from "@/components/WithAppProps/appProps";
import { BlogPostPreview } from "@/components/Blog/Post/Preview";
import { getBaseURL, getEnvironment } from "@/scripts/Utils/Environment";

export default async function generateRSSFeed(
posts: BlogPostPreview[],
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/SiteWebmanifest/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEnvironment } from "../../components/WithAppProps";
import { getEnvironment } from "@/scripts/Utils/Environment";

export default async function generateSiteWebmanifest(): Promise<string> {
const json = JSON.parse(`{
Expand Down
Loading

0 comments on commit 1f86867

Please sign in to comment.