Files
discord-decomp/modules/premium/promotions/PromotionsHooks.tsx
T
2026-11-01 12:12:55 +02:00

124 lines
5.8 KiB
TypeScript

// Metro module 12498 (f12499).
import { useMemo, useEffect } from "react";
import UserStore from "../../../stores/UserStore";
import PromotionsStore from "./PromotionsStore";
import { PremiumTypes } from "../PremiumConstants";
import { useStateFromStoresArray, useStateFromStores } from "../../../_bundle/Module589";
import * as _PromotionUtils from "./PromotionUtils";
import { shouldShowOutboundPromotionOnPlatform, getClaimedOutboundPromotionCodeMap, isRecurringPromotion } from "./PromotionUtils";
import PremiumUtils from "../../../utils/PremiumUtils";
import Dispatcher from "../../../Dispatcher";
import PromotionsActionCreators, { maybeFetchActivePromotions } from "./PromotionsActionCreators";
const useEligibleActiveOutboundPromotions = function useEligibleActiveOutboundPromotions(r2 = {}) {
let includeClaimedPromotions = r2.includeClaimedPromotions;
if (includeClaimedPromotions === void 0) {
includeClaimedPromotions = false;
}
let stateFromStores, stateFromStores2;
let c19869_0 = includeClaimedPromotions;
stateFromStores = void 0;
stateFromStores2 = void 0;
const stateFromStoresArray = useStateFromStoresArray([PromotionsStore], () => PromotionsStore.outboundPromotions);
stateFromStores = useStateFromStores([PromotionsStore], () => PromotionsStore.consumedInboundPromotionId);
stateFromStores2 = useStateFromStores([PromotionsStore], () => PromotionsStore.claimedOutboundPromotionCodes);
return useMemo(() => {
const r3 = c19869_0 ? new Set(stateFromStores2.map((item) => item.promotion.id)) : null;
let c27299_0 = r3;
return stateFromStoresArray.filter((item) => {
let r2 = item.id !== stateFromStores;
if (r2) {
let r16 = !!shouldShowOutboundPromotionOnPlatform(item);
if (r16) {
const isDedicatedSurfacePromotion = _PromotionUtils.isDedicatedSurfacePromotion(item);
let r21 = !isDedicatedSurfacePromotion;
if (!isDedicatedSurfacePromotion) {
const r4 = c27299_0 == null;
r21 = true;
if (!r4) {
c27299_0.has(item.id);
r21 = true;
}
}
r16 = r21;
}
r2 = r16;
}
return r2;
});
}, [stateFromStoresArray, stateFromStores, stateFromStores2, includeClaimedPromotions]);
};
export { useEligibleActiveOutboundPromotions };
export function useOutboundPromotions() {
let isPremiumExactly, stateFromStores4, eligibleActiveOutboundPromotions;
const stateFromStores3 = useStateFromStores([PromotionsStore], () => PromotionsStore.lastFetchedActivePromotions);
const stateFromStores6 = useStateFromStores([UserStore], () => UserStore.getCurrentUser());
const isPremiumExactly2 = PremiumUtils.isPremiumExactly(stateFromStores6, PremiumTypes.TIER_2);
const isPremium = PremiumUtils.isPremium(stateFromStores6);
isPremiumExactly = isPremium ? isPremiumExactly2 : !isPremium;
stateFromStores4 = useStateFromStores([PromotionsStore], () => PromotionsStore.claimedOutboundPromotionCodes);
let promotionsLoaded = useStateFromStores([PromotionsStore], () => PromotionsStore.claimedOutboundPromotionCodesLoaded);
useEffect(() => {
if (stateFromStores3 != null) {
Dispatcher.wait(() => PromotionsActionCreators.markOutboundPromotionsSeen());
}
}, [stateFromStores3]);
useEffect(() => {
Dispatcher.wait(() => {
let r1 = isPremiumExactly;
if (isPremiumExactly) {
r1 = stateFromStores3 == null;
}
if (r1) {
PromotionsActionCreators.fetchActivePromotions();
}
});
}, [stateFromStores3, isPremiumExactly]);
useEffect(() => {
Dispatcher.wait(() => {
PromotionsActionCreators.fetchClaimedOutboundPromotionCodes();
});
}, []);
const memo = useMemo(() => getClaimedOutboundPromotionCodeMap(stateFromStores4), [stateFromStores4]);
eligibleActiveOutboundPromotions = useEligibleActiveOutboundPromotions({
includeClaimedPromotions: true
});
const memo2 = useMemo(() => {
let c27300_0 = new Set(eligibleActiveOutboundPromotions.map((eligibleActiveOutboundPromotion) => eligibleActiveOutboundPromotion.id));
return stateFromStores4.filter(({ promotion }) => {
const r9 = c27300_0.has(promotion.id);
return (r9 ? !r9 : isRecurringPromotion({
promotionType: promotion.promotionType
}) === false) && !_PromotionUtils.isDedicatedSurfacePromotion(promotion) && shouldShowOutboundPromotionOnPlatform(promotion);
});
}, [eligibleActiveOutboundPromotions, stateFromStores4]);
if (promotionsLoaded) {
promotionsLoaded = isPremiumExactly ? stateFromStores3 != null : !isPremiumExactly;
}
return {
promotionsLoaded,
activeOutboundPromotions: eligibleActiveOutboundPromotions,
claimedEndedOutboundPromotions: memo2,
claimedOutboundPromotionCodeMap: memo
};
}
export function useUnseenOutboundPromotions() {
let eligibleActiveOutboundPromotions2;
const stateFromStores5 = useStateFromStores([PromotionsStore], () => PromotionsStore.lastSeenOutboundPromotionStartDate);
eligibleActiveOutboundPromotions2 = useEligibleActiveOutboundPromotions();
return useMemo(() => stateFromStores5 == null ? eligibleActiveOutboundPromotions2 : eligibleActiveOutboundPromotions2.filter((item) => new Date(item.startDate) > new Date(stateFromStores5)), [eligibleActiveOutboundPromotions2, stateFromStores5]).filter((item) => shouldShowOutboundPromotionOnPlatform(item));
}
export function useBogoPromotion() {
return {
promotion: useStateFromStores([PromotionsStore], () => PromotionsStore.bogoPromotion)
};
}
export function useIsInPromotion(c19872_0) {
return useStateFromStores([PromotionsStore], () => PromotionsStore.hasPromotion(c19872_0));
}
export function useHasActiveBogoPromotion() {
useEffect(() => {
maybeFetchActivePromotions();
}, []);
return useStateFromStores([PromotionsStore], () => PromotionsStore.getActiveBogoRewardPromotion() != null);
}