Arrow functions and so

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak
2024-05-13 01:18:04 +02:00
parent 615ec68931
commit a58c52631e
352 changed files with 2894 additions and 3693 deletions

View File

@@ -12,7 +12,7 @@ interface UseImageFieldOpts {
}
/** Returns props for `<input type="file">`, and optionally resizes the file. */
function useImageField(opts: UseImageFieldOpts = {}) {
const useImageField = (opts: UseImageFieldOpts = {}) => {
const [file, setFile] = useState<File | null>();
const src = usePreview(file) || (file === null ? undefined : opts.preview);
@@ -35,7 +35,7 @@ function useImageField(opts: UseImageFieldOpts = {}) {
onChange,
onClear,
};
}
};
export { useImageField };
export type { UseImageFieldOpts };

View File

@@ -1,12 +1,10 @@
import { useMemo } from 'react';
/** Return a preview URL for a file. */
function usePreview(file: File | null | undefined): string | undefined {
return useMemo(() => {
if (file) {
return URL.createObjectURL(file);
}
}, [file]);
}
const usePreview = (file: File | null | undefined): string | undefined => useMemo(() => {
if (file) {
return URL.createObjectURL(file);
}
}, [file]);
export { usePreview };

View File

@@ -4,7 +4,7 @@ import { useEffect, useState } from 'react';
* Returns props for `<input type="text">`.
* If `initialValue` changes from undefined to a string, it will set the value.
*/
function useTextField(initialValue?: string | undefined) {
const useTextField = (initialValue?: string | undefined) => {
const [value, setValue] = useState(initialValue);
const hasInitialValue = typeof initialValue === 'string';
@@ -22,6 +22,6 @@ function useTextField(initialValue?: string | undefined) {
value: value || '',
onChange,
};
}
};
export { useTextField };

View File

@@ -3,6 +3,5 @@ import { useAppSelector } from './useAppSelector';
import type { ReducerCompose } from 'soapbox/reducers/compose';
/** Get compose for given key with fallback to 'default' */
export const useCompose = <ID extends string>(composeId: ID extends 'default' ? never : ID): ReturnType<typeof ReducerCompose> => {
return useAppSelector((state) => state.compose.get(composeId, state.compose.get('default')!));
};
export const useCompose = <ID extends string>(composeId: ID extends 'default' ? never : ID): ReturnType<typeof ReducerCompose> =>
useAppSelector((state) => state.compose.get(composeId, state.compose.get('default')!));

View File

@@ -1,7 +1,7 @@
import React, { useCallback, useEffect, useState } from 'react';
/** Controls the state of files being dragged over a node. */
function useDraggedFiles<R extends HTMLElement>(node: React.RefObject<R>, onDrop?: (files: FileList) => void) {
const useDraggedFiles = <R extends HTMLElement>(node: React.RefObject<R>, onDrop?: (files: FileList) => void) => {
const [isDragging, setIsDragging] = useState(false);
const [isDraggedOver, setIsDraggedOver] = useState(false);
@@ -71,26 +71,23 @@ function useDraggedFiles<R extends HTMLElement>(node: React.RefObject<R>, onDrop
/** Whether the node is being dragged over. */
isDraggedOver,
};
}
};
/** Ensure only files are being dragged, and not eg highlighted text. */
function isDraggingFiles(e: DragEvent): e is DragEvent & { dataTransfer: DataTransfer } {
const isDraggingFiles = (e: DragEvent): e is DragEvent & { dataTransfer: DataTransfer } => {
if (e.dataTransfer) {
const { types } = e.dataTransfer;
return types.length === 1 && types[0] === 'Files';
} else {
return false;
}
}
};
/** Check whether the cursor is in the screen. Mostly useful for dragleave events. */
function isDraggedOffscreen(e: DragEvent): boolean {
return e.screenX === 0 && e.screenY === 0;
}
const isDraggedOffscreen = (e: DragEvent): boolean => e.screenX === 0 && e.screenY === 0;
/** Check whether the cursor is dragged out of the node. */
function isDraggedOutOfNode(e: DragEvent, node: Node): boolean {
return !node.contains(document.elementFromPoint(e.clientX, e.clientY));
}
const isDraggedOutOfNode = (e: DragEvent, node: Node): boolean =>
!node.contains(document.elementFromPoint(e.clientX, e.clientY));
export { useDraggedFiles };

View File

@@ -6,9 +6,9 @@ import type { RootState } from 'soapbox/store';
* Provides a `getState()` function to hooks.
* You should prefer `useAppSelector` when possible.
*/
function useGetState() {
const useGetState = () => {
const dispatch = useAppDispatch();
return () => dispatch((_, getState: () => RootState) => getState());
}
};
export { useGetState };

View File

@@ -1,6 +1,4 @@
import { useAppSelector } from './useAppSelector';
/** Get the Instance for the current backend. */
export const useInstance = () => {
return useAppSelector((state) => state.instance);
};
export const useInstance = () => useAppSelector((state) => state.instance);

View File

@@ -1,9 +1,9 @@
import { useState } from 'react';
function useLoading(initialState: boolean = false) {
const useLoading = (initialState: boolean = false) => {
const [isLoading, setIsLoading] = useState<boolean>(initialState);
function setPromise<T>(promise: Promise<T>) {
const setPromise = <T>(promise: Promise<T>) => {
setIsLoading(true);
promise
@@ -11,9 +11,9 @@ function useLoading(initialState: boolean = false) {
.catch(() => setIsLoading(false));
return promise;
}
};
return [isLoading, setPromise] as const;
}
};
export { useLoading };

View File

@@ -1,13 +1,14 @@
import { useAppSelector } from './useAppSelector';
function useLoggedIn() {
const useLoggedIn = () => {
const me = useAppSelector(state => state.me);
return {
isLoggedIn: typeof me === 'string',
isLoginLoading: me === null,
isLoginFailed: me === false,
me,
};
}
};
export { useLoggedIn };

View File

@@ -5,10 +5,10 @@ import { makeGetAccount } from 'soapbox/selectors';
import { useAppSelector } from './useAppSelector';
/** Get the logged-in account from the store, if any. */
export const useOwnAccount = () => {
const useOwnAccount = () => {
const getAccount = useCallback(makeGetAccount(), []);
const account = useAppSelector((state) => {
const account = useAppSelector((state) => {
const { me } = state;
if (typeof me === 'string') {
@@ -18,3 +18,7 @@ export const useOwnAccount = () => {
return { account: account || undefined };
};
export {
useOwnAccount,
};

View File

@@ -5,6 +5,4 @@ import { useAppSelector } from './useAppSelector';
import type { SoapboxConfig } from 'soapbox/types/soapbox';
/** Get the Soapbox config from the store */
export const useSoapboxConfig = (): SoapboxConfig => {
return useAppSelector((state) => getSoapboxConfig(state));
};
export const useSoapboxConfig = (): SoapboxConfig => useAppSelector((state) => getSoapboxConfig(state));