Refactor server list to object instead of array
- Improve performance due to frequency of accessing the list
This commit is contained in:
parent
3dfeed1432
commit
b2db2b27da
4 changed files with 22 additions and 14 deletions
|
@ -192,8 +192,8 @@ axiosClient.interceptors.response.use(
|
||||||
const serverId = currentServer.id;
|
const serverId = currentServer.id;
|
||||||
const token = currentServer.ndCredential;
|
const token = currentServer.ndCredential;
|
||||||
console.log(`token is expired: ${token}`);
|
console.log(`token is expired: ${token}`);
|
||||||
useAuthStore.getState().actions.setCurrentServer(null);
|
|
||||||
useAuthStore.getState().actions.updateServer(serverId, { ndCredential: undefined });
|
useAuthStore.getState().actions.updateServer(serverId, { ndCredential: undefined });
|
||||||
|
useAuthStore.getState().actions.setCurrentServer(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { Checkbox, Stack, Group } from '@mantine/core';
|
||||||
import { Button, PasswordInput, TextInput, toast } from '/@/renderer/components';
|
import { Button, PasswordInput, TextInput, toast } from '/@/renderer/components';
|
||||||
import { useForm } from '@mantine/form';
|
import { useForm } from '@mantine/form';
|
||||||
import { closeAllModals } from '@mantine/modals';
|
import { closeAllModals } from '@mantine/modals';
|
||||||
import { nanoid } from 'nanoid/non-secure';
|
|
||||||
import { RiInformationLine } from 'react-icons/ri';
|
import { RiInformationLine } from 'react-icons/ri';
|
||||||
import { jellyfinApi } from '/@/renderer/api/jellyfin.api';
|
import { jellyfinApi } from '/@/renderer/api/jellyfin.api';
|
||||||
import { navidromeApi } from '/@/renderer/api/navidrome.api';
|
import { navidromeApi } from '/@/renderer/api/navidrome.api';
|
||||||
|
@ -25,7 +24,7 @@ const AUTH_FUNCTIONS = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormProps) => {
|
export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormProps) => {
|
||||||
const { updateServer, setCurrentServer } = useAuthStoreActions();
|
const { updateServer } = useAuthStoreActions();
|
||||||
const [isLoading, setIsLoading] = useState(false);
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
|
@ -61,7 +60,6 @@ export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormPro
|
||||||
|
|
||||||
const serverItem = {
|
const serverItem = {
|
||||||
credential: data.credential,
|
credential: data.credential,
|
||||||
id: nanoid(),
|
|
||||||
name: values.name,
|
name: values.name,
|
||||||
ndCredential: data.ndCredential,
|
ndCredential: data.ndCredential,
|
||||||
type: values.type,
|
type: values.type,
|
||||||
|
@ -71,8 +69,6 @@ export const EditServerForm = ({ isUpdate, server, onCancel }: EditServerFormPro
|
||||||
};
|
};
|
||||||
|
|
||||||
updateServer(server.id, serverItem);
|
updateServer(server.id, serverItem);
|
||||||
setCurrentServer(serverItem);
|
|
||||||
|
|
||||||
toast.success({ message: 'Server has been updated' });
|
toast.success({ message: 'Server has been updated' });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
|
|
|
@ -61,7 +61,7 @@ export const ServerListItem = ({ server }: ServerListItemProps) => {
|
||||||
<Divider my="sm" />
|
<Divider my="sm" />
|
||||||
<TimeoutButton
|
<TimeoutButton
|
||||||
leftIcon={<RiDeleteBin2Line />}
|
leftIcon={<RiDeleteBin2Line />}
|
||||||
timeoutProps={{ callback: handleDeleteServer, duration: 1500 }}
|
timeoutProps={{ callback: handleDeleteServer, duration: 1000 }}
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
>
|
>
|
||||||
Remove server
|
Remove server
|
||||||
|
|
|
@ -18,7 +18,7 @@ export interface AuthSlice extends AuthState {
|
||||||
actions: {
|
actions: {
|
||||||
addServer: (args: ServerListItem) => void;
|
addServer: (args: ServerListItem) => void;
|
||||||
deleteServer: (id: string) => void;
|
deleteServer: (id: string) => void;
|
||||||
getServer: (id: string) => ServerListItem | undefined;
|
getServer: (id: string) => ServerListItem | null;
|
||||||
setCurrentServer: (server: ServerListItem | null) => void;
|
setCurrentServer: (server: ServerListItem | null) => void;
|
||||||
updateServer: (id: string, args: Partial<ServerListItem>) => void;
|
updateServer: (id: string, args: Partial<ServerListItem>) => void;
|
||||||
};
|
};
|
||||||
|
@ -44,7 +44,9 @@ export const useAuthStore = create<AuthSlice>()(
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
getServer: (id) => {
|
getServer: (id) => {
|
||||||
return get().serverList[id];
|
const server = get().serverList[id];
|
||||||
|
if (server) return server;
|
||||||
|
return null;
|
||||||
},
|
},
|
||||||
setCurrentServer: (server) => {
|
setCurrentServer: (server) => {
|
||||||
set((state) => {
|
set((state) => {
|
||||||
|
@ -62,11 +64,13 @@ export const useAuthStore = create<AuthSlice>()(
|
||||||
},
|
},
|
||||||
updateServer: (id: string, args: Partial<ServerListItem>) => {
|
updateServer: (id: string, args: Partial<ServerListItem>) => {
|
||||||
set((state) => {
|
set((state) => {
|
||||||
state.serverList[id] = { ...state.serverList[id], ...args };
|
const updatedServer = {
|
||||||
|
...state.serverList[id],
|
||||||
|
...args,
|
||||||
|
};
|
||||||
|
|
||||||
if (state.currentServer?.id === id) {
|
state.serverList[id] = updatedServer;
|
||||||
state.currentServer = { ...state.serverList[id], ...args };
|
state.currentServer = updatedServer;
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -79,7 +83,7 @@ export const useAuthStore = create<AuthSlice>()(
|
||||||
{
|
{
|
||||||
merge: (persistedState, currentState) => merge(currentState, persistedState),
|
merge: (persistedState, currentState) => merge(currentState, persistedState),
|
||||||
name: 'store_authentication',
|
name: 'store_authentication',
|
||||||
version: 1,
|
version: 2,
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -91,3 +95,11 @@ export const useCurrentServer = () => useAuthStore((state) => state.currentServe
|
||||||
export const useServerList = () => useAuthStore((state) => state.serverList);
|
export const useServerList = () => useAuthStore((state) => state.serverList);
|
||||||
|
|
||||||
export const useAuthStoreActions = () => useAuthStore((state) => state.actions);
|
export const useAuthStoreActions = () => useAuthStore((state) => state.actions);
|
||||||
|
|
||||||
|
export const getServerById = (id?: string) => {
|
||||||
|
if (!id) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return useAuthStore.getState().actions.getServer(id);
|
||||||
|
};
|
||||||
|
|
Reference in a new issue