mirror of
https://github.com/SableClient/Sable.git
synced 2026-12-04 07:42:24 -08:00
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { atom, useSetAtom } from 'jotai';
|
|
import type { MatrixClient, MatrixEvent } from 'matrix-js-sdk';
|
|
import { ClientEvent } from 'matrix-js-sdk';
|
|
import { useCallback, useEffect } from 'react';
|
|
import type { BookmarkItemContent } from '$types/matrix-sdk-events';
|
|
import { listBookmarks } from '$features/bookmarks/bookmarkRepository';
|
|
import { MATRIX_SABLE_UNSTABLE_BOOKMARKS_INDEX_EVENT } from '$unstable/prefixes';
|
|
import { useMatrixEvent } from '$hooks/useMatrixEvent';
|
|
|
|
export const bookmarkListAtom = atom<BookmarkItemContent[]>([]);
|
|
export const bookmarkLoadingAtom = atom<boolean>(false);
|
|
export const bookmarkRefreshErrorAtom = atom<Error | undefined>(undefined);
|
|
|
|
export const bookmarksAtom = {
|
|
list: bookmarkListAtom,
|
|
loading: bookmarkLoadingAtom,
|
|
refreshError: bookmarkRefreshErrorAtom,
|
|
};
|
|
|
|
export const bookmarkIdSetAtom = atom<Set<string>>((get) => {
|
|
const list = get(bookmarkListAtom);
|
|
return new Set(list.map((b) => b.bookmark_id));
|
|
});
|
|
|
|
export const useBindBookmarksAtom = (mx: MatrixClient, bookmarks: typeof bookmarksAtom) => {
|
|
const setList = useSetAtom(bookmarks.list);
|
|
const setLoading = useSetAtom(bookmarks.loading);
|
|
const setRefreshError = useSetAtom(bookmarks.refreshError);
|
|
|
|
const refresh = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
const items = await listBookmarks(mx);
|
|
setList(items);
|
|
setRefreshError(undefined);
|
|
} catch (error) {
|
|
setRefreshError(error as Error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [mx, setList, setLoading, setRefreshError]);
|
|
|
|
useEffect(() => {
|
|
refresh();
|
|
}, [refresh]);
|
|
|
|
const handleAccountData = useCallback(
|
|
(event: MatrixEvent) => {
|
|
if (event.getType() === MATRIX_SABLE_UNSTABLE_BOOKMARKS_INDEX_EVENT) {
|
|
refresh();
|
|
}
|
|
},
|
|
[refresh]
|
|
);
|
|
|
|
useMatrixEvent(mx, ClientEvent.AccountData, handleAccountData);
|
|
};
|