chore: fix exporting the SecretsContext

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2023-01-09 14:55:05 +01:00
parent fedc5074b1
commit 3c72c0be91
8 changed files with 44 additions and 41 deletions
+5 -6
View File
@@ -242,7 +242,9 @@ export interface ScaffolderDryRunResponse {
}
// @public
export const ScaffolderFieldExtensions: React_2.ComponentType;
export const ScaffolderFieldExtensions: React_2.ComponentType<
React_2.PropsWithChildren<{}>
>;
// @public
export interface ScaffolderGetIntegrationsListOptions {
@@ -318,15 +320,12 @@ export type ScaffolderTaskStatus =
// @public
export interface ScaffolderUseTemplateSecrets {
// (undocumented)
secrets: Record<string, string>;
// (undocumented)
setSecrets: (input: Record<string, string>) => void;
}
// @public
export const SecretsContext: React_2.Context<
SecretsContextContents | undefined
>;
// @public
export type SecretsContextContents = {
secrets: Record<string, string>;
@@ -13,12 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { useContext } from 'react';
import {
useTemplateSecrets,
SecretsContextProvider,
SecretsContext,
} from './SecretsContext';
import React from 'react';
import { useTemplateSecrets, SecretsContextProvider } from './SecretsContext';
import { renderHook, act } from '@testing-library/react-hooks';
describe('SecretsContext', () => {
@@ -26,7 +22,6 @@ describe('SecretsContext', () => {
const { result } = renderHook(
() => ({
hook: useTemplateSecrets(),
context: useContext(SecretsContext),
}),
{
wrapper: ({ children }) => (
@@ -34,10 +29,10 @@ describe('SecretsContext', () => {
),
},
);
expect(result.current.context?.secrets.foo).toEqual(undefined);
expect(result.current.hook?.secrets.foo).toEqual(undefined);
act(() => result.current.hook.setSecrets({ foo: 'bar' }));
expect(result.current.context?.secrets.foo).toEqual('bar');
expect(result.current.hook?.secrets.foo).toEqual('bar');
});
});
@@ -13,16 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
createVersionedContext,
createVersionedValueMap,
} from '@backstage/version-bridge';
import React, {
useState,
useCallback,
useContext,
createContext,
PropsWithChildren,
} from 'react';
/**
* The contents of the {@link SecretsContext}.
* The contents of the `SecretsContext`
* @public
*/
export type SecretsContextContents = {
@@ -32,11 +35,10 @@ export type SecretsContextContents = {
/**
* The context to hold the Secrets.
* @public
*/
export const SecretsContext = createContext<SecretsContextContents | undefined>(
undefined,
);
const SecretsContext = createVersionedContext<{
1: SecretsContextContents;
}>('secrets-context');
/**
* The Context Provider that holds the state for the secrets.
@@ -46,7 +48,9 @@ export const SecretsContextProvider = ({ children }: PropsWithChildren<{}>) => {
const [secrets, setSecrets] = useState<Record<string, string>>({});
return (
<SecretsContext.Provider value={{ secrets, setSecrets }}>
<SecretsContext.Provider
value={createVersionedValueMap({ 1: { secrets, setSecrets } })}
>
{children}
</SecretsContext.Provider>
);
@@ -58,6 +62,7 @@ export const SecretsContextProvider = ({ children }: PropsWithChildren<{}>) => {
*/
export interface ScaffolderUseTemplateSecrets {
setSecrets: (input: Record<string, string>) => void;
secrets: Record<string, string>;
}
/**
@@ -66,14 +71,15 @@ export interface ScaffolderUseTemplateSecrets {
* @public
*/
export const useTemplateSecrets = (): ScaffolderUseTemplateSecrets => {
const value = useContext(SecretsContext);
const value = useContext(SecretsContext)?.atVersion(1);
if (!value) {
throw new Error(
'useTemplateSecrets must be used within a SecretsContextProvider',
);
}
const { setSecrets: updateSecrets } = value;
const { setSecrets: updateSecrets, secrets = {} } = value;
const setSecrets = useCallback(
(input: Record<string, string>) => {
@@ -82,5 +88,5 @@ export const useTemplateSecrets = (): ScaffolderUseTemplateSecrets => {
[updateSecrets],
);
return { setSecrets };
return { setSecrets, secrets };
};
@@ -15,7 +15,6 @@
*/
export {
useTemplateSecrets,
SecretsContext,
SecretsContextProvider,
type ScaffolderUseTemplateSecrets,
type SecretsContextContents,