From 0825257a2e03df9f7cbad8fb1cafe63a84fcd2be Mon Sep 17 00:00:00 2001 From: Nicolas Meienberger Date: Wed, 22 Feb 2023 08:58:23 +0100 Subject: [PATCH] fix(dashboard): only check status if restart or update has been requested --- .gitignore | 11 +++++------ .../components/hoc/StatusProvider/StatusProvider.tsx | 7 ++++--- .../Apps/components/InstallForm/InstallForm.tsx | 6 +++--- .../SettingsContainer/SettingsContainer.tsx | 5 ++++- src/client/state/systemStore.ts | 4 ++++ src/pages/_app.tsx | 4 ++-- src/server/common/get-server-auth-session.ts | 3 ++- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 46a2626d..9d5a90cf 100644 --- a/.gitignore +++ b/.gitignore @@ -49,14 +49,13 @@ logs !.env.test github.secrets node_modules/ -app-data -data -data -repos -apps +/app-data/ +/data/ +/repos/ +/apps/ traefik/shared # media folder media -state +/state/ diff --git a/src/client/components/hoc/StatusProvider/StatusProvider.tsx b/src/client/components/hoc/StatusProvider/StatusProvider.tsx index 7f164d3e..d333a6da 100644 --- a/src/client/components/hoc/StatusProvider/StatusProvider.tsx +++ b/src/client/components/hoc/StatusProvider/StatusProvider.tsx @@ -1,6 +1,6 @@ import React, { ReactElement, useEffect, useRef } from 'react'; import router from 'next/router'; -import { useSystemStore } from '../../../state/systemStore'; +import { useSystemStore } from '../../../state/systemStore'; import { StatusScreen } from '../../StatusScreen'; interface IProps { @@ -8,12 +8,13 @@ interface IProps { } export const StatusProvider: React.FC = ({ children }) => { - const { status } = useSystemStore(); + const { status, setPollStatus } = useSystemStore(); const s = useRef(status); useEffect(() => { // If previous was not running and current is running, we need to refresh the page if (status === 'RUNNING' && s.current !== 'RUNNING') { + setPollStatus(false); router.reload(); } if (status === 'RUNNING') { @@ -25,7 +26,7 @@ export const StatusProvider: React.FC = ({ children }) => { if (status === 'UPDATING') { s.current = 'UPDATING'; } - }, [status, s]); + }, [status, s, setPollStatus]); if (s.current === 'LOADING') { return ; diff --git a/src/client/modules/Apps/components/InstallForm/InstallForm.tsx b/src/client/modules/Apps/components/InstallForm/InstallForm.tsx index 3732a47d..295dfe46 100644 --- a/src/client/modules/Apps/components/InstallForm/InstallForm.tsx +++ b/src/client/modules/Apps/components/InstallForm/InstallForm.tsx @@ -28,7 +28,7 @@ export const InstallForm: React.FC = ({ formFields, onSubmit, initalValu const { register, handleSubmit, - formState: { errors }, + formState: { errors, isDirty }, setValue, watch, setError, @@ -36,12 +36,12 @@ export const InstallForm: React.FC = ({ formFields, onSubmit, initalValu const watchExposed = watch('exposed', false); useEffect(() => { - if (initalValues) { + if (initalValues && !isDirty) { Object.entries(initalValues).forEach(([key, value]) => { setValue(key, value); }); } - }, [initalValues, setValue]); + }, [initalValues, isDirty, setValue]); const renderField = (field: FormField) => ( = ({ data }) => { const [loading, setLoading] = React.useState(false); const { current, latest } = data; const { addToast } = useToastStore(); + const { setPollStatus } = useSystemStore(); const restartDisclosure = useDisclosure(); const updateDisclosure = useDisclosure(); @@ -26,6 +28,7 @@ export const SettingsContainer: React.FC = ({ data }) => { }, onSuccess: async () => { setLoading(false); + setPollStatus(true); localStorage.removeItem('token'); }, onError: (error) => { @@ -41,7 +44,7 @@ export const SettingsContainer: React.FC = ({ data }) => { }, onSuccess: async () => { setLoading(false); - + setPollStatus(true); localStorage.removeItem('token'); }, onError: (error) => { diff --git a/src/client/state/systemStore.ts b/src/client/state/systemStore.ts index f0511419..c7fcf43c 100644 --- a/src/client/state/systemStore.ts +++ b/src/client/state/systemStore.ts @@ -10,14 +10,18 @@ export type SystemStatus = (typeof SYSTEM_STATUS)[keyof typeof SYSTEM_STATUS]; type Store = { status: SystemStatus; + pollStatus: boolean; version: { current: string; latest?: string }; setStatus: (status: SystemStatus) => void; setVersion: (version: { current: string; latest?: string }) => void; + setPollStatus: (pollStatus: boolean) => void; }; export const useSystemStore = create((set) => ({ status: 'RUNNING', version: { current: '0.0.0', latest: '0.0.0' }, + pollStatus: false, setStatus: (status: SystemStatus) => set((state) => ({ ...state, status })), setVersion: (version: { current: string; latest?: string }) => set((state) => ({ ...state, version })), + setPollStatus: (pollStatus: boolean) => set((state) => ({ ...state, pollStatus })), })); diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index eb139fa7..1e2ce1b2 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -11,9 +11,9 @@ import { SystemStatus, useSystemStore } from '../client/state/systemStore'; function MyApp({ Component, pageProps }: AppProps) { const { setDarkMode } = useUIStore(); - const { setStatus, setVersion } = useSystemStore(); + const { setStatus, setVersion, pollStatus } = useSystemStore(); - trpc.system.status.useQuery(undefined, { networkMode: 'online', refetchInterval: 5000, onSuccess: (d) => setStatus((d.status as SystemStatus) || 'RUNNING') }); + trpc.system.status.useQuery(undefined, { networkMode: 'online', refetchInterval: 2000, onSuccess: (d) => setStatus((d.status as SystemStatus) || 'RUNNING'), enabled: pollStatus }); const version = trpc.system.getVersion.useQuery(undefined, { networkMode: 'online' }); useEffect(() => { diff --git a/src/server/common/get-server-auth-session.ts b/src/server/common/get-server-auth-session.ts index 5f8c7c85..915a8497 100644 --- a/src/server/common/get-server-auth-session.ts +++ b/src/server/common/get-server-auth-session.ts @@ -2,6 +2,7 @@ import { type GetServerSidePropsContext } from 'next'; import jwt from 'jsonwebtoken'; import { getConfig } from '../core/TipiConfig'; import TipiCache from '../core/TipiCache'; +import { Logger } from '../core/Logger'; export const getServerAuthSession = async (ctx: { req: GetServerSidePropsContext['req']; res: GetServerSidePropsContext['res'] }) => { const { req } = ctx; @@ -19,7 +20,7 @@ export const getServerAuthSession = async (ctx: { req: GetServerSidePropsContext }; } } catch (err) { - console.error(err); + Logger.info(err); } }