From 4ca3f9cabf03c90acdbc6bcede989e4daedacf88 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Wed, 25 May 2022 08:58:26 +0100 Subject: [PATCH] Add types to IdeContext (#8107) GitOrigin-RevId: 6b1df81ce588558adab0c31c0f65e68e5b0c25bf --- .../frontend/js/shared/context/ide-context.js | 33 ------------------- .../js/shared/context/ide-context.tsx | 25 ++++++++++++++ 2 files changed, 25 insertions(+), 33 deletions(-) delete mode 100644 services/web/frontend/js/shared/context/ide-context.js create mode 100644 services/web/frontend/js/shared/context/ide-context.tsx diff --git a/services/web/frontend/js/shared/context/ide-context.js b/services/web/frontend/js/shared/context/ide-context.js deleted file mode 100644 index 39175888ff..0000000000 --- a/services/web/frontend/js/shared/context/ide-context.js +++ /dev/null @@ -1,33 +0,0 @@ -import { createContext, useContext, useState } from 'react' -import PropTypes from 'prop-types' -import { getMockIde } from './mock/mock-ide' - -const IdeContext = createContext() - -IdeContext.Provider.propTypes = { - value: PropTypes.shape({ - $scope: PropTypes.object.isRequired, - }), -} - -export function useIdeContext() { - const context = useContext(IdeContext) - - if (!context) { - throw new Error('useIdeContext is only available inside IdeProvider') - } - - return context -} - -export function IdeProvider({ ide, children }) { - const [value] = useState(() => ide || getMockIde()) - - return {children} -} -IdeProvider.propTypes = { - children: PropTypes.any.isRequired, - ide: PropTypes.shape({ - $scope: PropTypes.object.isRequired, - }), -} diff --git a/services/web/frontend/js/shared/context/ide-context.tsx b/services/web/frontend/js/shared/context/ide-context.tsx new file mode 100644 index 0000000000..5a79cb69e0 --- /dev/null +++ b/services/web/frontend/js/shared/context/ide-context.tsx @@ -0,0 +1,25 @@ +import { createContext, FC, useContext, useState } from 'react' +import { getMockIde } from './mock/mock-ide' + +type Ide = { + [key: string]: any // TODO: define the rest of the `ide` and `$scope` properties + $scope: Record +} + +const IdeContext = createContext(null) + +export const IdeProvider: FC<{ ide: Ide }> = ({ ide, children }) => { + const [value] = useState(() => ide || getMockIde()) + + return {children} +} + +export function useIdeContext(): Ide { + const context = useContext(IdeContext) + + if (!context) { + throw new Error('useIdeContext is only available inside IdeProvider') + } + + return context +}