Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds SuitesReminder hook to get a consistent look across platforms for the reminder notification #221

Merged
merged 13 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { type Meta, type StoryObj } from '@storybook/react'
import { expect, screen, userEvent } from '@storybook/test'
import React from 'react'
import { Button, Center, GlobalNavigation, Icon, type INavigationCreateProps, Space } from 'src/components'
import { Button, Center, Flex, GlobalNavigation, Icon, type INavigationCreateProps, Space } from 'src/components'
import { Badge } from 'src/components/data-display/Badge/Badge'
import {
type IGlobalNavigationItem,
type IGlobalNavigationLogo,
} from 'src/components/navigation/GlobalNavigation/GlobalNavigationItems'
import { generateOrgs } from 'src/components/navigation/GlobalNavigation/stories-utils'
import { type INavigationOrg } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSelectorItems'
import { useSuitesReminder } from 'src/hooks/SuitesReminder/useSuitesReminder'

const defaultLogo: IGlobalNavigationLogo = {
label: 'Aqua',
Expand Down Expand Up @@ -995,3 +996,44 @@ export const WorkspaceSearchWithNoResults: Meta<typeof GlobalNavigation> = {
await userEvent.type(searchInput, '123{enter}')
},
}

export const UseSuitesReminderHook: Story = {
render: props => {
const [openNotification, contextHolder] = useSuitesReminder({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!
would be a good place to write some tests too 😉

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're so right. I totally forget about it. Will try some 👍🏼

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

onClose: () => {
alert('Notification closed')
},
onRemindMeLater: () => {
alert('Remind me later')
},
onTakeMeThere: () => {
alert('Take me there')
},
})

return (
<Flex style={{ minHeight: 800, width: 600, border: '1px solid black' }} justify="space-between" vertical={false}>
tibuurcio marked this conversation as resolved.
Show resolved Hide resolved
{contextHolder}
<div>
<GlobalNavigation {...props}></GlobalNavigation>
</div>
<Button onClick={openNotification}>Show Notification</Button>
</Flex>
)
},
args: {
logo: defaultLogo,
tools: defaultTools,
management: defaultManagement,
orgs: defaultOrgs,
navigationButtonItemOptions: {
label: 'Sign Out of mParticle',
onClick: () => {
alert('signing out!')
},
},
onMpHomeClick: () => {
alert('Going to mP!')
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { NavigationCreate } from 'src/components/navigation/GlobalNavigation/Nav
import { WorkspaceSelector } from 'src/components/navigation/GlobalNavigation/WorkspaceSelector/WorkspaceSelector'
import { type IGlobalNavigationItem } from 'src/components/navigation/GlobalNavigation/GlobalNavigationItems'
import { NavigationItem } from 'src/components/navigation/GlobalNavigation/NavigationItem'
import { useSuitesReminder } from 'src/hooks/SuitesReminder/useSuitesReminder'

export interface IGlobalNavigationProps {
logo: IGlobalNavigationLogo
Expand Down Expand Up @@ -93,3 +94,5 @@ export const GlobalNavigation = (props: IGlobalNavigationProps) => {
</Layout>
)
}

GlobalNavigation.useSuitesReminder = useSuitesReminder
3 changes: 3 additions & 0 deletions src/hooks/SuitesReminder/suites-reminder.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.globalNavigation__suitesReminder.globalNavigation__suitesReminder-bottomLeft {
jared-dickman marked this conversation as resolved.
Show resolved Hide resolved
left: calc(var(--nav-width) + var(--margin-xs)) !important;
}
78 changes: 78 additions & 0 deletions src/hooks/SuitesReminder/useSuitesReminder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'src/styles/_variables.css'
import './suites-reminder.css'

tibuurcio marked this conversation as resolved.
Show resolved Hide resolved
import { type ReactNode } from 'react'
import { Button, notification, Space } from 'src/components'
import { FontWeightStrong } from 'src/styles/style'

export interface ISuitesReminderOptions {
onClose: () => void
onRemindMeLater: () => void
onTakeMeThere: () => void
duration?: number
title?: string
message?: string
}

type OpenNotificationFn = () => void
type ContextHolder = ReactNode

export type SuitesReminderHook = [OpenNotificationFn, ContextHolder]

const DefaultReminderDuration = 4.5 // same as antd notification default duration
tibuurcio marked this conversation as resolved.
Show resolved Hide resolved
const DefaultTitle = 'Join the new mParticle Experience!'
const DefaultMessage =
'Managing your data is easier than ever with the new mParticle experience. Try out the latest features with ease, and switch back to the classic experience anytime from the side navigation.'

export const useSuitesReminder = (options: ISuitesReminderOptions): SuitesReminderHook => {
const {
onClose,
onRemindMeLater,
onTakeMeThere,
duration = DefaultReminderDuration,
title = DefaultTitle,
message = DefaultMessage,
} = options

const [notificationApi, contextHolder] = notification.useNotification({
prefixCls: 'globalNavigation__suitesReminder',
duration,
placement: 'bottomLeft',
})

const openNotification = (): void => {
const key = `notification-${Date.now()}`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a bit of a nit on my part, but I think using date conversion for a key might be overkill and might have a performance hit. Why not just create a simple increment function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Haven't given much thought to it but am always concerned with performance. I think in this case we will be fine since it will never be the case of showing more than just a few notifications at a time. Also it's the way Ant suggests in their docs.

const btn = (
<Space>
<Button
type="link"
size="small"
onClick={_event => {
onRemindMeLater()
notificationApi.destroy(key)
}}>
Remind me later
</Button>
<Button
type="primary"
size="small"
onClick={_event => {
onTakeMeThere()
notificationApi.destroy(key)
}}>
Take me there
</Button>
</Space>
)

notificationApi.open({
message: <span style={{ fontWeight: FontWeightStrong }}>{title}</span>,
description: message,
btn,
key,
onClose,
})
}

return [openNotification, contextHolder]
}
Loading