Skip to content

Commit

Permalink
check for subscribed on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed Nov 21, 2023
1 parent c2fe07c commit 6146e7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 69 deletions.
37 changes: 1 addition & 36 deletions src/routes/settings/Plus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from "~/components";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import { eify, subscriptionValid, vibrateSuccess } from "~/utils";
import { eify, vibrateSuccess } from "~/utils";

function Perks(props: { alreadySubbed?: boolean }) {
const i18n = useI18n();
Expand Down Expand Up @@ -56,7 +56,6 @@ function PlusCTA() {

const [subbing, setSubbing] = createSignal(false);
const [confirmOpen, setConfirmOpen] = createSignal(false);
const [restoring, setRestoring] = createSignal(false);

const [error, setError] = createSignal<Error>();

Expand Down Expand Up @@ -104,32 +103,6 @@ function PlusCTA() {
}
}

async function restore() {
try {
setError(undefined);
setRestoring(true);
await actions.checkForSubscription();
if (!state.subscription_timestamp) {
setError(
new Error(i18n.t("settings.plus.error_no_subscription"))
);
}

if (!subscriptionValid(state.subscription_timestamp)) {
setError(
new Error(
i18n.t("settings.plus.error_expired_subscription")
)
);
}
} catch (e) {
console.error(e);
setError(eify(e));
} finally {
setRestoring(false);
}
}

const hasEnough = () => {
if (!planDetails()) return false;
return (state.balance?.lightning || 0n) > planDetails().amount_sat;
Expand Down Expand Up @@ -170,14 +143,6 @@ function PlusCTA() {
>
{i18n.t("settings.plus.join")}
</Button>
<Button
intent="green"
layout="flex"
onClick={restore}
loading={restoring()}
>
{i18n.t("settings.plus.restore")}
</Button>
</div>
</VStack>
<ConfirmDialog
Expand Down
47 changes: 14 additions & 33 deletions src/state/megaStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,16 @@ export const Provider: ParentComponent = (props) => {
async checkForSubscription(justPaid?: boolean): Promise<void> {
try {
const timestamp = await state.mutiny_wallet?.check_subscribed();
console.log("timestamp:", timestamp);
if (timestamp) {
localStorage.setItem(
"subscription_timestamp",
timestamp?.toString()
);

// Check that timestamp is a number
if (timestamp && !isNaN(Number(timestamp))) {
setState({ subscription_timestamp: Number(timestamp) });
}
} catch (e) {
if (justPaid) {
} else if (justPaid) {
// we make a fake timestamp for 24 hours from now, in case the server is down
const timestamp = Math.ceil(Date.now() / 1000) + 86400;
setState({ subscription_timestamp: timestamp });
}
} catch (e) {
console.error(e);
}
},
Expand Down Expand Up @@ -192,30 +188,16 @@ export const Provider: ParentComponent = (props) => {
// If we get this far then we don't need the password anymore
setState({ needs_password: false });

// Subscription stuff. Skip if it's not already in localstorage
let subscription_timestamp: number | undefined = undefined;
const stored_subscription_timestamp = localStorage.getItem(
"subscription_timestamp"
);
// If we have a stored timestamp, check if it's still valid
if (stored_subscription_timestamp) {
try {
const timestamp =
await mutinyWallet?.check_subscribed();

// Check that timestamp is a number
if (!timestamp || isNaN(Number(timestamp))) {
throw new Error("Timestamp is not a number");
}

subscription_timestamp = Number(timestamp);
localStorage.setItem(
"subscription_timestamp",
timestamp.toString()
);
} catch (e) {
console.error(e);
// Check if we're subscribed and update the timestamp
try {
const timestamp = await mutinyWallet?.check_subscribed();

// Check that timestamp is a number
if (timestamp && !isNaN(Number(timestamp))) {
setState({ subscription_timestamp: Number(timestamp) });
}
} catch (e) {
console.error(e);
}

// Get balance
Expand All @@ -224,7 +206,6 @@ export const Provider: ParentComponent = (props) => {
setState({
mutiny_wallet: mutinyWallet,
wallet_loading: false,
subscription_timestamp: subscription_timestamp,
load_stage: "done",
balance
});
Expand Down

0 comments on commit 6146e7e

Please sign in to comment.