11<script setup>
22import { Inbox } from " lucide-vue-next" ;
33import Knock from " @knocklabs/client" ;
4+ import { useToast } from " @/components/ui/toast/use-toast" ;
5+ const { toast } = useToast ();
46const runtimeConfig = useRuntimeConfig ();
57const knockClient = new Knock (runtimeConfig .public .knockPublicApiKey );
6- knockClient .authenticate (runtimeConfig .public .knockUserId );
8+ knockClient .authenticate (runtimeConfig .public .knockUserId , undefined , {
9+ logLevel: " debug" ,
10+ });
711const knockFeed = knockClient .feeds .initialize (
812 runtimeConfig .public .knockFeedChannelId ,
913 {
@@ -15,6 +19,7 @@ const feed = ref({});
1519await knockFeed .fetch ();
1620const feedState = knockFeed .getState ();
1721feed .value = feedState;
22+ knockFeed .listenForUpdates ();
1823
1924const feedItems = computed (() => {
2025 return feed .value .items .filter ((item ) => ! item .archived_at );
@@ -27,7 +32,23 @@ knockFeed.on("items.received.*", ({ items }) => {
2732});
2833
2934knockFeed .on (" items.*" , () => {
30- console .log (" calling items.*" );
35+ feed .value = knockFeed .getState ();
36+ });
37+ knockFeed .on (" items.received.realtime" , ({ items }) => {
38+ items .forEach ((item ) => {
39+ console .log (item);
40+ if (item .data && item .data .showToast ) {
41+ toast ({
42+ title: ` 📨 New feed item at ${ new Date (
43+ item .inserted_at
44+ ).toLocaleString ()} ` ,
45+ description: " Snap! This real-time feed is mind-blowing 🤯" ,
46+ });
47+ }
48+ });
49+ });
50+
51+ knockFeed .on (" items.*" , () => {
3152 feed .value = knockFeed .getState ();
3253});
3354 </script >
@@ -57,23 +78,23 @@ knockFeed.on("items.*", () => {
5778 </Dialog >
5879 </TabsList >
5980 <TabsContent value =" inbox" >
60- <div class =" my-6 flex" >
61- <Button
62- variant =" outline"
63- class =" w-full mr-2"
64- @click =" knockFeed.markAllAsRead()"
65- >
66- Mark all as read
67- </Button >
68- <Button
69- variant =" outline"
70- class =" w-full ml-2"
71- @click =" knockFeed.markAllAsArchived()"
72- >
73- Archive all
74- </Button >
75- </div >
7681 <div v-if =" feedItems.length > 0" >
82+ <div class =" my-6 flex" >
83+ <Button
84+ variant =" outline"
85+ class =" w-full mr-2"
86+ @click =" knockFeed.markAllAsRead()"
87+ >
88+ Mark all as read
89+ </Button >
90+ <Button
91+ variant =" outline"
92+ class =" w-full ml-2"
93+ @click =" knockFeed.markAllAsArchived()"
94+ >
95+ Archive all
96+ </Button >
97+ </div >
7798 <FeedItemCard
7899 v-for =" item in feedItems"
79100 :key =" item.id"
@@ -90,13 +111,11 @@ knockFeed.on("items.*", () => {
90111 </div >
91112
92113 <div
93- class =" flex flex-col items-center my-12 py-12 bg-slate-50 rounded-md"
94- v-if =" feed.items. length < 0"
114+ class =" flex flex-col items-center content-center my-12 py-12 bg-slate-50 rounded-md"
115+ v-if =" !feedItems || feedItems. length === 0"
95116 >
96- <div >
97- <Inbox class =" w-16 h-16" ></Inbox >
98- <p class =" mt-6" >You&apos ; re all caught up</p >
99- </div >
117+ <Inbox class =" w-16 h-16" ></Inbox >
118+ <p class =" mt-6" >You&apos ; re all caught up</p >
100119 </div >
101120 </TabsContent >
102121 <TabsContent value =" archived" >
0 commit comments