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

Add notification when storage is running low #2148

Merged
merged 6 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -77,6 +77,7 @@ export interface Notification {
title: string
createdAt: number
onClick?: () => void
dismissOnClick?: boolean
}

const NotificationsDropdown = () => {
Expand Down
21 changes: 18 additions & 3 deletions packages/files-ui/src/Components/Layouts/AppHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useState } from "react"
import { createStyles, makeStyles, useThemeSwitcher } from "@chainsafe/common-theme"
import clsx from "clsx"
import { Link, Typography, ChainsafeFilesLogo, HamburgerMenu, Button, SearchIcon } from "@chainsafe/common-components"
import { Link, Typography, ChainsafeFilesLogo, HamburgerMenu, Button, SearchIcon, useHistory } from "@chainsafe/common-components"
import { ROUTE_LINKS } from "../FilesRoutes"
import SearchModule from "../Modules/SearchModule"
import { Trans } from "@lingui/macro"
Expand All @@ -10,6 +10,7 @@ import { CSFTheme } from "../../Themes/types"
import { useFilesApi } from "../../Contexts/FilesApiContext"
import BetaModal from "../Elements/BetaModal"
import NotificationsDropdown from "../Elements/Notifications/NotificationsDropdown"
import { useBilling } from "../../Contexts/BillingContext"

const useStyles = makeStyles(
({ palette, animation, breakpoints, constants, zIndex }: CSFTheme) => {
Expand Down Expand Up @@ -140,11 +141,13 @@ interface IAppHeader {

const AppHeader = ({ navOpen, setNavOpen }: IAppHeader) => {
const { desktop } = useThemeSwitcher()
const { isBillingEnabled } = useBilling()
const classes = useStyles()
const { isLoggedIn, secured } = useFilesApi()
const { publicKey, isNewDevice, shouldInitializeAccount } = useThresholdKey()
const [searchActive, setSearchActive] = useState(false)
const [isBetaModalOpen, setIsBetaModalOpen] = useState(false)
const { redirect } = useHistory()

const onReportBugClick = useCallback(() => {
window.open(ROUTE_LINKS.DiscordInvite, "_blank")
Expand All @@ -154,6 +157,10 @@ const AppHeader = ({ navOpen, setNavOpen }: IAppHeader) => {
setIsBetaModalOpen(true)
}, [])

const onUpgradeClick = useCallback(() => {
redirect(ROUTE_LINKS.SettingsPath("plan"))
}, [redirect])

return (
<header
className={clsx(classes.root, {
Expand Down Expand Up @@ -190,14 +197,22 @@ const AppHeader = ({ navOpen, setNavOpen }: IAppHeader) => {
>
<Trans>Report a bug</Trans>
</Button>
<Button
{!isBillingEnabled && <Button
data-posthog="Join-beta"
variant="tertiary"
size="small"
onClick={onJoinBetaClick}
>
<Trans>Need more storage?</Trans>
</Button>
</Button>}
{isBillingEnabled && <Button
data-posthog="Upgrade"
variant="tertiary"
size="small"
onClick={onUpgradeClick}
>
<Trans>Upgrade</Trans>
</Button>}
</section>
<section>
<NotificationsDropdown />
Expand Down
21 changes: 20 additions & 1 deletion packages/files-ui/src/Contexts/BillingContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,21 @@ const BillingProvider = ({ children }: BillingContextProps) => {
const { filesApiClient, isLoggedIn, accountRestricted } = useFilesApi()
const { redirect } = useHistory()
const { addNotification, removeNotification } = useNotifications()
const { refreshBuckets } = useFiles()
const { refreshBuckets, storageSummary } = useFiles()
const [currentSubscription, setCurrentSubscription] = useState<CurrentSubscription | undefined>()
const [defaultCard, setDefaultCard] = useState<Card | undefined>(undefined)
const [invoices, setInvoices] = useState<InvoiceResponse[] | undefined>()
const isPendingInvoice = useMemo(() => currentSubscription?.status === "pending_update", [currentSubscription])
const openInvoice = useMemo(() => invoices?.find((i) => i.status === "open"), [invoices])
const [restrictedNotification, setRestrictedNotification] = useState<string | undefined>()
const [upgradeNotification, setUpgradeNotification] = useState<string | undefined>()
const [unpaidInvoiceNotification, setUnpaidInvoiceNotification] = useState<string | undefined>()
const [cardExpiringNotification, setCardExpiringNotification] = useState<string | undefined>()
const [isBillingEnabled, setIsBillingEnabled] = useState(false)
const shouldProposeUpgrade = useMemo(() => storageSummary
? storageSummary.used_storage > storageSummary.total_storage * 0.75
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@asnaith or @juans-chainsafe if you want to test, just change this line to whatever percentage you want, to see the notification appear.

: false
, [storageSummary])

const refreshInvoices = useCallback(() => {
if (!currentSubscription) return
Expand Down Expand Up @@ -118,6 +123,20 @@ const BillingProvider = ({ children }: BillingContextProps) => {
}
}, [accountRestricted, addNotification, redirect, removeNotification, restrictedNotification])

useEffect(() => {
if (shouldProposeUpgrade && !upgradeNotification) {
tanmoyAtb marked this conversation as resolved.
Show resolved Hide resolved
const notif = addNotification({
createdAt: dayjs().unix(),
title: t`Space running low. Upgrade here.`,
onClick: () => {
redirect(ROUTE_LINKS.SettingsPath("plan"))
},
dismissOnClick: true
})
setUpgradeNotification(notif)
}
}, [addNotification, redirect, shouldProposeUpgrade, upgradeNotification])

useEffect(() => {
if (!!openInvoice && !unpaidInvoiceNotification) {
const notif = addNotification({
Expand Down
16 changes: 10 additions & 6 deletions packages/files-ui/src/Contexts/NotificationsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ const NotificationsContext = React.createContext<INotificationsContext | undefin
const NotificationsProvider = ({ children }: NotificationsContextProps) => {
const [notifications, setNotifications] = useState<Notification[]>([])

const removeNotification = useCallback((id: string) => {
setNotifications(notifications.filter((notification) => notification.id !== id))
}, [notifications])

const addNotification = useCallback((notification: Omit<Notification, "id">) => {
const id = uuidv4()
setNotifications([...notifications, {
...notification,
id,
...notification
onClick: () => {
notification.dismissOnClick && removeNotification(id)
notification.onClick && notification.onClick()
}
}])
return id
}, [notifications])

const removeNotification = useCallback((id: string) => {
setNotifications(notifications.filter((notification) => notification.id !== id))
}, [notifications])
}, [notifications, removeNotification])

return (
<NotificationsContext.Provider
Expand Down