Skip to content

Commit

Permalink
Simplify version info on frontend by using api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
FoxxMD committed Jun 26, 2024
1 parent ca9acbb commit 26dab38
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/backend/server/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ export const setupApi = (app: ExpressWithAsync, logger: Logger, appLoggerStream:
return res.status((clientsReady && sourcesReady) ? 200 : 500).json({messages: sourceMessages.concat(clientMessages)});
});

app.getAsync('/api/version', async (req, res) => {
return res.json({version: root.get('version')});
});

app.useAsync('/api/*', async (req, res) => {
const remote = req.connection.remoteAddress;
const proxyRemote = req.headers["x-forwarded-for"];
Expand Down
7 changes: 2 additions & 5 deletions src/client/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ScrobbledPage from "./scrobbled/ScrobbledPage";
import DeadPage from "./deadLetter/DeadPage";
import {clientUpdate, sourceUpdate} from "./status/ducks";
import {useEventSource, useEventSourceListener} from "@react-nano/use-event-source";
import Version from "./Version";

function NoMatch() {
let location = useLocation();
Expand Down Expand Up @@ -86,8 +87,6 @@ const Global = (props: PropsFromRedux) => {

const ConnectedGlobal = connector(Global);

const version = __APP_VERSION__;

function App() {
return (
<Provider store={store}>
Expand All @@ -97,9 +96,7 @@ function App() {
<div className="flex items-center justify-between">
<a href="/" className="flex items-center flex-grow no-underline pr-4">
<img src="icon.svg" style={{maxWidth: '30px'}}/>
<span className="px-4 break-normal">
Multi Scrobbler <span className="ml-2 text-xs version">v{version}</span>
</span>
<Version/>
</a>
</div>
</div>
Expand Down
26 changes: 26 additions & 0 deletions src/client/Version.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react';
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/dist/query/react/index";

export const versionApi = createApi({
reducerPath: 'versionApi',
baseQuery: fetchBaseQuery({ baseUrl: './api/' }),
endpoints: (builder) => ({
getVersion: builder.query<{version: string}, undefined>({
query: () => `version`,
}),
}),
});

const { useGetVersionQuery } = versionApi;

const Version = () => {
const {
data = undefined,
} = useGetVersionQuery(undefined);

return <span className="px-4 break-normal">
Multi Scrobbler <span className="ml-2 text-xs version">{data === undefined ? null : `${data.version}`}</span>
</span>;
}

export default Version;
24 changes: 13 additions & 11 deletions src/client/store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {configureStore} from '@reduxjs/toolkit'
import { configureStore } from '@reduxjs/toolkit'
// Or from '@reduxjs/toolkit/query/react'
import {setupListeners} from '@reduxjs/toolkit/query'
import {statusApi} from './status/statusApi';
import {clientSlice, sourceSlice} from "./status/ducks";
import {logsReducer} from "./logs/logDucks";
import {logsApi} from "./logs/logsApi";
import {recentApi} from "./recent/recentDucks";
import {scrobbledApi} from "./scrobbled/scrobbledDucks";
import {deadApi, deadSlice} from "./deadLetter/deadLetterDucks";
import {scrobblerApi} from './components/statusCard/clientDucks';
import { setupListeners } from '@reduxjs/toolkit/query'
import { scrobblerApi } from './components/statusCard/clientDucks';
import { deadApi, deadSlice } from "./deadLetter/deadLetterDucks";
import { logsReducer } from "./logs/logDucks";
import { logsApi } from "./logs/logsApi";
import { recentApi } from "./recent/recentDucks";
import { scrobbledApi } from "./scrobbled/scrobbledDucks";
import { clientSlice, sourceSlice } from "./status/ducks";
import { statusApi } from './status/statusApi';
import { versionApi } from "./Version";

export const store = configureStore({
reducer: {
Expand All @@ -19,6 +20,7 @@ export const store = configureStore({
[deadApi.reducerPath]: deadApi.reducer,
[scrobbledApi.reducerPath]: scrobbledApi.reducer,
[scrobblerApi.reducerPath]: scrobblerApi.reducer,
[versionApi.reducerPath]: versionApi.reducer,
//parts: statusReducer
clients: clientSlice.reducer,
sources: sourceSlice.reducer,
Expand All @@ -28,7 +30,7 @@ export const store = configureStore({
// Adding the api middleware enables caching, invalidation, polling,
// and other useful features of `rtk-query`.
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat([statusApi.middleware, logsApi.middleware, recentApi.middleware, scrobbledApi.middleware, deadApi.middleware, scrobblerApi.middleware]),
getDefaultMiddleware().concat([statusApi.middleware, logsApi.middleware, recentApi.middleware, scrobbledApi.middleware, deadApi.middleware, scrobblerApi.middleware, versionApi.middleware]),
})

// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
Expand Down
1 change: 0 additions & 1 deletion src/client/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/// <reference types="vite/client" />
declare const __APP_VERSION__: string
declare const __USE_HASH_ROUTER__: bool
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default defineConfig(() => {
sourcemap: true
},
define: {
"__APP_VERSION__": JSON.stringify((process.env.APP_VERSION ?? 'Unknown').toString()),
"__USE_HASH_ROUTER__": JSON.stringify((process.env.USE_HASH_ROUTER ?? false))
}
};
Expand Down

0 comments on commit 26dab38

Please sign in to comment.