overhaul complete
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
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 }
|
||||
}
|
||||
Reference in New Issue
Block a user