77import {computed , onMounted , ref } from ' vue'
88import {useI18n } from ' vue-i18n'
99import {api } from ' @/api'
10- import type {ActiveFeaturePOJO , Feature , PremiumFeaturesPOJO } from ' @/api/types'
10+ import type {ActiveFeaturePOJO , Feature } from ' @/api/types'
1111import {useSession } from ' @/composables/useSession'
1212import TokenFeatureCard from ' @/components/TokenFeatureCard.vue'
1313import ViewContainer from ' @/components/ViewContainer.vue'
@@ -17,14 +17,13 @@ import BaseButton from '@/components/BaseButton.vue'
1717import TokenValue from ' @/components/TokenValue.vue'
1818
1919const {t} = useI18n ()
20- const {userSession, currentGuildId, userTokens, guildTokens, refreshUserTokens, refreshGuildTokens, refreshGuildPremium} = useSession ()
20+ const {userSession, currentGuildId, userTokens, guildTokens, refreshUserTokens, refreshGuildTokens, refreshGuildPremium, premiumFeatures : sessionPremiumFeatures } = useSession ()
2121
2222const features = ref <Feature []>([])
2323const activeFeatures = ref <ActiveFeaturePOJO []>([])
2424const loading = ref (true )
2525const actionLoading = ref (false )
2626const useGuildTokens = ref (false )
27- const premiumFeatures = ref <PremiumFeaturesPOJO | null >(null )
2827const everyoneTokenPurchase = ref (true )
2928const transferAmount = ref (1 )
3029const transferLoading = ref (false )
@@ -37,20 +36,17 @@ const isGuildAdmin = computed(() => {
3736const fetchData = async (silent = false ) => {
3837 if (! silent ) loading .value = true
3938 try {
40- const [featuresRes, activeRes, premiumRes, everyoneTokenPurchaseRes] = await Promise .all ([
39+ const [featuresRes, activeRes, everyoneTokenPurchaseRes] = await Promise .all ([
4140 api .getTokenFeatures (),
4241 api .getActiveFeatures (),
43- refreshGuildPremium (),
4442 api .getEveryoneTokenPurchase (),
43+ refreshGuildPremium (),
4544 refreshUserTokens (),
4645 refreshGuildTokens ()
4746 ])
4847
49- // api.getTokenFeatures() returns a Map-like structure or Array depending on how it was implemented/cached
50- // In our case, the response from backend is an array of features
5148 features .value = Object .values (featuresRes )
5249 activeFeatures .value = activeRes
53- premiumFeatures .value = premiumRes as any
5450 everyoneTokenPurchase .value = everyoneTokenPurchaseRes
5551 } catch (error ) {
5652 console .error (' Failed to fetch token shop data' , error )
@@ -121,6 +117,31 @@ const getActiveFeature = (featureId: number) => {
121117 return activeFeatures .value .find (f => f .featureId === featureId )
122118}
123119
120+ const getRequiredSkus = (localeKey : string ) => {
121+ if (! sessionPremiumFeatures .value ) return []
122+ // localeKey is like "sku.reputationlog" -> strip "sku." -> camelCase key in premiumFeatures
123+ const key = localeKey .replace (' sku.' , ' ' ).toLowerCase ()
124+ const map: Record <string , string > = {
125+ reputationlog: ' reputationLog' ,
126+ analyzerlog: ' analyzerLog' ,
127+ channelblacklist: ' channelBlacklist' ,
128+ localeoverrides: ' localeOverrides' ,
129+ autopost: ' autopost' ,
130+ advancedrankings: ' advancedRankings' ,
131+ detailedprofile: ' detailedProfile' ,
132+ logchannel: ' logChannel' ,
133+ additionalemojis: ' additionalEmojis' ,
134+ profile: ' profile' ,
135+ integrationbypass: ' integrationBypass' ,
136+ reputationchannel: ' reputationChannel' ,
137+ reputationcategories: ' reputationCategories' ,
138+ }
139+ const field = map [key ]
140+ if (! field ) return []
141+ const feature = (sessionPremiumFeatures .value as any )[field ]
142+ return feature ?.requiredSkus ?? []
143+ }
144+
124145const canAfford = (tokens : number ) => {
125146 if (! everyoneTokenPurchase .value && ! useGuildTokens .value && ! isGuildAdmin .value ) return false
126147 return (useGuildTokens .value ? guildTokens .value : userTokens .value ) >= tokens
@@ -226,7 +247,8 @@ const canPurchase = computed(() => {
226247 :key =" feature .id "
227248 :feature =" feature "
228249 :active-feature =" getActiveFeature (feature .id )"
229- :active-skus =" premiumFeatures ?.activeSkus ?? []"
250+ :active-skus =" [... (sessionPremiumFeatures ?.activeSkus ?? [])] "
251+ :required-skus =" getRequiredSkus (feature .localeKey )"
230252 :is-guild-admin =" isGuildAdmin "
231253 :can-afford =" canAfford (feature .tokens ) && (canPurchase || useGuildTokens )"
232254 :loading =" actionLoading "
0 commit comments