1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 04:46:40 +07:00

add eslint-plugin-prettier

This commit is contained in:
Fedor Katurov 2025-01-24 17:51:59 +07:00
parent 0e4d2bf44d
commit ba0604ab9d
69 changed files with 419 additions and 249 deletions

View file

@ -10,7 +10,7 @@ export const useLastSeenBoris = () => {
async (date: string) => {
await update({ last_seen_boris: date }, false);
},
[update]
[update],
);
return { setLastSeen, lastSeen };

View file

@ -20,7 +20,7 @@ export const useLoginLogoutRestore = () => {
showToastInfo(getRandomPhrase('WELCOME'));
return result.user;
},
[auth]
[auth],
);
return { logout, login };

View file

@ -5,8 +5,9 @@ import { API } from '~/constants/api';
import { getErrorMessage } from '~/utils/errors/getErrorMessage';
export const useRestoreCode = (code: string) => {
const { data, isValidating, error } = useSWR(API.USER.REQUEST_CODE(code), () =>
apiCheckRestoreCode({ code })
const { data, isValidating, error } = useSWR(
API.USER.REQUEST_CODE(code),
() => apiCheckRestoreCode({ code }),
);
const codeUser = data?.user;

View file

@ -18,7 +18,7 @@ const validationSchema = object({
.test(
'sameAsPassword',
'Должен совпадать с паролем',
(val, ctx) => val === ctx.parent.newPassword
(val, ctx) => val === ctx.parent.newPassword,
),
});
@ -26,15 +26,21 @@ export type RestorePasswordData = Asserts<typeof validationSchema>;
export const useRestorePasswordForm = (
code: string,
fetcher: (props: { code: string; password: string }) => Promise<{ token: string; user: IUser }>,
onSuccess: () => void
fetcher: (props: {
code: string;
password: string;
}) => Promise<{ token: string; user: IUser }>,
onSuccess: () => void,
) => {
const auth = useAuthStore();
const onSubmit = useCallback<FormikConfig<RestorePasswordData>['onSubmit']>(
async (values, { setErrors }) => {
try {
const { token, user } = await fetcher({ password: values.newPassword, code });
const { token, user } = await fetcher({
password: values.newPassword,
code,
});
auth.setUser(user);
auth.setToken(token);
onSuccess();
@ -47,7 +53,7 @@ export const useRestorePasswordForm = (
}
}
},
[onSuccess, fetcher, code, auth]
[onSuccess, fetcher, code, auth],
);
return useFormik<RestorePasswordData>({

View file

@ -15,7 +15,7 @@ type RestoreRequestData = Asserts<typeof validationSchema>;
export const useRestoreRequestForm = (
fetcher: (field: string) => Promise<unknown>,
onSuccess: () => void
onSuccess: () => void,
) => {
const onSubmit = useCallback<FormikConfig<RestoreRequestData>['onSubmit']>(
async (values, { setErrors }) => {
@ -31,7 +31,7 @@ export const useRestoreRequestForm = (
}
}
},
[fetcher, onSuccess]
[fetcher, onSuccess],
);
return useFormik({

View file

@ -13,6 +13,6 @@ export const useSessionCookie = () => {
autorun(() => {
setCookie('session', auth.token, 30);
}),
[auth]
[auth],
);
};

View file

@ -9,9 +9,7 @@ import { showErrorToast } from '~/utils/errors/showToast';
const validationSchema = object({
username: string().required(ERRORS.REQUIRED),
password: string()
.required(ERRORS.REQUIRED)
.min(6, ERRORS.PASSWORD_IS_SHORT),
password: string().required(ERRORS.REQUIRED).min(6, ERRORS.PASSWORD_IS_SHORT),
});
type SocialRegisterData = Asserts<typeof validationSchema>;
@ -23,7 +21,7 @@ export const useSocialRegisterForm = (
username: string;
password: string;
}) => Promise<{ token: string }>,
onSuccess: (token: string) => void
onSuccess: (token: string) => void,
) => {
const onSubmit = useCallback<FormikConfig<SocialRegisterData>['onSubmit']>(
async (values, { setErrors }) => {
@ -43,7 +41,7 @@ export const useSocialRegisterForm = (
}
}
},
[token, onSuccess, fetcher]
[token, onSuccess, fetcher],
);
return useFormik<SocialRegisterData>({

View file

@ -7,7 +7,10 @@ const today = new Date();
export const useUserActiveStatus = (lastSeen?: string) => {
try {
const lastSeenDate = lastSeen ? parseISO(lastSeen) : undefined;
return lastSeenDate && differenceInDays(today, lastSeenDate) < INACTIVE_ACCOUNT_DAYS;
return (
lastSeenDate &&
differenceInDays(today, lastSeenDate) < INACTIVE_ACCOUNT_DAYS
);
} catch (e) {
return false;
}

View file

@ -6,12 +6,14 @@ import { initialBackendStats } from '~/constants/boris/constants';
import { BorisUsageStats } from '~/types/boris';
export const useBorisStats = () => {
const { data: backend = initialBackendStats, isValidating: isValidatingBackend } = useSWR(
API.BORIS.GET_BACKEND_STATS,
() => getBorisBackendStats()
);
const {
data: backend = initialBackendStats,
isValidating: isValidatingBackend,
} = useSWR(API.BORIS.GET_BACKEND_STATS, () => getBorisBackendStats());
const { data: issues = [] } = useSWR(API.BORIS.GITHUB_ISSUES, () => getGithubIssues());
const { data: issues = [] } = useSWR(API.BORIS.GITHUB_ISSUES, () =>
getGithubIssues(),
);
const stats: BorisUsageStats = {
backend,

View file

@ -3,9 +3,16 @@ import { useMemo } from 'react';
import { normalizeBrightColor } from '~/utils/color';
import { stringToColour } from '~/utils/dom';
export const useColorFromString = (val?: string, saturation = 3, lightness = 3) => {
export const useColorFromString = (
val?: string,
saturation = 3,
lightness = 3,
) => {
return useMemo(
() => (val && normalizeBrightColor(stringToColour(val), saturation, lightness)) || '',
[lightness, saturation, val]
() =>
(val &&
normalizeBrightColor(stringToColour(val), saturation, lightness)) ||
'',
[lightness, saturation, val],
);
};

View file

@ -6,7 +6,7 @@ export const useColorGradientFromString = (
val?: string,
saturation = 3,
lightness = 3,
angle = 155
angle = 155,
) =>
useMemo(() => {
if (!val) {

View file

@ -1,6 +1,9 @@
import { useEffect, useMemo, useState } from 'react';
export const usePersistedState = (key: string, initial: string): [string, (val: string) => any] => {
export const usePersistedState = (
key: string,
initial: string,
): [string, (val: string) => any] => {
const stored = useMemo(() => {
try {
return localStorage.getItem(`vault_${key}`) || initial;

View file

@ -4,15 +4,18 @@ export const useFocusEvent = (initialState = false, delay = 0) => {
const [focused, setFocused] = useState(initialState);
const onFocus = useCallback(
event => {
(event) => {
event.preventDefault();
event.stopPropagation();
setFocused(true);
},
[setFocused]
[setFocused],
);
const onBlur = useCallback(
() => setTimeout(() => setFocused(false), delay),
[delay],
);
const onBlur = useCallback(() => setTimeout(() => setFocused(false), delay), [delay]);
return { focused, onBlur, onFocus };
};

View file

@ -7,12 +7,13 @@ export const useFormatWrapper = (onChange: (val: string) => void) => {
target: HTMLTextAreaElement,
prefix = '',
suffix = ''
) => event => {
event.preventDefault();
wrapTextInsideInput(target, prefix, suffix, onChange);
},
[onChange]
suffix = '',
) =>
(event) => {
event.preventDefault();
wrapTextInsideInput(target, prefix, suffix, onChange);
},
[onChange],
);
};
@ -21,7 +22,7 @@ export const wrapTextInsideInput = (
target: HTMLTextAreaElement,
prefix: string,
suffix: string,
onChange: (val: string) => void
onChange: (val: string) => void,
) => {
if (!target) return;
@ -34,7 +35,7 @@ export const wrapTextInsideInput = (
onChange(
target.value.substring(0, start) +
replacement +
target.value.substring(end, target.value.length)
target.value.substring(end, target.value.length),
);
target.focus();

View file

@ -2,7 +2,8 @@ import { useCallback, useEffect } from 'react';
export const useInfiniteLoader = (loader: () => void, isLoading?: boolean) => {
const onLoadMore = useCallback(() => {
const pos = window.scrollY + window.innerHeight - document.body.scrollHeight;
const pos =
window.scrollY + window.innerHeight - document.body.scrollHeight;
if (isLoading || pos < -600) return;

View file

@ -5,13 +5,13 @@ import { getImageFromPaste } from '~/utils/uploader';
// useInputPasteUpload attaches event listener to input, that calls onUpload if user pasted any image
export const useInputPasteUpload = (onUpload: (files: File[]) => void) => {
return useCallback(
async event => {
async (event) => {
const image = await getImageFromPaste(event);
if (!image) return;
onUpload([image]);
},
[onUpload]
[onUpload],
);
};

View file

@ -17,7 +17,11 @@ const sameWidth = {
},
};
export const usePopperModifiers = (offsetX = 0, offsetY = 10, justify?: boolean): Modifier<any>[] =>
export const usePopperModifiers = (
offsetX = 0,
offsetY = 10,
justify?: boolean,
): Modifier<any>[] =>
useMemo(
() =>
[
@ -35,5 +39,5 @@ export const usePopperModifiers = (offsetX = 0, offsetY = 10, justify?: boolean)
},
...(justify ? [sameWidth] : []),
] as Modifier<any>[],
[offsetX, offsetY, justify]
[offsetX, offsetY, justify],
);

View file

@ -11,7 +11,7 @@ const getHeight = () => {
body.offsetHeight,
html.clientHeight,
html.scrollHeight,
html.offsetHeight
html.offsetHeight,
);
};
export const useScrollHeight = () => getHeight();

View file

@ -18,6 +18,6 @@ export const useScrollToTop = (deps?: any[]) => {
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
deps && Array.isArray(deps) ? deps : []
deps && Array.isArray(deps) ? deps : [],
);
};

View file

@ -1,7 +1,9 @@
import { useEffect, useState } from 'react';
export const useScrollTop = () => {
const [top, setTop] = useState(typeof window !== 'undefined' ? window.scrollY : 0);
const [top, setTop] = useState(
typeof window !== 'undefined' ? window.scrollY : 0,
);
useEffect(() => {
setTop(window.scrollY);

View file

@ -6,11 +6,12 @@ export const useFlowCellControls = (
id: INode['id'],
description: string | undefined,
flow: FlowDisplay,
onChangeCellView: (id: INode['id'], flow: FlowDisplay) => void
onChangeCellView: (id: INode['id'], flow: FlowDisplay) => void,
) => {
const onChange = useCallback(
(value: Partial<FlowDisplay>) => onChangeCellView(id, { ...flow, ...value }),
[flow, id, onChangeCellView]
(value: Partial<FlowDisplay>) =>
onChangeCellView(id, { ...flow, ...value }),
[flow, id, onChangeCellView],
);
const hasDescription = !!description && description.length > 32;

View file

@ -17,6 +17,6 @@ export const useFlowSetCellView = () => {
showErrorToast(error);
}
},
[updateNode]
[updateNode],
);
};

View file

@ -21,15 +21,19 @@ export const useGetLabStats = () => {
heroes: lab.heroes,
tags: lab.tags,
},
onSuccess: data => {
onSuccess: (data) => {
lab.setHeroes(data.heroes);
lab.setTags(data.tags);
},
refreshInterval,
}
},
);
const { data: updatesData, isValidating: isValidatingUpdates, mutate: mutateUpdates } = useSWR(
const {
data: updatesData,
isValidating: isValidatingUpdates,
mutate: mutateUpdates,
} = useSWR(
isUser ? API.LAB.UPDATES : null,
async () => {
const result = await getLabUpdates();
@ -37,26 +41,27 @@ export const useGetLabStats = () => {
},
{
fallbackData: lab.updates,
onSuccess: data => {
onSuccess: (data) => {
lab.setUpdates(data);
},
refreshInterval,
}
},
);
const heroes = useMemo(() => stats?.heroes || [], [stats]);
const tags = useMemo(() => stats?.tags || [], [stats]);
const updates = useMemo(() => updatesData || [], [updatesData]);
const isLoading = (!stats || !updates) && (isValidatingStats || isValidatingUpdates);
const isLoading =
(!stats || !updates) && (isValidatingStats || isValidatingUpdates);
const seenNode = useCallback(
async (nodeId: number) => {
await mutateUpdates(
updates.filter(it => it.id !== nodeId),
false
updates.filter((it) => it.id !== nodeId),
false,
);
},
[mutateUpdates, updates]
[mutateUpdates, updates],
);
return { heroes, tags, updates, isLoading, seenNode };

View file

@ -11,7 +11,7 @@ const getKey = (username: string): string | null => {
};
export const useMessages = (username: string) => {
const { data, isValidating } = useSWR(getKey(username), async () =>
apiGetUserMessages({ username })
apiGetUserMessages({ username }),
);
const messages: IMessage[] = useMemo(() => data?.messages || [], [data]);

View file

@ -5,7 +5,9 @@ import { useModalStore } from '~/store/modal/useModalStore';
import { DialogComponentProps } from '~/types/modal';
export type DialogContentProps = {
[K in keyof typeof DIALOG_CONTENT]: typeof DIALOG_CONTENT[K] extends (props: infer U) => any
[K in keyof typeof DIALOG_CONTENT]: (typeof DIALOG_CONTENT)[K] extends (
props: infer U,
) => any
? U extends DialogComponentProps
? keyof Omit<U, 'onRequestClose' | 'children'> extends never
? {}
@ -21,7 +23,7 @@ export const useModal = () => {
<T extends Dialog>(dialog: T, props: DialogContentProps[T]) => {
setCurrent(dialog, props);
},
[setCurrent]
[setCurrent],
);
return { showModal, hideModal: hide, current, isOpened: !!current };

View file

@ -10,6 +10,6 @@ export const useShowModal = <T extends Dialog>(dialog: T) => {
(props: DialogContentProps[T]) => {
modal.showModal(dialog, props);
},
[dialog, modal]
[dialog, modal],
);
};

View file

@ -11,6 +11,6 @@ export const useImageModal = () => {
(images: IFile[], index: number) => {
showModal({ items: images, index });
},
[showModal]
[showModal],
);
};

View file

@ -17,7 +17,7 @@ export const useNavigation = () => {
craHistory.push(url);
}
},
[craHistory, nextRouter]
[craHistory, nextRouter],
);
return { push };

View file

@ -16,9 +16,13 @@ export const useCreateNode = () => {
if (node.is_promoted) {
flow.setNodes([result.node, ...flow.nodes]);
} else {
await lab.unshift({ node: result.node, comment_count: 0, last_seen: node.created_at });
await lab.unshift({
node: result.node,
comment_count: 0,
last_seen: node.created_at,
});
}
},
[flow, lab]
[flow, lab],
);
};

View file

@ -6,13 +6,13 @@ import { groupCommentsByUser } from '~/utils/fn';
export const useGrouppedComments = (
comments: IComment[],
order: 'ASC' | 'DESC',
lastSeen?: string
lastSeen?: string,
) =>
useMemo(
() =>
(order === 'DESC' ? [...comments].reverse() : comments).reduce(
groupCommentsByUser(lastSeen),
[]
[],
),
[comments, lastSeen, order]
[comments, lastSeen, order],
);

View file

@ -6,7 +6,10 @@ import { useModal } from '~/hooks/modal/useModal';
import { INode } from '~/types';
import { showErrorToast } from '~/utils/errors/showToast';
export const useNodeActions = (node: INode, update: (node: Partial<INode>) => Promise<unknown>) => {
export const useNodeActions = (
node: INode,
update: (node: Partial<INode>) => Promise<unknown>,
) => {
const { showModal } = useModal();
const onLike = useCallback(async () => {
@ -35,17 +38,20 @@ export const useNodeActions = (node: INode, update: (node: Partial<INode>) => Pr
const onLock = useCallback(async () => {
try {
const result = await apiLockNode({ id: node.id, is_locked: !node.deleted_at });
const result = await apiLockNode({
id: node.id,
is_locked: !node.deleted_at,
});
await update({ deleted_at: result.deleted_at });
} catch (error) {
showErrorToast(error);
}
}, [node.deleted_at, node.id, update]);
const onEdit = useCallback(() => showModal(Dialog.EditNode, { nodeId: node.id! }), [
node,
showModal,
]);
const onEdit = useCallback(
() => showModal(Dialog.EditNode, { nodeId: node.id! }),
[node, showModal],
);
return { onLike, onStar, onLock, onEdit };
};

View file

@ -4,7 +4,8 @@ import { UploadType } from '~/constants/uploads';
import { INode } from '~/types';
export const useNodeAudios = (node: INode) => {
return useMemo(() => node.files.filter(file => file && file.type === UploadType.Audio), [
node.files,
]);
return useMemo(
() => node.files.filter((file) => file && file.type === UploadType.Audio),
[node.files],
);
};

View file

@ -1,6 +1,11 @@
import { useCallback, useRef } from 'react';
import { FormikConfig, FormikHelpers, useFormik, useFormikContext } from 'formik';
import {
FormikConfig,
FormikHelpers,
useFormik,
useFormikContext,
} from 'formik';
import { object } from 'yup';
import { INode } from '~/types';
@ -10,31 +15,31 @@ import { showErrorToast } from '~/utils/errors/showToast';
const validationSchema = object().shape({});
const afterSubmit = ({ resetForm, setSubmitting, setErrors }: FormikHelpers<INode>) => (
error?: unknown
) => {
setSubmitting(false);
const afterSubmit =
({ resetForm, setSubmitting, setErrors }: FormikHelpers<INode>) =>
(error?: unknown) => {
setSubmitting(false);
if (error) {
showErrorToast(error);
return;
}
if (error) {
showErrorToast(error);
return;
}
if (getValidationErrors(error)) {
setErrors(getValidationErrors(error)!);
return;
}
if (getValidationErrors(error)) {
setErrors(getValidationErrors(error)!);
return;
}
if (resetForm) {
resetForm();
}
};
if (resetForm) {
resetForm();
}
};
export const useNodeFormFormik = (
values: INode,
uploader: Uploader,
stopEditing: () => void,
sendSaveRequest: (node: INode) => Promise<unknown>
sendSaveRequest: (node: INode) => Promise<unknown>,
) => {
const { current: initialValues } = useRef(values);
@ -53,7 +58,7 @@ export const useNodeFormFormik = (
afterSubmit(helpers)(error);
}
},
[sendSaveRequest, uploader.files]
[sendSaveRequest, uploader.files],
);
return useFormik<INode>({

View file

@ -4,7 +4,8 @@ import { UploadType } from '~/constants/uploads';
import { INode } from '~/types';
export const useNodeImages = (node: INode) => {
return useMemo(() => node.files.filter(file => file && file.type === UploadType.Image), [
node.files,
]);
return useMemo(
() => node.files.filter((file) => file && file.type === UploadType.Image),
[node.files],
);
};

View file

@ -27,6 +27,6 @@ export const useUpdateNode = (id: number) => {
await lab.updateNode(result.node.id!, result.node);
}
},
[update, flow, lab]
[update, flow, lab],
);
};

View file

@ -20,7 +20,7 @@ export const useGetProfile = (username?: string) => {
},
{
refreshInterval: 60000,
}
},
);
const profile = data || EMPTY_USER;
@ -29,7 +29,7 @@ export const useGetProfile = (username?: string) => {
async (user: Partial<IUser>) => {
await mutate({ ...profile, ...user });
},
[mutate, profile]
[mutate, profile],
);
return { profile, isLoading: !data && isValidating, update };

View file

@ -9,21 +9,19 @@ import { flatten } from '~/utils/ramda';
const RESULTS_COUNT = 20;
const getKey: (text: string) => SWRInfiniteKeyLoader = text => (
pageIndex,
previousPageData: INode[]
) => {
if ((pageIndex > 0 && !previousPageData?.length) || !text) return null;
const getKey: (text: string) => SWRInfiniteKeyLoader =
(text) => (pageIndex, previousPageData: INode[]) => {
if ((pageIndex > 0 && !previousPageData?.length) || !text) return null;
const props: GetSearchResultsRequest = {
text,
skip: pageIndex * RESULTS_COUNT,
take: RESULTS_COUNT,
const props: GetSearchResultsRequest = {
text,
skip: pageIndex * RESULTS_COUNT,
take: RESULTS_COUNT,
};
return JSON.stringify(props);
};
return JSON.stringify(props);
};
export const useSearch = () => {
const [searchText, setSearchText] = useState('');
const [debouncedSearchText, setDebouncedSearchText] = useState('');
@ -40,7 +38,7 @@ export const useSearch = () => {
const result = await getSearchResults(props);
return result.nodes;
}
},
);
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);

View file

@ -26,5 +26,5 @@ export const useTagAutocomplete = (
},
);
return useMemo(() => (search ? data ?? [] : []), [data, search]);
return useMemo(() => (search ? (data ?? []) : []), [data, search]);
};

View file

@ -9,13 +9,11 @@ import { flatten, isNil } from '~/utils/ramda';
const PAGE_SIZE = 10;
const getKey: (tag: string) => SWRInfiniteKeyLoader = tag => (
pageIndex,
previousPageData: INode[]
) => {
if (pageIndex > 0 && !previousPageData?.length) return null;
return `${API.TAG.NODES}?tag=${tag}&page=${pageIndex}`;
};
const getKey: (tag: string) => SWRInfiniteKeyLoader =
(tag) => (pageIndex, previousPageData: INode[]) => {
if (pageIndex > 0 && !previousPageData?.length) return null;
return `${API.TAG.NODES}?tag=${tag}&page=${pageIndex}`;
};
const extractKey = (key: string) => {
const re = new RegExp(`${API.TAG.NODES}\\?tag=[^&]+&page=(\\d+)`);
@ -39,7 +37,7 @@ export const useTagNodes = (tag: string) => {
});
return result.nodes;
}
},
);
const nodes = useMemo(() => flatten(data || []), [data]);
@ -47,5 +45,12 @@ export const useTagNodes = (tag: string) => {
const loadMore = useCallback(() => setSize(size + 1), [setSize, size]);
return { nodes, hasMore, loadMore, isLoading: !data && isValidating, mutate, data };
return {
nodes,
hasMore,
loadMore,
isLoading: !data && isValidating,
mutate,
data,
};
};

View file

@ -9,7 +9,7 @@ export const useUpdates = () => {
const { data } = useSWR(
isUser ? API.USER.GET_UPDATES : null,
() => apiAuthGetUpdates({ exclude_dialogs: 0, last: '' }),
{ refreshInterval: 5 * 60 * 1000 }
{ refreshInterval: 5 * 60 * 1000 },
);
const borisCommentedAt = data?.boris?.commented_at || '';