You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.1 KiB
102 lines
3.1 KiB
import client from '@/services/client.service' |
|
import storage from '@/services/local-storage.service' |
|
import { createContext, useCallback, useContext, useEffect, useState } from 'react' |
|
import { useNostr } from './NostrProvider' |
|
|
|
type TUserTrustContext = { |
|
hideUntrustedInteractions: boolean |
|
hideUntrustedNotifications: boolean |
|
hideUntrustedNotes: boolean |
|
updateHideUntrustedInteractions: (hide: boolean) => void |
|
updateHideUntrustedNotifications: (hide: boolean) => void |
|
updateHideUntrustedNotes: (hide: boolean) => void |
|
isUserTrusted: (pubkey: string) => boolean |
|
} |
|
|
|
const UserTrustContext = createContext<TUserTrustContext | undefined>(undefined) |
|
|
|
export const useUserTrust = () => { |
|
const context = useContext(UserTrustContext) |
|
if (!context) { |
|
throw new Error('useUserTrust must be used within a UserTrustProvider') |
|
} |
|
return context |
|
} |
|
|
|
const wotSet = new Set<string>() |
|
|
|
export function UserTrustProvider({ children }: { children: React.ReactNode }) { |
|
const { pubkey: currentPubkey } = useNostr() |
|
const [hideUntrustedInteractions, setHideUntrustedInteractions] = useState(() => |
|
storage.getHideUntrustedInteractions() |
|
) |
|
const [hideUntrustedNotifications, setHideUntrustedNotifications] = useState(() => |
|
storage.getHideUntrustedNotifications() |
|
) |
|
const [hideUntrustedNotes, setHideUntrustedNotes] = useState(() => |
|
storage.getHideUntrustedNotes() |
|
) |
|
|
|
useEffect(() => { |
|
if (!currentPubkey) return |
|
|
|
const initWoT = async () => { |
|
const followings = await client.fetchFollowings(currentPubkey) |
|
followings.forEach((pubkey) => wotSet.add(pubkey)) |
|
|
|
const batchSize = 20 |
|
for (let i = 0; i < followings.length; i += batchSize) { |
|
const batch = followings.slice(i, i + batchSize) |
|
await Promise.allSettled( |
|
batch.map(async (pubkey) => { |
|
const _followings = await client.fetchFollowings(pubkey) |
|
_followings.forEach((following) => { |
|
wotSet.add(following) |
|
}) |
|
}) |
|
) |
|
await new Promise((resolve) => setTimeout(resolve, 200)) |
|
} |
|
} |
|
initWoT() |
|
}, [currentPubkey]) |
|
|
|
const isUserTrusted = useCallback( |
|
(pubkey: string) => { |
|
if (!currentPubkey || pubkey === currentPubkey) return true |
|
return wotSet.has(pubkey) |
|
}, |
|
[currentPubkey] |
|
) |
|
|
|
const updateHideUntrustedInteractions = (hide: boolean) => { |
|
setHideUntrustedInteractions(hide) |
|
storage.setHideUntrustedInteractions(hide) |
|
} |
|
|
|
const updateHideUntrustedNotifications = (hide: boolean) => { |
|
setHideUntrustedNotifications(hide) |
|
storage.setHideUntrustedNotifications(hide) |
|
} |
|
|
|
const updateHideUntrustedNotes = (hide: boolean) => { |
|
setHideUntrustedNotes(hide) |
|
storage.setHideUntrustedNotes(hide) |
|
} |
|
|
|
return ( |
|
<UserTrustContext.Provider |
|
value={{ |
|
hideUntrustedInteractions, |
|
hideUntrustedNotifications, |
|
hideUntrustedNotes, |
|
updateHideUntrustedInteractions, |
|
updateHideUntrustedNotifications, |
|
updateHideUntrustedNotes, |
|
isUserTrusted |
|
}} |
|
> |
|
{children} |
|
</UserTrustContext.Provider> |
|
) |
|
}
|
|
|