Skip to content

Commit a0e3d06

Browse files
committed
fix minor glitches when changing authorization
1 parent a7d9322 commit a0e3d06

File tree

8 files changed

+48
-26
lines changed

8 files changed

+48
-26
lines changed

src/Serilog.Ui.Web/src/app/components/Authorization/BasicModal.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@ const BasicModal = ({ onClose }: { onClose: () => void }) => {
6161
>
6262
Save
6363
</Button>
64-
<Button
65-
display={!isHeaderReady ? 'none' : 'inherit'}
66-
onClick={() => {
67-
clearAuthState();
68-
}}
69-
>
64+
<Button display={!isHeaderReady ? 'none' : 'inherit'} onClick={clearAuthState}>
7065
Change
7166
</Button>
7267
<Button onClick={onClose}>Close</Button>

src/Serilog.Ui.Web/src/app/components/Authorization/JwtModal.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,7 @@ const JwtModal = ({ onClose }: { onClose: () => void }) => {
4444
>
4545
Save
4646
</Button>
47-
<Button
48-
display={!isHeaderReady ? 'none' : 'inherit'}
49-
onClick={() => {
50-
clearAuthState();
51-
}}
52-
>
47+
<Button display={!isHeaderReady ? 'none' : 'inherit'} onClick={clearAuthState}>
5348
Change Token
5449
</Button>
5550
<Button onClick={onClose}>Close</Button>

src/Serilog.Ui.Web/src/app/components/Search/Search.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import useQueryLogs from 'app/hooks/useQueryLogs';
1414
import { useQueryTableKeys } from 'app/hooks/useQueryTableKeys';
1515
import { useSearchForm } from 'app/hooks/useSearchForm';
1616
import { useSerilogUiProps } from 'app/hooks/useSerilogUiProps';
17-
import { memo } from 'react';
17+
import { memo, useEffect } from 'react';
1818
import { useController, useWatch } from 'react-hook-form';
1919
import classes from 'style/search.module.css';
20-
import { LogLevel } from '../../../types/types';
20+
import { DispatchedCustomEvents, LogLevel } from '../../../types/types';
2121

2222
const levelsArray = Object.keys(LogLevel).map((level) => ({
2323
value: level,
@@ -49,6 +49,18 @@ const Search = ({ onRefetch }: { onRefetch?: () => void }) => {
4949
}
5050
};
5151

52+
useEffect(() => {
53+
const resetTableKey = () => {
54+
reset(true);
55+
};
56+
57+
document.addEventListener(DispatchedCustomEvents.RemoveTableKey, resetTableKey);
58+
59+
return () =>
60+
document.removeEventListener(DispatchedCustomEvents.RemoveTableKey, resetTableKey);
61+
// eslint-disable-next-line react-hooks/exhaustive-deps
62+
}, []);
63+
5264
return (
5365
<form aria-label="search-logs-form" onSubmit={() => {}}>
5466
<Grid className={classes.searchFiltersGrid}>

src/Serilog.Ui.Web/src/app/hooks/useAuthProperties.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQueryClient } from '@tanstack/react-query';
12
import {
23
checkErrors,
34
clearAuth,
@@ -8,10 +9,17 @@ import {
89
saveAuthKey,
910
} from 'app/util/auth';
1011
import { createRequestInit } from 'app/util/queries';
11-
import { createContext, type ReactNode, useCallback, useContext, useMemo, useState } from 'react';
12-
import { useSerilogUiProps } from './useSerilogUiProps';
12+
import {
13+
createContext,
14+
type ReactNode,
15+
useCallback,
16+
useContext,
17+
useMemo,
18+
useState,
19+
} from 'react';
20+
import { AuthType, DispatchedCustomEvents } from '../../types/types.ts';
1321
import { isStringGuard } from '../util/guards.ts';
14-
import { AuthType } from '../../types/types.ts';
22+
import { useSerilogUiProps } from './useSerilogUiProps';
1523

1624
interface AuthProps {
1725
authProps: IAuthPropertiesData;
@@ -46,6 +54,7 @@ export const AuthPropertiesProvider = ({
4654
}: {
4755
children: ReactNode | undefined;
4856
}) => {
57+
const queryClient = useQueryClient();
4958
const { authType, routePrefix } = useSerilogUiProps();
5059

5160
const [authInfo, setAuthInfo] = useState<IAuthPropertiesData>({
@@ -57,7 +66,7 @@ export const AuthPropertiesProvider = ({
5766
[authInfo, authType],
5867
);
5968
const isHeaderReady = authType === AuthType.Custom || isStringGuard(authHeader);
60-
69+
6170
const fetchInfo = useMemo(
6271
() => ({
6372
headers: createRequestInit(authType, authHeader),
@@ -68,8 +77,12 @@ export const AuthPropertiesProvider = ({
6877

6978
const clearAuthState = useCallback(() => {
7079
const cleanState = clearAuth();
80+
81+
queryClient.removeQueries({ queryKey: ['get-keys'], exact: false });
82+
document.dispatchEvent(new CustomEvent(DispatchedCustomEvents.RemoveTableKey));
83+
7184
setAuthInfo(cleanState);
72-
}, []);
85+
}, [queryClient]);
7386

7487
const saveAuthState = useCallback((input: { [key: string]: string }) => {
7588
const validationInfo: string[] = [];
@@ -114,8 +127,14 @@ export const AuthPropertiesProvider = ({
114127
};
115128

116129
export const useAuthProperties = () => {
117-
const { authProps, authHeader, fetchInfo, isHeaderReady, clearAuthState, saveAuthState } =
118-
useContext(AuthPropertiesContext);
130+
const {
131+
authProps,
132+
authHeader,
133+
fetchInfo,
134+
isHeaderReady,
135+
clearAuthState,
136+
saveAuthState,
137+
} = useContext(AuthPropertiesContext);
119138

120139
return {
121140
...authProps,

src/Serilog.Ui.Web/src/app/hooks/useColumnsInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const useColumnsInfo = (currentColumn = '', logPropertyType = '') => {
1212
const { getValues } = useSearchForm();
1313
const { columnsInfo } = useSerilogUiProps();
1414

15-
const currentTable = getValues('table');
15+
const currentTable = getValues('table') ?? '';
1616
const hasInfoOnCurrentTable = columnsInfo?.[currentTable];
1717

1818
// treating Properties as an additional column, in the rendering

src/Serilog.Ui.Web/src/app/hooks/useQueryLogs.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const useQueryLogs = () => {
1414
const sortBy = useWatch({ name: 'sortBy' })
1515
const sortOn = useWatch({ name: 'sortOn' })
1616

17-
console.log(['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey])
1817
return useQuery({
1918
enabled: true,
2019
queryKey: ['get-logs', entriesPerPage, page, sortBy, sortOn, currentDbKey],

src/Serilog.Ui.Web/src/app/hooks/useSearchForm.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ export const useSearchForm = () => {
4141
const { data } = useQueryTableKeys();
4242
const tableKeysDefaultValue = isArrayGuard(data) ? data.at(0)! : '';
4343

44-
const resetForm = () => {
44+
const resetForm = (blankTable?: boolean) => {
4545
const runRefetch = runManualRefetch(useSearchContext.getValues, tableKeysDefaultValue)
4646

4747
useSearchContext.reset({
4848
...searchFormInitialValues,
49-
table: tableKeysDefaultValue,
49+
table: !blankTable ? tableKeysDefaultValue : null,
5050
});
5151

5252
return runRefetch

src/Serilog.Ui.Web/src/types/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export enum SearchParameters {
4949
}
5050

5151
export interface SearchForm {
52-
table: string;
52+
table: string | null;
5353
level: LogLevel | null;
5454
startDate: Date | null;
5555
endDate: Date | null;
@@ -96,6 +96,8 @@ export type ColumnsInfo = {
9696
};
9797
};
9898

99+
export enum DispatchedCustomEvents { RemoveTableKey = 'remove-table-key' }
100+
99101
export interface SerilogUiConfig {
100102
authType?: AuthType;
101103
routePrefix?: string;

0 commit comments

Comments
 (0)