Repository navigation
[PB-6491]:feat/usage advertisements #112
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5a4e09b
fix: use npm publish instead of yarn publish in GitHub registry workflow
jaaaaavier 9a1ae99
feat: UsageWarningBanner
jaaaaavier 2d36f6b
add to the export files
jaaaaavier a61804e
feat: update sidenav storage component and add stories
jaaaaavier 9248e9f
Update publish-github.yml
jaaaaavier 0c22d9e
add test
jaaaaavier 8d80fdd
Update Sidenav.test.tsx.snap
jaaaaavier 103ae17
Update package.json
jaaaaavier 6323c4a
Merge branch 'master' into feat/usage-warning-banner
jaaaaavier 9aeb41a
update review
jaaaaavier 8f09771
refactor(usageBanner): make storage components presentational
jaaaaavier 039419b
feat: create & use Skeleton component
jaaaaavier 8a4fe17
feat: skeleton
jaaaaavier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/components/feedback/notifications/usageBanner/UsageWarningBanner.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { CloudWarningIcon, XIcon } from '@phosphor-icons/react'; | ||
| import React from 'react'; | ||
| import { Button } from '../../../input/button'; | ||
|
|
||
| export interface UsageWarningBannerProps { | ||
| usage: string; | ||
| limit: string; | ||
| percentage: number; | ||
| titleLabel: string; | ||
| descriptionLabelLine1: string; | ||
| descriptionLabelLine2: string; | ||
| upgradeLabel: string; | ||
| closeButtonLabel?: string; | ||
| onUpgradeClick: () => void; | ||
| onCloseButtonClick: () => void; | ||
| isLoading?: boolean; | ||
| darkMode?: boolean; | ||
| } | ||
|
|
||
| export type StorageLevel = 'low warning' | 'middle warning' | 'high warning'; | ||
|
|
||
| export const getStorageLevel = (percentage: number): StorageLevel => { | ||
| if (percentage >= 95) return 'high warning'; | ||
| if (percentage >= 80) return 'middle warning'; | ||
| return 'low warning'; | ||
| }; | ||
|
|
||
| const STORAGE_LEVEL_STYLES: Record<StorageLevel, { bar: string }> = { | ||
| 'low warning': { bar: 'bg-yellow-60' }, | ||
| 'middle warning': { bar: 'bg-orange-60' }, | ||
| 'high warning': { bar: 'bg-danger' }, | ||
| }; | ||
|
|
||
|
|
||
| const renderWithBold = (text: string): React.ReactNode => | ||
| text.split(/(\*\*[^*]+\*\*)/g).map((segment, index) => { | ||
| if (segment.startsWith('**') && segment.endsWith('**')) { | ||
| return <strong key={index}>{segment.slice(2, -2)}</strong>; | ||
| } | ||
| return <React.Fragment key={index}>{segment}</React.Fragment>; | ||
| }); | ||
|
jaaaaavier marked this conversation as resolved.
Outdated
|
||
|
|
||
| const UsageWarningBanner: React.FC<UsageWarningBannerProps> = ({ | ||
| usage, | ||
| limit, | ||
| percentage, | ||
| titleLabel, | ||
| descriptionLabelLine1, | ||
| descriptionLabelLine2, | ||
| upgradeLabel, | ||
| closeButtonLabel = 'Close', | ||
| onUpgradeClick, | ||
| onCloseButtonClick, | ||
| isLoading = true, | ||
| }) => { | ||
| const level = getStorageLevel(percentage); | ||
| const styles = STORAGE_LEVEL_STYLES[level]; | ||
|
|
||
| return ( | ||
| <div className="flex flex-col w-full px-4 py-3 rounded-xl bg-alert border-alert-dark border h-min"> | ||
| <div className="flex flex-col w-full gap-1"> | ||
| <div className="flex flex-row justify-between items-center"> | ||
| <span className="flex flex-row items-center gap-2"> | ||
| <CloudWarningIcon weight="fill" className="size-5 text-yellow-60" /> | ||
| <p className="text-sm font-semibold text-gray-80">{titleLabel}</p> | ||
| </span> | ||
| <button | ||
| type="button" | ||
| aria-label={closeButtonLabel} | ||
| onClick={onCloseButtonClick} | ||
| className="flex items-center justify-center" | ||
| > | ||
| <XIcon className="size-5 text-gray-53 cursor-pointer" /> | ||
| </button> | ||
| </div> | ||
| <span> | ||
| <p className="text-xs font-medium text-gray-52">{renderWithBold(descriptionLabelLine1)}</p> | ||
| <p className="text-xs font-medium text-gray-52">{renderWithBold(descriptionLabelLine2)}</p> | ||
| </span> | ||
| </div> | ||
| <div className="flex flex-row items-center gap-6 h-10"> | ||
| <div className="flex flex-1 flex-col gap-1"> | ||
| <div className="flex w-full h-1.5 bg-gray-10 rounded-full"> | ||
| <div className={`${styles.bar} h-full rounded-full`} style={{ width: `${percentage}%` }} /> | ||
| </div> | ||
| {isLoading ? ( | ||
| <div className="flex flex-row items-center gap-2"> | ||
| <div className="h-3 w-8 rounded-lg bg-gray-5 animate-pulse" /> | ||
| <div className="h-3 w-2 rounded-lg bg-gray-5 animate-pulse" /> | ||
| <div className="h-3 w-8 rounded-lg bg-gray-5 animate-pulse" /> | ||
| </div> | ||
| ) : ( | ||
| <span className="flex flex-row gap-1"> | ||
| <p className="text-gray-60 text-xs">{usage}</p> | ||
| <p className="text-gray-60 text-xs">/</p> | ||
| <p className="text-gray-60 text-xs">{limit}</p> | ||
| </span> | ||
| )} | ||
| </div> | ||
| <Button variant="secondary" size="medium" onClick={onUpgradeClick}> | ||
| {upgradeLabel} | ||
| </Button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default UsageWarningBanner; | ||
107 changes: 107 additions & 0 deletions
107
src/components/feedback/notifications/usageBanner/__test__/UsageWarningBanner.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import React from 'react'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { UsageWarningBanner, UsageWarningBannerProps } from '../'; | ||
| import { getStorageLevel } from '../UsageWarningBanner'; | ||
|
|
||
| const renderBanner = (overrides: Partial<UsageWarningBannerProps> = {}) => { | ||
| const props: UsageWarningBannerProps = { | ||
| usage: '10 GB', | ||
| limit: '20 GB', | ||
| percentage: 50, | ||
| titleLabel: 'Storage almost full', | ||
| descriptionLabelLine1: 'You are running out of space', | ||
| descriptionLabelLine2: 'Upgrade to keep saving files', | ||
| upgradeLabel: 'Upgrade now', | ||
| closeButtonLabel: 'Close', | ||
| onUpgradeClick: vi.fn(), | ||
| onCloseButtonClick: vi.fn(), | ||
| isLoading: false, | ||
| ...overrides, | ||
| }; | ||
|
|
||
| return { props, ...render(<UsageWarningBanner {...props} />) }; | ||
| }; | ||
|
|
||
| describe('UsageWarningBanner', () => { | ||
| it('warns with a danger-coloured bar when storage is critically full', () => { | ||
| const { container } = renderBanner({ percentage: 96 }); | ||
|
|
||
| expect(container.querySelector('.bg-danger')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('warns with an orange bar when storage is nearly full', () => { | ||
| const { container } = renderBanner({ percentage: 85 }); | ||
|
|
||
| expect(container.querySelector('.bg-orange-60')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('warns with a yellow bar when storage still has room', () => { | ||
| const { container } = renderBanner({ percentage: 40 }); | ||
|
|
||
| expect(container.querySelector('.bg-yellow-60')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('fills the bar in proportion to the storage used', () => { | ||
| const { container } = renderBanner({ percentage: 73 }); | ||
|
|
||
| const bar = container.querySelector<HTMLElement>('.bg-yellow-60'); | ||
| expect(bar?.style.width).toBe('73%'); | ||
| }); | ||
|
|
||
| it('emphasises the portions of the description wrapped in double asterisks', () => { | ||
| renderBanner({ descriptionLabelLine1: 'You have used **90%** of your storage' }); | ||
|
|
||
| expect(screen.getByText('90%').tagName).toBe('STRONG'); | ||
| }); | ||
|
|
||
| it('shows a loading placeholder instead of the usage figures while data loads', () => { | ||
| const { container } = renderBanner({ isLoading: true }); | ||
|
|
||
| expect(container.querySelectorAll('.animate-pulse').length).toBeGreaterThan(0); | ||
| expect(screen.queryByText('10 GB')).toBeNull(); | ||
| expect(screen.queryByText('20 GB')).toBeNull(); | ||
| }); | ||
|
|
||
| it('shows the used and total storage once data has loaded', () => { | ||
| renderBanner({ isLoading: false }); | ||
|
|
||
| expect(screen.getByText('10 GB')).toBeTruthy(); | ||
| expect(screen.getByText('20 GB')).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('notifies the parent when the user chooses to upgrade', () => { | ||
| const onUpgradeClick = vi.fn(); | ||
| renderBanner({ onUpgradeClick }); | ||
|
|
||
| screen.getByRole('button', { name: 'Upgrade now' }).click(); | ||
|
|
||
| expect(onUpgradeClick).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('notifies the parent when the user dismisses the banner', () => { | ||
| const onCloseButtonClick = vi.fn(); | ||
| renderBanner({ onCloseButtonClick, closeButtonLabel: 'Close' }); | ||
|
|
||
| screen.getByRole('button', { name: 'Close' }).click(); | ||
|
|
||
| expect(onCloseButtonClick).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getStorageLevel', () => { | ||
| it('flags critical usage from ninety-five percent upwards', () => { | ||
| expect(getStorageLevel(95)).toBe('high warning'); | ||
| expect(getStorageLevel(100)).toBe('high warning'); | ||
| }); | ||
|
|
||
| it('flags near-full usage between eighty and ninety-five percent', () => { | ||
| expect(getStorageLevel(80)).toBe('middle warning'); | ||
| expect(getStorageLevel(94)).toBe('middle warning'); | ||
| }); | ||
|
|
||
| it('keeps usage below eighty percent as a low warning', () => { | ||
| expect(getStorageLevel(0)).toBe('low warning'); | ||
| expect(getStorageLevel(79)).toBe('low warning'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { default as UsageWarningBanner } from './UsageWarningBanner'; | ||
| export type { UsageWarningBannerProps } from './UsageWarningBanner'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.