refactor(shared): 提取公共状态逻辑并修复用户列表分页
统一存储解析、通知已读状态、表格偏好和企业日志筛选参数。修复切换每页条数时沿用旧页码、丢失筛选条件和重复触发分页回调的问题。 同步调整项目检查脚本、格式配置和开发文档。
This commit is contained in:
@@ -17,49 +17,31 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import {
|
||||
getUnreadAnnouncementKeys,
|
||||
markAnnouncementsRead,
|
||||
readNoticeReadKeys,
|
||||
} from '../../helpers/notificationReadState.js';
|
||||
|
||||
const EMPTY_ANNOUNCEMENTS = [];
|
||||
|
||||
export const useNotifications = (statusState) => {
|
||||
const [noticeVisible, setNoticeVisible] = useState(false);
|
||||
const [unreadCount, setUnreadCount] = useState(0);
|
||||
|
||||
const announcements = statusState?.status?.announcements || [];
|
||||
const announcements =
|
||||
statusState?.status?.announcements || EMPTY_ANNOUNCEMENTS;
|
||||
|
||||
// Helper functions
|
||||
const getAnnouncementKey = (a) =>
|
||||
`${a?.publishDate || ''}-${(a?.content || '').slice(0, 30)}`;
|
||||
|
||||
const calculateUnreadCount = () => {
|
||||
if (!announcements.length) return 0;
|
||||
let readKeys = [];
|
||||
try {
|
||||
readKeys = JSON.parse(localStorage.getItem('notice_read_keys')) || [];
|
||||
} catch (_) {
|
||||
readKeys = [];
|
||||
}
|
||||
const readSet = new Set(readKeys);
|
||||
return announcements.filter((a) => !readSet.has(getAnnouncementKey(a)))
|
||||
.length;
|
||||
};
|
||||
|
||||
const getUnreadKeys = () => {
|
||||
const getUnreadKeys = useCallback(() => {
|
||||
if (!announcements.length) return [];
|
||||
let readKeys = [];
|
||||
try {
|
||||
readKeys = JSON.parse(localStorage.getItem('notice_read_keys')) || [];
|
||||
} catch (_) {
|
||||
readKeys = [];
|
||||
}
|
||||
const readSet = new Set(readKeys);
|
||||
return announcements
|
||||
.filter((a) => !readSet.has(getAnnouncementKey(a)))
|
||||
.map(getAnnouncementKey);
|
||||
};
|
||||
return getUnreadAnnouncementKeys(announcements, readNoticeReadKeys());
|
||||
}, [announcements]);
|
||||
|
||||
// Effects
|
||||
useEffect(() => {
|
||||
setUnreadCount(calculateUnreadCount());
|
||||
}, [announcements]);
|
||||
setUnreadCount(getUnreadKeys().length);
|
||||
}, [getUnreadKeys]);
|
||||
|
||||
// Actions
|
||||
const handleNoticeOpen = () => {
|
||||
@@ -69,16 +51,7 @@ export const useNotifications = (statusState) => {
|
||||
const handleNoticeClose = () => {
|
||||
setNoticeVisible(false);
|
||||
if (announcements.length) {
|
||||
let readKeys = [];
|
||||
try {
|
||||
readKeys = JSON.parse(localStorage.getItem('notice_read_keys')) || [];
|
||||
} catch (_) {
|
||||
readKeys = [];
|
||||
}
|
||||
const mergedKeys = Array.from(
|
||||
new Set([...readKeys, ...announcements.map(getAnnouncementKey)]),
|
||||
);
|
||||
localStorage.setItem('notice_read_keys', JSON.stringify(mergedKeys));
|
||||
markAnnouncementsRead(announcements);
|
||||
}
|
||||
setUnreadCount(0);
|
||||
};
|
||||
|
||||
@@ -18,8 +18,12 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { getTableCompactMode, setTableCompactMode } from '../../helpers';
|
||||
import { TABLE_COMPACT_MODES_KEY } from '../../constants';
|
||||
import {
|
||||
getTableCompactMode,
|
||||
parseTableCompactMode,
|
||||
setTableCompactMode,
|
||||
} from '../../helpers/tableCompactMode.js';
|
||||
import { TABLE_COMPACT_MODES_KEY } from '../../constants/common.constant.js';
|
||||
|
||||
/**
|
||||
* 自定义 Hook:管理表格紧凑/自适应模式
|
||||
@@ -42,12 +46,7 @@ export function useTableCompactMode(tableKey = 'global') {
|
||||
useEffect(() => {
|
||||
const handleStorage = (e) => {
|
||||
if (e.key === TABLE_COMPACT_MODES_KEY) {
|
||||
try {
|
||||
const modes = JSON.parse(e.newValue || '{}');
|
||||
setCompactModeState(!!modes[tableKey]);
|
||||
} catch {
|
||||
// ignore parse error
|
||||
}
|
||||
setCompactModeState(parseTableCompactMode(e.newValue, tableKey));
|
||||
}
|
||||
};
|
||||
window.addEventListener('storage', handleStorage);
|
||||
|
||||
Reference in New Issue
Block a user