Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript
813 B
Raw Blame
"use client";

import { useMountEffect } from "@crm/ui/hooks/use-mount-effect";
import { useRef, useState } from "react";

export function useSearchInput(
	committed: string,
	commit: (value: string) => void,
	delayMs = 250,
) {
	const [state, setState] = useState({ committed, value: committed });
	const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
	let value = state.value;

	if (state.committed !== committed) {
		value = committed;
		setState({ committed, value: committed });
	}

	useMountEffect(() => () => {
		if (timer.current) clearTimeout(timer.current);
	});

	const change = (next: string) => {
		setState({ committed, value: next });
		if (timer.current) clearTimeout(timer.current);
		timer.current = setTimeout(() => commit(next), delayMs);
	};

	return [value, change] as const;
}