React 18 and Vite (#4510)

This commit is contained in:
Matthew Penner 2022-11-25 13:25:03 -07:00 committed by GitHub
parent 1bb1b13f6d
commit 21613fa602
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
244 changed files with 4547 additions and 8933 deletions

View file

@ -26,7 +26,7 @@ export class Websocket extends EventEmitter {
this.url = url;
this.socket = new Sockette(`${this.url}`, {
onmessage: (e) => {
onmessage: e => {
try {
const { event, args } = JSON.parse(e.data);
args ? this.emit(event, ...args) : this.emit(event);
@ -47,7 +47,7 @@ export class Websocket extends EventEmitter {
this.authenticate();
},
onclose: () => this.emit('SOCKET_CLOSE'),
onerror: (error) => this.emit('SOCKET_ERROR', error),
onerror: error => this.emit('SOCKET_ERROR', error),
});
this.timer = setTimeout(() => {
@ -100,7 +100,7 @@ export class Websocket extends EventEmitter {
JSON.stringify({
event,
args: Array.isArray(payload) ? payload : [payload],
})
}),
);
}
}

View file

@ -3,7 +3,7 @@ import { useEffect, useRef } from 'react';
export default (
eventName: string,
handler: (e: Event | CustomEvent | UIEvent | any) => void,
options?: boolean | EventListenerOptions
options?: boolean | EventListenerOptions,
) => {
const savedHandler = useRef<any>(null);

View file

@ -6,8 +6,8 @@ import { ServerContext } from '@/state/server';
export const getDirectorySwrKey = (uuid: string, directory: string): string => `${uuid}:files:${directory}`;
export default () => {
const uuid = ServerContext.useStoreState((state) => state.server.data!.uuid);
const directory = ServerContext.useStoreState((state) => state.files.directory);
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
const directory = ServerContext.useStoreState(state => state.files.directory);
return useSWR<FileObject[]>(
getDirectorySwrKey(uuid, directory),
@ -17,6 +17,6 @@ export default () => {
revalidateOnMount: false,
refreshInterval: 0,
errorRetryCount: 2,
}
},
);
};

View file

@ -18,7 +18,7 @@ const useFlashKey = (key: string): KeyedFlashStore => {
return {
addError: (message, title) => addFlash({ key, message, title, type: 'error' }),
clearFlashes: () => clearFlashes(key),
clearAndAddHttpError: (error) => clearAndAddHttpError({ key, error }),
clearAndAddHttpError: error => clearAndAddHttpError({ key, error }),
};
};

View file

@ -9,7 +9,7 @@ export default () => {
.substring(1)
.split('&')
.reduce((obj, str) => {
const [key, value = ''] = str.split('=');
const [key = '', value = ''] = str.split('=');
return !str.trim() ? obj : { ...obj, [key]: value };
}, {});
@ -18,11 +18,11 @@ export default () => {
const current = getHashObject(location.hash);
for (const key in params) {
current[key] = params[key];
current[key] = params[key] ?? '';
}
return Object.keys(current)
.map((key) => `${key}=${current[key]}`)
.map(key => `${key}=${current[key]}`)
.join('&');
};

View file

@ -2,7 +2,7 @@ import { ServerContext } from '@/state/server';
import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo';
export const usePermissions = (action: string | string[]): boolean[] => {
const userPermissions = ServerContext.useStoreState((state) => state.server.permissions);
const userPermissions = ServerContext.useStoreState(state => state.server.permissions);
return useDeepCompareMemo(() => {
if (userPermissions[0] === '*') {
@ -10,13 +10,13 @@ export const usePermissions = (action: string | string[]): boolean[] => {
}
return (Array.isArray(action) ? action : [action]).map(
(permission) =>
permission =>
// Allows checking for any permission matching a name, for example files.*
// will return if the user has any permission under the file.XYZ namespace.
(permission.endsWith('.*') &&
userPermissions.filter((p) => p.startsWith(permission.split('.')[0])).length > 0) ||
userPermissions.filter(p => p.startsWith(permission.split('.')?.[0] ?? '')).length > 0) ||
// Otherwise just check if the entire permission exists in the array or not.
userPermissions.indexOf(permission) >= 0
userPermissions.indexOf(permission) >= 0,
);
}, [action, userPermissions]);
};

View file

@ -2,7 +2,7 @@ import { Dispatch, SetStateAction, useEffect, useState } from 'react';
export function usePersistedState<S = undefined>(
key: string,
defaultValue: S
defaultValue: S,
): [S | undefined, Dispatch<SetStateAction<S | undefined>>] {
const [state, setState] = useState(() => {
try {

View file

@ -7,7 +7,7 @@ type Context = string | string[] | (string | number | null | {})[];
function useSWRKey(context: Context, prefix: string | null = null): string {
const key = useDeepCompareMemo((): string => {
return (Array.isArray(context) ? context : [context]).map((value) => JSON.stringify(value)).join(':');
return (Array.isArray(context) ? context : [context]).map(value => JSON.stringify(value)).join(':');
}, [context]);
if (!key.trim().length) {
@ -18,13 +18,13 @@ function useSWRKey(context: Context, prefix: string | null = null): string {
}
function useServerSWRKey(context: Context): string {
const uuid = ServerContext.useStoreState((state) => state.server.data?.uuid);
const uuid = ServerContext.useStoreState(state => state.server.data?.uuid);
return useSWRKey(context, `server:${uuid}`);
}
function useUserSWRKey(context: Context): string {
const uuid = useStoreState((state) => state.user.data?.uuid);
const uuid = useStoreState(state => state.user.data?.uuid);
return useSWRKey(context, `user:${uuid}`);
}

View file

@ -3,7 +3,7 @@ import { useEffect, useRef } from 'react';
import { SocketEvent } from '@/components/server/events';
const useWebsocketEvent = (event: SocketEvent, callback: (data: string) => void) => {
const { connected, instance } = ServerContext.useStoreState((state) => state.socket);
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
const savedCallback = useRef<any>(null);
useEffect(() => {