Add types for interactions and remove useMemo
This commit is contained in:
+24
-8
@@ -91,23 +91,39 @@ export const STAGING_FEEDS = [
|
|||||||
|
|
||||||
export const FEEDBACK_FEEDS = [...PROD_FEEDS, ...STAGING_FEEDS]
|
export const FEEDBACK_FEEDS = [...PROD_FEEDS, ...STAGING_FEEDS]
|
||||||
|
|
||||||
export const PASSIVE_INTERACTIONS = [
|
export const PASSIVE_FEEDBACK_INTERACTIONS = [
|
||||||
'app.bsky.feed.defs#clickthroughItem',
|
'app.bsky.feed.defs#clickthroughItem',
|
||||||
'app.bsky.feed.defs#clickthroughAuthor',
|
'app.bsky.feed.defs#clickthroughAuthor',
|
||||||
'app.bsky.feed.defs#clickthroughReposter',
|
'app.bsky.feed.defs#clickthroughReposter',
|
||||||
'app.bsky.feed.defs#clickthroughEmbed',
|
'app.bsky.feed.defs#clickthroughEmbed',
|
||||||
'app.bsky.feed.defs#interactionSeen',
|
'app.bsky.feed.defs#interactionSeen',
|
||||||
]
|
] as const
|
||||||
|
|
||||||
export const DIRECT_INTERACTIONS = [
|
export type PassiveFeedbackInteraction =
|
||||||
|
(typeof PASSIVE_FEEDBACK_INTERACTIONS)[number]
|
||||||
|
|
||||||
|
export const DIRECT_FEEDBACK_INTERACTIONS = [
|
||||||
'app.bsky.feed.defs#requestLess',
|
'app.bsky.feed.defs#requestLess',
|
||||||
'app.bsky.feed.defs#requestMore',
|
'app.bsky.feed.defs#requestMore',
|
||||||
]
|
] as const
|
||||||
|
|
||||||
export const ALL_INTERACTIONS = [
|
export type DirectFeedbackInteraction =
|
||||||
...PASSIVE_INTERACTIONS,
|
(typeof DIRECT_FEEDBACK_INTERACTIONS)[number]
|
||||||
...DIRECT_INTERACTIONS,
|
|
||||||
]
|
export const ALL_FEEDBACK_INTERACTIONS = [
|
||||||
|
...PASSIVE_FEEDBACK_INTERACTIONS,
|
||||||
|
...DIRECT_FEEDBACK_INTERACTIONS,
|
||||||
|
] as const
|
||||||
|
|
||||||
|
export type FeedbackInteraction = (typeof ALL_FEEDBACK_INTERACTIONS)[number]
|
||||||
|
|
||||||
|
export function isFeedbackInteraction(
|
||||||
|
interactionEvent: string,
|
||||||
|
): interactionEvent is FeedbackInteraction {
|
||||||
|
return ALL_FEEDBACK_INTERACTIONS.includes(
|
||||||
|
interactionEvent as FeedbackInteraction,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export const POST_IMG_MAX = {
|
export const POST_IMG_MAX = {
|
||||||
width: 2000,
|
width: 2000,
|
||||||
|
|||||||
+22
-10
@@ -11,9 +11,11 @@ import {type AppBskyFeedDefs} from '@atproto/api'
|
|||||||
import throttle from 'lodash.throttle'
|
import throttle from 'lodash.throttle'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
ALL_INTERACTIONS,
|
ALL_FEEDBACK_INTERACTIONS,
|
||||||
DIRECT_INTERACTIONS,
|
DIRECT_FEEDBACK_INTERACTIONS,
|
||||||
FEEDBACK_FEEDS,
|
FEEDBACK_FEEDS,
|
||||||
|
type FeedbackInteraction,
|
||||||
|
isFeedbackInteraction,
|
||||||
STAGING_FEEDS,
|
STAGING_FEEDS,
|
||||||
} from '#/lib/constants'
|
} from '#/lib/constants'
|
||||||
import {logEvent} from '#/lib/statsig/statsig'
|
import {logEvent} from '#/lib/statsig/statsig'
|
||||||
@@ -61,12 +63,7 @@ export function useFeedFeedback(
|
|||||||
const feedInfo = feed ? buildFeedInfo(feed) : null
|
const feedInfo = feed ? buildFeedInfo(feed) : null
|
||||||
const enabled = !!feedInfo && feedInfo.acceptsInteractions && hasSession
|
const enabled = !!feedInfo && feedInfo.acceptsInteractions && hasSession
|
||||||
|
|
||||||
const enabledInteractions = useMemo(() => {
|
const enabledInteractions = getEnabledInteractions(enabled, feedInfo)
|
||||||
if (!enabled) {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
return feedInfo.isDiscover ? ALL_INTERACTIONS : DIRECT_INTERACTIONS
|
|
||||||
}, [enabled, feedInfo])
|
|
||||||
|
|
||||||
const queue = useRef<Set<string>>(new Set())
|
const queue = useRef<Set<string>>(new Set())
|
||||||
const history = useRef<
|
const history = useRef<
|
||||||
@@ -89,8 +86,11 @@ export function useFeedFeedback(
|
|||||||
const interactions = Array.from(queue.current).map(toInteraction)
|
const interactions = Array.from(queue.current).map(toInteraction)
|
||||||
queue.current.clear()
|
queue.current.clear()
|
||||||
|
|
||||||
const interactionsToSend = interactions.filter(interaction =>
|
const interactionsToSend = interactions.filter(
|
||||||
enabledInteractions.includes(interaction.event ?? ''),
|
interaction =>
|
||||||
|
interaction.event &&
|
||||||
|
isFeedbackInteraction(interaction.event) &&
|
||||||
|
enabledInteractions.includes(interaction.event),
|
||||||
)
|
)
|
||||||
|
|
||||||
if (interactionsToSend.length === 0) {
|
if (interactionsToSend.length === 0) {
|
||||||
@@ -259,6 +259,18 @@ function buildFeedInfo(feed: FeedSourceInfo | FeedDescriptor): FeedInfo | null {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getEnabledInteractions(
|
||||||
|
enabled: boolean,
|
||||||
|
feedInfo: FeedInfo | null,
|
||||||
|
): readonly FeedbackInteraction[] {
|
||||||
|
if (!enabled || !feedInfo) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
return feedInfo.isDiscover
|
||||||
|
? ALL_FEEDBACK_INTERACTIONS
|
||||||
|
: DIRECT_FEEDBACK_INTERACTIONS
|
||||||
|
}
|
||||||
|
|
||||||
function toString(interaction: AppBskyFeedDefs.Interaction): string {
|
function toString(interaction: AppBskyFeedDefs.Interaction): string {
|
||||||
return `${interaction.item}|${interaction.event}|${
|
return `${interaction.item}|${interaction.event}|${
|
||||||
interaction.feedContext || ''
|
interaction.feedContext || ''
|
||||||
|
|||||||
Reference in New Issue
Block a user