Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: context field usage frontend (#3938)
- Loading branch information
Showing
6 changed files
with
204 additions
and
11 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
frontend/src/component/context/ContextFieldUsage/ContextFieldUsage.test.tsx
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,55 @@ | ||
import { render } from '../../../utils/testRenderer'; | ||
import { screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { testServerRoute, testServerSetup } from '../../../utils/testServer'; | ||
import { UIProviderContainer } from '../../providers/UIProvider/UIProviderContainer'; | ||
import { ContextFieldUsage } from './ContextFieldUsage'; | ||
|
||
const server = testServerSetup(); | ||
const contextFieldName = 'appName'; | ||
|
||
const setupRoutes = () => { | ||
testServerRoute( | ||
server, | ||
`api/admin/context/${contextFieldName}/strategies`, | ||
{ | ||
strategies: [ | ||
{ | ||
id: '4b3ad603-4727-4782-bd61-efc530e37209', | ||
projectId: 'faaa', | ||
featureName: 'tests', | ||
strategyName: 'flexibleRollout', | ||
environment: 'development', | ||
}, | ||
], | ||
} | ||
); | ||
testServerRoute(server, '/api/admin/ui-config', { | ||
flags: { | ||
segmentContextFieldUsage: true, | ||
}, | ||
}); | ||
|
||
testServerRoute(server, '/api/admin/projects', { | ||
version: 1, | ||
projects: [ | ||
{ | ||
id: 'faaa', | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
test('should show usage of context field', async () => { | ||
setupRoutes(); | ||
|
||
const contextFieldName = 'appName'; | ||
render( | ||
<UIProviderContainer> | ||
<ContextFieldUsage contextName={contextFieldName} /> | ||
</UIProviderContainer> | ||
); | ||
|
||
await screen.findByText('Usage of this context field:'); | ||
await screen.findByText('tests (Gradual rollout)'); | ||
}); |
89 changes: 89 additions & 0 deletions
89
frontend/src/component/context/ContextFieldUsage/ContextFieldUsage.tsx
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,89 @@ | ||
import { Alert, styled } from '@mui/material'; | ||
import { formatEditStrategyPath } from 'component/feature/FeatureStrategy/FeatureStrategyEdit/FeatureStrategyEdit'; | ||
import { IFeatureStrategy } from 'interfaces/strategy'; | ||
import { Link } from 'react-router-dom'; | ||
import { formatStrategyName } from 'utils/strategyNames'; | ||
import { useStrategiesByContext } from 'hooks/api/getters/useStrategiesByContext/useStrategiesByContext'; | ||
import useProjects from 'hooks/api/getters/useProjects/useProjects'; | ||
|
||
const StyledUl = styled('ul')(({ theme }) => ({ | ||
listStyle: 'none', | ||
paddingLeft: 0, | ||
})); | ||
|
||
const StyledAlert = styled(Alert)(({ theme }) => ({ | ||
marginTop: theme.spacing(1), | ||
})); | ||
|
||
interface IContextFieldUsageProps { | ||
contextName: string; | ||
} | ||
|
||
export const ContextFieldUsage = ({ contextName }: IContextFieldUsageProps) => { | ||
const { strategies } = useStrategiesByContext(contextName); | ||
const { projects } = useProjects(); | ||
|
||
const projectsUsed = Array.from( | ||
new Set<string>( | ||
strategies.map(({ projectId }) => projectId!).filter(Boolean) | ||
) | ||
); | ||
|
||
const projectList = ( | ||
<StyledUl> | ||
{projectsUsed.map(projectId => ( | ||
<li key={projectId}> | ||
<Link | ||
to={`/projects/${projectId}`} | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
{projects.find(({ id }) => id === projectId)?.name ?? | ||
projectId} | ||
</Link> | ||
<ul> | ||
{strategies | ||
?.filter( | ||
strategy => strategy.projectId === projectId | ||
) | ||
.map(strategy => ( | ||
<li key={strategy.id}> | ||
<Link | ||
to={formatEditStrategyPath( | ||
strategy.projectId!, | ||
strategy.featureName!, | ||
strategy.environment!, | ||
strategy.id | ||
)} | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
{strategy.featureName!}{' '} | ||
{formatStrategyNameParens(strategy)} | ||
</Link> | ||
</li> | ||
))} | ||
</ul> | ||
</li> | ||
))} | ||
</StyledUl> | ||
); | ||
if (projectsUsed.length > 0) { | ||
return ( | ||
<StyledAlert severity="info"> | ||
Usage of this context field: | ||
{projectList} | ||
</StyledAlert> | ||
); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const formatStrategyNameParens = (strategy: IFeatureStrategy): string => { | ||
if (!strategy.strategyName) { | ||
return ''; | ||
} | ||
|
||
return `(${formatStrategyName(strategy.strategyName)})`; | ||
}; |
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
39 changes: 39 additions & 0 deletions
39
frontend/src/hooks/api/getters/useStrategiesByContext/useStrategiesByContext.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,39 @@ | ||
import { mutate } from 'swr'; | ||
import { useCallback } from 'react'; | ||
import { formatApiPath } from 'utils/formatPath'; | ||
import handleErrorResponses from '../httpErrorResponseHandler'; | ||
import { IFeatureStrategy } from 'interfaces/strategy'; | ||
import { useConditionalSWR } from '../useConditionalSWR/useConditionalSWR'; | ||
|
||
export interface IUseStrategiesByContextOutput { | ||
strategies: IFeatureStrategy[]; | ||
refetchUsedSegments: () => void; | ||
loading: boolean; | ||
error?: Error; | ||
} | ||
|
||
export const useStrategiesByContext = ( | ||
id?: string | number | ||
): IUseStrategiesByContextOutput => { | ||
const path = formatApiPath(`api/admin/context/${id}/strategies`); | ||
const { data, error } = useConditionalSWR(id, [], path, () => | ||
fetchUsedSegment(path) | ||
); | ||
|
||
const refetchUsedSegments = useCallback(() => { | ||
mutate(path).catch(console.warn); | ||
}, [path]); | ||
|
||
return { | ||
strategies: data?.strategies || [], | ||
refetchUsedSegments, | ||
loading: !error && !data, | ||
error, | ||
}; | ||
}; | ||
|
||
const fetchUsedSegment = (path: string) => { | ||
return fetch(path, { method: 'GET' }) | ||
.then(handleErrorResponses('Strategies by context')) | ||
.then(res => res.json()); | ||
}; |
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
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