1.23.4 • Published 13 days ago

@leaflink/vue-notification-feed v1.23.4

Weekly downloads
-
License
MIT
Repository
github
Last release
13 days ago

vue-notification-feed

A set of components for integrating Knock in-app notifications into a Vue application.

version downloads MIT License semantic-release Commitizen friendly

Note: these components are currently designed to be used in conjunction with the Knock in-app feed channel, and via Vue for web only.

Knock documentation

Table of Contents

Installation

npm install @leaflink/vue-notification-feed

Configuration

To configure the feed you will need:

  1. A public API key (found in the Knock dashboard)
  2. A feed channel ID (found in the Knock dashboard)
  3. A user ID (this should be unique to each user)
  4. A user auth token for production environments with enhanced security mode enabled

Usage

You can integrate the feed into your app as follows:

<script setup lang="ts">
import {
  KnockFeedProvider,
  NotificationFeedPopover,
  NotificationIconButton,
} from "@leaflink/vue-notification-feed";
import { useUserStore } from "@/stores/user";

// Required CSS import, unless you're fully overriding the styles
import "@leaflink/vue-notification-feed/style.css";

const userStore = useUserStore();
</script>

<template>
  <KnockFeedProvider
    apiKey="{import.meta.env.VITE_KNOCK_PUBLIC_API_KEY}"
    feedId="{import.meta.env.VITE_KNOCK_FEED_ID}"
    userId="{userStore.id}"
  >
    <Dropdown close-manually>
      <template #toggle="{ toggle }">
        <NotificationIconButton @click="toggle" />
      </template>
      <template #default="{ dismiss }">
        <NotificationFeedPopover @keydown.esc="dismiss" />
      </template>
    </Dropdown>
  </KnockFeedProvider>
</template>

If you want to display notifications as a listing page rather then a popover:

<script setup lang="ts">
import {
  KnockFeedProvider,
  NotificationFeed,
} from "@leaflink/vue-notification-feed";
import { useUserStore } from "@/stores/user";

// Required CSS import, unless you're fully overriding the styles
import "@leaflink/vue-notification-feed/style.css";

const userStore = useUserStore();
</script>

<template>
  <KnockFeedProvider
    apiKey="{import.meta.env.VITE_KNOCK_PUBLIC_API_KEY}"
    feedId="{import.meta.env.VITE_KNOCK_FEED_ID}"
    userId="{userStore.id}"
  >
    <NotificationFeed />
  </KnockFeedProvider>
</template>

Headless usage

Alternatively, if you don't want to use our components you can render the feed in a headless mode using our composables:

<script setup lang="ts">
import {
  useAuthenticatedKnockClient,
  useNotifications,
} from "@leaflink/vue-notification-feed";
import { useUserStore } from "@/stores/user";

const store = useUserStore();
const knockClient = useAuthenticatedKnockClient(
  import.meta.env.VITE_KNOCK_PUBLIC_API_KEY,
  store.id
);
const feed = useNotifications(knockClient, import.meta.env.VITE_KNOCK_FEED_ID);
const store = useFeedStore();

feed.value.on("items.received.*", (payload) => {
  console.log("items.received.*", payload, payload.items);
});
feed.value.fetch();
</script>

<template>
  <p>Total notifications: {{ store.metadata.total_count }}</p>
  <p>Total unread: {{ store.metadata.unread_count }}</p>

  <template
    v-for="item in store.items"
    :key="item.id"
    :item="item"
    :feed="feed"
  >
    <div v-for="block in item.blocks" v-html="block.rendered" />
  </template>
</template>

Headless usage w/ refs

In the event you can't initialize the feed synchronously (i.e. you're waiting for the user's id to be populated by an Auth0 call), you can pass refs to the composables and they will adjust accordingly.

Important:

  • You won't be able to use a tool like vue-zustand though, so you'll need to define your own refs.

In this example, you can expect store.id to be undefined to begin with and then eventually get set to the users id.

<script setup lang="ts">
import {
  useAuthenticatedKnockClient,
  useNotifications,
  FeedItem,
  FeedMetadata,
} from "@leaflink/vue-notification-feed";
import { useUserStore } from "@/stores/user";

const store = useUserStore();
const knockClient = useAuthenticatedKnockClient(
  import.meta.env.VITE_KNOCK_PUBLIC_API_KEY,
  store.id
);
const feed = useNotifications(knockClient, import.meta.env.VITE_KNOCK_FEED_ID);

const items = ref<FeedItem[]>([]);
const metadata = ref<FeedMetadata>({});

watch(feed, () => {
  if (!feed.value) return;

  feed.value.on("items.received.*", (payload) => {
    items.value = payload.items;
    metadata.value = payload.metadata;
  });
  feed.value.fetch();
});
</script>

Enhanced security mode

On production environments, we have Enhanced security mode enabled. So it's necessary to provide a user auth token to knock.

!TIP Please follow the documentation instructions on how to generate this token.

Component version

<script setup lang="ts">
const userToken = "my-user-token";
</script>

<template>
  <KnockFeedProvider [...] :user-token="userToken">
    <!-- your code here -->
  </KnockFeedProvider>
</template>

Headless version

<script setup lang="ts">
import {
  useAuthenticatedKnockClient,
  useNotifications,
} from "@leaflink/vue-notification-feed";
import { useUserStore } from "@/stores/user";

const store = useUserStore();
const userToken = "my-user-token";
const knockClient = useAuthenticatedKnockClient(
  import.meta.env.VITE_KNOCK_PUBLIC_API_KEY,
  store.id,
  userToken
);
</script>

!WARNING Be aware that if a knock environment doesn't have Enhanced security mode enabled, the request for that environment will fail. Make sure to provide the user token only for environments that has enhanced security enabled.

Related links

Contribution

Contribution guidelines

1.23.4

13 days ago

1.23.3

1 month ago

1.23.2

1 month ago

1.23.1

1 month ago

1.23.0

1 month ago

1.22.0

2 months ago

1.21.1

2 months ago

1.21.0

2 months ago

1.20.3

3 months ago

1.20.2

4 months ago

1.20.1

4 months ago

1.20.0

4 months ago

1.19.1

4 months ago

1.18.1

5 months ago

1.19.0

5 months ago

1.18.0

5 months ago

1.17.4

5 months ago

1.17.2

5 months ago

1.17.3

5 months ago

1.17.1

5 months ago

1.17.0

5 months ago

1.16.0

5 months ago

1.14.0

5 months ago

1.15.0

5 months ago

1.2.0

8 months ago

1.10.2

7 months ago

1.6.0

7 months ago

1.1.1

9 months ago

1.11.1

6 months ago

1.9.0

7 months ago

1.5.0

7 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.10.1

7 months ago

1.10.0

7 months ago

1.12.3

6 months ago

1.12.2

6 months ago

1.12.1

6 months ago

1.12.0

6 months ago

1.8.1

7 months ago

1.8.0

7 months ago

1.4.0

7 months ago

1.11.0

6 months ago

1.13.1

6 months ago

1.13.0

6 months ago

1.7.0

7 months ago

1.3.0

8 months ago

1.1.0

2 years ago

1.0.0

2 years ago