1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36: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

@ -1,9 +1,19 @@
import { adjustHue, darken, desaturate, parseToHsla, transparentize } from 'color2k';
import {
adjustHue,
darken,
desaturate,
parseToHsla,
transparentize,
} from 'color2k';
import { DEFAULT_DOMINANT_COLOR } from '~/constants/node';
import { stringToColour } from '~/utils/dom';
export const normalizeBrightColor = (color?: string, saturationExp = 3, lightnessExp = 3) => {
export const normalizeBrightColor = (
color?: string,
saturationExp = 3,
lightnessExp = 3,
) => {
if (!color) {
return '';
}
@ -12,12 +22,23 @@ export const normalizeBrightColor = (color?: string, saturationExp = 3, lightnes
const saturation = hsla[1];
const lightness = hsla[2];
const desaturated = saturationExp > 1 ? desaturate(color, saturation ** saturationExp) : color;
return lightnessExp > 1 ? darken(desaturated, lightness ** lightnessExp) : desaturated;
const desaturated =
saturationExp > 1 ? desaturate(color, saturation ** saturationExp) : color;
return lightnessExp > 1
? darken(desaturated, lightness ** lightnessExp)
: desaturated;
};
export const generateColorTriplet = (val: string, saturation: number, lightness: number) => {
const color = normalizeBrightColor(stringToColour(val), saturation, lightness);
export const generateColorTriplet = (
val: string,
saturation: number,
lightness: number,
) => {
const color = normalizeBrightColor(
stringToColour(val),
saturation,
lightness,
);
return [
color,
@ -31,9 +52,13 @@ export const generateGradientFromColor = (
saturation = 3,
lightness = 3,
angle = 155,
opacity = 1
opacity = 1,
) => {
const [first, second, third] = generateColorTriplet(val, saturation, lightness).map(it => {
const [first, second, third] = generateColorTriplet(
val,
saturation,
lightness,
).map((it) => {
if (opacity > 1 || opacity < 0) {
return it;
}

View file

@ -6,5 +6,6 @@ export const CONFIG = {
// image storage endpoint (sames as backend, but with /static usualy)
remoteCurrent: process.env.NEXT_PUBLIC_REMOTE_CURRENT || '',
// transitional prop, marks migration to nextjs
isNextEnvironment: !!process.env.NEXT_PUBLIC_REMOTE_CURRENT || typeof window === 'undefined',
isNextEnvironment:
!!process.env.NEXT_PUBLIC_REMOTE_CURRENT || typeof window === 'undefined',
};

View file

@ -3,11 +3,19 @@ import { differenceInDays, isAfter, isValid, parseISO } from 'date-fns';
import { IComment, ICommentGroup } from '~/types';
import { curry, insert, nth, path, remove } from '~/utils/ramda';
export const moveArrItem = curry((at, to, list) => insert(to, nth(at, list), remove(at, 1, list)));
export const moveArrItem = curry((at, to, list) =>
insert(to, nth(at, list), remove(at, 1, list)),
);
export const objFromArray = (array: any[], key: string) =>
array.reduce((obj, el) => (key && el[key] ? { ...obj, [el[key]]: el } : obj), {});
array.reduce(
(obj, el) => (key && el[key] ? { ...obj, [el[key]]: el } : obj),
{},
);
const compareCommentDates = (commentDateValue?: string, lastSeenDateValue?: string) => {
const compareCommentDates = (
commentDateValue?: string,
lastSeenDateValue?: string,
) => {
if (!commentDateValue || !lastSeenDateValue) {
return false;
}
@ -37,45 +45,49 @@ const getCommentDistance = (firstDate?: string, secondDate?: string) => {
}
};
export const groupCommentsByUser = (lastSeen?: string) => (
grouppedComments: ICommentGroup[],
comment: IComment
): ICommentGroup[] => {
const last: ICommentGroup | undefined = path([grouppedComments.length - 1], grouppedComments);
export const groupCommentsByUser =
(lastSeen?: string) =>
(grouppedComments: ICommentGroup[], comment: IComment): ICommentGroup[] => {
const last: ICommentGroup | undefined = path(
[grouppedComments.length - 1],
grouppedComments,
);
if (!comment.user) {
return grouppedComments;
}
if (!comment.user) {
return grouppedComments;
}
return [
...(!last || path(['user', 'id'], last) !== path(['user', 'id'], comment)
? [
// add new group
...grouppedComments,
{
user: comment.user,
comments: [comment],
distancesInDays: [0],
ids: [comment.id],
hasNew: compareCommentDates(comment.created_at, lastSeen),
},
]
: [
// append to last group
...grouppedComments.slice(0, grouppedComments.length - 1),
{
...last,
distancesInDays: [
...last.distancesInDays,
getCommentDistance(
comment?.created_at,
last.comments[last.comments.length - 1]?.created_at
),
],
comments: [...last.comments, comment],
ids: [...last.ids, comment.id],
hasNew: last.hasNew || compareCommentDates(comment.created_at, lastSeen),
},
]),
];
};
return [
...(!last || path(['user', 'id'], last) !== path(['user', 'id'], comment)
? [
// add new group
...grouppedComments,
{
user: comment.user,
comments: [comment],
distancesInDays: [0],
ids: [comment.id],
hasNew: compareCommentDates(comment.created_at, lastSeen),
},
]
: [
// append to last group
...grouppedComments.slice(0, grouppedComments.length - 1),
{
...last,
distancesInDays: [
...last.distancesInDays,
getCommentDistance(
comment?.created_at,
last.comments[last.comments.length - 1]?.created_at,
),
],
comments: [...last.comments, comment],
ids: [...last.ids, comment.id],
hasNew:
last.hasNew ||
compareCommentDates(comment.created_at, lastSeen),
},
]),
];
};

View file

@ -18,11 +18,16 @@ const ProfileContext = createContext<ProfileContextValue>({
isLoading: false,
});
export const ProfileProvider: FC<ProfileProviderProps> = ({ children, username }) => {
export const ProfileProvider: FC<ProfileProviderProps> = ({
children,
username,
}) => {
const { profile, isLoading } = useGetProfile(username);
return (
<ProfileContext.Provider value={{ profile, isLoading }}>{children}</ProfileContext.Provider>
<ProfileContext.Provider value={{ profile, isLoading }}>
{children}
</ProfileContext.Provider>
);
};

View file

@ -2,10 +2,12 @@ import { flatten, isEmpty } from '~/utils/ramda';
export const splitTextByYoutube = (strings: string[]): string[] =>
flatten(
strings.map(str =>
str.split(/(https?:\/\/(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?[\w\-&=]+)/)
)
strings.map((str) =>
str.split(
/(https?:\/\/(?:www\.)?(?:youtube\.com|youtu\.be)\/(?:watch)?(?:\?v=)?[\w\-&=]+)/,
),
),
);
export const splitTextOmitEmpty = (strings: string[]): string[] =>
strings.map(el => el.trim()).filter(el => !isEmpty(el));
strings.map((el) => el.trim()).filter((el) => !isEmpty(el));

View file

@ -1,4 +1,4 @@
/** just combines title elements to form title of the page */
export const getPageTitle = (...props: string[]): string => {
return ['Убежище', ...props].filter(it => it.trim()).join(' • ');
return ['Убежище', ...props].filter((it) => it.trim()).join(' • ');
};

View file

@ -3,11 +3,13 @@ import { ITag } from '~/types';
export const separateTags = (tags: Partial<ITag>[]): Partial<ITag>[][] =>
(tags || []).reduce(
(obj, tag) =>
tag?.title?.substr(0, 1) === '/' ? [[...obj[0], tag], obj[1]] : [obj[0], [...obj[1], tag]],
[[], []] as Partial<ITag>[][]
tag?.title?.substr(0, 1) === '/'
? [[...obj[0], tag], obj[1]]
: [obj[0], [...obj[1], tag]],
[[], []] as Partial<ITag>[][],
);
export const separateTagOptions = (options: string[]): string[][] =>
separateTags(options.map((title): Partial<ITag> => ({ title }))).map(item =>
item.filter(tag => tag.title).map(({ title }) => title!)
separateTags(options.map((title): Partial<ITag> => ({ title }))).map((item) =>
item.filter((tag) => tag.title).map(({ title }) => title!),
);

View file

@ -1,5 +1,6 @@
import { ERROR_LITERAL, ERRORS } from '~/constants/errors';
import { ValueOf } from '~/types';
export const t = (string: ValueOf<typeof ERRORS>): ValueOf<typeof ERROR_LITERAL> =>
ERROR_LITERAL[string] || string;
export const t = (
string: ValueOf<typeof ERRORS>,
): ValueOf<typeof ERROR_LITERAL> => ERROR_LITERAL[string] || string;

View file

@ -2,10 +2,10 @@ import { FILE_MIMES, UploadType } from '~/constants/uploads';
import { isMimeOfImage } from '~/utils/validators';
/** if file is image, returns data-uri of thumbnail */
export const uploadGetThumb = async file => {
export const uploadGetThumb = async (file) => {
if (!file.type || !isMimeOfImage(file.type)) return '';
return new Promise<string>(resolve => {
return new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result?.toString() || '');
reader.readAsDataURL(file);
@ -15,14 +15,17 @@ export const uploadGetThumb = async file => {
/** returns UploadType by file */
export const getFileType = (file: File): UploadType | undefined =>
((file.type &&
Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) as UploadType) ||
undefined;
Object.keys(FILE_MIMES).find((mime) =>
FILE_MIMES[mime].includes(file.type),
)) as UploadType) || undefined;
/** getImageFromPaste returns any images from paste event */
export const getImageFromPaste = (event: ClipboardEvent): Promise<File | undefined> => {
export const getImageFromPaste = (
event: ClipboardEvent,
): Promise<File | undefined> => {
const items = event.clipboardData?.items;
return new Promise(resolve => {
return new Promise((resolve) => {
for (let index in items) {
const item = items[index];
@ -31,7 +34,7 @@ export const getImageFromPaste = (event: ClipboardEvent): Promise<File | undefin
const reader = new FileReader();
const type = item.type;
reader.onload = function(e) {
reader.onload = function (e) {
if (!e.target?.result) {
return;
}
@ -40,7 +43,7 @@ export const getImageFromPaste = (event: ClipboardEvent): Promise<File | undefin
new File([e.target?.result], 'paste.png', {
type,
lastModified: new Date().getTime(),
})
}),
);
};

View file

@ -1,3 +1,4 @@
import { IMAGE_MIME_TYPES } from '~/constants/uploads';
export const isMimeOfImage = (mime): boolean => !!mime && IMAGE_MIME_TYPES.indexOf(mime) >= 0;
export const isMimeOfImage = (mime): boolean =>
!!mime && IMAGE_MIME_TYPES.indexOf(mime) >= 0;