@dashx/react 0.3.0 → 0.3.1

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.
Files changed (36) hide show
  1. package/dist/cjs/hooks/index.d.ts +2 -0
  2. package/dist/cjs/hooks/index.js +1 -1
  3. package/dist/cjs/hooks/index.js.map +3 -3
  4. package/dist/cjs/hooks/use-in-app-chat-notifications.d.ts +27 -0
  5. package/dist/cjs/hooks/use-in-app-chat-notifications.js +2 -0
  6. package/dist/cjs/hooks/use-in-app-chat-notifications.js.map +7 -0
  7. package/dist/cjs/hooks/use-in-app-chat.d.ts +4 -1
  8. package/dist/cjs/hooks/use-in-app-chat.js +1 -1
  9. package/dist/cjs/hooks/use-in-app-chat.js.map +3 -3
  10. package/dist/cjs/widgets/InAppChat.d.ts +5 -2
  11. package/dist/cjs/widgets/InAppChat.js +1 -1
  12. package/dist/cjs/widgets/InAppChat.js.map +3 -3
  13. package/dist/cjs/widgets/InAppChatButton.d.ts +1 -1
  14. package/dist/cjs/widgets/InAppChatButton.js +1 -1
  15. package/dist/cjs/widgets/InAppChatButton.js.map +3 -3
  16. package/dist/esm/hooks/index.d.ts +2 -0
  17. package/dist/esm/hooks/index.js +1 -1
  18. package/dist/esm/hooks/index.js.map +2 -2
  19. package/dist/esm/hooks/use-in-app-chat-notifications.d.ts +27 -0
  20. package/dist/esm/hooks/use-in-app-chat-notifications.js +2 -0
  21. package/dist/esm/hooks/use-in-app-chat-notifications.js.map +7 -0
  22. package/dist/esm/hooks/use-in-app-chat.d.ts +4 -1
  23. package/dist/esm/hooks/use-in-app-chat.js +1 -1
  24. package/dist/esm/hooks/use-in-app-chat.js.map +3 -3
  25. package/dist/esm/widgets/InAppChat.d.ts +5 -2
  26. package/dist/esm/widgets/InAppChat.js +1 -1
  27. package/dist/esm/widgets/InAppChat.js.map +3 -3
  28. package/dist/esm/widgets/InAppChatButton.d.ts +1 -1
  29. package/dist/esm/widgets/InAppChatButton.js +1 -1
  30. package/dist/esm/widgets/InAppChatButton.js.map +3 -3
  31. package/package.json +2 -2
  32. package/src/hooks/index.ts +2 -0
  33. package/src/hooks/use-in-app-chat-notifications.ts +117 -0
  34. package/src/hooks/use-in-app-chat.ts +31 -4
  35. package/src/widgets/InAppChat.tsx +33 -5
  36. package/src/widgets/InAppChatButton.tsx +72 -28
@@ -1,8 +1,10 @@
1
- import React from 'react';
1
+ import React, { useCallback, useRef, useState } from 'react';
2
2
  import { MessageCircle } from 'lucide-react';
3
3
 
4
4
  import { Button, Flex, Popover, Theme, type ThemeProps } from '../components';
5
- import InAppChat, { type InAppChatWrapperProps } from './InAppChat.js';
5
+ import { InAppChat, type InAppChatWrapperProps } from './InAppChat.js';
6
+ import useInAppChat from '../hooks/use-in-app-chat.js';
7
+ import useInAppChatNotifications from '../hooks/use-in-app-chat-notifications.js';
6
8
 
7
9
  type LauncherPosition = 'bottom-right' | 'bottom-left' | 'top-right' | 'top-left';
8
10
 
@@ -24,35 +26,77 @@ const POSITION_CLASS: Record<LauncherPosition, string> = {
24
26
  const InAppChatButton = ({
25
27
  identityId,
26
28
  idempotencyKey,
29
+ initialMessage,
30
+ notifications = true,
27
31
  position = 'bottom-right',
28
32
  theme,
29
33
  ...props
30
- }: InAppChatButtonProps) => (
31
- <Theme {...theme}>
32
- <Flex className={`fixed z-50 ${POSITION_CLASS[position]}`}>
33
- <Popover.Root>
34
- <Popover.Trigger>
35
- <Button shape="square" roundness="full" variant="fill" size="large">
36
- <MessageCircle />
37
- </Button>
38
- </Popover.Trigger>
39
- {/* Radix unmounts Content while closed, so the chat hook (start →
40
- subscribe → fetch) runs only once the visitor opens the launcher —
41
- no empty conversation is created on page load. */}
42
- <Popover.Content spacing="large" width="350px" height="450px">
43
- <InAppChat
44
- identityId={identityId}
45
- idempotencyKey={idempotencyKey}
46
- borderless
47
- withChatHeader
48
- withPopoverClose
49
- {...props}
50
- />
51
- </Popover.Content>
52
- </Popover.Root>
53
- </Flex>
54
- </Theme>
55
- );
34
+ }: InAppChatButtonProps) => {
35
+ const [isOpen, setIsOpen] = useState(false);
36
+
37
+ // Latch: once the launcher has been opened, keep the chat hook active so its
38
+ // channel subscription persists while the panel is closed — that's what lets
39
+ // us notify on replies that land while the panel is shut. Before the first
40
+ // open the hook is idle, so no conversation is created on page load.
41
+ const hasOpenedRef = useRef(false);
42
+ if (isOpen) hasOpenedRef.current = true;
43
+ const enabled = hasOpenedRef.current;
44
+
45
+ const open = useCallback(() => setIsOpen(true), []);
46
+
47
+ const { handleInboundMessage, requestPermissionOnOpen } = useInAppChatNotifications({
48
+ isOpen,
49
+ enabled: notifications !== false,
50
+ options: typeof notifications === 'object' ? notifications : undefined,
51
+ onActivate: open,
52
+ });
53
+
54
+ // Request notification permission inside the open gesture (synchronously), so
55
+ // the prompt runs within the browser's transient user activation — requesting
56
+ // from an effect after the click unwinds is ignored on some browsers.
57
+ const handleOpenChange = useCallback(
58
+ (nextOpen: boolean) => {
59
+ if (nextOpen) requestPermissionOnOpen();
60
+ setIsOpen(nextOpen);
61
+ },
62
+ [requestPermissionOnOpen],
63
+ );
64
+
65
+ const chat = useInAppChat({
66
+ identityId,
67
+ idempotencyKey,
68
+ initialMessage,
69
+ enabled,
70
+ onInboundMessage: handleInboundMessage,
71
+ });
72
+
73
+ return (
74
+ <Theme {...theme}>
75
+ <Flex className={`fixed z-50 ${POSITION_CLASS[position]}`}>
76
+ <Popover.Root open={isOpen} onOpenChange={handleOpenChange}>
77
+ <Popover.Trigger>
78
+ <Button shape="square" roundness="full" variant="fill" size="large">
79
+ <MessageCircle />
80
+ </Button>
81
+ </Popover.Trigger>
82
+ {/* The chat hook lives at the launcher level (above the popover) so its
83
+ subscription survives the panel closing — required to notify on
84
+ replies that arrive while the panel is shut. It activates lazily on
85
+ first open, so no empty conversation is created on page load. */}
86
+ <Popover.Content spacing="large" width="350px" height="450px">
87
+ <InAppChat
88
+ {...chat}
89
+ borderless
90
+ withChatHeader
91
+ withPopoverClose
92
+ {...props}
93
+ />
94
+ </Popover.Content>
95
+ </Popover.Root>
96
+ </Flex>
97
+ </Theme>
98
+ );
99
+ };
56
100
 
57
101
  export type { InAppChatButtonProps };
58
102