Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add version checker #240

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bundlerAnalyzer from "@next/bundle-analyzer";
import packageInfo from './package.json' with { type: "json" };

const withBundleAnalyzer = bundlerAnalyzer({
enabled: process.env.ANALYZE === 'true',
Expand Down Expand Up @@ -28,6 +29,9 @@ const nextConfig = {
typescript: {
ignoreBuildErrors: false,
},
env: {
NEXT_PUBLIC_VERSION: packageInfo.version,
},
images: {
remotePatterns: [
{
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Nounspace",
"version": "0.1",
"version": "0.0.4",
"license": "",
"private": true,
"scripts": {
Expand Down Expand Up @@ -155,7 +155,7 @@
"@stylistic/eslint-plugin-js": "^2.1.0",
"@stylistic/eslint-plugin-jsx": "^2.1.0",
"@stylistic/eslint-plugin-ts": "^2.1.0",
"@types/node": "^18.7.15",
"@types/node": "^20.14.10",
"@types/ramda": "^0.29.11",
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
Expand Down
53 changes: 53 additions & 0 deletions src/common/providers/VersionCheckProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useEffect, useRef, useState } from "react";
import axiosBackend from "../data/api/backend";
import { Button } from "../components/atoms/button";

export default function VersionCheckProivder({
children,
}: {
children: React.ReactNode;
}) {
const pollTimeOut = useRef<NodeJS.Timeout | undefined>();
const [needsRecheck, setNeedsRecheck] = useState(false);
const [versionsMisaligned, setVersionMisaligned] = useState(false);

async function checkVersion() {
setNeedsRecheck(false);
clearTimeout(pollTimeOut.current);
try {
const { data } = await axiosBackend.get<{ buildId: string }>(
"/api/buildId",
);
if (data.buildId !== process.env.NEXT_PUBLIC_VERSION) {
setVersionMisaligned(true);
}
} catch {
console.error("Failed to check for new versio");
} finally {
if (!versionsMisaligned) {
pollTimeOut.current = setTimeout(() => setNeedsRecheck(true), 100000);
}
}
}

useEffect(() => {
if (needsRecheck) checkVersion();
}, [needsRecheck]);

useEffect(() => {
checkVersion();
});

return (
<>
{versionsMisaligned && (
<div className="flex justify-center items-center align-center z-infinity absolute w-full">
<Button onClick={() => window.location.reload()}>
A new version is available! Click to refresh
</Button>
</div>
)}
{children}
</>
);
}
37 changes: 20 additions & 17 deletions src/common/providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,28 @@ import { AppStoreProvider } from "@/common/data/stores/app";
import UserThemeProvider from "@/common/lib/theme/UserThemeProvider";
import LoggedInStateProvider from "./LoggedInStateProvider";
import AnalyticsProvider from "./AnalyticsProvider";
import VersionCheckProivder from "./VersionCheckProvider";

export default function Providers({ children }: { children: React.ReactNode }) {
return (
<Privy>
<Query>
<Wagmi>
<Theme>
<AppStoreProvider>
<UserThemeProvider>
<AuthenticatorProvider>
<LoggedInStateProvider>
<AnalyticsProvider>{children}</AnalyticsProvider>
</LoggedInStateProvider>
</AuthenticatorProvider>
</UserThemeProvider>
</AppStoreProvider>
</Theme>
</Wagmi>
</Query>
</Privy>
<VersionCheckProivder>
<Privy>
<Query>
<Wagmi>
<Theme>
<AppStoreProvider>
<UserThemeProvider>
<AuthenticatorProvider>
<LoggedInStateProvider>
<AnalyticsProvider>{children}</AnalyticsProvider>
</LoggedInStateProvider>
</AuthenticatorProvider>
</UserThemeProvider>
</AppStoreProvider>
</Theme>
</Wagmi>
</Query>
</Privy>
</VersionCheckProivder>
);
}
7 changes: 7 additions & 0 deletions src/pages/api/buildId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NextApiResponse } from "next";

export default function buildId(_, res: NextApiResponse) {
res.status(200).json({
buildId: process.env.NEXT_PUBLIC_VERSION,
});
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5343,10 +5343,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240"
integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==

"@types/node@^18.7.15":
version "18.19.39"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.19.39.tgz#c316340a5b4adca3aee9dcbf05de385978590593"
integrity sha512-nPwTRDKUctxw3di5b4TfT3I0sWDiWoPQCZjXhvdkINntwr8lcoVCKsTgnXeRubKIlfnV+eN/HYk6Jb40tbcEAQ==
"@types/node@^20.14.10":
version "20.14.10"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.14.10.tgz#a1a218290f1b6428682e3af044785e5874db469a"
integrity sha512-MdiXf+nDuMvY0gJKxyfZ7/6UFsETO7mGKF54MVD/ekJS6HdFtpZFBgrh6Pseu64XTb2MLyFPlbW6hj8HYRQNOQ==
dependencies:
undici-types "~5.26.4"

Expand Down