Skip to content

Commit

Permalink
chore: add mixpanel singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
0xApotheosis committed Jan 9, 2024
1 parent 25a77a6 commit 6a9ba46
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
VITE_FOO=bar
VITE_FEATURE_MIXPANEL=true
VITE_MIXPANEL_TOKEN=faf859177254ec1a02752132c2f73954
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"devDependencies": {
"@types/inquirer": "^9.0.7",
"@types/mixpanel-browser": "^2.48.1",
"@types/node": "^20.10.7",
"@types/react": "^18.2.43",
"@types/react-dom": "^18.2.17",
Expand Down
4 changes: 3 additions & 1 deletion src/components/SelectPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import { useCallback, useMemo } from 'react'
import { FaArrowRightArrowLeft } from 'react-icons/fa6'
import { useNavigate } from 'react-router-dom'
import { BTCImage, ETHImage } from 'lib/const'
import { getMixPanel } from 'lib/mixpanelSingleton'

import { AssetSelection } from './AssetSelection'

export const SelectPair = () => {
const mixpanel = getMixPanel()
const navigate = useNavigate()
const switchIcon = useMemo(() => <FaArrowRightArrowLeft />, [])
const handleSubmit = useCallback(() => {
navigate('/input')
}, [navigate])
}, [mixpanel, navigate])

const handleFromAssetClick = useCallback(() => {
console.info('asset click')
Expand Down
18 changes: 18 additions & 0 deletions src/lib/mixpanelSingleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Mixpanel from 'mixpanel-browser'

export type MixPanelType = typeof Mixpanel

// don't export me, access me through the getter
let _mixPanel: typeof Mixpanel | undefined = undefined

// we need to be able to access this outside react
export const getMixPanel = (): MixPanelType | undefined => {
const mixPanelEnabled = import.meta.env.VITE_MIXPANEL_ENABLED
if (!mixPanelEnabled) return
if (_mixPanel) return _mixPanel
const mixpanelToken = import.meta.env.VITE_MIXPANEL_TOKEN
Mixpanel.init(mixpanelToken)
_mixPanel = Mixpanel // identify once per session
_mixPanel.identify()
return _mixPanel
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2502,6 +2502,11 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.202.tgz#f09dbd2fb082d507178b2f2a5c7e74bd72ff98f8"
integrity sha512-OvlIYQK9tNneDlS0VN54LLd5uiPCBOp7gS5Z0f1mjoJYBrtStzgmJBxONW3U6OZqdtNzZPmn9BS/7WI7BFFcFQ==

"@types/mixpanel-browser@^2.48.1":
version "2.48.1"
resolved "https://registry.npmjs.org/@types/mixpanel-browser/-/mixpanel-browser-2.48.1.tgz#ccab3d1fe3d10b18191171e884dcf061cfbf11d5"
integrity sha512-qcckCw9znV98KUkvImglkiVd303Fnn1NTpbKkboQS0bB1SbkNc0Qzz6M9AsS6MaKSzINN4e5z28qUBsK9Wzm5g==

"@types/node@*", "@types/node@^20.10.7":
version "20.10.7"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.10.7.tgz#40fe8faf25418a75de9fe68a8775546732a3a901"
Expand Down

0 comments on commit 6a9ba46

Please sign in to comment.