refactor(shared): 提取公共状态逻辑并修复用户列表分页
统一存储解析、通知已读状态、表格偏好和企业日志筛选参数。修复切换每页条数时沿用旧页码、丢失筛选条件和重复触发分页回调的问题。 同步调整项目检查脚本、格式配置和开发文档。
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { parseStoredObject } from './storageJson.js';
|
||||
|
||||
// Resolve raw localStorage JSON without mutating defaults or performing I/O.
|
||||
export const resolveColumnVisibility = (
|
||||
defaults,
|
||||
savedColumns,
|
||||
hiddenKeys = [],
|
||||
) => {
|
||||
const resolved = { ...defaults };
|
||||
const preferences = parseStoredObject(savedColumns);
|
||||
|
||||
for (const key of Object.keys(defaults)) {
|
||||
if (
|
||||
Object.prototype.hasOwnProperty.call(preferences, key) &&
|
||||
typeof preferences[key] === 'boolean'
|
||||
) {
|
||||
resolved[key] = preferences[key];
|
||||
}
|
||||
}
|
||||
|
||||
// Role restrictions take precedence over saved preferences.
|
||||
for (const key of hiddenKeys) {
|
||||
resolved[key] = false;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
};
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { resolveColumnVisibility } from './columnVisibility.js';
|
||||
|
||||
const defaults = Object.freeze({ time: true, model: true, channel: false });
|
||||
|
||||
test('missing or malformed saved JSON falls back to a fresh copy of defaults', () => {
|
||||
for (const saved of [
|
||||
undefined,
|
||||
null,
|
||||
'',
|
||||
'{',
|
||||
'not-json',
|
||||
'{"time":false,}',
|
||||
]) {
|
||||
const resolved = resolveColumnVisibility(defaults, saved);
|
||||
assert.deepEqual(resolved, defaults);
|
||||
assert.notStrictEqual(resolved, defaults);
|
||||
}
|
||||
});
|
||||
|
||||
test('null, arrays, and primitive JSON values are not preference maps', () => {
|
||||
for (const saved of [
|
||||
'null',
|
||||
'[]',
|
||||
'[false]',
|
||||
'"text"',
|
||||
'0',
|
||||
'true',
|
||||
'false',
|
||||
]) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, saved), defaults);
|
||||
}
|
||||
});
|
||||
|
||||
test('non-string saved data is ignored without coercion or mutation', () => {
|
||||
const saved = Object.freeze({ time: false });
|
||||
for (const value of [saved, [false], 1, true]) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, value), defaults);
|
||||
}
|
||||
assert.deepEqual(saved, { time: false });
|
||||
});
|
||||
|
||||
test('saved false and true values override defaults while new columns keep defaults', () => {
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(defaults, '{"time":false,"channel":true}'),
|
||||
{ time: false, model: true, channel: true },
|
||||
);
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, '{}'), defaults);
|
||||
});
|
||||
|
||||
test('invalid individual preferences keep defaults without discarding valid booleans', () => {
|
||||
for (const value of [null, 0, 1, '', 'false', [], {}]) {
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(
|
||||
defaults,
|
||||
JSON.stringify({ time: value, channel: value, model: false }),
|
||||
),
|
||||
{ time: true, model: false, channel: false },
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('unknown and prototype-related saved keys do not enter the result', () => {
|
||||
const resolved = resolveColumnVisibility(
|
||||
defaults,
|
||||
'{"time":false,"removedColumn":true,"__proto__":{"polluted":true},"constructor":false,"toString":false}',
|
||||
);
|
||||
assert.deepEqual(resolved, { time: false, model: true, channel: false });
|
||||
assert.equal(Object.getPrototypeOf(resolved), Object.prototype);
|
||||
assert.equal(Object.hasOwn(resolved, '__proto__'), false);
|
||||
});
|
||||
|
||||
for (const [table, restrictedKeys] of [
|
||||
['usage logs', ['channel', 'username', 'retry']],
|
||||
['task logs', ['channel']],
|
||||
]) {
|
||||
for (const isAdmin of [true, false]) {
|
||||
test(`${table}: ${isAdmin ? 'admin' : 'non-admin'} preferences respect role restrictions`, () => {
|
||||
const roleDefaults = {
|
||||
model: true,
|
||||
...Object.fromEntries(restrictedKeys.map((key) => [key, isAdmin])),
|
||||
};
|
||||
const hiddenKeys = isAdmin ? [] : restrictedKeys;
|
||||
for (const checked of [true, false]) {
|
||||
const preferences = {
|
||||
model: false,
|
||||
...Object.fromEntries(restrictedKeys.map((key) => [key, checked])),
|
||||
};
|
||||
const resolved = resolveColumnVisibility(
|
||||
roleDefaults,
|
||||
JSON.stringify(preferences),
|
||||
hiddenKeys,
|
||||
);
|
||||
assert.equal(resolved.model, false);
|
||||
for (const key of restrictedKeys) {
|
||||
assert.equal(resolved[key], isAdmin && checked);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(
|
||||
resolveColumnVisibility(roleDefaults, null, hiddenKeys),
|
||||
roleDefaults,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test('forced-hidden keys apply even when saved preferences are missing or invalid', () => {
|
||||
for (const saved of [null, '{', 'null', '[]', '{}', '{"time":true}']) {
|
||||
assert.deepEqual(resolveColumnVisibility(defaults, saved, ['time']), {
|
||||
time: false,
|
||||
model: true,
|
||||
channel: false,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
test('resolution never mutates defaults or hidden keys and returns independent results', () => {
|
||||
const hiddenKeys = Object.freeze(['channel']);
|
||||
const saved = '{"time":false,"channel":true}';
|
||||
const first = resolveColumnVisibility(defaults, saved, hiddenKeys);
|
||||
const second = resolveColumnVisibility(defaults, saved, hiddenKeys);
|
||||
|
||||
assert.notStrictEqual(first, second);
|
||||
first.model = false;
|
||||
assert.deepEqual(second, { time: false, model: true, channel: false });
|
||||
assert.deepEqual(defaults, { time: true, model: true, channel: false });
|
||||
assert.deepEqual(hiddenKeys, ['channel']);
|
||||
assert.equal(saved, '{"time":false,"channel":true}');
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { parseStoredArray, readStorageValue } from './storageJson.js';
|
||||
|
||||
export const NOTICE_READ_KEYS_KEY = 'notice_read_keys';
|
||||
|
||||
export const getAnnouncementKey = (announcement) =>
|
||||
`${announcement?.publishDate || ''}-${(announcement?.content || '').slice(0, 30)}`;
|
||||
|
||||
export function getUnreadAnnouncementKeys(announcements, readKeys) {
|
||||
const readSet = new Set(readKeys);
|
||||
return announcements
|
||||
.map(getAnnouncementKey)
|
||||
.filter((key) => !readSet.has(key));
|
||||
}
|
||||
|
||||
export function mergeAnnouncementReadKeys(announcements, readKeys) {
|
||||
return Array.from(
|
||||
new Set([...readKeys, ...announcements.map(getAnnouncementKey)]),
|
||||
);
|
||||
}
|
||||
|
||||
export function readNoticeReadKeys() {
|
||||
return parseStoredArray(readStorageValue(NOTICE_READ_KEYS_KEY));
|
||||
}
|
||||
|
||||
export function markAnnouncementsRead(announcements) {
|
||||
const mergedKeys = mergeAnnouncementReadKeys(
|
||||
announcements,
|
||||
readNoticeReadKeys(),
|
||||
);
|
||||
// Let write failures propagate so callers do not report unread items as saved.
|
||||
localStorage.setItem(NOTICE_READ_KEYS_KEY, JSON.stringify(mergedKeys));
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
/** Map the shared consumption filters without adding endpoint-specific options. */
|
||||
export function buildOrgLogFilters({
|
||||
userId,
|
||||
startTimestamp,
|
||||
endTimestamp,
|
||||
modelName,
|
||||
} = {}) {
|
||||
const params = {};
|
||||
// Preserve the API callers' existing omission of empty values (including zero).
|
||||
if (userId) params.user_id = userId;
|
||||
if (startTimestamp) params.start_timestamp = startTimestamp;
|
||||
if (endTimestamp) params.end_timestamp = endTimestamp;
|
||||
if (modelName) params.model_name = modelName;
|
||||
return params;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { buildOrgLogFilters } from './orgLogFilters.js';
|
||||
|
||||
test('consumption filters map to the existing API field names', () => {
|
||||
const filters = Object.freeze({
|
||||
userId: 42,
|
||||
startTimestamp: 1700000000,
|
||||
endTimestamp: 1700003600,
|
||||
modelName: 'example/model + preview',
|
||||
});
|
||||
assert.deepEqual(buildOrgLogFilters(filters), {
|
||||
user_id: 42,
|
||||
start_timestamp: 1700000000,
|
||||
end_timestamp: 1700003600,
|
||||
model_name: 'example/model + preview',
|
||||
});
|
||||
});
|
||||
|
||||
test('empty filters and existing falsy values are omitted', () => {
|
||||
assert.deepEqual(buildOrgLogFilters(), {});
|
||||
for (const value of [undefined, null, '', 0, false, NaN]) {
|
||||
assert.deepEqual(
|
||||
buildOrgLogFilters({
|
||||
userId: value,
|
||||
startTimestamp: value,
|
||||
endTimestamp: value,
|
||||
modelName: value,
|
||||
}),
|
||||
{},
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('partial filters preserve values without coercion', () => {
|
||||
assert.deepEqual(buildOrgLogFilters({ userId: '0', modelName: 'a&b' }), {
|
||||
user_id: '0',
|
||||
model_name: 'a&b',
|
||||
});
|
||||
});
|
||||
|
||||
test('endpoint-specific parameters cannot leak into shared filters', () => {
|
||||
const filters = Object.freeze({
|
||||
userId: 42,
|
||||
limit: 10,
|
||||
nocache: 1,
|
||||
p: 5,
|
||||
page_size: 20,
|
||||
memberId: 7,
|
||||
});
|
||||
const params = buildOrgLogFilters(filters);
|
||||
assert.deepEqual(params, { user_id: 42 });
|
||||
params.limit = 15;
|
||||
assert.deepEqual(buildOrgLogFilters(filters), { user_id: 42 });
|
||||
assert.equal(filters.limit, 10);
|
||||
});
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
function parseStoredJson(json, isValid, fallback) {
|
||||
if (typeof json !== 'string') return fallback;
|
||||
try {
|
||||
const value = JSON.parse(json);
|
||||
return isValid(value) ? value : fallback;
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
|
||||
export function parseStoredArray(json) {
|
||||
return parseStoredJson(json, Array.isArray, []);
|
||||
}
|
||||
|
||||
export function parseStoredObject(json) {
|
||||
return parseStoredJson(
|
||||
json,
|
||||
(value) =>
|
||||
value !== null && typeof value === 'object' && !Array.isArray(value),
|
||||
{},
|
||||
);
|
||||
}
|
||||
|
||||
export function readStorageValue(key) {
|
||||
try {
|
||||
return localStorage.getItem(key);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,300 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
parseStoredArray,
|
||||
parseStoredObject,
|
||||
readStorageValue,
|
||||
} from './storageJson.js';
|
||||
|
||||
import {
|
||||
getAnnouncementKey,
|
||||
getUnreadAnnouncementKeys,
|
||||
markAnnouncementsRead,
|
||||
mergeAnnouncementReadKeys,
|
||||
NOTICE_READ_KEYS_KEY,
|
||||
readNoticeReadKeys,
|
||||
} from './notificationReadState.js';
|
||||
import {
|
||||
getTableCompactMode,
|
||||
parseTableCompactMode,
|
||||
setTableCompactMode,
|
||||
} from './tableCompactMode.js';
|
||||
import { TABLE_COMPACT_MODES_KEY } from '../constants/common.constant.js';
|
||||
|
||||
function mockStorage(t, storage) {
|
||||
const descriptor = Object.getOwnPropertyDescriptor(
|
||||
globalThis,
|
||||
'localStorage',
|
||||
);
|
||||
Object.defineProperty(globalThis, 'localStorage', {
|
||||
configurable: true,
|
||||
value: storage,
|
||||
});
|
||||
t.after(() => {
|
||||
if (descriptor) {
|
||||
Object.defineProperty(globalThis, 'localStorage', descriptor);
|
||||
} else {
|
||||
delete globalThis.localStorage;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const invalidJson = [
|
||||
null,
|
||||
undefined,
|
||||
'',
|
||||
'{broken',
|
||||
'null',
|
||||
'true',
|
||||
'42',
|
||||
'"text"',
|
||||
];
|
||||
|
||||
test('stored arrays reject malformed JSON and non-array values', () => {
|
||||
for (const json of [...invalidJson, '{}', '{"0":"notice"}']) {
|
||||
assert.deepEqual(parseStoredArray(json), [], String(json));
|
||||
}
|
||||
});
|
||||
|
||||
test('stored objects reject malformed JSON and non-object values', () => {
|
||||
for (const json of [...invalidJson, '[]', '[true]']) {
|
||||
assert.deepEqual(parseStoredObject(json), {}, String(json));
|
||||
}
|
||||
});
|
||||
|
||||
test('valid stored containers keep their values without coercion or filtering', () => {
|
||||
const array = ['notice', 'notice', null, 1];
|
||||
const object = { global: true, tokens: false, other: 'yes' };
|
||||
assert.deepEqual(parseStoredArray(JSON.stringify(array)), array);
|
||||
assert.deepEqual(parseStoredObject(JSON.stringify(object)), object);
|
||||
});
|
||||
|
||||
test('fallback containers are fresh for each parse', () => {
|
||||
const array = parseStoredArray(null);
|
||||
const object = parseStoredObject(null);
|
||||
array.push('notice');
|
||||
object.global = true;
|
||||
assert.deepEqual(parseStoredArray(null), []);
|
||||
assert.deepEqual(parseStoredObject(null), {});
|
||||
});
|
||||
|
||||
test('storage reads return the original stored value', (t) => {
|
||||
mockStorage(t, {
|
||||
getItem(key) {
|
||||
assert.equal(key, 'preference');
|
||||
return 'stored';
|
||||
},
|
||||
});
|
||||
assert.equal(readStorageValue('preference'), 'stored');
|
||||
});
|
||||
|
||||
test('storage read failures fall back to null', (t) => {
|
||||
mockStorage(t, {
|
||||
getItem() {
|
||||
throw new Error('Storage blocked');
|
||||
},
|
||||
});
|
||||
assert.equal(readStorageValue('preference'), null);
|
||||
assert.deepEqual(readNoticeReadKeys(), []);
|
||||
assert.equal(getTableCompactMode(), false);
|
||||
});
|
||||
|
||||
test('announcement identity keeps the date and first 30 content characters', () => {
|
||||
assert.equal(
|
||||
getAnnouncementKey({ publishDate: '2025-01-01', content: 'a'.repeat(40) }),
|
||||
`2025-01-01-${'a'.repeat(30)}`,
|
||||
);
|
||||
assert.equal(getAnnouncementKey({}), '-');
|
||||
assert.equal(getAnnouncementKey(null), '-');
|
||||
});
|
||||
|
||||
const first = { publishDate: '2025-01-01', content: 'First' };
|
||||
const second = { publishDate: '2025-01-02', content: 'Second' };
|
||||
|
||||
test('unread keys preserve announcement order and duplicate counts', () => {
|
||||
const notices = [first, second, second];
|
||||
const readKeys = [getAnnouncementKey(first)];
|
||||
assert.deepEqual(getUnreadAnnouncementKeys(notices, readKeys), [
|
||||
getAnnouncementKey(second),
|
||||
getAnnouncementKey(second),
|
||||
]);
|
||||
assert.deepEqual(
|
||||
getUnreadAnnouncementKeys(notices, []),
|
||||
notices.map(getAnnouncementKey),
|
||||
);
|
||||
assert.deepEqual(
|
||||
getUnreadAnnouncementKeys(notices, notices.map(getAnnouncementKey)),
|
||||
[],
|
||||
);
|
||||
assert.deepEqual(getUnreadAnnouncementKeys([], readKeys), []);
|
||||
});
|
||||
|
||||
test('merging read keys retains history, deduplicates, and does not mutate inputs', () => {
|
||||
const notices = [first, second, first];
|
||||
const readKeys = ['old', getAnnouncementKey(first), 'old'];
|
||||
assert.deepEqual(mergeAnnouncementReadKeys(notices, readKeys), [
|
||||
'old',
|
||||
getAnnouncementKey(first),
|
||||
getAnnouncementKey(second),
|
||||
]);
|
||||
assert.deepEqual(readKeys, ['old', getAnnouncementKey(first), 'old']);
|
||||
assert.deepEqual(notices, [first, second, first]);
|
||||
});
|
||||
|
||||
test('notification persistence uses the existing key and merges the latest stored history', (t) => {
|
||||
let stored = '["old"]';
|
||||
mockStorage(t, {
|
||||
getItem(key) {
|
||||
assert.equal(key, 'notice_read_keys');
|
||||
return stored;
|
||||
},
|
||||
setItem(key, value) {
|
||||
assert.equal(key, NOTICE_READ_KEYS_KEY);
|
||||
stored = value;
|
||||
},
|
||||
});
|
||||
markAnnouncementsRead([first, first]);
|
||||
assert.deepEqual(JSON.parse(stored), ['old', getAnnouncementKey(first)]);
|
||||
stored = JSON.stringify([...JSON.parse(stored), 'another-tab']);
|
||||
markAnnouncementsRead([second]);
|
||||
assert.deepEqual(JSON.parse(stored), [
|
||||
'old',
|
||||
getAnnouncementKey(first),
|
||||
'another-tab',
|
||||
getAnnouncementKey(second),
|
||||
]);
|
||||
});
|
||||
|
||||
test('invalid notice storage is treated as unread and can be repaired on write', (t) => {
|
||||
let stored;
|
||||
mockStorage(t, {
|
||||
getItem: () => stored,
|
||||
setItem: (_key, value) => {
|
||||
stored = value;
|
||||
},
|
||||
});
|
||||
for (const json of [...invalidJson, '{}']) {
|
||||
stored = json;
|
||||
assert.deepEqual(readNoticeReadKeys(), []);
|
||||
assert.deepEqual(getUnreadAnnouncementKeys([first], readNoticeReadKeys()), [
|
||||
getAnnouncementKey(first),
|
||||
]);
|
||||
markAnnouncementsRead([first]);
|
||||
assert.deepEqual(JSON.parse(stored), [getAnnouncementKey(first)]);
|
||||
}
|
||||
});
|
||||
|
||||
test('notification write failures propagate instead of reporting a saved read state', (t) => {
|
||||
const failure = new Error('Quota exceeded');
|
||||
mockStorage(t, {
|
||||
getItem: () => '[]',
|
||||
setItem() {
|
||||
throw failure;
|
||||
},
|
||||
});
|
||||
assert.throws(
|
||||
() => markAnnouncementsRead([first]),
|
||||
(error) => error === failure,
|
||||
);
|
||||
});
|
||||
|
||||
test('compact parsing preserves global defaults, per-table selection and truthiness', () => {
|
||||
const json = '{"global":true,"tokens":false,"channels":"enabled","logs":0}';
|
||||
assert.equal(parseTableCompactMode(json), true);
|
||||
assert.equal(parseTableCompactMode(json, 'tokens'), false);
|
||||
assert.equal(parseTableCompactMode(json, 'channels'), true);
|
||||
assert.equal(parseTableCompactMode(json, 'logs'), false);
|
||||
assert.equal(parseTableCompactMode(json, 'missing'), false);
|
||||
});
|
||||
|
||||
test('compact storage-event payloads use the same fallback as initial reads', (t) => {
|
||||
let stored;
|
||||
mockStorage(t, { getItem: () => stored });
|
||||
for (const json of [...invalidJson, '[]', '[true]', '{"global":true}']) {
|
||||
stored = json;
|
||||
assert.equal(parseTableCompactMode(json), getTableCompactMode());
|
||||
}
|
||||
assert.equal(parseTableCompactMode(null), false);
|
||||
});
|
||||
|
||||
test('compact writes preserve other table preferences and the storage key', (t) => {
|
||||
let stored = '{"channels":true,"tokens":false}';
|
||||
mockStorage(t, {
|
||||
getItem(key) {
|
||||
assert.equal(key, 'table_compact_modes');
|
||||
return stored;
|
||||
},
|
||||
setItem(key, value) {
|
||||
assert.equal(key, TABLE_COMPACT_MODES_KEY);
|
||||
stored = value;
|
||||
},
|
||||
});
|
||||
assert.equal(setTableCompactMode(true), undefined);
|
||||
setTableCompactMode(true, 'tokens');
|
||||
assert.deepEqual(JSON.parse(stored), {
|
||||
channels: true,
|
||||
tokens: true,
|
||||
global: true,
|
||||
});
|
||||
setTableCompactMode(false, 'channels');
|
||||
assert.equal(getTableCompactMode('channels'), false);
|
||||
});
|
||||
|
||||
test('invalid compact preferences can be replaced by a valid object on write', (t) => {
|
||||
let stored;
|
||||
mockStorage(t, {
|
||||
getItem: () => stored,
|
||||
setItem: (_key, value) => {
|
||||
stored = value;
|
||||
},
|
||||
});
|
||||
for (const json of [...invalidJson, '[]', '[true]']) {
|
||||
stored = json;
|
||||
assert.equal(getTableCompactMode(), false);
|
||||
setTableCompactMode(true);
|
||||
assert.deepEqual(JSON.parse(stored), { global: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('compact write failures remain non-fatal', (t) => {
|
||||
mockStorage(t, {
|
||||
getItem: () => '{}',
|
||||
setItem() {
|
||||
throw new Error('Quota exceeded');
|
||||
},
|
||||
});
|
||||
assert.doesNotThrow(() => setTableCompactMode(true));
|
||||
});
|
||||
|
||||
test('blocked storage access is guarded for reads and compact writes but not notice writes', (t) => {
|
||||
mockStorage(t, undefined);
|
||||
Object.defineProperty(globalThis, 'localStorage', {
|
||||
configurable: true,
|
||||
get() {
|
||||
throw new Error('Storage blocked');
|
||||
},
|
||||
});
|
||||
assert.deepEqual(readNoticeReadKeys(), []);
|
||||
assert.equal(getTableCompactMode(), false);
|
||||
assert.doesNotThrow(() => setTableCompactMode(true));
|
||||
assert.throws(() => markAnnouncementsRead([first]), /Storage blocked/);
|
||||
});
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
import { TABLE_COMPACT_MODES_KEY } from '../constants/common.constant.js';
|
||||
import { parseStoredObject, readStorageValue } from './storageJson.js';
|
||||
|
||||
export function parseTableCompactMode(json, tableKey = 'global') {
|
||||
const modes = parseStoredObject(json);
|
||||
return !!modes[tableKey];
|
||||
}
|
||||
|
||||
export function getTableCompactMode(tableKey = 'global') {
|
||||
return parseTableCompactMode(
|
||||
readStorageValue(TABLE_COMPACT_MODES_KEY),
|
||||
tableKey,
|
||||
);
|
||||
}
|
||||
|
||||
export function setTableCompactMode(compact, tableKey = 'global') {
|
||||
const modes = parseStoredObject(readStorageValue(TABLE_COMPACT_MODES_KEY));
|
||||
modes[tableKey] = compact;
|
||||
try {
|
||||
localStorage.setItem(TABLE_COMPACT_MODES_KEY, JSON.stringify(modes));
|
||||
} catch {
|
||||
// Compact mode remains usable in memory when preferences cannot be saved.
|
||||
}
|
||||
}
|
||||
+6
-28
@@ -26,7 +26,6 @@ import {
|
||||
THINK_TAG_REGEX,
|
||||
MESSAGE_ROLES,
|
||||
} from '../constants/playground.constants';
|
||||
import { TABLE_COMPACT_MODES_KEY } from '../constants';
|
||||
import { MOBILE_BREAKPOINT } from '../hooks/common/useIsMobile';
|
||||
// 官方价折扣计算(纯函数):管理端与市场页共用
|
||||
import { buildOfficialComparison, formatDiscountLabel } from './officialPrice';
|
||||
@@ -735,33 +734,10 @@ export const formatDateTimeString = (date) => {
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}`;
|
||||
};
|
||||
|
||||
function readTableCompactModes() {
|
||||
try {
|
||||
const json = localStorage.getItem(TABLE_COMPACT_MODES_KEY);
|
||||
return json ? JSON.parse(json) : {};
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function writeTableCompactModes(modes) {
|
||||
try {
|
||||
localStorage.setItem(TABLE_COMPACT_MODES_KEY, JSON.stringify(modes));
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
export function getTableCompactMode(tableKey = 'global') {
|
||||
const modes = readTableCompactModes();
|
||||
return !!modes[tableKey];
|
||||
}
|
||||
|
||||
export function setTableCompactMode(compact, tableKey = 'global') {
|
||||
const modes = readTableCompactModes();
|
||||
modes[tableKey] = compact;
|
||||
writeTableCompactModes(modes);
|
||||
}
|
||||
export {
|
||||
getTableCompactMode,
|
||||
setTableCompactMode,
|
||||
} from './tableCompactMode.js';
|
||||
|
||||
// -------------------------------
|
||||
// Select 组件统一过滤逻辑
|
||||
@@ -1111,6 +1087,7 @@ export const createCardProPagination = ({
|
||||
isMobile = false,
|
||||
pageSizeOpts = [10, 20, 50, 100],
|
||||
showSizeChanger = true,
|
||||
preventPageChangeOnPageSizeChange = false,
|
||||
t = (key) => key,
|
||||
}) => {
|
||||
if (!total || total <= 0) return null;
|
||||
@@ -1140,6 +1117,7 @@ export const createCardProPagination = ({
|
||||
showSizeChanger={showSizeChanger}
|
||||
onPageSizeChange={onPageSizeChange}
|
||||
onPageChange={onPageChange}
|
||||
preventPageChangeOnPageSizeChange={preventPageChangeOnPageSizeChange}
|
||||
size={isMobile ? 'small' : 'default'}
|
||||
showQuickJumper={isMobile}
|
||||
showTotal
|
||||
|
||||
Reference in New Issue
Block a user