Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 85x 85x 85x 185x 185x 2x 2x 180x 3x 3x 181x 179x 179x 372x 370x 370x 368x 365x 360x 179x 176x 170x 170x 168x 107x 3x 3x 77x 75x 89x 75x 75x 2x 75x 38x 38x 38x 94x 38x 38x 76x 50x 50x 38x 200x 48x 48x 48x 19x 60x 60x 60x 42x 40x 100x 100x 12x 40x 40x 30x 10x 5x 5x 5x 85x 42x 40x 40x 2x 2x 16x 16x 16x 1x 5x 4x 8x 8x 1x 45x 54x 43x 40x | /*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { getLoggerWithTag } from '@global/logger/v2/logger.mjs';
import type { UserRight } from '@front/store/userRight';
export const RECENT_ARCHITECTURES_KEY = 'seaf.archSwitcher.recent';
const logger = getLoggerWithTag('f/h/recentArchitectures');
const RECENT_LIMIT = 4;
export interface RecentEntry {
alias: string;
usedAt: number;
}
export interface RecentStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
}
function defaultStorage(): (RecentStorage & Pick<Storage, 'removeItem'>) | null {
try {
if (typeof localStorage === 'undefined') {
logger.warn(() => 'localStorage is unavailable');
return null;
}
return localStorage;
} catch {
logger.warn(() => 'Failed to access localStorage');
return null;
}
}
function normalize(parsed: unknown): RecentEntry[] {
if (!Array.isArray(parsed)) return [];
// Сохраняем порядок хранения и первый дубль; оставляем не больше четырёх корректных записей.
const byAlias = new Map<string, RecentEntry>();
for (const item of parsed) {
if (!item || typeof item !== 'object') continue;
const row = item as { alias?: unknown; usedAt?: unknown };
if (!row.alias || typeof row.alias !== 'string') continue;
if (typeof row.usedAt !== 'number' || !Number.isFinite(row.usedAt)) continue;
if (byAlias.has(row.alias)) continue;
byAlias.set(row.alias, {
alias: row.alias,
usedAt: row.usedAt
});
}
return [...byAlias.values()]
.slice(0, RECENT_LIMIT);
}
export function readRecent(storage: RecentStorage | null = defaultStorage()): RecentEntry[] {
if (!storage) return [];
try {
const raw = storage.getItem(RECENT_ARCHITECTURES_KEY);
if (!raw) return [];
return normalize(JSON.parse(raw));
} catch {
logger.warn(() => `Failed to get item ${RECENT_ARCHITECTURES_KEY} from storage`);
return [];
}
}
export function remember(
alias: string,
storage: RecentStorage | null = defaultStorage(),
now: number = Date.now()
): RecentEntry[] {
if (!alias || !storage) return readRecent(storage);
// При открытии организации запись переносится в начало без сортировки по времени.
const next = normalize([
{alias, usedAt: now},
...readRecent(storage).filter((item) => item.alias !== alias)
]);
try {
storage.setItem(RECENT_ARCHITECTURES_KEY, JSON.stringify(next));
} catch {
logger.warn(() => `Failed to set item ${RECENT_ARCHITECTURES_KEY} in storage`);
}
return next;
}
export function recentSection<T extends UserRight>(
rights: readonly T[] | null | undefined,
currentAlias: string,
storage: RecentStorage | null = defaultStorage()
): T[] {
const list = Array.isArray(rights) ? rights : [];
const byAlias = new Map<string, T>();
for (const right of list) {
if (right?.alias) byAlias.set(right.alias, right);
}
const matched: T[] = [];
for (const item of readRecent(storage)) {
if (item.alias === currentAlias) continue;
const right = byAlias.get(item.alias);
if (right) matched.push(right);
}
return matched;
}
function sortLabel(right: UserRight | null | undefined): string {
return right?.title || right?.alias || '';
}
// Локальный фильтр секции ВСЕ: подстрока без учёта регистра по title ИЛИ alias
// Пустой или пробельный запрос возвращает список как есть.
export function filterByQuery<T extends UserRight>(
rights: readonly T[] | null | undefined,
query: string
): T[] {
const list = Array.isArray(rights) ? [...rights] : [];
const needle = (query || '').trim().toLowerCase();
if (!needle) return list;
return list.filter((right) => {
const title = (right?.title || '').toLowerCase();
const alias = (right?.alias || '').toLowerCase();
return title.includes(needle) || alias.includes(needle);
});
}
export interface AllSortedOptions {
allFirstAlias?: string;
}
export function allSorted<T extends UserRight>(
rights: readonly T[] | null | undefined,
options: AllSortedOptions = {}
): T[] {
if (!Array.isArray(rights)) return [];
const sorted = [...rights].sort((a, b) => {
const byLabel = sortLabel(a).localeCompare(sortLabel(b), 'ru');
if (byLabel !== 0) return byLabel;
return (a?.alias || '').localeCompare(b?.alias || '', 'ru');
});
const allFirstAlias = options.allFirstAlias;
if (!allFirstAlias) return sorted;
const index = sorted.findIndex((right) => right?.alias === allFirstAlias);
if (index <= 0) return sorted;
const [first] = sorted.splice(index, 1);
sorted.unshift(first);
return sorted;
}
export const LAST_ARCHITECTURE_KEY = 'seaf.archSwitcher.last';
export function readLast(storage: RecentStorage | null = defaultStorage()): string | null {
if (!storage) return null;
try {
return storage.getItem(LAST_ARCHITECTURE_KEY) || null;
} catch {
logger.warn(() => `Failed to get item ${LAST_ARCHITECTURE_KEY} from storage`);
return null;
}
}
export function rememberLast(alias: string, storage: RecentStorage | null = defaultStorage()): void {
Iif (!alias || !storage) return;
try {
storage.setItem(LAST_ARCHITECTURE_KEY, alias);
} catch {
logger.warn(() => `Failed to set item ${LAST_ARCHITECTURE_KEY} in storage`);
}
}
export function clearArchitectures(
storage: Pick<Storage, 'removeItem'> | null = defaultStorage()
): void {
if (!storage) return;
for (const key of [RECENT_ARCHITECTURES_KEY, LAST_ARCHITECTURE_KEY]) {
try {
storage.removeItem(key);
} catch {
logger.warn(() => `Failed to remove item ${key} from storage`);
}
}
}
export function pickEntryAlias({last, recent, rights}: {
last: string | null;
recent: readonly RecentEntry[];
rights: readonly UserRight[] | null | undefined;
}): string | null {
if (!Array.isArray(rights)) return null;
const aliases = new Set(rights.map(right => right?.alias).filter(Boolean));
// Сначала выбираем доступную явно выбранную организацию, затем первую доступную из недавних.
if (last && aliases.has(last)) return last;
return recent.find(entry => aliases.has(entry.alias))?.alias || null;
}
|