Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🐛 fix: Correct Next Refill Date Logic for Balance Settings (#9121)

Ihsan Soydemirb9bc3123d6cb
diff --git a/client/src/components/Nav/SettingsTabs/Balance/AutoRefillSettings.tsx b/client/src/components/Nav/SettingsTabs/Balance/AutoRefillSettings.tsx
index d2c78928ac..38c1ae37e0 100644
--- a/client/src/components/Nav/SettingsTabs/Balance/AutoRefillSettings.tsx
+++ b/client/src/components/Nav/SettingsTabs/Balance/AutoRefillSettings.tsx
@@ -47,6 +47,56 @@ const addIntervalToDate = (
return result;
};
+/**
+ * Calculates the next future refill date.
+ * This function determines how many intervals have passed since the base date
+ * and advances to the next eligible date. It correctly handles both fixed-duration
+ * intervals (e.g., days, weeks) and variable-duration intervals (e.g., months).
+ *
+ * @param {Date} baseDate - The starting date for the calculation (e.g., the last refill date).
+ * @param {number} value - The numeric value of the interval (e.g., 7 for 7 days).
+ * @param {'seconds'|'minutes'|'hours'|'days'|'weeks'|'months'} unit - The unit of time for the interval.
+ * @returns {Date} The next calculated future refill date.
+ */
+function getNextFutureInterval(
+ baseDate: Date,
+ value: number,
+ unit: 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months',
+): Date {
+ const now = new Date();
+
+ if (baseDate > now) {
+ return addIntervalToDate(baseDate, value, unit);
+ }
+
+ if (unit === 'months') {
+ let nextRefillDate = new Date(baseDate);
+ while (nextRefillDate <= now) {
+ nextRefillDate = addIntervalToDate(nextRefillDate, value, unit);
+ }
+ return nextRefillDate;
+ }
+
+ const intervalInMs = {
+ seconds: value * 1000,
+ minutes: value * 1000 * 60,
+ hours: value * 1000 * 60 * 60,
+ days: value * 1000 * 60 * 60 * 24,
+ weeks: value * 1000 * 60 * 60 * 24 * 7,
+ }[unit];
+
+ if (intervalInMs <= 0) {
+ return addIntervalToDate(baseDate, value, unit);
+ }
+
+ const timeSinceBase = now.getTime() - baseDate.getTime();
+ const intervalsPassed = Math.floor(timeSinceBase / intervalInMs);
+ const intervalsToNext = intervalsPassed + 1;
+ const nextRefillTime = baseDate.getTime() + intervalsToNext * intervalInMs;
+
+ return new Date(nextRefillTime);
+}
+
const AutoRefillSettings: React.FC<AutoRefillSettingsProps> = ({
lastRefill,
refillAmount,
@@ -57,7 +107,7 @@ const AutoRefillSettings: React.FC<AutoRefillSettingsProps> = ({
const lastRefillDate = lastRefill ? new Date(lastRefill) : null;
const nextRefill = lastRefillDate
- ? addIntervalToDate(lastRefillDate, refillIntervalValue, refillIntervalUnit)
+ ? getNextFutureInterval(lastRefillDate, refillIntervalValue, refillIntervalUnit)
: null;
// Return the localized unit based on singular/plural values