Skip to content

Commit

Permalink
feat: Show announcement 1h after the feature has been activated
Browse files Browse the repository at this point in the history
To avoid spamming the interface with new users, we're putting a delay between the first opening of the Home and the appearance of the announcements dialog.
  • Loading branch information
cballevre committed Sep 18, 2024
1 parent 3ef7e23 commit 508a115
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
26 changes: 23 additions & 3 deletions src/components/Announcements/Announcements.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC, useState } from 'react'
import React, { FC, useEffect, useState } from 'react'
import { differenceInHours } from 'date-fns'

import flag from 'cozy-flags'
Expand All @@ -11,17 +11,37 @@ import { useAnnouncementsSettings } from 'hooks/useAnnouncementsSettings'
const Announcements: FC = () => {
const config = flag<AnnouncementsConfigFlag>('home.announcements')
const [hasBeenDismissed, setBeenDismissed] = useState(false)
const { values, save } = useAnnouncementsSettings()
const { fetchStatus, values, save } = useAnnouncementsSettings()
const [hasBeenActivated, setBeenActivated] = useState(false)

useEffect(() => {
if (
!values.firstActivatedAt &&
!hasBeenActivated &&
fetchStatus === 'loaded'
) {
save({
firstActivatedAt: new Date().toISOString()
})
setBeenActivated(true)
}
}, [hasBeenActivated, save, values.firstActivatedAt, fetchStatus])

const handleDismiss = (): void => {
setBeenDismissed(true)
}

const hasBeenActivatedForMoreThanAHour = values.firstActivatedAt
? differenceInHours(new Date(), Date.parse(values.firstActivatedAt)) >= 1
: false
const moreThan = config?.delayAfterDismiss ?? 24
const hasBeenDismissedForMoreThan = values.dismissedAt
? differenceInHours(new Date(), Date.parse(values.dismissedAt)) >= moreThan
: true
const canBeDisplayed = !hasBeenDismissed && hasBeenDismissedForMoreThan
const canBeDisplayed =
!hasBeenDismissed &&
hasBeenDismissedForMoreThan &&
hasBeenActivatedForMoreThanAHour
const announcements = useAnnouncements({
canBeDisplayed
})
Expand Down
20 changes: 17 additions & 3 deletions src/hooks/useAnnouncementsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,31 @@ import { useSettings } from 'cozy-client'

const defaultAnnouncements = {
dismissedAt: undefined,
seen: []
seen: [],
firstActivatedAt: undefined
}

const useAnnouncementsSettings = (): {
fetchStatus: string
values: {
dismissedAt?: string
seen: string[]
firstActivatedAt?: string
}
save: (data: { dismissedAt?: string; seen?: string[] }) => void
save: (data: {
dismissedAt?: string
seen?: string[]
firstActivatedAt?: string
}) => void
} => {
const { values, save } = useSettings('home', [
const { query, values, save } = useSettings('home', [
'announcements'
]) as unknown as UseSettingsType

const saveAnnouncement = (data: {
dismissedAt?: string
seen?: string[]
firstActivatedAt?: string
}): void => {
const announcements = {
...(values?.announcements ?? defaultAnnouncements),
Expand All @@ -30,22 +38,28 @@ const useAnnouncementsSettings = (): {
}

return {
fetchStatus: query.fetchStatus,
values: values?.announcements ?? defaultAnnouncements,
save: saveAnnouncement
}
}

interface UseSettingsType {
query: {
fetchStatus: string
}
values?: {
announcements: {
dismissedAt: string
seen: string[]
firstActivatedAt: string
}
}
save: (data: {
announcements: {
dismissedAt?: string
seen: string[]
firstActivatedAt?: string
}
}) => void
}
Expand Down

0 comments on commit 508a115

Please sign in to comment.