Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
"use client";
/**
* useReorderByAvailability — extracted from useProviderConnections (file-size
* ratchet: useProviderConnections.ts is frozen at 954 lines; this feature
* pushed it to 974) to keep the god-file from growing.
*
* Owns the "Reorder by availability" toolbar action: sorts a provider's
* connections so available ones float to the top and unavailable ones sink
* to the bottom (stable sort — see `sortConnectionsByAvailability`), then
* persists the new order via the same per-connection priority PUT endpoint
* `handleSwapPriority` already uses in useProviderConnections.
*
* Cycle-safe: imports only from leaf modules. No import from
* ProviderDetailPageClient or useProviderConnections.
*/
import { useState } from "react";
import {
buildPriorityUpdates,
persistPriorityUpdates,
sortConnectionsByAvailability,
} from "../components/connectionRowHelpers";
import type { ConnectionRowConnection } from "../components/ConnectionRow";
/** Minimal surface of the notification store this hook needs. */
interface ReorderNotifier {
error: (message: string) => void;
}
export interface UseReorderByAvailabilityParams {
connections: ConnectionRowConnection[];
setConnections: (
updater:
ConnectionRowConnection[] | ((prev: ConnectionRowConnection[]) => ConnectionRowConnection[])
) => void;
fetchConnections: () => Promise<void>;
notify: ReorderNotifier;
t: (key: string, params?: Record<string, unknown>) => string;
}
export interface UseReorderByAvailabilityReturn {
reorderingByAvailability: boolean;
handleReorderByAvailability: () => Promise<void>;
}
export function useReorderByAvailability({
connections,
setConnections,
fetchConnections,
notify,
t,
}: UseReorderByAvailabilityParams): UseReorderByAvailabilityReturn {
const [reorderingByAvailability, setReorderingByAvailability] = useState(false);
/**
* Reorder every connection for this provider by availability: connections
* whose effective status is active/success move to the top, the rest move
* to the bottom, each group keeping its existing relative order (stable
* sort — see `sortConnectionsByAvailability`). Persists the new order as
* sequential `priority` values via the same PUT endpoint `handleSwapPriority`
* already uses, then re-fetches from the server so the UI never runs ahead
* of persisted state on a partial failure (#2558 upstream: fzrilsh).
*/
const handleReorderByAvailability = async () => {
if (reorderingByAvailability || (connections as any[]).length < 2) return;
setReorderingByAvailability(true);
const sorted = sortConnectionsByAvailability(connections as any[]);
setConnections(sorted as ConnectionRowConnection[]);
try {
// 1-based: a 0-based `priority: 0` on the first row was rejected (400).
await persistPriorityUpdates(buildPriorityUpdates(sorted));
await fetchConnections();
} catch (error) {
console.log("Error reordering connections by availability:", error);
notify.error(t("reorderByAvailabilityError"));
await fetchConnections();
} finally {
setReorderingByAvailability(false);
}
};
return { reorderingByAvailability, handleReorderByAvailability };
}