π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { useState } from 'react';
import type { SaveStateHandle } from '@/hooks/useSaveState';
import { useUserStore } from '@/store/user';
import { settingsSelectors } from '@/store/user/slices/settings/selectors';
export type ChannelKey = 'crawlerImpls' | 'searchProviders';
export interface ChannelRow {
enabled: boolean;
id: string;
}
/**
* Build the initial ordered rows from the server-provided available channels
* and the user's saved config.
*
* - No saved config β all channels enabled, in server default order.
* - Saved config β enabled channels first (in the user's saved priority order,
* filtered to still-available ids), then the remaining available channels as
* disabled, in server order.
*
* An empty saved array β or a saved array whose ids no longer intersect the
* available set (e.g. the configured providers were disabled/renamed on the
* server) β is treated as "unconfigured" (all enabled). This matches the
* server-side `resolveOrderedChannels` fallback (empty intersection β default
* order), keeps the "at least one enabled" invariant, and avoids rendering every
* channel as disabled while search/crawl actually runs on the default set.
*/
const buildRows = (availableIds: string[], savedOrder: string[] | undefined): ChannelRow[] => {
if (!savedOrder?.length) return availableIds.map((id) => ({ enabled: true, id }));
const enabledIds = savedOrder.filter((id) => availableIds.includes(id));
if (enabledIds.length === 0) return availableIds.map((id) => ({ enabled: true, id }));
const enabledSet = new Set(enabledIds);
const disabledIds = availableIds.filter((id) => !enabledSet.has(id));
return [
...enabledIds.map((id) => ({ enabled: true, id })),
...disabledIds.map((id) => ({ enabled: false, id })),
];
};
/**
* Manage the local order + enabled state for a single channel list and persist
* changes to the corresponding top-level `settings.tool.<channelKey>` field.
*
* Only enabled ids are persisted (as an ordered array = priority). Disabled
* ids are simply absent, matching the `UserChannelPreferences` contract.
*
* Local UI state is seeded ONCE from `availableIds` + `savedOrder` on mount and
* is intentionally NOT re-synced afterwards (avoids the write-back β re-render β
* reset loop). The parent (`ToolSetting`) MUST therefore only mount this
* component after BOTH the available channels have been fetched AND the user
* settings store has hydrated (`isUserStateInit`) β otherwise `savedOrder` reads
* as `undefined` at mount and the saved priority order is lost on a hard refresh.
*/
export const useChannelRows = (
channelKey: ChannelKey,
availableIds: string[],
save: SaveStateHandle['save'],
) => {
const savedOrder = useUserStore((s) => settingsSelectors.currentSettings(s).tool?.[channelKey]);
const updateToolChannels = useUserStore((s) => s.updateToolChannels);
const [rows, setRows] = useState<ChannelRow[]>(() => buildRows(availableIds, savedOrder));
const persist = (nextRows: ChannelRow[]) => {
const enabledIds = nextRows.filter((row) => row.enabled).map((row) => row.id);
return updateToolChannels({ [channelKey]: enabledIds });
};
const reorder = (nextRows: ChannelRow[]) => {
setRows(nextRows);
void save(() => persist(nextRows));
};
const toggle = (id: string, enabled: boolean) => {
const nextRows = rows.map((row) => (row.id === id ? { ...row, enabled } : row));
setRows(nextRows);
void save(() => persist(nextRows));
};
return { reorder, rows, toggle };
};