about summary refs log tree commit diff
path: root/src/components/dialogs/nuxs/index.tsx
blob: 781696070778cd6c34ca390efab308a8a3262d3d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React from 'react'

import {useGate} from '#/lib/statsig/statsig'
import {logger} from '#/logger'
import {
  Nux,
  useNuxs,
  useRemoveNuxsMutation,
  useUpsertNuxMutation,
} from '#/state/queries/nuxs'
import {useSession} from '#/state/session'
import {isSnoozed, snooze, unsnooze} from '#/components/dialogs/nuxs/snoozing'
import {TenMillion} from '#/components/dialogs/nuxs/TenMillion'
import {IS_DEV} from '#/env'

type Context = {
  activeNux: Nux | undefined
  dismissActiveNux: () => void
}

/**
 * If we fail to complete a NUX here, it may show again on next reload,
 * or if prefs state updates. If `true`, this fallback ensures that the last
 * shown NUX won't show again, at least for this session.
 *
 * This is temporary, and only needed for the 10Milly dialog rn, since we
 * aren't snoozing that one in device storage.
 */
let __isSnoozedFallback = false

const queuedNuxs: {
  id: Nux
  enabled(props: {gate: ReturnType<typeof useGate>}): boolean
  /**
   * TEMP only intended for use with the 10Milly dialog rn, since there are no
   * other NUX dialogs configured
   */
  unsafe_disableSnooze: boolean
}[] = [
  {
    id: Nux.TenMillionDialog,
    enabled({gate}) {
      return gate('ten_million_dialog')
    },
    unsafe_disableSnooze: true,
  },
]

const Context = React.createContext<Context>({
  activeNux: undefined,
  dismissActiveNux: () => {},
})

export function useNuxDialogContext() {
  return React.useContext(Context)
}

export function NuxDialogs() {
  const {hasSession} = useSession()
  return hasSession ? <Inner /> : null
}

function Inner() {
  const gate = useGate()
  const {nuxs} = useNuxs()
  const [snoozed, setSnoozed] = React.useState(() => {
    return isSnoozed()
  })
  const [activeNux, setActiveNux] = React.useState<Nux | undefined>()
  const {mutateAsync: upsertNux} = useUpsertNuxMutation()
  const {mutate: removeNuxs} = useRemoveNuxsMutation()

  const snoozeNuxDialog = React.useCallback(() => {
    snooze()
    setSnoozed(true)
  }, [setSnoozed])

  const dismissActiveNux = React.useCallback(() => {
    if (!activeNux) return
    setActiveNux(undefined)
  }, [activeNux, setActiveNux])

  if (IS_DEV && typeof window !== 'undefined') {
    // @ts-ignore
    window.clearNuxDialog = (id: Nux) => {
      if (!IS_DEV || !id) return
      removeNuxs([id])
      unsnooze()
    }
  }

  React.useEffect(() => {
    if (__isSnoozedFallback) return
    if (snoozed) return
    if (!nuxs) return

    for (const {id, enabled, unsafe_disableSnooze} of queuedNuxs) {
      const nux = nuxs.find(nux => nux.id === id)

      // check if completed first
      if (nux && nux.completed) continue

      // then check gate (track exposure)
      if (!enabled({gate})) continue

      // we have a winner
      setActiveNux(id)

      /**
       * TEMP only intended for use with the 10Milly dialog rn, since there are no
       * other NUX dialogs configured
       */
      if (!unsafe_disableSnooze) {
        // immediately snooze for a day
        snoozeNuxDialog()
      }

      // immediately update remote data (affects next reload)
      upsertNux({
        id,
        completed: true,
        data: undefined,
      }).catch(e => {
        logger.error(`NUX dialogs: failed to upsert '${id}' NUX`, {
          safeMessage: e.message,
        })
        /*
         * TEMP only intended for use with the 10Milly dialog rn
         */
        if (unsafe_disableSnooze) {
          __isSnoozedFallback = true
        }
      })

      break
    }
  }, [nuxs, snoozed, snoozeNuxDialog, upsertNux, gate])

  const ctx = React.useMemo(() => {
    return {
      activeNux,
      dismissActiveNux,
    }
  }, [activeNux, dismissActiveNux])

  return (
    <Context.Provider value={ctx}>
      {activeNux === Nux.TenMillionDialog && <TenMillion />}
    </Context.Provider>
  )
}