Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 12 additions & 3 deletions workspaces/frontend/.eslintrc → workspaces/frontend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
const noReactHookNamespace = require('./eslint-local-rules/no-react-hook-namespace');

module.exports = {
"parser": "@typescript-eslint/parser",
"env": {
"browser": true,
Expand Down Expand Up @@ -216,7 +218,8 @@
"no-useless-return": "error",
"symbol-description": "error",
"yoda": "error",
"func-names": "warn"
"func-names": "warn",
"no-react-hook-namespace": "error"
},
"overrides": [
{
Expand Down Expand Up @@ -262,6 +265,12 @@
}
]
}
},
{
files: ['**/*.{js,jsx,ts,tsx}'],
rules: {
'no-react-hook-namespace': 'error',
},
}
]
}
};
34 changes: 34 additions & 0 deletions workspaces/frontend/eslint-local-rules/no-react-hook-namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow using React hooks through React namespace',
},
messages: {
avoidNamespaceHook: 'Import React hook "{{hook}}" directly instead of using React.{{hook}}.',
},
schema: [],
},
create(context) {
const hooks = new Set([
'useState', 'useEffect', 'useContext', 'useReducer',
'useCallback', 'useMemo', 'useRef', 'useLayoutEffect',
'useImperativeHandle', 'useDebugValue', 'useDeferredValue',
'useTransition', 'useId', 'useSyncExternalStore',
]);
return {
MemberExpression(node) {
if (
node.object?.name === 'React' &&
hooks.has(node.property?.name)
) {
context.report({
node,
messageId: 'avoidNamespaceHook',
data: { hook: node.property.name },
});
}
},
};
},
};
4 changes: 2 additions & 2 deletions workspaces/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"test:unit": "npm run test:jest -- --silent",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:fix": "eslint --ext .js,.ts,.jsx,.tsx ./src --fix",
"test:lint": "eslint --max-warnings 0 --ext .js,.ts,.jsx,.tsx ./src",
"test:fix": "eslint --rulesdir eslint-local-rules --ext .js,.ts,.jsx,.tsx ./src --fix",
"test:lint": "eslint --rulesdir eslint-local-rules --max-warnings 0 --ext .js,.ts,.jsx,.tsx ./src",
"cypress:open": "cypress open --project src/__tests__/cypress",
"cypress:open:mock": "CY_MOCK=1 CY_WS_PORT=9002 npm run cypress:open -- ",
"cypress:run": "cypress run -b chrome --project src/__tests__/cypress",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as React from 'react';
import { useEffect, useRef, useState } from 'react';
import { createComparativeValue, renderHook, standardUseFetchState, testHook } from './hooks';

const useSayHello = (who: string, showCount = false) => {
const countRef = React.useRef(0);
const countRef = useRef(0);
countRef.current++;
return `Hello ${who}!${showCount && countRef.current > 1 ? ` x${countRef.current}` : ''}`;
};

const useSayHelloDelayed = (who: string, delay = 0) => {
const [speech, setSpeech] = React.useState('');
React.useEffect(() => {
const [speech, setSpeech] = useState('');
useEffect(() => {
const handle = setTimeout(() => setSpeech(`Hello ${who}!`), delay);
return () => clearTimeout(handle);
}, [who, delay]);
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React, { useEffect } from 'react';
import '@patternfly/react-core/dist/styles/base.css';
import './app.css';
import {
Expand Down Expand Up @@ -26,7 +26,7 @@ import { isMUITheme, Theme } from './const';
import { BrowserStorageContextProvider } from './context/BrowserStorageContext';

const App: React.FC = () => {
React.useEffect(() => {
useEffect(() => {
// Apply the theme based on the value of STYLE_THEME
if (isMUITheme()) {
document.documentElement.classList.add(Theme.MUI);
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/AppRoutes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import { Route, Routes, Navigate } from 'react-router-dom';
import { AppRoutePaths } from '~/app/routes';
import { WorkspaceForm } from '~/app/pages/Workspaces/Form/WorkspaceForm';
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/EnsureAPIAvailability.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import { Bullseye, Spinner } from '@patternfly/react-core';
import { useNotebookAPI } from './hooks/useNotebookAPI';

Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/NavSidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React, { useState } from 'react';
import { NavLink, useLocation } from 'react-router-dom';
import {
Brand,
Expand Down Expand Up @@ -29,7 +29,7 @@ const NavHref: React.FC<{ item: NavDataHref }> = ({ item }) => {

const NavGroup: React.FC<{ item: NavDataGroup }> = ({ item }) => {
const { children } = item;
const [expanded, setExpanded] = React.useState(false);
const [expanded, setExpanded] = useState(false);

return (
<NavExpandable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import { SearchInput, SearchInputProps, TextInput } from '@patternfly/react-core';
import FormFieldset from 'app/components/FormFieldset';
import { isMUITheme } from 'app/const';
Expand Down
16 changes: 12 additions & 4 deletions workspaces/frontend/src/app/context/BrowserStorageContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import React, { createContext, useCallback, useContext, useEffect } from 'react';
import React, {
createContext,
useCallback,
useContext,
useEffect,
useMemo,
useRef,
useState,
} from 'react';

export interface BrowserStorageContextType {
getValue: (key: string) => unknown;
Expand All @@ -17,8 +25,8 @@ const BrowserStorageContext = createContext<BrowserStorageContextType>({
export const BrowserStorageContextProvider: React.FC<BrowserStorageContextProviderProps> = ({
children,
}) => {
const [values, setValues] = React.useState<{ [key: string]: unknown }>({});
const valuesRef = React.useRef(values);
const [values, setValues] = useState<{ [key: string]: unknown }>({});
const valuesRef = useRef(values);
useEffect(() => {
valuesRef.current = values;
}, [values]);
Expand Down Expand Up @@ -49,7 +57,7 @@ export const BrowserStorageContextProvider: React.FC<BrowserStorageContextProvid
);

// eslint-disable-next-line react-hooks/exhaustive-deps
const contextValue = React.useMemo(() => ({ getValue, setValue }), [getValue, setValue, values]);
const contextValue = useMemo(() => ({ getValue, setValue }), [getValue, setValue, values]);

return (
<BrowserStorageContext.Provider value={contextValue}>{children}</BrowserStorageContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext, ReactNode, useMemo, useCallback } from 'react';
import React, { ReactNode, useCallback, useContext, useMemo, useState } from 'react';
import useMount from '~/app/hooks/useMount';
import useNamespaces from '~/app/hooks/useNamespaces';
import { useStorage } from './BrowserStorageContext';
Expand Down
5 changes: 2 additions & 3 deletions workspaces/frontend/src/app/context/NotebookContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { ReactNode } from 'react';
import React, { ReactNode, useMemo } from 'react';
import { BFF_API_VERSION } from '~/app/const';
import EnsureAPIAvailability from '~/app/EnsureAPIAvailability';
import useNotebookAPIState, { NotebookAPIState } from './useNotebookAPIState';
Expand All @@ -26,7 +25,7 @@ export const NotebookContextProvider: React.FC<NotebookContextProviderProps> = (

return (
<NotebookContext.Provider
value={React.useMemo(
value={useMemo(
() => ({
apiState,
refreshAPIState,
Expand Down
6 changes: 3 additions & 3 deletions workspaces/frontend/src/app/context/useNotebookAPIState.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import { useCallback } from 'react';
import { NotebookAPIs } from '~/shared/api/notebookApi';
import {
createWorkspace,
Expand Down Expand Up @@ -48,7 +48,7 @@ const MOCK_API_ENABLED = process.env.WEBPACK_REPLACE__mockApiEnabled === 'true';
const useNotebookAPIState = (
hostPath: string | null,
): [apiState: NotebookAPIState, refreshAPIState: () => void] => {
const createApi = React.useCallback(
const createApi = useCallback(
(path: string): NotebookAPIs => ({
// Health
getHealthCheck: getHealthCheck(path),
Expand All @@ -75,7 +75,7 @@ const useNotebookAPIState = (
[],
);

const createMockApi = React.useCallback(
const createMockApi = useCallback(
(path: string): NotebookAPIs => ({
// Health
getHealthCheck: mockGetHealthCheck(path),
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/error/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import { Link } from 'react-router-dom';
import { Button, Split, SplitItem, Title } from '@patternfly/react-core';
import { TimesIcon } from '@patternfly/react-icons';
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/error/ErrorDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import {
ClipboardCopy,
ClipboardCopyVariant,
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/error/UpdateState.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import {
Button,
EmptyState,
Expand Down
12 changes: 6 additions & 6 deletions workspaces/frontend/src/app/hooks/useGenericObjectState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback, useRef, useState } from 'react';

export type UpdateObjectAtPropAndValue<T> = <K extends keyof T>(
propKey: K,
Expand All @@ -13,9 +13,9 @@ export type GenericObjectState<T> = [
];

const useGenericObjectState = <T>(defaultData: T | (() => T)): GenericObjectState<T> => {
const [value, setValue] = React.useState<T>(defaultData);
const [value, setValue] = useState<T>(defaultData);

const setPropValue = React.useCallback<UpdateObjectAtPropAndValue<T>>((propKey, propValue) => {
const setPropValue = useCallback<UpdateObjectAtPropAndValue<T>>((propKey, propValue) => {
setValue((oldValue) => {
if (oldValue[propKey] !== propValue) {
return { ...oldValue, [propKey]: propValue };
Expand All @@ -24,12 +24,12 @@ const useGenericObjectState = <T>(defaultData: T | (() => T)): GenericObjectStat
});
}, []);

const defaultDataRef = React.useRef(value);
const resetToDefault = React.useCallback(() => {
const defaultDataRef = useRef(value);
const resetToDefault = useCallback(() => {
setValue(defaultDataRef.current);
}, []);

const replace = React.useCallback((newValue: T) => {
const replace = useCallback((newValue: T) => {
setValue(newValue);
}, []);

Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useNamespaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback } from 'react';
import useFetchState, {
FetchState,
FetchStateCallbackPromise,
Expand All @@ -9,7 +9,7 @@ import { Namespace } from '~/shared/api/backendApiTypes';
const useNamespaces = (): FetchState<Namespace[] | null> => {
const { api, apiAvailable } = useNotebookAPI();

const call = React.useCallback<FetchStateCallbackPromise<Namespace[] | null>>(
const call = useCallback<FetchStateCallbackPromise<Namespace[] | null>>(
(opts) => {
if (!apiAvailable) {
return Promise.reject(new Error('API not yet available'));
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useNotebookAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useContext } from 'react';
import { NotebookAPIState } from '~/app/context/useNotebookAPIState';
import { NotebookContext } from '~/app/context/NotebookContext';

Expand All @@ -7,7 +7,7 @@ type UseNotebookAPI = NotebookAPIState & {
};

export const useNotebookAPI = (): UseNotebookAPI => {
const { apiState, refreshAPIState: refreshAllAPI } = React.useContext(NotebookContext);
const { apiState, refreshAPIState: refreshAllAPI } = useContext(NotebookContext);

return {
refreshAllAPI,
Expand Down
8 changes: 3 additions & 5 deletions workspaces/frontend/src/app/hooks/useWorkspaceCountPerKind.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useEffect, useState } from 'react';
import { useNotebookAPI } from '~/app/hooks/useNotebookAPI';
import { Workspace, WorkspaceKind } from '~/shared/api/backendApiTypes';
import { WorkspaceCountPerOption } from '~/app/types';
Expand All @@ -8,11 +8,9 @@ export type WorkspaceCountPerKind = Record<WorkspaceKind['name'], WorkspaceCount
export const useWorkspaceCountPerKind = (): WorkspaceCountPerKind => {
const { api } = useNotebookAPI();

const [workspaceCountPerKind, setWorkspaceCountPerKind] = React.useState<WorkspaceCountPerKind>(
{},
);
const [workspaceCountPerKind, setWorkspaceCountPerKind] = useState<WorkspaceCountPerKind>({});

React.useEffect(() => {
useEffect(() => {
api.listAllWorkspaces({}).then((workspaces) => {
const countPerKind = workspaces.reduce((acc: WorkspaceCountPerKind, workspace: Workspace) => {
acc[workspace.workspaceKind.name] = acc[workspace.workspaceKind.name] ?? {
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useWorkspaceFormData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback } from 'react';
import { useNotebookAPI } from '~/app/hooks/useNotebookAPI';
import { WorkspaceFormData } from '~/app/types';
import useFetchState, {
Expand All @@ -25,7 +25,7 @@ const useWorkspaceFormData = (args: {
}): FetchState<WorkspaceFormData> => {
const { api, apiAvailable } = useNotebookAPI();

const call = React.useCallback<FetchStateCallbackPromise<WorkspaceFormData>>(
const call = useCallback<FetchStateCallbackPromise<WorkspaceFormData>>(
async (opts) => {
if (!apiAvailable) {
throw new Error('API not yet available');
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useWorkspaceKindByName.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback } from 'react';
import useFetchState, {
FetchState,
FetchStateCallbackPromise,
Expand All @@ -9,7 +9,7 @@ import { WorkspaceKind } from '~/shared/api/backendApiTypes';
const useWorkspaceKindByName = (kind: string): FetchState<WorkspaceKind | null> => {
const { api, apiAvailable } = useNotebookAPI();

const call = React.useCallback<FetchStateCallbackPromise<WorkspaceKind | null>>(
const call = useCallback<FetchStateCallbackPromise<WorkspaceKind | null>>(
(opts) => {
if (!apiAvailable) {
return Promise.reject(new Error('API not yet available'));
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useWorkspaceKinds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback } from 'react';
import useFetchState, {
FetchState,
FetchStateCallbackPromise,
Expand All @@ -8,7 +8,7 @@ import { useNotebookAPI } from '~/app/hooks/useNotebookAPI';

const useWorkspaceKinds = (): FetchState<WorkspaceKind[]> => {
const { api, apiAvailable } = useNotebookAPI();
const call = React.useCallback<FetchStateCallbackPromise<WorkspaceKind[]>>(
const call = useCallback<FetchStateCallbackPromise<WorkspaceKind[]>>(
(opts) => {
if (!apiAvailable) {
return Promise.reject(new Error('API not yet available'));
Expand Down
4 changes: 2 additions & 2 deletions workspaces/frontend/src/app/hooks/useWorkspaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import { useCallback } from 'react';
import useFetchState, {
FetchState,
FetchStateCallbackPromise,
Expand All @@ -9,7 +9,7 @@ import { Workspace } from '~/shared/api/backendApiTypes';
const useWorkspaces = (namespace: string): FetchState<Workspace[] | null> => {
const { api, apiAvailable } = useNotebookAPI();

const call = React.useCallback<FetchStateCallbackPromise<Workspace[] | null>>(
const call = useCallback<FetchStateCallbackPromise<Workspace[] | null>>(
(opts) => {
if (!apiAvailable) {
return Promise.reject(new Error('API not yet available'));
Expand Down
2 changes: 1 addition & 1 deletion workspaces/frontend/src/app/pages/Debug/Debug.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react';
import React from 'react';
import { CubesIcon } from '@patternfly/react-icons';
import {
Button,
Expand Down
Loading
Loading