TL;DR
이 짧은 게시물에서는 디자인에 Chakra UI만 사용하고 알림에 Novu만 사용하여 Notion의 내장 알림 기능을 복제하는 완전한 기능의 실시간 알림 받은 편지함 구성 요소를 어떻게 다시 만들었는지 배우게 됩니다.
모습은 이렇습니다.
이 앱의 소스코드 링크와 배포 버전은 포스팅 마지막에 있습니다.
일상적인 Notion 사용자로서 저는 그들의 알림 경험에 정말 감사하고 있으며 이것이 제가 그들의 앱을 그토록 많이 사용하는 이유 중 큰 부분을 차지합니다. 저는 궁금했습니다. Notion의 세련된 알림 시스템과 유사한 받은 편지함을 어떻게 다시 만들 수 있을까요? 알고 보니 Novu의 인앱 알림 구성 요소 덕분에 매우 간단했습니다.
Novu는 최근 풀 스택 알림 구성 요소를 출시했습니다. 이는 사용자 정의가 가능하고 바로 사용할 수 있는 상태 저장형 내장형 React 구성 요소 또는 위젯입니다.
몇 가지 간단한 단계만으로 React 앱에 추가하는 방법은 다음과 같습니다.
Novu 패키지 설치
$ npm install @novu/react
구성요소 가져오기
import { Inbox } from "@novu/react";
앱에서 구성요소 초기화
function Novu() { return ( <Inbox applicationIdentifier="YOUR_APPLICATION_IDENTIFIER" subscriberId="YOUR_SUBSCRIBER_ID" /> ); }
그렇습니다! 이제 모든 기능을 갖춘 인앱 받은 편지함 구성 요소가 생겼습니다.
외형은 정말 멋져 보입니다.
자랑하려는 건 아니지만 Novu의 받은 편지함은 가장 유연하고 사용자 정의가 가능합니다. 스타일링 방법을 확인하고 직접 실험해 보세요.
이 기술에 관심이 있다면 Novu의 공동 창립자인 Dima Grossman이 이 기술을 구축한 방법과 이유에 대한 훌륭한 게시물을 작성했습니다.
받은 편지함이 Notion의 알림 패널처럼 보이길 원하시나요? 문제 없습니다! Notion의 깔끔하고 미니멀한 미학에 맞게 Novu의 알림을 쉽게 포장하고 사용자 정의할 수 있습니다.
내가 한 방법
@novu/react에서 Inbox 구성 요소를 가져오는 대신 각 항목 렌더링을 완전히 제어하기 위해 알림 및 알림 구성 요소를 가져왔습니다.
import { Inbox, Notification, Notifications } from "@novu/react";
사용자 정의를 시작하기 전에 알림 개체의 구조는 다음과 같습니다.
interface Notification = { id: string; subject?: string; body: string; to: Subscriber; isRead: boolean; isArchived: boolean; createdAt: string; readAt?: string | null; archivedAt?: string | null; avatar?: string; primaryAction?: Action; secondaryAction?: Action; channelType: ChannelType; tags?: string[]; data?: Record<string, unknown>; redirect?: Redirect; };
이를 염두에 두고 차크라 UI를 사용하여(Tailwind 클래스를 쟁탈하는 것은 피곤하기 때문에) 각 알림 항목을 디자인했습니다.
Notion에서 영감을 받아 알림 항목을 만든 방법은 다음과 같습니다.
const InboxItem = ({ notification }: { notification: Notification }) => { const [isHovered, setIsHovered] = useState(false); const notificationType = notification.tags?.[0]; return ( <Box p={2} bg="white" position="relative" onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} > <Flex align="flex-start" position="relative"> <VStack spacing={0} position="absolute" top="0" right="0"> {isHovered && ( <Box bg="white" display="flex" gap={1}> {notification.isRead ? ( <IconButton aria-label="Mark as unread" icon={<PiNotificationFill />} onClick={() => notification.unread()} size="sm" variant="ghost" /> ) : ( <IconButton aria-label="Mark as read" icon={<FaRegCheckSquare />} onClick={() => notification.read()} size="sm" variant="ghost" /> )} {notification.isArchived ? ( <IconButton aria-label="Unarchive" icon={<PiNotificationFill />} onClick={() => notification.unarchive()} size="sm" variant="ghost" /> ) : ( <IconButton aria-label="Archive" icon={<FiArchive />} onClick={() => notification.archive()} size="sm" variant="ghost" /> )} </Box> )} </VStack> <Box position="relative" display="flex" alignItems="center" mr="8px" height="26px" > {!notification.isRead && ( <Box> <Box width="8px" height="8px" bg="blue.500" borderRadius="full" /> </Box> )} {notification.avatar !== undefined && ( <Avatar width="24px" height="24px" marginLeft="8px" name={notification.to.firstName} src={notification.avatar || undefined} /> )} </Box> <VStack align="start" spacing="8px" flex="1" mt="3px"> <Flex justify="space-between" width="100%"> <Text fontSize="14px" color="gray.800" fontWeight="600"> {notification.subject} </Text> <Text fontSize="xs" color="gray.400"> {formatTime(notification.createdAt)} </Text> </Flex> {notificationType !== "Mention" && notificationType !== "Comment" && notificationType !== "Invite" && ( <Text fontSize="14px" color="gray.800"> {notification.body} </Text> )} {(notificationType === "Mention" || notificationType === "Comment") && ( <Button variant="ghost" size="sm" leftIcon={<GrDocumentText />} _hover={{ bg: "rgba(0, 0, 0, 0.03)" }} pl="2px" pr="5px" height="25px" > <Text fontSize="14px" color="gray.800" fontWeight="500" backgroundImage="linear-gradient(to right, rgba(55, 53, 47, 0.16) 0%, rgba(55, 53, 47, 0.16) 100%)" backgroundRepeat="repeat-x" backgroundSize="100% 1px" backgroundPosition="0 100%" mr="-2px" > {notification.body} </Text> </Button> )} {notificationType === "Invite" && ( <Button variant="outline" size="md" _hover={{ bg: "rgba(0, 0, 0, 0.03)" }} padding="12px" height="50px" fontSize="14px" width="100%" borderRadius="8px" textAlign="left" border="1px solid rgba(227, 226, 224, 0.5)" justifyContent="space-between" > {notification.body} </Button> )} {notificationType === "Comment" && ( <Box> <Text fontSize="12px" color="rgb(120, 119, 116)" fontWeight="400"> John Doe </Text> <Text fontSize="14px" color="rgb(55, 53, 47)" fontWeight="400"> This is a notification Comment made by John Doe and posted on the page Top Secret Project </Text> </Box> )} <HStack spacing={3}> {notification.primaryAction && ( <Button variant="outline" size="xs" colorScheme="gray" borderRadius="md" borderColor="gray.300" _hover={{ bg: "gray.100" }} paddingRight="8px" paddingLeft="8px" lineHeight="26px" height="26px" > {notification.primaryAction.label} </Button> )} {notification.secondaryAction && ( <Button variant="ghost" size="xs" colorScheme="gray" borderRadius="md" borderColor="gray.300" _hover={{ bg: "gray.100" }} paddingRight="8px" paddingLeft="8px" lineHeight="26px" height="26px" > {notification.secondaryAction.label} </Button> )} </HStack> </VStack> </Flex> </Box> ); };
코드에서 볼 수 있듯이 다음 알림 키를 활용했습니다.
notification.data 개체에는 애플리케이션 로직이 사용자 또는 구독자와 연결하려는 실제 정보가 포함될 수 있습니다. 이러한 유연한 구조를 통해 특정 사용 사례에 맞게 알림을 맞춤화하고 사용자에게 더욱 풍부하고 상황에 맞는 정보를 제공할 수 있습니다.
notification.data 사용 예:
전자상거래 주문 업데이트:
notification.data = { orderId: "ORD-12345", status: "shipped", trackingNumber: "1Z999AA1234567890", estimatedDelivery: "2023-09-25" };
소셜 미디어 상호작용:
notification.data = { postId: "post-789", interactionType: "like", interactingUser: "johndoe", interactionTime: "2023-09-22T14:30:00Z" };
금융거래:
notification.data = { transactionId: "TRX-98765", amount: 150.75, currency: "USD", merchantName: "Coffee Shop", category: "Food & Drink" };
notification.data 개체를 활용하면 애플리케이션의 특정 요구 사항과 원활하게 통합되는 더 유익하고 실행 가능한 알림을 만들 수 있습니다.
이러한 유연성을 통해 사용자에게 필요한 정보를 정확하게 제공하여 사용자 경험과 알림 시스템의 전반적인 효율성을 향상할 수 있습니다.
코드를 자세히 살펴보면 알림 상태를 관리하기 위해 4개의 키 후크가 사용되는 것을 알 수 있습니다.
The novu/react package exposes these hooks, offering enhanced flexibility for managing notification states. It's important to note that these hooks not only update the local state but also synchronize changes with the backend.
These hooks provide a seamless way to:
By utilizing these hooks, you can create more interactive and responsive notification systems in your applications.
I've implemented Notion-inspired sidebar navigation to enhance the similarity to the Notion theme. This design choice aims to capture the essence and aesthetics of Notion's interface, creating a familiar and intuitive environment for users.
For the icons, I've leveraged the versatile react-icons library, which offers a wide range of icon sets to choose from.
$ npm install react-icons
import { FiArchive, FiSearch, FiHome, FiInbox, FiSettings, FiChevronDown } from "react-icons/fi"; import { FaRegCheckSquare, FaUserFriends } from "react-icons/fa"; import { PiNotificationFill } from "react-icons/pi"; import { BsFillFileTextFill, BsTrash } from "react-icons/bs"; import { AiOutlineCalendar } from "react-icons/ai"; import { GrDocumentText } from "react-icons/gr"; const AppContainer = () => { const borderColor = useColorModeValue("gray.200", "gray.700"); const [isInboxOpen, setIsInboxOpen] = useState(true); const toggleInbox = () => { setIsInboxOpen(!isInboxOpen); }; return ( <Flex width="100vw" height="100vh" bg="gray.100" overflow="hidden" justifyContent="center" alignItems="center" style={{ fontFamily: 'ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI Variable Display", "Segoe UI", Helvetica, "Apple Color Emoji", Arial, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"', }} > <Box width="80%" height="80%" bg="white" borderRadius="lg" boxShadow="xl" overflow="hidden" > <Flex height="100%"> {/* Sidebar */} <Box width="240px" bg="rgb(247, 247, 245)" padding="8px" display="flex" flexDirection="column" borderColor={borderColor} borderRightWidth="1px" > <Flex alignItems="center" mb="4px" padding="0.6rem"> <Text fontSize="1.25rem" fontWeight="bold" color="rgb(55, 53, 47)" > <Icon as={NotionIcon} sx={{ width: "20px", height: "20px", marginRight: "8px", display: "inline-block", }} />{" "} Workspace </Text> <IconButton aria-label="User Settings" icon={<FiChevronDown />} variant="ghost" size="sm" /> </Flex> <VStack align="stretch" spacing={1} mb="15px"> <SidebarItem icon={FiSearch} label="Search" /> <SidebarItem icon={FiHome} label="Home" /> <SidebarItem icon={FiInbox} label="Inbox" isActive={isInboxOpen} onClick={toggleInbox} /> <SidebarItem icon={FiSettings} label="Settings & members" /> </VStack> <Text fontSize="xs" fontWeight="bold" color="gray.500" mb={2}> Favorites </Text> <VStack align="stretch" spacing={1} mb="15px"> <SidebarItem icon={FiHome} label="Teamspaces" /> <SidebarItem icon={BsFillFileTextFill} label="Shared" /> </VStack> <Text fontSize="xs" fontWeight="bold" color="gray.500" mb={2}> Private </Text> <VStack align="stretch" spacing={1} mb="15px"> <SidebarItem icon={AiOutlineCalendar} label="Calendar" /> <SidebarItem icon={FaUserFriends} label="Templates" /> <SidebarItem icon={BsTrash} label="Trash" /> </VStack> </Box> // ... (rest of the code)
Another important aspect was time formatting to match how Notion does it:
function formatTime(timestamp: number | string | Date): string { const date = new Date(timestamp); const now = new Date(); const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); const secondsInMinute = 60; const secondsInHour = secondsInMinute * 60; const secondsInDay = secondsInHour * 24; const secondsInWeek = secondsInDay * 7; const secondsInYear = secondsInDay * 365; if (diffInSeconds < secondsInMinute) { return `${diffInSeconds} seconds`; } else if (diffInSeconds < secondsInHour) { const minutes = Math.floor(diffInSeconds / secondsInMinute); return `${minutes} minute${minutes !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInDay) { const hours = Math.floor(diffInSeconds / secondsInHour); return `${hours} hour${hours !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInWeek) { const days = Math.floor(diffInSeconds / secondsInDay); return `${days} day${days !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInYear) { const options: Intl.DateTimeFormatOptions = { month: "short", day: "numeric" }; return date.toLocaleDateString(undefined, options); } else { return date.getFullYear().toString(); } }
Now that we have covered all the pieces, here is the complete code:
'use client' import React, { useState } from 'react'; import { Box, Flex, Text, IconButton, VStack, Avatar, HStack, Link, Icon, useColorModeValue, Button, Heading, } from "@chakra-ui/react"; import { FiArchive, FiSearch, FiHome, FiInbox, FiSettings, FiChevronDown } from "react-icons/fi"; import { FaRegCheckSquare, FaUserFriends } from "react-icons/fa"; import { PiNotificationFill } from "react-icons/pi"; import { BsFillFileTextFill, BsTrash } from "react-icons/bs"; import { AiOutlineCalendar } from "react-icons/ai"; import { GrDocumentText } from "react-icons/gr"; import { Inbox, Notification, Notifications } from "@novu/react"; import { NotionIcon } from "../icons/Notion"; const subscriberId = process.env.NEXT_PUBLIC_SUBSCRIBERID; const applicationIdentifier = process.env.NEXT_PUBLIC_NOVU_CLIENT_APP_ID; const AppContainer = () => { const borderColor = useColorModeValue("gray.200", "gray.700"); const [isInboxOpen, setIsInboxOpen] = useState(true); const toggleInbox = () => { setIsInboxOpen(!isInboxOpen); }; return (); }; // Sidebar Item Component interface SidebarItemProps { icon: React.ElementType; label: string; isActive?: boolean; external?: boolean; onClick?: () => void; } const SidebarItem: React.FC {/* Sidebar */} {/* Main Content Area */} {" "} Workspace } variant="ghost" size="sm" /> Favorites Private {/* Injected Content Behind the Inbox */} {/* Inbox Popover */} {isInboxOpen && ( Notion Inbox Notification Theme Checkout the deployed version now )} ( )} /> = ({ icon, label, isActive = false, external = false, onClick, }) => { return ( ); }; const InboxItem = ({ notification }: { notification: Notification }) => { const [isHovered, setIsHovered] = useState(false); const notificationType = notification.tags?.[0]; return ( <Box p={2} bg="white" position="relative" onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} > <Flex align="flex-start" position="relative"> <VStack spacing={0} position="absolute" top="0" right="0"> {isHovered && ( <Box bg="white" display="flex" gap={1}> {notification.isRead ? ( <IconButton aria-label="Mark as unread" icon={<PiNotificationFill />} onClick={() => notification.unread()} size="sm" variant="ghost" /> ) : ( <IconButton aria-label="Mark as read" icon={<FaRegCheckSquare />} onClick={() => notification.read()} size="sm" variant="ghost" /> )} {notification.isArchived ? ( <IconButton aria-label="Unarchive" icon={<PiNotificationFill />} onClick={() => notification.unarchive()} size="sm" variant="ghost" /> ) : ( <IconButton aria-label="Archive" icon={<FiArchive />} onClick={() => notification.archive()} size="sm" variant="ghost" /> )} </Box> )} </VStack> <Box position="relative" display="flex" alignItems="center" mr="8px" height="26px" > {!notification.isRead && ( <Box> <Box width="8px" height="8px" bg="blue.500" borderRadius="full" /> </Box> )} {notification.avatar !== undefined && ( <Avatar width="24px" height="24px" marginLeft="8px" name={notification.to.firstName} src={notification.avatar || undefined} /> )} </Box> <VStack align="start" spacing="8px" flex="1" mt="3px"> <Flex justify="space-between" width="100%"> <Text fontSize="14px" color="gray.800" fontWeight="600"> {notification.subject} </Text> <Text fontSize="xs" color="gray.400"> {formatTime(notification.createdAt)} </Text> </Flex> {notificationType !== "Mention" && notificationType !== "Comment" && notificationType !== "Invite" && ( <Text fontSize="14px" color="gray.800"> {notification.body} </Text> )} {(notificationType === "Mention" || notificationType === "Comment") && ( <Button variant="ghost" size="sm" leftIcon={<GrDocumentText />} _hover={{ bg: "rgba(0, 0, 0, 0.03)" }} pl="2px" pr="5px" height="25px" > <Text fontSize="14px" color="gray.800" fontWeight="500" backgroundImage="linear-gradient(to right, rgba(55, 53, 47, 0.16) 0%, rgba(55, 53, 47, 0.16) 100%)" backgroundRepeat="repeat-x" backgroundSize="100% 1px" backgroundPosition="0 100%" mr="-2px" > {notification.body} </Text> </Button> )} {notificationType === "Invite" && ( <Button variant="outline" size="md" _hover={{ bg: "rgba(0, 0, 0, 0.03)" }} padding="12px" height="50px" fontSize="14px" width="100%" borderRadius="8px" textAlign="left" border="1px solid rgba(227, 226, 224, 0.5)" justifyContent="space-between" > {notification.body} </Button> )} {notificationType === "Comment" && ( <Box> <Text fontSize="12px" color="rgb(120, 119, 116)" fontWeight="400"> John Doe </Text> <Text fontSize="14px" color="rgb(55, 53, 47)" fontWeight="400"> This is a notification Comment made by John Doe and posted on the page Top Secret Project </Text> </Box> )} <HStack spacing={3}> {notification.primaryAction && ( <Button variant="outline" size="xs" colorScheme="gray" borderRadius="md" borderColor="gray.300" _hover={{ bg: "gray.100" }} paddingRight="8px" paddingLeft="8px" lineHeight="26px" height="26px" > {notification.primaryAction.label} </Button> )} {notification.secondaryAction && ( <Button variant="ghost" size="xs" colorScheme="gray" borderRadius="md" borderColor="gray.300" _hover={{ bg: "gray.100" }} paddingRight="8px" paddingLeft="8px" lineHeight="26px" height="26px" > {notification.secondaryAction.label} </Button> )} </HStack> </VStack> </Flex> </Box> ); }; function formatTime(timestamp: number | string | Date): string { const date = new Date(timestamp); const now = new Date(); const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); const secondsInMinute = 60; const secondsInHour = secondsInMinute * 60; const secondsInDay = secondsInHour * 24; const secondsInWeek = secondsInDay * 7; const secondsInYear = secondsInDay * 365; if (diffInSeconds < secondsInMinute) { return `${diffInSeconds} seconds`; } else if (diffInSeconds < secondsInHour) { const minutes = Math.floor(diffInSeconds / secondsInMinute); return `${minutes} minute${minutes !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInDay) { const hours = Math.floor(diffInSeconds / secondsInHour); return `${hours} hour${hours !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInWeek) { const days = Math.floor(diffInSeconds / secondsInDay); return `${days} day${days !== 1 ? 's' : ''}`; } else if (diffInSeconds < secondsInYear) { const options: Intl.DateTimeFormatOptions = { month: "short", day: "numeric" }; return date.toLocaleDateString(undefined, options); } else { return date.getFullYear().toString(); } } export default AppContainer; {label}
Ready to get customizing? Here’s the source code for the Notion Inbox theme. You can also see and play with it live in our Inbox playground. I also did the same for a Reddit notifications example. Two totally different experiences, but powered by the same underlying component and notifications infrastructure.
위 내용은 Chakra UI 및 Novu를 사용하여 Notion과 유사한 알림 받은 편지함을 구축하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!