-
-
Notifications
You must be signed in to change notification settings - Fork 892
chore: add ImpactMetricRegistrationContext #11854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { createContext, useContext } from 'react'; | ||
|
|
||
| type ImpactMetricRegistrationContextType = { | ||
| openRegisterDialog: () => void; | ||
| }; | ||
|
|
||
| const ImpactMetricRegistrationContext = createContext< | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not super set on the name here |
||
| ImpactMetricRegistrationContextType | undefined | ||
| >(undefined); | ||
|
|
||
| export const ImpactMetricRegistrationProvider = | ||
| ImpactMetricRegistrationContext.Provider; | ||
|
|
||
| export const useRegisterImpactMetric = () => | ||
| useContext(ImpactMetricRegistrationContext); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,8 @@ import { Typography, styled, Box } from '@mui/material'; | |
| import { PageHeader } from 'component/common/PageHeader/PageHeader.tsx'; | ||
| import { useImpactMetricsOptions } from 'hooks/api/getters/useImpactMetricsMetadata/useImpactMetricsMetadata'; | ||
| import { ImpactMetricModal } from './ImpactMetricModal/ImpactMetricModal.tsx'; | ||
| import { RegisterMetricDialog } from './RegisterMetricDialog/RegisterMetricDialog'; | ||
| import { ImpactMetricRegistrationProvider } from './ImpactMetricRegistrationContext'; | ||
| import { ChartItem } from './ChartItem.tsx'; | ||
| import { PlausibleChartItem } from './PlausibleChartItem.tsx'; | ||
| import { GridLayoutWrapper, type GridItem } from './GridLayoutWrapper.tsx'; | ||
|
|
@@ -31,7 +33,9 @@ const _StyledDragHandle = styled(Box)(({ theme }) => ({ | |
| })); | ||
|
|
||
| export const ImpactMetrics: FC = () => { | ||
| const [modalOpen, setModalOpen] = useState(false); | ||
| const [createChartDialogOpen, setCreateChartDialogOpen] = useState(false); | ||
| const [registerMetricDialogOpen, setRegisterMetricDialogOpen] = | ||
| useState(false); | ||
|
Comment on lines
+36
to
+38
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if they're both true, though? Instead of two independent pieces of state, could we model this as const [openDialog, setOpenDialog] useState<'createChart' | 'registerMetrics' | null>(null)Or something like that? |
||
| const [editingChart, setEditingChart] = useState<ChartConfig | undefined>(); | ||
| const { setToastApiError } = useToast(); | ||
| const plausibleMetricsEnabled = useUiFlag('plausibleMetrics'); | ||
|
|
@@ -54,7 +58,7 @@ export const ImpactMetrics: FC = () => { | |
|
|
||
| const handleAddChart = () => { | ||
| setEditingChart(undefined); | ||
| setModalOpen(true); | ||
| setCreateChartDialogOpen(true); | ||
| trackEvent('impact-metrics', { | ||
| props: { | ||
| eventType: 'global chart modal open', | ||
|
|
@@ -64,7 +68,7 @@ export const ImpactMetrics: FC = () => { | |
|
|
||
| const handleEditChart = (config: ChartConfig) => { | ||
| setEditingChart(config); | ||
| setModalOpen(true); | ||
| setCreateChartDialogOpen(true); | ||
| }; | ||
|
|
||
| const handleSaveChart = async (config: Omit<ChartConfig, 'id'>) => { | ||
|
|
@@ -74,7 +78,7 @@ export const ImpactMetrics: FC = () => { | |
| } else { | ||
| await addChart(config); | ||
| } | ||
| setModalOpen(false); | ||
| setCreateChartDialogOpen(false); | ||
| } catch (error) { | ||
| setToastApiError(formatUnknownError(error)); | ||
| } | ||
|
|
@@ -180,14 +184,27 @@ export const ImpactMetrics: FC = () => { | |
| </> | ||
| )} | ||
|
|
||
| <ImpactMetricModal | ||
| open={modalOpen} | ||
| onClose={() => setModalOpen(false)} | ||
| onSave={handleSaveChart} | ||
| initialConfig={editingChart} | ||
| metrics={metricOptions} | ||
| loading={metadataLoading || settingsLoading} | ||
| /> | ||
| <ImpactMetricRegistrationProvider | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could have also achieved the same thing by prop drilling (which is what I initially did), but I'm kinda allergic to it
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a more generic way to solve this, though? We can probably assume that modals should replace one another, and it'd be neat to not have to this over and over again? Also, we're assuming that we don't want that. Are we sure we don't? (we should check with ux) |
||
| value={{ | ||
| openRegisterDialog: () => { | ||
| setCreateChartDialogOpen(false); | ||
| setRegisterMetricDialogOpen(true); | ||
| }, | ||
| }} | ||
| > | ||
| <ImpactMetricModal | ||
| open={createChartDialogOpen} | ||
| onClose={() => setCreateChartDialogOpen(false)} | ||
| onSave={handleSaveChart} | ||
| initialConfig={editingChart} | ||
| metrics={metricOptions} | ||
| loading={metadataLoading || settingsLoading} | ||
| /> | ||
| <RegisterMetricDialog | ||
| open={registerMetricDialogOpen} | ||
| onClose={() => setRegisterMetricDialogOpen(false)} | ||
| /> | ||
| </ImpactMetricRegistrationProvider> | ||
| </> | ||
| ); | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having trouble understanding why this is here? What's it doing?