@xcelsior/ui-chat 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ import { useCallback, useState } from 'react';
2
+
3
+ export type ChatWidgetState = 'open' | 'minimized' | 'closed' | 'undefined';
4
+
5
+ export interface UseChatWidgetStateOptions {
6
+ /**
7
+ * Controlled state. When provided, the component is controlled.
8
+ */
9
+ state?: ChatWidgetState;
10
+ /**
11
+ * Default state for uncontrolled mode
12
+ * @default 'minimized'
13
+ */
14
+ defaultState?: ChatWidgetState;
15
+ /**
16
+ * Callback when state changes
17
+ */
18
+ onStateChange?: (state: ChatWidgetState) => void;
19
+ }
20
+
21
+ export interface UseChatWidgetStateReturn {
22
+ /**
23
+ * Current state of the widget
24
+ */
25
+ currentState: ChatWidgetState;
26
+ /**
27
+ * Function to update the state
28
+ */
29
+ setState: (newState: ChatWidgetState) => void;
30
+ /**
31
+ * Whether the state is controlled
32
+ */
33
+ isControlled: boolean;
34
+ }
35
+
36
+ /**
37
+ * Hook to manage chat widget state (controlled vs uncontrolled)
38
+ * Encapsulates the logic for handling both controlled and uncontrolled state patterns
39
+ */
40
+ export function useChatWidgetState({
41
+ state: controlledState,
42
+ defaultState = 'minimized',
43
+ onStateChange,
44
+ }: UseChatWidgetStateOptions): UseChatWidgetStateReturn {
45
+ // Handle controlled vs uncontrolled state
46
+ const [uncontrolledState, setUncontrolledState] = useState<ChatWidgetState>(defaultState);
47
+ const isControlled = controlledState !== undefined && controlledState !== 'undefined';
48
+ const currentState = isControlled ? controlledState : uncontrolledState;
49
+
50
+ // State setter that works for both controlled and uncontrolled modes
51
+ const setState = useCallback(
52
+ (newValue: ChatWidgetState) => {
53
+ // Update internal state if uncontrolled
54
+ if (!isControlled) {
55
+ setUncontrolledState(newValue);
56
+ }
57
+ // Always call the change handler if provided
58
+ onStateChange?.(newValue);
59
+ },
60
+ [isControlled, onStateChange]
61
+ );
62
+
63
+ return {
64
+ currentState,
65
+ setState,
66
+ isControlled,
67
+ };
68
+ }
package/src/index.tsx CHANGED
@@ -1,6 +1,6 @@
1
1
  // Main components
2
2
  export { ChatWidget } from './components/ChatWidget';
3
- import type { ChatWidgetProps } from './components/ChatWidget';
3
+ export type { ChatWidgetProps } from './components/ChatWidget';
4
4
  export { Chat } from './components/Chat';
5
5
 
6
6
  // Individual components (for custom implementations)
@@ -16,6 +16,12 @@ export { useWebSocket } from './hooks/useWebSocket';
16
16
  export { useMessages } from './hooks/useMessages';
17
17
  export { useFileUpload } from './hooks/useFileUpload';
18
18
  export { useTypingIndicator } from './hooks/useTypingIndicator';
19
+ export { useChatWidgetState } from './hooks/useChatWidgetState';
20
+ export type {
21
+ ChatWidgetState,
22
+ UseChatWidgetStateOptions,
23
+ UseChatWidgetStateReturn,
24
+ } from './hooks/useChatWidgetState';
19
25
 
20
26
  // Utilities
21
27
  export { fetchMessages } from './utils/api';
@@ -40,7 +46,7 @@ import type {
40
46
  ConversationChannel,
41
47
  } from './types';
42
48
 
43
- export {
49
+ export type {
44
50
  IUser,
45
51
  IMessage,
46
52
  IConversation,
@@ -57,5 +63,4 @@ export {
57
63
  ConversationStatus,
58
64
  ConversationPriority,
59
65
  ConversationChannel,
60
- ChatWidgetProps,
61
66
  };