Skip to content

Commit 02e27d5

Browse files
committed
refactor: remove duplicate code and unify color utilities
1 parent 60f1d9c commit 02e27d5

File tree

4 files changed

+10
-114
lines changed

4 files changed

+10
-114
lines changed

client/pages/admin/article/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import React, { useCallback, useEffect, useState } from 'react';
77
import { LocaleTime } from '@/components/LocaleTime';
88
import { PaginationTable } from '@/components/PaginationTable';
99
import { ViewChart } from '@/components/ViewChart';
10-
import { getRandomColor } from '@/constants';
10+
import { getRandomColor } from '@/utils';
1111
import { useAsyncLoading } from '@/hooks/useAsyncLoading';
1212
import { usePagination } from '@/hooks/usePagination';
1313
import { useSetting } from '@/hooks/useSetting';

client/src/components/Pagination/index.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Pagination as APagination } from 'antd';
2-
import React, { useCallback, useState } from 'react';
2+
import React from 'react';
33

44
import style from './index.module.scss';
55

@@ -12,21 +12,6 @@ interface IProps {
1212
hideOnSinglePage?: boolean;
1313
}
1414

15-
export const usePagination = ({
16-
page: defaultPage = 1,
17-
pageSize: defaultPageSize = 12,
18-
}): [{ page: number; pageSize: number }, (arg, arg1) => void] => {
19-
const [page, setPage] = useState(defaultPage);
20-
const [pageSize, setPageSize] = useState(defaultPageSize);
21-
22-
const updatePagination = useCallback((page, pageSize) => {
23-
setPage(page);
24-
setPageSize(pageSize);
25-
}, []);
26-
27-
return [{ page, pageSize }, updatePagination];
28-
};
29-
3015
export const Pagination: React.FC<IProps> = ({ total, onChange, page, pageSize, hideOnSinglePage = false }) => {
3116
return (
3217
<div className={style.wrapper}>

client/src/constants/index.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,3 @@ export const TAG_COLORS = [
2020
'#ff0064',
2121
'#722ed1',
2222
];
23-
24-
export const getRandomColor = (() => {
25-
const cache = {};
26-
27-
return (key): string => {
28-
if (!cache[key]) {
29-
const color = TAG_COLORS[Math.floor(Math.random() * TAG_COLORS.length)];
30-
cache[key] = color;
31-
return color;
32-
}
33-
return cache[key];
34-
};
35-
})();

client/src/utils/index.tsx

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -130,26 +130,11 @@ const iconMap = {
130130
skin: SkinIcon,
131131
};
132132

133-
134-
const colors = ['#52c41a', '#f5222d', '#1890ff', '#faad14', '#ff0064', '#722ed1'];
135-
136-
const tagColors = [
137-
'#dc3545',
138-
'#17a2b8',
139-
'#00b74a',
140-
'#fc651f',
141-
'#6c757d',
142-
'#f5c800',
143-
'#808695',
144-
'#2db7f5',
145-
'#87d068',
146-
'#108ee9',
147-
'#52c41a',
148-
'#f5222d',
149-
'#1890ff',
150-
'#faad14',
151-
'#ff0064',
152-
'#722ed1',
133+
// 统一的颜色数组,避免重复
134+
const colors = [
135+
'#52c41a', '#f5222d', '#1890ff', '#faad14', '#ff0064', '#722ed1',
136+
'#dc3545', '#17a2b8', '#00b74a', '#fc651f', '#6c757d', '#f5c800',
137+
'#808695', '#2db7f5', '#87d068', '#108ee9'
153138
];
154139

155140
export const getRandomColor = (() => {
@@ -164,29 +149,6 @@ export const getRandomColor = (() => {
164149
};
165150
})();
166151

167-
export function throttle(fn, threshhold) {
168-
let last;
169-
let timer;
170-
threshhold || (threshhold = 250);
171-
172-
return function () {
173-
const context = this; // eslint-disable-line @typescript-eslint/no-this-alias
174-
const args = arguments; // eslint-disable-line prefer-rest-params
175-
const now = +new Date();
176-
177-
if (last && now < last + threshhold) {
178-
clearTimeout(timer);
179-
timer = setTimeout(function () {
180-
last = now;
181-
fn.apply(context, args);
182-
}, threshhold);
183-
} else {
184-
last = now;
185-
fn.apply(context, args);
186-
}
187-
};
188-
}
189-
190152
export function elementInViewport(el) {
191153
let top = el.offsetTop;
192154
let left = el.offsetLeft;
@@ -206,20 +168,11 @@ export function elementInViewport(el) {
206168
left + width > window.pageXOffset
207169
);
208170
}
171+
209172
export function getDocumentScrollTop() {
210173
return document.documentElement.scrollTop || window.pageYOffset || window.scrollY || document.body.scrollTop;
211174
}
212175

213-
export function download({ name, url }) {
214-
const eleLink = document.createElement('a');
215-
eleLink.download = name;
216-
eleLink.style.display = 'none';
217-
eleLink.href = url;
218-
document.body.appendChild(eleLink);
219-
eleLink.click();
220-
document.body.removeChild(eleLink);
221-
}
222-
223176
export const groupBy = function (data, condition) {
224177
if (!condition || !Array.isArray(data)) {
225178
return data;
@@ -252,33 +205,6 @@ export const formatFileSize = (size) => {
252205
return (size / 1024 / 1024).toFixed(2) + ' MB';
253206
};
254207

255-
export function debounce(func, wait, immediate = false) {
256-
let timeout;
257-
258-
const debounced = function () {
259-
const context = this; // eslint-disable-line @typescript-eslint/no-this-alias
260-
const args = arguments; // eslint-disable-line prefer-rest-params
261-
const later = function () {
262-
timeout = null;
263-
if (!immediate) {
264-
func.apply(context, args);
265-
}
266-
};
267-
const callNow = immediate && !timeout;
268-
clearTimeout(timeout);
269-
timeout = setTimeout(later, wait);
270-
if (callNow) {
271-
func.apply(context, args);
272-
}
273-
};
274-
275-
debounced.cancel = () => {
276-
clearTimeout(timeout);
277-
};
278-
279-
return debounced;
280-
}
281-
282208
export function resolveUrl(baseURL, relativeURL) {
283209
if (!baseURL) {
284210
baseURL = '/';
@@ -298,11 +224,10 @@ export const scrollToBottom = (el: HTMLElement) => {
298224
};
299225

300226
export function getColorFromNumber(num) {
301-
const index = num % tagColors.length;
302-
return tagColors[index];
227+
const index = num % colors.length;
228+
return colors[index];
303229
}
304230

305-
306231
export function getIconByName (name) {
307232
if (React.isValidElement(name)) {
308233
return name;
@@ -311,7 +236,6 @@ export function getIconByName (name) {
311236
return IconComponent;
312237
};
313238

314-
315239
export function getFirstLevelRoute(path) {
316240
// 确保路径以斜杠开头,如果不是则添加斜杠
317241
if (!path.startsWith('/')) {

0 commit comments

Comments
 (0)