Files
ForgeBucket/frontend/src/hooks/useRecentRepos.ts
T
2026-05-07 11:02:34 +02:00

31 lines
714 B
TypeScript

import { useCallback } from 'react'
export interface RecentRepo {
ownerName: string
name: string
visitedAt: number
}
const KEY = 'fb_recent_repos'
const MAX = 5
function read(): RecentRepo[] {
try {
return JSON.parse(localStorage.getItem(KEY) ?? '[]')
} catch {
return []
}
}
export function useRecentRepos() {
const repos = read()
const track = useCallback((ownerName: string, name: string) => {
const existing = read().filter(r => !(r.ownerName === ownerName && r.name === name))
const updated: RecentRepo[] = [{ ownerName, name, visitedAt: Date.now() }, ...existing].slice(0, MAX)
localStorage.setItem(KEY, JSON.stringify(updated))
}, [])
return { repos, track }
}