File browser as widget
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { useCallback } from 'react';
|
||||
import { useUserState } from '@/state/useUserState';
|
||||
|
||||
type PinnedFile = { path: string; name: string; pinnedAt: number };
|
||||
|
||||
export const usePinnedFiles = () => {
|
||||
const [pinned, setPinned] = useUserState<PinnedFile[]>('pinnedFiles', []);
|
||||
|
||||
const togglePin = useCallback(
|
||||
(path: string, name: string) => {
|
||||
setPinned((prev) => {
|
||||
const exists = prev.some((f) => f.path === path);
|
||||
if (exists) return prev.filter((f) => f.path !== path);
|
||||
return [{ path, name, pinnedAt: Date.now() }, ...prev];
|
||||
});
|
||||
},
|
||||
[setPinned],
|
||||
);
|
||||
|
||||
const isPinned = useCallback((path: string) => pinned.some((f) => f.path === path), [pinned]);
|
||||
|
||||
return { pinned, togglePin, isPinned };
|
||||
};
|
||||
Reference in New Issue
Block a user