@greatapps/greatchat-ui 0.1.1 → 0.1.2

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,127 @@
1
+ import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
2
+ import type { Inbox, InboxStats } from "../types";
3
+ import {
4
+ type GchatHookConfig,
5
+ DEFAULT_INBOX_POLLING,
6
+ useGchatClient,
7
+ } from "./types";
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // Queries
11
+ // ---------------------------------------------------------------------------
12
+
13
+ export function useInboxes(
14
+ config: GchatHookConfig,
15
+ statusFilter?: string,
16
+ pollingInterval?: number,
17
+ ) {
18
+ const client = useGchatClient(config);
19
+
20
+ return useQuery({
21
+ queryKey: ["greatchat", "inboxes", config.accountId, statusFilter],
22
+ queryFn: () => {
23
+ const params: Record<string, string> = {};
24
+ if (statusFilter && statusFilter !== "all") {
25
+ params.status = statusFilter;
26
+ }
27
+ return client.listInboxes(config.accountId, params);
28
+ },
29
+ enabled: !!config.accountId && !!config.token,
30
+ refetchInterval: pollingInterval ?? DEFAULT_INBOX_POLLING,
31
+ select: (res) => res.data || [],
32
+ });
33
+ }
34
+
35
+ export function useInbox(config: GchatHookConfig, id: number | null) {
36
+ const client = useGchatClient(config);
37
+
38
+ return useQuery({
39
+ queryKey: ["greatchat", "inbox", config.accountId, id],
40
+ queryFn: () => client.getInbox(config.accountId, id!),
41
+ enabled: !!config.accountId && !!config.token && !!id,
42
+ select: (res) => {
43
+ const d = res.data;
44
+ return (Array.isArray(d) ? d[0] : d) as Inbox;
45
+ },
46
+ });
47
+ }
48
+
49
+ export function useInboxStats(
50
+ config: GchatHookConfig,
51
+ pollingInterval?: number,
52
+ ) {
53
+ const client = useGchatClient(config);
54
+
55
+ return useQuery({
56
+ queryKey: ["greatchat", "inbox-stats", config.accountId],
57
+ queryFn: () => client.getInboxStats(config.accountId),
58
+ enabled: !!config.accountId && !!config.token,
59
+ refetchInterval: pollingInterval ?? DEFAULT_INBOX_POLLING,
60
+ select: (res) => {
61
+ const d = res.data;
62
+ return (Array.isArray(d) ? d[0] : d) as InboxStats;
63
+ },
64
+ });
65
+ }
66
+
67
+ // ---------------------------------------------------------------------------
68
+ // Mutations
69
+ // ---------------------------------------------------------------------------
70
+
71
+ export function useCreateInbox(config: GchatHookConfig) {
72
+ const client = useGchatClient(config);
73
+ const queryClient = useQueryClient();
74
+
75
+ return useMutation({
76
+ mutationFn: (body: { id_channel: number; id_contact: number }) =>
77
+ client.createInbox(config.accountId, body),
78
+ onSuccess: () => {
79
+ queryClient.invalidateQueries({
80
+ queryKey: ["greatchat", "inboxes"],
81
+ });
82
+ queryClient.invalidateQueries({
83
+ queryKey: ["greatchat", "inbox-stats"],
84
+ });
85
+ },
86
+ });
87
+ }
88
+
89
+ export function useUpdateInbox(config: GchatHookConfig) {
90
+ const client = useGchatClient(config);
91
+ const queryClient = useQueryClient();
92
+
93
+ return useMutation({
94
+ mutationFn: ({
95
+ id,
96
+ body,
97
+ }: {
98
+ id: number;
99
+ body: Partial<Pick<Inbox, "status" | "id_agent">>;
100
+ }) => client.updateInbox(config.accountId, id, body),
101
+ onSuccess: () => {
102
+ queryClient.invalidateQueries({
103
+ queryKey: ["greatchat", "inboxes"],
104
+ });
105
+ queryClient.invalidateQueries({
106
+ queryKey: ["greatchat", "inbox-stats"],
107
+ });
108
+ },
109
+ });
110
+ }
111
+
112
+ export function useDeleteInbox(config: GchatHookConfig) {
113
+ const client = useGchatClient(config);
114
+ const queryClient = useQueryClient();
115
+
116
+ return useMutation({
117
+ mutationFn: (id: number) => client.deleteInbox(config.accountId, id),
118
+ onSuccess: () => {
119
+ queryClient.invalidateQueries({
120
+ queryKey: ["greatchat", "inboxes"],
121
+ });
122
+ queryClient.invalidateQueries({
123
+ queryKey: ["greatchat", "inbox-stats"],
124
+ });
125
+ },
126
+ });
127
+ }
package/src/index.ts CHANGED
@@ -18,6 +18,9 @@ export type { GchatClientConfig } from "./client";
18
18
  export { cn } from "./lib/utils";
19
19
  export { groupMessagesByDate, formatDateGroup, formatMessageTime } from "./utils";
20
20
 
21
+ // Hooks
22
+ export * from "./hooks";
23
+
21
24
  // Components
22
25
  export { ChatView, ChatInput, MessageBubble } from "./components";
23
26
  export type { ChatViewProps, ChatInputProps, MessageBubbleProps } from "./components";