Skip to content

Commit

Permalink
Free gifting on thanksgiving
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman authored and futurepaul committed Nov 20, 2023
1 parent 8a427c6 commit d7ee953
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/routes/settings/Gift.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from "~/components";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import { eify } from "~/utils";
import { eify, isFreeGiftingDay } from "~/utils";
import { baseUrlAccountingForNative } from "~/utils/baseUrl";
import { createDeepSignal } from "~/utils/deepSignal";

Expand Down Expand Up @@ -203,11 +203,9 @@ export function Gift() {
};

const selfHosted = state.settings?.selfhosted === "true";
const today = new Date();
// days are 1 indexed, months are 0 indexed
const isChristmas = today.getDate() === 25 && today.getMonth() === 11;
const freeDay = isFreeGiftingDay();

const canGift = state.mutiny_plus || selfHosted || isChristmas;
const canGift = state.mutiny_plus || selfHosted || freeDay;

return (
<MutinyWalletGuard>
Expand Down
9 changes: 4 additions & 5 deletions src/routes/settings/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from "~/components";
import { useI18n } from "~/i18n/context";
import { useMegaStore } from "~/state/megaStore";
import { isFreeGiftingDay } from "~/utils";

function SettingsLinkList(props: {
header: string;
Expand Down Expand Up @@ -74,9 +75,7 @@ export function Settings() {
const COMMIT_HASH = import.meta.env.__COMMIT_HASH__;

const selfHosted = state.settings?.selfhosted === "true";
const today = new Date();
// days are 1 indexed, months are 0 indexed
const isChristmas = today.getDate() === 25 && today.getMonth() === 11;
const freeDay = isFreeGiftingDay();

const ios = Capacitor.getPlatform() === "ios";

Expand Down Expand Up @@ -138,13 +137,13 @@ export function Settings() {
disabled: !(
state.mutiny_plus ||
selfHosted ||
isChristmas
freeDay
),
text: i18n.t("settings.gift.title"),
caption: !(
state.mutiny_plus ||
selfHosted ||
isChristmas
freeDay
)
? i18n.t("settings.gift.no_plus_caption")
: undefined
Expand Down
24 changes: 24 additions & 0 deletions src/utils/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@ export function subscriptionValid(subscriptionExpiresTimestamp?: number) {

return subscriptionExpiresTimestamp + GRACE > Math.ceil(Date.now() / 1000);
}

export function isFreeGiftingDay() {
const today = new Date();
// days are 1 indexed, months are 0 indexed
const isChristmas = today.getDate() === 25 && today.getMonth() === 11;

return isChristmas || isThanksgiving(today);
}

function isThanksgiving(today: Date) {
if (today.getMonth() !== 10) return false;

const year = today.getFullYear();
const thanksgivingDay = new Date(year, 10, 1);

// Find out what day of the week Nov 1 is
const dayOfWeek = thanksgivingDay.getDay();

// Calculate the date of the fourth Thursday in November
thanksgivingDay.setDate(1 + ((4 - dayOfWeek) % 7) + 3 * 7);

// Compare the current date with Thanksgiving date
return today.getDate() === thanksgivingDay.getDate();
}

0 comments on commit d7ee953

Please sign in to comment.