Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/classic-shared/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ export type BlocksToWebviewMessage =
payload: {
access: HardcoreAccessStatus;
};
}
| {
// relayed broadcast message from PURCHASE_REALTIME_CHANNEL channel
type: 'PURCHASE_PRODUCT_SUCCESS_BROADCAST';
payload: {
access: HardcoreAccessStatus;
};
Comment thread
cytommi marked this conversation as resolved.
Outdated
};

export type DevvitMessage = {
Expand All @@ -185,3 +192,9 @@ export type DevvitMessage = {
};

export type GameMode = 'regular' | 'hardcore';

export type PurchasedProductBroadcast = {
Comment thread
cytommi marked this conversation as resolved.
Outdated
payload: {
access: HardcoreAccessStatus;
};
};
7 changes: 7 additions & 0 deletions packages/classic-webview/src/hooks/useHardcoreAccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const HardcoreAccessContextProvider = (props: { children: React.ReactNode
const [access, setAccess] = useState<HardcoreAccessStatus>({ status: 'inactive' });
const hardcoreAccessInitResponse = useDevvitListener('HARDCORE_ACCESS_INIT_RESPONSE');
const productPurchaseResponse = useDevvitListener('PURCHASE_PRODUCT_SUCCESS_RESPONSE');
const productPurchaseBroadcast = useDevvitListener('PURCHASE_PRODUCT_SUCCESS_BROADCAST');

useEffect(() => {
if (hardcoreAccessInitResponse?.hardcoreAccessStatus != null) {
Expand All @@ -28,6 +29,12 @@ export const HardcoreAccessContextProvider = (props: { children: React.ReactNode
}
}, [productPurchaseResponse, setAccess]);

useEffect(() => {
if (productPurchaseBroadcast != null) {
setAccess(productPurchaseBroadcast.access);
}
}, [productPurchaseBroadcast, setAccess]);

return (
<hardcoreAccessContext.Provider value={{ access, setAccess }}>
{props.children}
Expand Down
36 changes: 33 additions & 3 deletions packages/classic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import './menu-actions/newChallenge.js';
import './menu-actions/addWordToDictionary.js';
import './menu-actions/totalReminders.js';

import { Devvit, useInterval, useState } from '@devvit/public-api';
import { Devvit, JSONValue, useInterval, useState } from '@devvit/public-api';
import { DEVVIT_SETTINGS_KEYS } from './constants.js';
import { isServerCall, omit } from '@hotandcold/shared/utils';
import { GameMode, HardcoreAccessStatus, WebviewToBlocksMessage } from '@hotandcold/classic-shared';
import {
GameMode,
HardcoreAccessStatus,
PurchasedProductBroadcast as PurchasedProductBroadcastMessage,
WebviewToBlocksMessage,
} from '@hotandcold/classic-shared';
import { GuessService } from './core/guess.js';
import { ChallengeToPost, PostIdentifier } from './core/challengeToPost.js';
import { Preview } from './components/Preview.js';
Expand All @@ -21,6 +26,7 @@ import { RedditApiCache } from './core/redditApiCache.js';
import { sendMessageToWebview } from './utils/index.js';
import { initPayments, PaymentsRepo } from './payments.js';
import { OnPurchaseResult, OrderResultStatus, usePayments } from '@devvit/payments';
import { useChannel } from '@devvit/public-api';

initPayments();

Expand Down Expand Up @@ -60,22 +66,46 @@ type InitialState =
hardcoreModeAccess: HardcoreAccessStatus;
};

const PURCHASE_REALTIME_CHANNEL = 'PURCHASE_REALTIME_CHANNEL';

// Add a post type definition
Devvit.addCustomPostType({
name: 'HotAndCold',
height: 'tall',
render: (context) => {
const purchaseRealtimeChannel = useChannel({
Comment thread
cytommi marked this conversation as resolved.
name: PURCHASE_REALTIME_CHANNEL,
onMessage(msg: JSONValue) {
Comment thread
cytommi marked this conversation as resolved.
const msgCasted = msg as PurchasedProductBroadcastMessage;
sendMessageToWebview(context, {
type: 'PURCHASE_PRODUCT_SUCCESS_BROADCAST',
payload: msgCasted.payload,
});
},
onSubscribed: () => {
console.log('listening for purchase success broadcast events');
},
});
purchaseRealtimeChannel.subscribe();
Comment thread
cytommi marked this conversation as resolved.
Outdated

const paymentsRepo = new PaymentsRepo(context.redis);
const payments = usePayments(async (paymentsResult: OnPurchaseResult) => {
switch (paymentsResult.status) {
case OrderResultStatus.Success: {
context.ui.showToast(`Purchase successful!`);
const access = await paymentsRepo.getHardcoreAccessStatus(context.userId!);
sendMessageToWebview(context, {
type: 'PURCHASE_PRODUCT_SUCCESS_RESPONSE',
payload: {
access: await paymentsRepo.getHardcoreAccessStatus(context.userId!),
access,
},
});
await purchaseRealtimeChannel.send({
payload: {
access,
Comment thread
cytommi marked this conversation as resolved.
Outdated
},
});

break;
}
case OrderResultStatus.Error: {
Expand Down