mirror of
https://github.com/logos-messaging/OpChan.git
synced 2026-01-11 09:13:07 +00:00
49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import { localDatabase } from '@/lib/database/LocalDatabase';
|
|
import { useAuth } from '@/contexts/useAuth';
|
|
|
|
export function usePending(id: string | undefined) {
|
|
const [isPending, setIsPending] = useState<boolean>(
|
|
id ? localDatabase.isPending(id) : false
|
|
);
|
|
|
|
useEffect(() => {
|
|
if (!id) return;
|
|
setIsPending(localDatabase.isPending(id));
|
|
const unsubscribe = localDatabase.onPendingChange(() => {
|
|
setIsPending(localDatabase.isPending(id));
|
|
});
|
|
return unsubscribe;
|
|
}, [id]);
|
|
|
|
return { isPending };
|
|
}
|
|
|
|
export function usePendingVote(targetId: string | undefined) {
|
|
const { currentUser } = useAuth();
|
|
const [isPending, setIsPending] = useState<boolean>(false);
|
|
|
|
useEffect(() => {
|
|
const compute = () => {
|
|
if (!targetId || !currentUser?.address) return setIsPending(false);
|
|
// Find a vote authored by current user for this target that is pending
|
|
const pending = Object.values(localDatabase.cache.votes).some(v => {
|
|
return (
|
|
v.targetId === targetId &&
|
|
v.author === currentUser.address &&
|
|
localDatabase.isPending(v.id)
|
|
);
|
|
});
|
|
setIsPending(pending);
|
|
};
|
|
|
|
compute();
|
|
const unsub = localDatabase.onPendingChange(compute);
|
|
return unsub;
|
|
}, [targetId, currentUser?.address]);
|
|
|
|
return { isPending };
|
|
}
|
|
|
|
|