27 changed files with 209 additions and 91 deletions
@ -0,0 +1,34 @@ |
|||||||
|
import { getPubkeysFromPTags } from '@/lib/tag' |
||||||
|
import { resolveAutoLoadMediaForAuthor } from '@/lib/media-auto-load-policy' |
||||||
|
import { useContentPolicyOptional } from '@/providers/ContentPolicyProvider' |
||||||
|
import { useNostrOptional } from '@/providers/nostr-context' |
||||||
|
import storage from '@/services/local-storage.service' |
||||||
|
import { useMemo } from 'react' |
||||||
|
|
||||||
|
export function useShouldAutoLoadMedia(authorPubkey?: string | null): boolean { |
||||||
|
const contentPolicy = useContentPolicyOptional() |
||||||
|
const nostr = useNostrOptional() |
||||||
|
|
||||||
|
const followings = useMemo( |
||||||
|
() => (nostr?.followListEvent ? getPubkeysFromPTags(nostr.followListEvent.tags) : []), |
||||||
|
[nostr?.followListEvent] |
||||||
|
) |
||||||
|
|
||||||
|
return useMemo( |
||||||
|
() => |
||||||
|
resolveAutoLoadMediaForAuthor({ |
||||||
|
policy: contentPolicy?.mediaAutoLoadPolicy ?? storage.getMediaAutoLoadPolicy(), |
||||||
|
connectionType: contentPolicy?.connectionType, |
||||||
|
authorPubkey, |
||||||
|
followings, |
||||||
|
accountPubkey: nostr?.pubkey ?? null |
||||||
|
}), |
||||||
|
[ |
||||||
|
contentPolicy?.mediaAutoLoadPolicy, |
||||||
|
contentPolicy?.connectionType, |
||||||
|
authorPubkey, |
||||||
|
followings, |
||||||
|
nostr?.pubkey |
||||||
|
] |
||||||
|
) |
||||||
|
} |
||||||
@ -0,0 +1,44 @@ |
|||||||
|
import { MEDIA_AUTO_LOAD_POLICY } from '@/constants' |
||||||
|
import type { TMediaAutoLoadPolicy } from '@/types' |
||||||
|
|
||||||
|
export type TResolveAutoLoadMediaParams = { |
||||||
|
policy: TMediaAutoLoadPolicy |
||||||
|
connectionType?: string |
||||||
|
authorPubkey?: string | null |
||||||
|
followings?: readonly string[] |
||||||
|
accountPubkey?: string | null |
||||||
|
} |
||||||
|
|
||||||
|
/** Whether media for a given author should load without an explicit tap. */ |
||||||
|
export function resolveAutoLoadMediaForAuthor({ |
||||||
|
policy, |
||||||
|
connectionType, |
||||||
|
authorPubkey, |
||||||
|
followings = [], |
||||||
|
accountPubkey |
||||||
|
}: TResolveAutoLoadMediaParams): boolean { |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.NEVER) return false |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.ALWAYS) return true |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.WIFI_ONLY) { |
||||||
|
return connectionType !== 'cellular' |
||||||
|
} |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.FOLLOWS_ONLY) { |
||||||
|
if (!authorPubkey) return false |
||||||
|
if (accountPubkey && authorPubkey === accountPubkey) return true |
||||||
|
return followings.includes(authorPubkey) |
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
/** Coarse global flag (false when the policy needs per-author resolution). */ |
||||||
|
export function resolveGlobalAutoLoadMedia( |
||||||
|
policy: TMediaAutoLoadPolicy, |
||||||
|
connectionType?: string |
||||||
|
): boolean { |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.ALWAYS) return true |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.NEVER) return false |
||||||
|
if (policy === MEDIA_AUTO_LOAD_POLICY.WIFI_ONLY) { |
||||||
|
return connectionType !== 'cellular' |
||||||
|
} |
||||||
|
return false |
||||||
|
} |
||||||
Loading…
Reference in new issue