-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(settings): 9router-style Routing Strategy card + sticky parity #6678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SeaXen
wants to merge
2
commits into
diegosouzapw:release/v3.8.47
Choose a base branch
from
SeaXen:feat/9router-routing-strategy-ui
base: release/v3.8.47
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+506
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/app/(dashboard)/dashboard/settings/components/ProviderAccountRoutingCard.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useEffect, useState } from "react"; | ||
| import { Input, Select } from "@/shared/components"; | ||
| import { useTranslations } from "next-intl"; | ||
| import { ACCOUNT_FALLBACK_STRATEGY_VALUES } from "@/shared/constants/routingStrategies"; | ||
|
|
||
| type ProviderStrategyOverride = { | ||
| fallbackStrategy?: string; | ||
| stickyRoundRobinLimit?: number; | ||
| }; | ||
|
|
||
| type Props = { | ||
| providerKey: string; | ||
| connectionCount: number; | ||
| }; | ||
|
|
||
| const STRATEGY_OPTIONS = ACCOUNT_FALLBACK_STRATEGY_VALUES.filter((v) => | ||
| ["fill-first", "round-robin", "priority", "p2c", "random", "least-used"].includes(v) | ||
| ); | ||
|
|
||
| function clampProviderStickyLimit(raw: string): number { | ||
| const val = parseInt(raw, 10); | ||
| return Math.min(10, Math.max(1, Number.isNaN(val) ? 3 : val)); | ||
| } | ||
|
|
||
| /** Loads/saves the per-provider account-routing override. Extracted out of the | ||
| * component body to keep ProviderAccountRoutingCard's own render function small. */ | ||
| function useProviderAccountRoutingState(providerKey: string) { | ||
| const [strategy, setStrategy] = useState<string>(""); | ||
| const [stickyLimit, setStickyLimit] = useState("3"); | ||
| const [busy, setBusy] = useState(false); | ||
|
|
||
| const load = useCallback(async () => { | ||
| const res = await fetch("/api/settings", { cache: "no-store" }); | ||
| if (!res.ok) return; | ||
| const data = await res.json(); | ||
| const override = ((data?.providerStrategies || {}) as Record<string, ProviderStrategyOverride>)[ | ||
| providerKey | ||
| ]; | ||
| setStrategy(override?.fallbackStrategy || ""); | ||
| setStickyLimit( | ||
| override?.stickyRoundRobinLimit != null ? String(override.stickyRoundRobinLimit) : "" | ||
| ); | ||
| }, [providerKey]); | ||
|
|
||
| useEffect(() => { | ||
| load().catch(console.error); | ||
| }, [load]); | ||
|
|
||
| const save = useCallback( | ||
| async (nextStrategy: string, nextSticky: string) => { | ||
| setBusy(true); | ||
| try { | ||
| const res = await fetch("/api/settings", { cache: "no-store" }); | ||
| if (!res.ok) throw new Error("Failed to fetch current settings"); | ||
| const data = await res.json(); | ||
| const current = (data?.providerStrategies || {}) as Record< | ||
| string, | ||
| ProviderStrategyOverride | ||
| >; | ||
| const override: ProviderStrategyOverride = {}; | ||
| if (nextStrategy) override.fallbackStrategy = nextStrategy; | ||
| if (nextStrategy === "round-robin" && nextSticky !== "") { | ||
| override.stickyRoundRobinLimit = clampProviderStickyLimit(nextSticky); | ||
| } | ||
| const updated = { ...current }; | ||
| if (Object.keys(override).length === 0) delete updated[providerKey]; | ||
| else updated[providerKey] = override; | ||
| const patchRes = await fetch("/api/settings", { | ||
| method: "PATCH", | ||
| headers: { "Content-Type": "application/json" }, | ||
| body: JSON.stringify({ providerStrategies: updated }), | ||
| }); | ||
| if (!patchRes.ok) throw new Error("Failed to save provider routing settings"); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } finally { | ||
| setBusy(false); | ||
| } | ||
| }, | ||
| [providerKey] | ||
| ); | ||
|
|
||
| return { strategy, setStrategy, stickyLimit, setStickyLimit, busy, save }; | ||
| } | ||
|
|
||
| export default function ProviderAccountRoutingCard({ providerKey, connectionCount }: Props) { | ||
| const t = useTranslations("settings"); | ||
| const { strategy, setStrategy, stickyLimit, setStickyLimit, busy, save } = | ||
| useProviderAccountRoutingState(providerKey); | ||
|
|
||
| if (connectionCount < 2) return null; | ||
|
|
||
| return ( | ||
| <div className="mb-4 rounded-lg border border-border/60 bg-black/[0.02] dark:bg-white/[0.02] p-3"> | ||
| <p className="text-sm font-medium">{t("providerAccountRoutingTitle")}</p> | ||
| <p className="text-xs text-text-muted mb-3">{t("providerAccountRoutingDesc")}</p> | ||
| <div className="grid grid-cols-1 sm:grid-cols-2 gap-3"> | ||
| <Select | ||
| label={t("providerRoutingStrategy")} | ||
| disabled={busy} | ||
| value={strategy} | ||
| onChange={(e) => { | ||
| const v = e.target.value; | ||
| setStrategy(v); | ||
| save(v, stickyLimit).catch(console.error); | ||
| }} | ||
| > | ||
| <option value="">{t("providerRoutingInheritGlobal")}</option> | ||
| {STRATEGY_OPTIONS.map((opt) => ( | ||
| <option key={opt} value={opt}> | ||
| {opt} | ||
| </option> | ||
| ))} | ||
| </Select> | ||
| {strategy === "round-robin" && ( | ||
| <Input | ||
| label={t("stickyLimit")} | ||
| type="number" | ||
| min={1} | ||
| max={10} | ||
| disabled={busy} | ||
| value={stickyLimit || "3"} | ||
| onChange={(e) => setStickyLimit(e.target.value)} | ||
| onBlur={() => save(strategy, stickyLimit).catch(console.error)} | ||
| /> | ||
| )} | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function
clampStickyRoundRobinTargetLimitis used inresolveComboStickyRoundRobinLimitbut is not imported or defined in this file. This will cause a compilation or runtime error. Please ensure it is imported, or if it is defined inopen-sse/services/combo.ts, move its definition torrState.ts(or a shared utility file) to avoid a circular dependency betweencombo.tsandrrState.ts.