Skip to content

Commit

Permalink
fixed reset timing for generations
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutlope committed Mar 11, 2023
1 parent 4a2bb40 commit d6e6b26
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"@upstash/ratelimit": "^0.3.8",
"@upstash/redis": "^1.19.1",
"@vercel/analytics": "^0.1.9-beta.4",
"date-fns": "^2.29.3",
"date-fns-tz": "^2.0.0",
"framer-motion": "^8.2.4",
"next": "latest",
"next-auth": "^4.20.1",
Expand Down
18 changes: 1 addition & 17 deletions pages/api/remaining.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,8 @@ export default async function handler(
const usedGenerations =
(await redis?.get(`@upstash/ratelimit:${identifier!}:${bucket}`)) || 0;

// Move this using date-fns on the client-side
const resetDate = new Date();
// Check if the current time is before 7pm EST
if (resetDate.getUTCHours() < 23) {
// 23 is equivalent to 7pm EST in UTC
// If before 7pm EST, set the time to 7pm EST
resetDate.setUTCHours(19, 0, 0, 0);
} else {
// If after 7pm EST, add one day and set the time to 7pm EST
resetDate.setDate(resetDate.getDate() + 1); // Add one day
resetDate.setUTCHours(19, 0, 0, 0);
}
const diff = Math.abs(resetDate.getTime() - new Date().getTime());
const hours = Math.floor(diff / 1000 / 60 / 60);
const minutes = Math.floor(diff / 1000 / 60) - hours * 60;

const remainingGenerations =
Number(usedGenerations) > 3 ? 0 : 3 - Number(usedGenerations);

return res.status(200).json({ remainingGenerations, hours, minutes });
return res.status(200).json({ remainingGenerations });
}
9 changes: 5 additions & 4 deletions pages/dream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { GenerateResponseData } from "./api/generate";
import { useSession, signIn } from "next-auth/react";
import useSWR from "swr";
import { Rings } from "react-loader-spinner";
import getRemainingTime from "../utils/getRemainingTime";

// Configuration for the uploader
const uploader = Uploader({
Expand All @@ -41,6 +42,7 @@ const Home: NextPage = () => {
const fetcher = (url: string) => fetch(url).then((res) => res.json());
const { data, mutate } = useSWR("/api/remaining", fetcher);
const { data: session, status } = useSession();
const { hours, minutes } = getRemainingTime();

const options = {
maxFileCount: 1,
Expand All @@ -63,7 +65,7 @@ const Home: NextPage = () => {
},
onValidate: async (file: File): Promise<undefined | string> => {
return data.remainingGenerations === 0
? "No more generations left for the day."
? `No more generations left. Try again in ${hours} hours and ${minutes} minutes.`
: undefined;
},
};
Expand Down Expand Up @@ -137,10 +139,9 @@ const Home: NextPage = () => {
<span className="font-semibold text-gray-300">
{data.remainingGenerations} generations
</span>{" "}
left today. Your generation
{Number(data.remainingGenerations) > 1 ? "s" : ""} will renew in{" "}
left today. Your generations will renew in{" "}
<span className="font-semibold text-gray-300">
{data.hours + 5} hours and {data.minutes} minutes.
{hours} hours and {minutes} minutes.
</span>
</p>
)}
Expand Down
24 changes: 24 additions & 0 deletions utils/getRemainingTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { utcToZonedTime } from "date-fns-tz";
import { differenceInMilliseconds } from "date-fns";

export default function getRemainingTime() {
const now = new Date();

// Convert current date and time to Eastern Timezone
const et = utcToZonedTime(now, "America/New_York");

// Determine if current time is before or after 7pm ET
const isBefore7pmET = et.getHours() < 19; // 7pm ET is 19:00 in 24-hour time

// Calculate time difference between now and 7pm ET today or tomorrow
const targetDate = new Date(et);
targetDate.setHours(19, 0, 0, 0);
if (!isBefore7pmET) {
targetDate.setDate(targetDate.getDate() + 1);
}
const timeDiff = differenceInMilliseconds(targetDate, et);
const hours = Math.floor(timeDiff / 1000 / 60 / 60);
const minutes = Math.floor(timeDiff / 1000 / 60) - hours * 60;

return { hours, minutes };
}

0 comments on commit d6e6b26

Please sign in to comment.