关联 Issue: P25-2, P25-3, P25-4, P26-2, P26-4, P28-1, P28-2, P28-3, P28-4, P24-2, P29-3
31 行
988 B
TypeScript
31 行
988 B
TypeScript
export const RECENT_VISITS_STORAGE_KEY = "hyb_oa_recent_visits";
|
|
|
|
export type RecentVisit = {
|
|
path: string;
|
|
title: string;
|
|
visitedAt: string;
|
|
};
|
|
|
|
function isWindowAvailable() {
|
|
return typeof window !== "undefined" && typeof window.localStorage !== "undefined";
|
|
}
|
|
|
|
export function readRecentVisits(): RecentVisit[] {
|
|
if (!isWindowAvailable()) return [];
|
|
try {
|
|
const raw = window.localStorage.getItem(RECENT_VISITS_STORAGE_KEY);
|
|
if (!raw) return [];
|
|
const parsed = JSON.parse(raw);
|
|
return Array.isArray(parsed) ? parsed.filter((item) => item && typeof item.path === "string") : [];
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
export function writeRecentVisit(path: string, title: string) {
|
|
if (!isWindowAvailable()) return;
|
|
const current = readRecentVisits().filter((item) => item.path !== path);
|
|
current.unshift({ path, title, visitedAt: new Date().toISOString() });
|
|
window.localStorage.setItem(RECENT_VISITS_STORAGE_KEY, JSON.stringify(current.slice(0, 10)));
|
|
}
|