|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { ArrowRight, Plus, Zap, CheckCircle2, ExternalLink } from "lucide-react"; |
| 6 | +import { PROVIDERS, type Provider } from "@/lib/providers"; |
| 7 | +import { getActiveProviderId, setActiveProviderId, getProviderKey, getProvidersWithKeys } from "@/lib/settings"; |
| 8 | +import { getProviderLimits } from "@/lib/provider-limits"; |
| 9 | + |
| 10 | +/** |
| 11 | + * One-click provider switcher. Two surfaces: |
| 12 | + * - "inline" → rendered inside rate-limit error panels with actionable buttons |
| 13 | + * - "compact" → rendered in the StatusBar / page header as a dropdown |
| 14 | + * |
| 15 | + * Logic: |
| 16 | + * - Providers with saved keys ≠ active: instant-switch buttons. |
| 17 | + * - Providers without keys but with free tier: "Add free key" CTAs to /settings. |
| 18 | + * - Free-tier providers are highlighted with green/pos coloring. |
| 19 | + */ |
| 20 | + |
| 21 | +interface Props { |
| 22 | + variant?: "inline" | "compact"; |
| 23 | + /** Reason for the switcher to be visible — affects copy at top. */ |
| 24 | + reason?: "rate-limit" | "default"; |
| 25 | + onSwitch?: (providerId: string) => void; |
| 26 | +} |
| 27 | + |
| 28 | +export function ProviderSwitcher({ variant = "inline", reason = "default", onSwitch }: Props) { |
| 29 | + const [activeId, setActiveId] = useState<string>(""); |
| 30 | + const [configured, setConfigured] = useState<string[]>([]); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const refresh = () => { |
| 34 | + setActiveId(getActiveProviderId() ?? ""); |
| 35 | + setConfigured(getProvidersWithKeys()); |
| 36 | + }; |
| 37 | + refresh(); |
| 38 | + window.addEventListener("ados:provider-changed", refresh); |
| 39 | + window.addEventListener("storage", refresh); |
| 40 | + return () => { |
| 41 | + window.removeEventListener("ados:provider-changed", refresh); |
| 42 | + window.removeEventListener("storage", refresh); |
| 43 | + }; |
| 44 | + }, []); |
| 45 | + |
| 46 | + function switchTo(p: Provider) { |
| 47 | + setActiveProviderId(p.id); |
| 48 | + setActiveId(p.id); |
| 49 | + onSwitch?.(p.id); |
| 50 | + } |
| 51 | + |
| 52 | + // Partition providers |
| 53 | + const others = PROVIDERS.filter((p) => p.id !== activeId); |
| 54 | + const otherConfigured = others.filter((p) => configured.includes(p.id)); |
| 55 | + const otherUnconfigured = others.filter((p) => !configured.includes(p.id)); |
| 56 | + // Free-tier providers we recommend first when no key is set |
| 57 | + const freeRecommendations = otherUnconfigured.filter((p) => getProviderLimits(p.id)?.has_free_tier); |
| 58 | + |
| 59 | + if (variant === "compact") { |
| 60 | + return ( |
| 61 | + <div className="flex items-center gap-1.5 flex-wrap"> |
| 62 | + {otherConfigured.length > 0 ? ( |
| 63 | + otherConfigured.slice(0, 3).map((p) => ( |
| 64 | + <button |
| 65 | + key={p.id} |
| 66 | + onClick={() => switchTo(p)} |
| 67 | + className="text-[10px] font-mono uppercase tracking-ui-wide text-ink-muted hover:text-live transition border border-base-700 hover:border-live/40 px-1.5 py-0.5" |
| 68 | + title={`Switch active provider to ${p.name}`} |
| 69 | + > |
| 70 | + → {p.name} |
| 71 | + </button> |
| 72 | + )) |
| 73 | + ) : ( |
| 74 | + <Link href="/settings" className="text-[10px] font-mono uppercase tracking-ui-wide text-info hover:underline"> |
| 75 | + + add provider |
| 76 | + </Link> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <div className="space-y-3"> |
| 84 | + {otherConfigured.length > 0 ? ( |
| 85 | + <div> |
| 86 | + <div className="text-[10px] font-mono uppercase tracking-ui-mega text-ink-faint mb-2"> |
| 87 | + Switch to a configured provider · one click |
| 88 | + </div> |
| 89 | + <div className="grid sm:grid-cols-2 gap-2"> |
| 90 | + {otherConfigured.map((p) => { |
| 91 | + const lim = getProviderLimits(p.id); |
| 92 | + const isFree = lim?.has_free_tier; |
| 93 | + return ( |
| 94 | + <button |
| 95 | + key={p.id} |
| 96 | + onClick={() => switchTo(p)} |
| 97 | + className={`flex items-start gap-2 px-3 py-2.5 border text-left transition ${ |
| 98 | + isFree |
| 99 | + ? "border-pos/40 bg-pos/5 hover:bg-pos/10 hover:border-pos" |
| 100 | + : "border-base-600 bg-base-900/40 hover:bg-base-800/60 hover:border-base-500" |
| 101 | + }`} |
| 102 | + > |
| 103 | + <CheckCircle2 size={12} className={`shrink-0 mt-0.5 ${isFree ? "text-pos" : "text-live"}`} /> |
| 104 | + <div className="flex-1 min-w-0"> |
| 105 | + <div className="text-sm font-medium text-ink">{p.name}</div> |
| 106 | + {lim ? ( |
| 107 | + <div className={`text-[10px] font-mono uppercase tracking-ui-wide ${isFree ? "text-pos" : "text-ink-faint"} mt-0.5`}> |
| 108 | + {isFree ? "FREE" : "PAID"} · {lim.summary.split("·").slice(0, 2).join("·").replace(/^FREE/, "").replace(/^Paid/, "").trim()} |
| 109 | + </div> |
| 110 | + ) : null} |
| 111 | + </div> |
| 112 | + <ArrowRight size={11} className="text-ink-faint shrink-0 mt-1" /> |
| 113 | + </button> |
| 114 | + ); |
| 115 | + })} |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ) : null} |
| 119 | + |
| 120 | + {otherConfigured.length === 0 && freeRecommendations.length > 0 ? ( |
| 121 | + <div> |
| 122 | + <div className="text-[10px] font-mono uppercase tracking-ui-mega text-pos mb-2 flex items-center gap-1.5"> |
| 123 | + <Zap size={11} /> Add a free-tier provider in 30 seconds |
| 124 | + </div> |
| 125 | + <div className="grid sm:grid-cols-2 gap-2"> |
| 126 | + {freeRecommendations.slice(0, 4).map((p) => { |
| 127 | + const lim = getProviderLimits(p.id)!; |
| 128 | + return ( |
| 129 | + <Link |
| 130 | + key={p.id} |
| 131 | + href={`/settings#provider-${p.id}`} |
| 132 | + className="flex items-start gap-2 px-3 py-2.5 border border-pos/40 bg-pos/5 hover:bg-pos/10 hover:border-pos transition" |
| 133 | + > |
| 134 | + <Plus size={12} className="text-pos shrink-0 mt-0.5" /> |
| 135 | + <div className="flex-1 min-w-0"> |
| 136 | + <div className="text-sm font-medium text-ink">{p.name}</div> |
| 137 | + <div className="text-[10px] font-mono uppercase tracking-ui-wide text-pos mt-0.5"> |
| 138 | + FREE · {lim.summary.split("·").slice(0, 2).join("·").replace(/^FREE/, "").trim()} |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + <ExternalLink size={11} className="text-ink-faint shrink-0 mt-1" /> |
| 142 | + </Link> |
| 143 | + ); |
| 144 | + })} |
| 145 | + </div> |
| 146 | + <p className="text-[11px] text-ink-muted mt-2 leading-relaxed"> |
| 147 | + Each one takes a free key (no credit card on Groq / Cerebras / Gemini / OpenRouter). You'll be in /settings to paste it and verify — back here in 30 seconds. |
| 148 | + </p> |
| 149 | + </div> |
| 150 | + ) : null} |
| 151 | + |
| 152 | + {otherConfigured.length > 0 && freeRecommendations.length > 0 ? ( |
| 153 | + <div className="pt-2 border-t border-base-700"> |
| 154 | + <Link href="/settings" className="text-[11px] font-mono uppercase tracking-ui-wide text-info hover:underline"> |
| 155 | + + add more providers in settings |
| 156 | + </Link> |
| 157 | + </div> |
| 158 | + ) : null} |
| 159 | + |
| 160 | + {otherConfigured.length === 0 && freeRecommendations.length === 0 ? ( |
| 161 | + <Link href="/settings" className="btn-primary inline-flex"> |
| 162 | + <Plus size={11} /> Add another provider in Settings |
| 163 | + </Link> |
| 164 | + ) : null} |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments