Liveblocks 1.12: Advanced filtering and custom notifications
Liveblocks 1.12 introduces advanced filtering to threads and rooms, custom notifications in alpha, a new inbox notification thread hook, a new example, and an upgraded Next.js Starter Kit.
Liveblocks 1.12 introduces query-based thread filtering to
Comments, allowing you to create advanced UI components for
filtering threads by their metadata.
To enable filtering by metadata, pass a query option to
useThreads. Only threads
that match the query will be returned, and you can even use a startsWith
option to find partial metadata string matches. In this example, we’re filtering
for unresolved threads that are assigned to a specific string.
Liveblocks 1.12 also enables advanced room filtering on the server, allowing you
to fetch any rooms that match a specific
room ID naming pattern. In
this example we’re filtering for "whiteboard" rooms, and then using
startsWith to only match rooms with IDs that begin with "liveblocks:"
When using Comments, you can choose to render relevant
inbox notifications
for users when threads update. We’re extending these notifications, allowing you
to send any kind of custom notification, and we’ve just released the alpha in
Liveblocks 1.12.
To send a custom notification, call
liveblocks.triggerInboxNotification
on the server. Here’s an example that notifies users about a file upload.
functionFileUploadedNotification(props){const activityData = props.inboxNotification.activities[0].data;const{ user }=useUser(activityData.uploadedBy); return(<InboxNotification.Customtitle={`${user.name} uploaded a new file`}aside={<InboxNotification.AvataruserId={user.id}/>}{...props}><ahref={activityData.url}>🗎 {activityData.name}</a></InboxNotification.Custom>);}
We generally recommend using
<InboxNotification.Custom>
to render notifications, as it allows you to place your content in slots that
work with the existing design system. However, you can also use completely
custom styling, should you choose.
<InboxNotification kinds={{$fileUploaded:(props)=>{const{ name, url }= props.inboxNotification.activities[0].data;return<ahref={url}>{name} has been uploaded</a>;},}}/>
We’ll be sharing more about custom notifications soon, as we continue to work on
it. Make sure to join our Discord server or
follow us on Twitter and
LinkedIn for updates and upcoming
content.
An advanced use case for inbox notifications is building your own custom
components for thread notifications. Previously for these components, you’d
have to implement your own logic to fetch thread information, but we’ve now
created a new
useInboxNotificationThread
hook which will handle this for you.
We’ve built a new Comments example detailing how to build a canvas with
draggable threads. With a quick copy-and-paste, you can add this to your
application—the code and a live demo are available in our
examples gallery!
Our Next.js Starter Kit has been completely rewritten and now
uses the Next.js /app directory. Just to recap, the Next.js Starter Kit is a
great starting point when building your own collaborative application, and
highlights how to use every aspect of Liveblocks.
Previously, a number of files were necessary to synchronize types and
functionality between client, server, and API endpoints, but thanks to
server actions,
type-safe functions now work on both server and client.
import{ getDocument }from"@/lib/actions"; // Can be used in both browser and Node.jsconst{ data, error }=awaitgetDocument({ documentId:"my-document-id",});
Not only has this update greatly shrunk the codebase, but the main bundle size
is almost 50% smaller too.
To download and deploy the Next.js Starter Kit, run the following command.
There are no breaking changes in this update, and you can start using the new
features now. Update every Liveblocks package in your project to the @latest
version to get started.