Files
Kaname/src/hooks/use-recommended.ts
T
Pascal LinxweilerandClaude Opus 5.5 0247f257fb Recommended files with a quant; floating selection bar
Picking model weights in Add model or Download more also selects the
files that usually go with them (chat template, MTP head, mmproj, ...),
shown in a strip where each can be unchecked. Variants are matched by
quant, otherwise the best single one; kinds already selected aren't
added twice. Which kinds is set in Settings -> Downloads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 23:10:53 +02:00

44 lines
1.6 KiB
TypeScript

"use client";
import { useMemo, useState } from "react";
import { useRecommendedRoles } from "@/components/labels";
import { modelFilePaths, recommendFiles } from "@/lib/recommend";
/**
* Companion files (template, MTP, mmproj, …) for the model weights in `picked`, per the
* "Recommended files" setting. They count as selected unless the user unchecked them (`dismissed`),
* and drop out by themselves when the model file they belong to is deselected.
*/
export function useRecommended(
files: { path: string; size: number }[],
picked: Iterable<string>,
options: { isExcluded?: (path: string) => boolean; have?: string[] } = {},
) {
const { isExcluded, have } = options;
const roles = useRecommendedRoles();
const [dismissed, setDismissed] = useState<Set<string>>(new Set());
const pickedList = useMemo(() => [...picked], [picked]);
const all = useMemo(() => {
const already = new Set(pickedList);
return recommendFiles(files, modelFilePaths(pickedList), roles, [...pickedList, ...(have ?? [])]).filter(
(p) => !already.has(p) && !isExcluded?.(p),
);
}, [files, pickedList, roles, isExcluded, have]);
const active = useMemo(() => new Set(all.filter((p) => !dismissed.has(p))), [all, dismissed]);
/** Check or uncheck recommended paths (unchecking remembers the choice). */
function setOn(paths: string[], on: boolean) {
setDismissed((prev) => {
const next = new Set(prev);
for (const p of paths) {
if (on) next.delete(p);
else next.add(p);
}
return next;
});
}
return { all, active, setOn, reset: () => setDismissed(new Set()) };
}