-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persist metric filter preference (#849)
* feat: persist metric filter preference * fix: display even if 0 conversions
- Loading branch information
1 parent
a1219c8
commit 1e68198
Showing
5 changed files
with
83 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/user/analytics/analyticsOverview/hooks/usePersistMetricFilter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useCallback, useEffect, useRef, useState } from "react"; | ||
import { Metrics, StatsMetric } from "user/analytics/analyticsOverview/types"; | ||
import _ from "lodash"; | ||
|
||
export function usePersistMetricFilter( | ||
opts: { campaignId?: string; hasConversions?: boolean } = {}, | ||
) { | ||
const baseFilter: Metrics = { | ||
metric1: { key: "views", active: true }, | ||
metric2: { key: "clicks", active: false }, | ||
metric3: { | ||
key: opts.hasConversions ? "conversions" : "dismissals", | ||
active: false, | ||
}, | ||
metric4: { key: "landings", active: false }, | ||
}; | ||
|
||
const [metrics, setMetrics] = useState<Metrics>(); | ||
|
||
useEffect(() => { | ||
const rawFilter = window.localStorage.getItem("metricFilter"); | ||
if (rawFilter) { | ||
setMetrics(JSON.parse(rawFilter)); | ||
} else { | ||
setMetrics(baseFilter); | ||
} | ||
}, [opts.hasConversions]); | ||
|
||
const setMetric = useCallback( | ||
(metric: keyof Metrics, value: keyof StatsMetric, active: boolean) => { | ||
if (!metrics) { | ||
return; | ||
} | ||
|
||
const metricsCopy = _.cloneDeep(metrics); | ||
const selectedMetric = metricsCopy[metric]; | ||
|
||
if (selectedMetric) { | ||
selectedMetric.key = value; | ||
selectedMetric.active = active; | ||
} | ||
|
||
setMetrics({ ...metricsCopy }); | ||
window.localStorage.setItem("metricFilter", JSON.stringify(metricsCopy)); | ||
}, | ||
[metrics], | ||
); | ||
|
||
return { metrics: metrics!, setMetric }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters