Fix Codex auth import and reset credit details
# Conflicts: # app/(tabs)/codex.tsx # app/(tabs)/index.tsx # app/(tabs)/settings.tsx # hooks/useCodexUsage.ts # lib/api/codexApi.ts # package-lock.json # package.json
This commit is contained in:
+98
-12
@@ -15,6 +15,44 @@ import { LastUpdated } from "@/components/LastUpdated";
|
||||
import { ScreenErrorBoundary } from "@/components/ScreenErrorBoundary";
|
||||
import { COLORS } from "@/lib/constants";
|
||||
import { windowLabel } from "@/lib/timeUtils";
|
||||
import type { CodexResetCredit } from "@/types/codex";
|
||||
|
||||
function formatResetStatus(status?: string) {
|
||||
if (!status) return "Unknown";
|
||||
return status.replace(/_/g, " ").replace(/\b\w/g, (char) => char.toUpperCase());
|
||||
}
|
||||
|
||||
function ResetCreditRow({ credit }: { credit: CodexResetCredit }) {
|
||||
return (
|
||||
<View className="mt-2 rounded-xl bg-white dark:bg-neutral-900 border border-neutral-200 dark:border-neutral-700 p-3">
|
||||
<View className="flex-row items-center justify-between mb-1.5">
|
||||
<Text className="text-sm font-semibold text-neutral-900 dark:text-white">
|
||||
Credit {credit.index}
|
||||
</Text>
|
||||
<View className="px-2 py-0.5 rounded-full bg-green-100 dark:bg-green-900/30">
|
||||
<Text className="text-[11px] font-semibold text-green-700 dark:text-green-300">
|
||||
{formatResetStatus(credit.status)}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
{credit.timeUntilExpiry && (
|
||||
<Text className="text-sm text-neutral-700 dark:text-neutral-300">
|
||||
Expires in {credit.timeUntilExpiry}
|
||||
</Text>
|
||||
)}
|
||||
{credit.expiresAtLocal && (
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400 mt-1">
|
||||
Expires {credit.expiresAtLocal}
|
||||
</Text>
|
||||
)}
|
||||
{credit.grantedAtLocal && (
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400 mt-1">
|
||||
Granted {credit.grantedAtLocal}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
function CodexTabContent() {
|
||||
const {
|
||||
@@ -28,6 +66,9 @@ function CodexTabContent() {
|
||||
clearAuth,
|
||||
} =
|
||||
useCodexUsage();
|
||||
const resetCoupons = usage?.resetCoupons;
|
||||
const availableResetCredits =
|
||||
resetCoupons?.credits?.filter((credit) => credit.status === "available") ?? [];
|
||||
|
||||
return (
|
||||
<SafeAreaView
|
||||
@@ -134,21 +175,25 @@ function CodexTabContent() {
|
||||
<View className="flex-row items-center mb-5">
|
||||
<View className="px-2.5 py-1 rounded-full bg-green-100 dark:bg-green-900/40">
|
||||
<Text className="text-xs font-semibold text-green-700 dark:text-green-300 capitalize">
|
||||
{usage.plan_type}
|
||||
{usage.planType ?? "codex"}
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<UsageStat
|
||||
label={`Primary (${windowLabel(usage.rate_limit.primary_window.limit_window_seconds)})`}
|
||||
percent={usage.rate_limit.primary_window.used_percent}
|
||||
resetAtSeconds={usage.rate_limit.primary_window.reset_at}
|
||||
/>
|
||||
<UsageStat
|
||||
label={`Secondary (${windowLabel(usage.rate_limit.secondary_window.limit_window_seconds)})`}
|
||||
percent={usage.rate_limit.secondary_window.used_percent}
|
||||
resetAtSeconds={usage.rate_limit.secondary_window.reset_at}
|
||||
/>
|
||||
{usage.primary && (
|
||||
<UsageStat
|
||||
label={`Primary (${windowLabel(usage.primary.windowSeconds)})`}
|
||||
percent={usage.primary.usedPercent}
|
||||
resetAtSeconds={usage.primary.resetsAt}
|
||||
/>
|
||||
)}
|
||||
{usage.secondary && (
|
||||
<UsageStat
|
||||
label={`Secondary (${windowLabel(usage.secondary.windowSeconds)})`}
|
||||
percent={usage.secondary.usedPercent}
|
||||
resetAtSeconds={usage.secondary.resetsAt}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* Credits */}
|
||||
<View className="mt-1 p-3.5 rounded-xl bg-neutral-50 dark:bg-neutral-800 border border-neutral-100 dark:border-neutral-700">
|
||||
@@ -162,7 +207,7 @@ function CodexTabContent() {
|
||||
Unlimited
|
||||
</Text>
|
||||
</View>
|
||||
) : usage.credits.has_credits ? (
|
||||
) : usage.credits.hasCredits ? (
|
||||
<Text className="text-sm font-semibold text-neutral-900 dark:text-white">
|
||||
${Number(usage.credits.balance).toFixed(2)} remaining
|
||||
</Text>
|
||||
@@ -170,6 +215,47 @@ function CodexTabContent() {
|
||||
<Text className="text-sm text-neutral-400">No credits</Text>
|
||||
)}
|
||||
</View>
|
||||
|
||||
{resetCoupons && (
|
||||
<View className="mt-3 p-3.5 rounded-xl bg-neutral-50 dark:bg-neutral-800 border border-neutral-100 dark:border-neutral-700">
|
||||
<Text className="text-xs font-semibold uppercase tracking-widest text-neutral-400 mb-2">
|
||||
Reset Credits
|
||||
</Text>
|
||||
<Text className="text-sm font-semibold text-neutral-900 dark:text-white">
|
||||
{resetCoupons.availableCount ?? 0} available
|
||||
</Text>
|
||||
{typeof resetCoupons.totalEarnedCount === "number" && (
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400 mt-1">
|
||||
{resetCoupons.totalEarnedCount} earned total
|
||||
</Text>
|
||||
)}
|
||||
{resetCoupons.nextExpiringCredit?.expiresAtLocal && (
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400 mt-1">
|
||||
Next expires {resetCoupons.nextExpiringCredit.expiresAtLocal}
|
||||
</Text>
|
||||
)}
|
||||
{resetCoupons.nextExpiringCredit?.timeUntilExpiry && (
|
||||
<Text className="text-xs text-neutral-400 dark:text-neutral-500 mt-0.5">
|
||||
In {resetCoupons.nextExpiringCredit.timeUntilExpiry}
|
||||
</Text>
|
||||
)}
|
||||
{availableResetCredits.length > 0 && (
|
||||
<View className="mt-3 pt-3 border-t border-neutral-200 dark:border-neutral-700">
|
||||
<Text className="text-xs font-semibold uppercase tracking-widest text-neutral-400 mb-1">
|
||||
Available Now
|
||||
</Text>
|
||||
{availableResetCredits.map((credit) => (
|
||||
<ResetCreditRow key={`${credit.index}-${credit.expiresAt ?? "none"}`} credit={credit} />
|
||||
))}
|
||||
</View>
|
||||
)}
|
||||
{resetCoupons.source !== "live_api" && (
|
||||
<Text className="text-xs text-neutral-400 dark:text-neutral-500 mt-3">
|
||||
Source: {resetCoupons.sourceDescription}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
<View className="mt-4 border-t border-neutral-100 pt-3 dark:border-neutral-800">
|
||||
<LastUpdated timestamp={lastFetchedAt} />
|
||||
</View>
|
||||
|
||||
+34
-18
@@ -159,17 +159,17 @@ function DashboardTabContent() {
|
||||
</Text>
|
||||
</View>
|
||||
<View className="flex-row items-center gap-x-2">
|
||||
{codex.usage?.plan_type && (
|
||||
{codex.usage?.planType && (
|
||||
<View className="px-2.5 py-1 rounded-full bg-green-100 dark:bg-green-900/30">
|
||||
<Text className="text-xs font-semibold text-green-700 dark:text-green-300 capitalize">
|
||||
{codex.usage.plan_type}
|
||||
{codex.usage.planType}
|
||||
</Text>
|
||||
</View>
|
||||
)}
|
||||
{codex.auth && codex.status === "error" && (
|
||||
<View className="w-2 h-2 rounded-full bg-red-500" />
|
||||
)}
|
||||
{codex.status === "success" && !codex.usage?.plan_type && (
|
||||
{codex.status === "success" && !codex.usage?.planType && (
|
||||
<View className="w-2 h-2 rounded-full bg-green-500" />
|
||||
)}
|
||||
</View>
|
||||
@@ -189,22 +189,22 @@ function DashboardTabContent() {
|
||||
)}
|
||||
{codex.usage && (
|
||||
<View>
|
||||
<UsageRow
|
||||
label={`${windowLabel(codex.usage.rate_limit.primary_window.limit_window_seconds)} window`}
|
||||
percent={codex.usage.rate_limit.primary_window.used_percent}
|
||||
resetAtSeconds={codex.usage.rate_limit.primary_window.reset_at}
|
||||
/>
|
||||
<UsageRow
|
||||
label={`${windowLabel(codex.usage.rate_limit.secondary_window.limit_window_seconds)} window`}
|
||||
percent={
|
||||
codex.usage.rate_limit.secondary_window.used_percent
|
||||
}
|
||||
resetAtSeconds={
|
||||
codex.usage.rate_limit.secondary_window.reset_at
|
||||
}
|
||||
/>
|
||||
{codex.usage.primary && (
|
||||
<UsageRow
|
||||
label={`${windowLabel(codex.usage.primary.windowSeconds)} window`}
|
||||
percent={codex.usage.primary.usedPercent}
|
||||
resetAtSeconds={codex.usage.primary.resetsAt}
|
||||
/>
|
||||
)}
|
||||
{codex.usage.secondary && (
|
||||
<UsageRow
|
||||
label={`${windowLabel(codex.usage.secondary.windowSeconds)} window`}
|
||||
percent={codex.usage.secondary.usedPercent}
|
||||
resetAtSeconds={codex.usage.secondary.resetsAt}
|
||||
/>
|
||||
)}
|
||||
{!codex.usage.credits.unlimited &&
|
||||
codex.usage.credits.has_credits && (
|
||||
codex.usage.credits.hasCredits && (
|
||||
<View className="pt-3 border-t border-neutral-100 dark:border-neutral-800 flex-row items-center gap-x-2">
|
||||
<MaterialIcons name="toll" size={14} color="#a3a3a3" />
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400">
|
||||
@@ -226,6 +226,22 @@ function DashboardTabContent() {
|
||||
</View>
|
||||
)}
|
||||
<View className="mt-3 border-t border-neutral-100 pt-3 dark:border-neutral-800">
|
||||
{!!codex.usage.resetCoupons && (
|
||||
<View className="mb-3">
|
||||
<View className="flex-row items-center gap-x-2">
|
||||
<MaterialIcons name="restart-alt" size={14} color={COLORS.codex} />
|
||||
<Text className="text-xs text-neutral-500 dark:text-neutral-400">
|
||||
{codex.usage.resetCoupons.availableCount ?? 0} reset credits
|
||||
available
|
||||
</Text>
|
||||
</View>
|
||||
{!!codex.usage.resetCoupons.nextExpiringCredit?.timeUntilExpiry && (
|
||||
<Text className="text-xs text-neutral-400 dark:text-neutral-500 mt-1">
|
||||
Next expires in {codex.usage.resetCoupons.nextExpiringCredit.timeUntilExpiry}
|
||||
</Text>
|
||||
)}
|
||||
</View>
|
||||
)}
|
||||
<LastUpdated timestamp={codex.lastFetchedAt} />
|
||||
</View>
|
||||
</View>
|
||||
|
||||
@@ -35,6 +35,12 @@ import { ScreenErrorBoundary } from "@/components/ScreenErrorBoundary";
|
||||
import type { CodexAuth } from "@/types/codex";
|
||||
import Constants from "expo-constants";
|
||||
|
||||
function humanizeImportError(code: string): string {
|
||||
if (code === "DOCUMENT_NOT_READABLE") {
|
||||
return "The selected file could not be read. Try copying auth.json into Files or Downloads and import it again.";
|
||||
}
|
||||
return code;
|
||||
}
|
||||
function SettingRow({
|
||||
icon,
|
||||
label,
|
||||
@@ -160,7 +166,7 @@ function SettingsTabContent() {
|
||||
} catch (e: unknown) {
|
||||
const msg = e instanceof Error ? e.message : "UNKNOWN_ERROR";
|
||||
if (msg !== "PICKER_CANCELLED") {
|
||||
Alert.alert("Import failed", msg);
|
||||
Alert.alert("Import failed", humanizeImportError(msg));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user