import { useMemo } from "react"; import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; import { normalizeProviderAccentColor, resolveProviderInstanceDisplayName, shouldShowInstanceBadge, } from "@t3tools/client-runtime/state/provider-instance-display"; import type { EnvironmentId, ProviderDriverKind } from "@t3tools/contracts"; import type { ThreadListProvider } from "../../state/thread-list-environments"; /** What a thread row needs to draw the provider glyph and its account badge. */ export interface ThreadRowProviderInstance { readonly driverKind: ProviderDriverKind; readonly displayName: string; readonly accentColor?: string | undefined; readonly showBadge: boolean; } /** * Resolve the provider instance a thread runs on, scoped to the thread's own * environment: default instance ids are the driver slug, so the same id * names a different account on every server. */ export function resolveThreadProviderInstance( providers: ReadonlyArray | undefined, thread: EnvironmentThreadShell, ): ThreadRowProviderInstance | null { if (providers === undefined) return null; const instanceId = thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId; const snapshot = providers.find((provider) => provider.instanceId === instanceId); if (!snapshot) return null; const entry = { driverKind: snapshot.driver, displayName: resolveProviderInstanceDisplayName(snapshot), accentColor: normalizeProviderAccentColor(snapshot.accentColor), }; return { ...entry, showBadge: shouldShowInstanceBadge( entry, providers.map((provider) => ({ driverKind: provider.driver })), ), }; } /** * Builds a resolver handing out reference-stable `ThreadRowProviderInstance` * objects. `resolveThreadProviderInstance` builds a fresh object per call, * which breaks the memoized row's props comparison on every parent render — * the result only depends on (environment, instance id), so one cache per * provider-list generation keeps each row's `providerInstance` prop stable * until the instance behind the row actually changes. */ export function createThreadRowProviderInstanceResolver( providersByEnvironmentId: ReadonlyMap>, ): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null { const cache = new Map(); return (thread) => { const instanceId = thread.runtime?.providerInstanceId ?? thread.modelSelection.instanceId; const cacheKey = `${thread.environmentId}|${instanceId ?? ""}`; const cached = cache.get(cacheKey); if (cached !== undefined) return cached; const resolved = resolveThreadProviderInstance( providersByEnvironmentId.get(thread.environmentId), thread, ); cache.set(cacheKey, resolved); return resolved; }; } /** List-scoped wrapper: one cache per provider-list generation. */ export function useThreadRowProviderInstanceResolver( providersByEnvironmentId: ReadonlyMap>, ): (thread: EnvironmentThreadShell) => ThreadRowProviderInstance | null { return useMemo( () => createThreadRowProviderInstanceResolver(providersByEnvironmentId), [providersByEnvironmentId], ); }