Files
syops/frontend/src/shared/composables/useReportLayoutStorage.ts
T

61 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* shared/composables/useReportLayoutStorage.ts
* 用途:报表页面布局偏好 localStorage 持久化(按用户 + 表名隔离)。
* 依赖:localStorage / sessionStorage 中的 hxops_user。
* 逻辑:ReportsPage / useReportsAdmin 读写列宽、显隐与排序偏好;v3 起聊天日志默认列宽收窄。
*/
const getCurrentAuthUser = () => {
let stored = localStorage.getItem('hxops_user');
if (!stored) stored = sessionStorage.getItem('hxops_user');
if (!stored) return null;
try {
const data = JSON.parse(stored);
return data.user || data;
} catch {
return null;
}
};
const getCurrentIdentity = () => {
const user = getCurrentAuthUser();
return user?.id ? `id_${user.id}` : `u_${user?.username || 'guest'}`;
};
/**
* 生成报表布局偏好的 storage key。
* @param table - 报表表名(如 chat_logs)
*/
const getReportLayoutStorageKey = (table: string) => {
// v3:聊天日志默认列宽收窄(应用名/用户/姓名/Token),旧 v2 偏好不再沿用
return `hxops_report_layout_v3_${getCurrentIdentity()}_${table}`;
};
/**
* 读取指定报表表的布局偏好。
* @param table - 报表表名
* @returns 解析后的偏好,失败或不存在时返回 null
*/
export const loadReportLayoutPref = <T = any>(table: string): T | null => {
try {
const raw = localStorage.getItem(getReportLayoutStorageKey(table));
if (!raw) return null;
return JSON.parse(raw) as T;
} catch {
return null;
}
};
/**
* 写入指定报表表的布局偏好。
* @param table - 报表表名
* @param payload - 可序列化的布局对象
*/
export const saveReportLayoutPref = (table: string, payload: unknown) => {
try {
localStorage.setItem(getReportLayoutStorageKey(table), JSON.stringify(payload));
} catch {
// Ignore storage write failure to avoid blocking core report flow.
}
};