Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions static/app/components/codecov/container/codecovParamsProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {useEffect} from 'react';

import type {CodecovContextData} from 'sentry/components/codecov/context/codecovContext';
import {CodecovContext} from 'sentry/components/codecov/context/codecovContext';
import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
import {useLocation} from 'sentry/utils/useLocation';
import useOrganization from 'sentry/utils/useOrganization';

type CodecovQueryParamsProviderProps = {
children?: NonNullable<React.ReactNode>;
};

export default function CodecovQueryParamsProvider({
children,
}: CodecovQueryParamsProviderProps) {
const organization = useOrganization();

const location = useLocation();
const [localStorageState, setLocalStorageState] = useLocalStorageState(
`codecov-selection:${organization.slug}`,
{}
);

useEffect(() => {
const validEntries = {
repository: location.query.repository,
integratedOrg: location.query.integratedOrg,
branch: location.query.branch,
codecovPeriod: location.query.codecovPeriod,
};

for (const [key, value] of Object.entries(validEntries)) {
if (!value || typeof value !== 'string') {
delete validEntries[key as keyof CodecovContextData];
}
}

setLocalStorageState(prev => ({
...prev,
...validEntries,
}));
}, [setLocalStorageState, location.query]);

// Repository, org and branch default to null as its value to the option not being selected.
// These only represent the unselected values and shouldn't be used when fetching backend data.
const params: CodecovContextData = {
repository:
typeof location.query.repository === 'string'
? decodeURIComponent(location.query.repository)
: 'repository' in localStorageState
? (localStorageState.repository as string)
: null,
integratedOrg:
typeof location.query.integratedOrg === 'string'
? decodeURIComponent(location.query.integratedOrg)
: 'integratedOrg' in localStorageState
? (localStorageState.integratedOrg as string)
: null,
branch:
typeof location.query.branch === 'string'
? decodeURIComponent(location.query.branch)
: 'branch' in localStorageState
? (localStorageState.branch as string)
: null,
codecovPeriod:
typeof location.query.codecovPeriod === 'string'
? decodeURIComponent(location.query.codecovPeriod)
: 'codecovPeriod' in localStorageState
? (localStorageState.codecovPeriod as string)
: '24h',
};

return <CodecovContext.Provider value={params}>{children}</CodecovContext.Provider>;
}
17 changes: 17 additions & 0 deletions static/app/components/codecov/context/codecovContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {createContext, useContext} from 'react';

export type CodecovContextData = {
branch: string | null;
codecovPeriod: string | null;
integratedOrg: string | null;
repository: string | null;
};

export const CodecovContext = createContext<CodecovContextData | undefined>(undefined);

export function useCodecovContext() {
const context = useContext(CodecovContext);
if (context === undefined)
throw new Error('useCodecovContext was called outside of CodecovProvider');
return context;
}
Loading