Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

948 B
Raw Blame
/**
 * useUserPrompt — React 包装,订阅 userPromptStore 的变化。
 *
 * 真正的 storage 实现在 lib/userPromptStore.ts,本 hook 只负责把它接入 React 状态。
 * 非 React 路径(如 ChatInput 启 session 时透传)应直接用 store 的 getUserPrompt(),
 * 避免无谓的 hook context 依赖。
 */

import { useCallback, useEffect, useState } from 'react';

import {
  getUserPrompt,
  setUserPrompt,
  subscribeUserPrompt,
} from '@/lib/userPromptStore';

export function useUserPrompt(): {
  value: string;
  setValue: (next: string) => void;
} {
  const [value, setValueState] = useState<string>(getUserPrompt);

  const setValue = useCallback((next: string) => {
    setUserPrompt(next);
    // 走 store.setUserPrompt 之后,subscribe 回调会更新本地 state,
    // 这里不重复 setValueState。
  }, []);

  useEffect(() => subscribeUserPrompt(setValueState), []);

  return { value, setValue };
}