import { useMemo, useState } from 'react';
import { useDebugFunctions } from '../../api/queries/debug';
import type { FunctionInfo } from '../../api/types';
interface Props {
file: string;
selectedFunction: string | null;
onFunctionChange: (fn_name: string | null) => void;
showFilePath?: boolean;
}
export function FunctionSelector({
file,
selectedFunction,
onFunctionChange,
showFilePath = true,
}: Props) {
const { data: functions, isLoading } = useDebugFunctions(file || null);
const [showClosures, setShowClosures] = useState(false);
const closureCount = useMemo(
() => functions?.filter((fn) => fn.func_kind === 'closure').length ?? 0,
[functions],
);
const visible = useMemo(() => {
if (!functions) return functions;
return showClosures
? functions
: functions.filter((fn) => fn.func_kind !== 'closure');
}, [functions, showClosures]);
return (
{showFilePath && (
File:
{file || 'No file selected'}
)}
{closureCount > 0 && (
)}
);
}
function formatFunctionLabel(fn: FunctionInfo): string {
const sig = `(${fn.param_count} params), L${fn.line}`;
if (fn.func_kind === 'closure' && fn.container) {
return `${fn.name} [closure in ${fn.container}] ${sig}`;
}
if (fn.func_kind === 'closure') {
return `${fn.name} [closure] ${sig}`;
}
return `${fn.name}${sig}`;
}