Skip to content

feat(Codecov): add storage solution #90693

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {useCallback, 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 {useNavigate} from 'sentry/utils/useNavigate';
import useOrganization from 'sentry/utils/useOrganization';

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

export default function CodecovQueryParamsProvider({children}: CodecovProviderProps) {
const organization = useOrganization();
const orgSlug = organization.slug;

const location = useLocation();
const [localStorageState, setLocalStorageState] = useLocalStorageState(
`codecov-selection:${orgSlug}`,
{} as Partial<CodecovContextData>
);
const navigate = useNavigate();

const updateSelectorData: CodecovContextData['updateSelectorData'] = useCallback(
data => {
const currentParams = new URLSearchParams(location.search);
currentParams.set(Object.keys(data)[0] as string, Object.values(data)[0] as string);
navigate(`${location.pathname}?${currentParams.toString()}`, {replace: true});
},
[location.search, location.pathname, navigate]
);

useEffect(() => {
const repository =
typeof location.query.repository === 'string' ? location.query.repository : null;
const integratedOrg =
typeof location.query.integratedOrg === 'string'
? location.query.integratedOrg
: null;
const branch =
typeof location.query.branch === 'string' ? location.query.branch : null;
const codecovPeriod =
typeof location.query.codcovPeriod === 'string'
? location.query.codcovPeriod
: null;

setLocalStorageState({repository, integratedOrg, branch, codecovPeriod});
}, [setLocalStorageState, location.query]);

const params: CodecovContextData = {
repository:
decodeURIComponent(location.query.repository as string).trim() ||
localStorageState.repository ||
null,
integratedOrg:
decodeURIComponent(location.query.integratedOrg as string).trim() ||
localStorageState.integratedOrg ||
null,
branch:
decodeURIComponent(location.query.branch as string).trim() ||
localStorageState.branch ||
null,
codecovPeriod:
decodeURIComponent(location.query.codecovPeriod as string).trim() ||
localStorageState.codecovPeriod ||
null,
updateSelectorData,
};

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

export type CodecovContextData = {
branch: string | null;
codecovPeriod: string | null;
integratedOrg: string | null;
repository: string | null;
updateSelectorData: (value: Record<string, string | null>) => void;
};

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