jaehyuk-jang

Add common shared func

1 +import moment from 'moment';
2 +import { useWindowSize } from '@src/hooks';
3 +
4 +export const getMaxLengthByWidth = (width: number) => {
5 + if (width > 590) return 30;
6 + if (width > 474) return 20;
7 + else return 13;
8 +};
9 +
10 +export const sliceTextByLength = (text: string, length: number) =>
11 + text.length > length ? text.substr(0, length) + '...' : text;
12 +
13 +export const makeArticleURLWithNumber = (name: string, id: number) => {
14 + return `${window.location.origin}/${name}/article?num=${id}`;
15 +};
16 +
17 +export const makeDateForDayOrTime = (date) =>
18 + moment().diff(moment(date), 'days') > 1
19 + ? moment(date).format('YY.MM.DD')
20 + : moment(date).format('HH:mm:ss');
21 +
22 +export const makeCategoryTableBody = (list) => {
23 + const [width] = useWindowSize();
24 + const newData = list.map(({ title, id, created_date, ...rest }) => {
25 + const textMaxLength = getMaxLengthByWidth(width);
26 + const newTitle = sliceTextByLength(title, textMaxLength);
27 + const createdDate = makeDateForDayOrTime(created_date);
28 +
29 + return {
30 + ...rest,
31 + id,
32 + key: String(id),
33 + title: newTitle,
34 + created_date: createdDate,
35 + };
36 + });
37 +
38 + newData.sort((a, b) => Number(b.id) - Number(a.id));
39 +
40 + return newData;
41 +};