Skip to content

Commit

Permalink
Add notification when storage is running low (#2148)
Browse files Browse the repository at this point in the history
* notification

* 75%

* lingui extract

* redirect to form when not beta tester yet

* lingui extract

Co-authored-by: GitHub Actions <actions@github.com>
  • Loading branch information
Tbaut and actions-user authored May 26, 2022
1 parent c67ac4f commit 47db353
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 10 deletions.
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
25 changes: 24 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
: false
, [storageSummary])

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

useEffect(() => {
if (shouldProposeUpgrade && !upgradeNotification) {
const notif = addNotification({
createdAt: dayjs().unix(),
title: isBillingEnabled
? t`Space running low. Upgrade here.`
: t`Space running low. Join the beta to upgrade.`,
onClick: () => {
isBillingEnabled
? redirect(ROUTE_LINKS.SettingsPath("plan"))
: window.open(ROUTE_LINKS.SubscriptionWhitelistForm, "_blank")
},
dismissOnClick: true
})
setUpgradeNotification(notif)
}
}, [addNotification, isBillingEnabled, 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
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ msgstr "Es ist etwas schief gelaufen. Wir konnten Ihre Datei nicht hochladen"
msgid "Sort By:"
msgstr ""

msgid "Space running low. Join the beta to upgrade."
msgstr ""

msgid "Space running low. Upgrade here."
msgstr ""

msgid "Start Upload"
msgstr "Hochladen starten"

Expand Down Expand Up @@ -1036,6 +1042,9 @@ msgstr ""
msgid "Update your credit card"
msgstr ""

msgid "Upgrade"
msgstr ""

msgid "Upload"
msgstr ""

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/en/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,12 @@ msgstr "Something went wrong. We couldn't upload your file"
msgid "Sort By:"
msgstr "Sort By:"

msgid "Space running low. Join the beta to upgrade."
msgstr "Space running low. Join the beta to upgrade."

msgid "Space running low. Upgrade here."
msgstr "Space running low. Upgrade here."

msgid "Start Upload"
msgstr "Start Upload"

Expand Down Expand Up @@ -1039,6 +1045,9 @@ msgstr "Update credit card"
msgid "Update your credit card"
msgstr "Update your credit card"

msgid "Upgrade"
msgstr "Upgrade"

msgid "Upload"
msgstr "Upload"

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,12 @@ msgstr "Algo salió mal. No pudimos subir tu archivo"
msgid "Sort By:"
msgstr ""

msgid "Space running low. Join the beta to upgrade."
msgstr ""

msgid "Space running low. Upgrade here."
msgstr ""

msgid "Start Upload"
msgstr "Iniciar la subida"

Expand Down Expand Up @@ -1040,6 +1046,9 @@ msgstr ""
msgid "Update your credit card"
msgstr ""

msgid "Upgrade"
msgstr ""

msgid "Upload"
msgstr ""

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,12 @@ msgstr "Un problème est survenu. Nous n’avons pas pu téléverser votre fichi
msgid "Sort By:"
msgstr "Trier par :"

msgid "Space running low. Join the beta to upgrade."
msgstr ""

msgid "Space running low. Upgrade here."
msgstr ""

msgid "Start Upload"
msgstr "Démarrer le téléversement"

Expand Down Expand Up @@ -1040,6 +1046,9 @@ msgstr "Mettre à jour la carte de crédit"
msgid "Update your credit card"
msgstr "Mettre à jour votre carte de crédit"

msgid "Upgrade"
msgstr ""

msgid "Upload"
msgstr "Téléverser"

Expand Down
9 changes: 9 additions & 0 deletions packages/files-ui/src/locales/no/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,12 @@ msgstr ""
msgid "Sort By:"
msgstr ""

msgid "Space running low. Join the beta to upgrade."
msgstr ""

msgid "Space running low. Upgrade here."
msgstr ""

msgid "Start Upload"
msgstr ""

Expand Down Expand Up @@ -1036,6 +1042,9 @@ msgstr ""
msgid "Update your credit card"
msgstr ""

msgid "Upgrade"
msgstr ""

msgid "Upload"
msgstr ""

Expand Down

0 comments on commit 47db353

Please sign in to comment.