File size: 1,143 Bytes
ffcad5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useCallback, useEffect, useState } from 'react';
import {
  readChatInputFollowupsVisible,
  writeChatInputFollowupsVisible,
  CHAT_INPUT_FOLLOWUPS_EVENT,
} from '../utils/chatInputFollowupsPref';

/** On/off for follow-up suggestion chips below the chat input. */
export default function useChatInputFollowupsVisible() {
  const [visible, setVisible] = useState(readChatInputFollowupsVisible);

  useEffect(() => {
    const sync = () => setVisible(readChatInputFollowupsVisible());
    window.addEventListener(CHAT_INPUT_FOLLOWUPS_EVENT, sync);
    window.addEventListener('storage', sync);
    return () => {
      window.removeEventListener(CHAT_INPUT_FOLLOWUPS_EVENT, sync);
      window.removeEventListener('storage', sync);
    };
  }, []);

  const setFollowupsVisible = useCallback((next) => {
    setVisible(writeChatInputFollowupsVisible(next));
  }, []);

  const toggleFollowupsVisible = useCallback(() => {
    setVisible((prev) => writeChatInputFollowupsVisible(!prev));
  }, []);

  return {
    followupsVisible: visible,
    setFollowupsVisible,
    toggleFollowupsVisible,
  };
}