refactor(ui): Migrate components to ui-next (#4930)
Co-authored-by: Dan Rukas <dan.rukas@gmail.com>
This commit is contained in:
1 parent
1b8729dded
commit
2bdf4afa4d
80 files changed
+2196
-521
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { DndProvider } from 'react-dnd';
|
||||
import { HTML5Backend } from 'react-dnd-html5-backend';
|
||||
|
||||
// import TouchBackend from 'react-dnd-touch-backend';
|
||||
|
||||
// TODO: this is false when it should not be :thinking:
|
||||
const isTouchDevice =
|
||||
typeof window !== `undefined` && !!('ontouchstart' in window || navigator.maxTouchPoints);
|
||||
|
||||
/**
|
||||
* Relevant:
|
||||
* https://github.com/react-dnd/react-dnd/issues/186#issuecomment-335429067
|
||||
* https://github.com/react-dnd/react-dnd/issues/186#issuecomment-282789420
|
||||
*
|
||||
* Docs:
|
||||
* http://react-dnd.github.io/react-dnd/docs/api/drag-drop-context
|
||||
*/
|
||||
function DragAndDropProvider({ children }) {
|
||||
const backend = HTML5Backend; // isTouchDevice ? TouchBackend : HTML5Backend;
|
||||
const opts = {}; // isTouchDevice ? { enableMouseEvents: true } : {};
|
||||
|
||||
console.log('using... touch backend?', isTouchDevice);
|
||||
|
||||
return (
|
||||
<DndProvider
|
||||
backend={backend}
|
||||
opts={opts}
|
||||
>
|
||||
{children}
|
||||
</DndProvider>
|
||||
);
|
||||
}
|
||||
|
||||
DragAndDropProvider.propTypes = {
|
||||
children: PropTypes.any,
|
||||
};
|
||||
|
||||
export default DragAndDropProvider;
|
||||
@@ -0,0 +1,17 @@
|
||||
import React, { ReactNode, createContext, useContext, useMemo } from 'react';
|
||||
|
||||
interface ImageViewerProviderProps {
|
||||
StudyInstanceUIDs: string[];
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const ImageViewerContext = createContext(null);
|
||||
export const useImageViewer = () => useContext(ImageViewerContext);
|
||||
|
||||
export function ImageViewerProvider({ StudyInstanceUIDs, children }: ImageViewerProviderProps) {
|
||||
const value = useMemo(() => {
|
||||
return { StudyInstanceUIDs };
|
||||
}, [StudyInstanceUIDs]);
|
||||
|
||||
return <ImageViewerContext.Provider value={value}>{children}</ImageViewerContext.Provider>;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import React, { createContext, useCallback, useContext, useEffect, useReducer } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const DEFAULT_STATE = {
|
||||
user: null,
|
||||
enabled: false,
|
||||
};
|
||||
|
||||
export const UserAuthenticationContext = createContext(DEFAULT_STATE);
|
||||
|
||||
export function UserAuthenticationProvider({ children, service }) {
|
||||
const userAuthenticationReducer = (state, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_USER': {
|
||||
return {
|
||||
...state,
|
||||
...{ user: action.payload.user },
|
||||
};
|
||||
}
|
||||
case 'RESET': {
|
||||
return {
|
||||
user: null,
|
||||
};
|
||||
}
|
||||
|
||||
case 'SET': {
|
||||
return {
|
||||
...state,
|
||||
...action.payload,
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
return action.payload;
|
||||
}
|
||||
};
|
||||
|
||||
const [userAuthenticationState, dispatch] = useReducer(userAuthenticationReducer, DEFAULT_STATE);
|
||||
|
||||
const getState = useCallback(() => userAuthenticationState, [userAuthenticationState]);
|
||||
|
||||
const setUser = useCallback(
|
||||
user =>
|
||||
dispatch({
|
||||
type: 'SET_USER',
|
||||
payload: {
|
||||
user,
|
||||
},
|
||||
}),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const getUser = useCallback(() => userAuthenticationState.user, [userAuthenticationState]);
|
||||
|
||||
const reset = useCallback(
|
||||
() =>
|
||||
dispatch({
|
||||
type: 'RESET',
|
||||
payload: {},
|
||||
}),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
const set = useCallback(
|
||||
payload =>
|
||||
dispatch({
|
||||
type: 'SET',
|
||||
payload,
|
||||
}),
|
||||
[dispatch]
|
||||
);
|
||||
|
||||
/**
|
||||
* Sets the implementation of the UserAuthenticationService that can be used by extensions.
|
||||
*
|
||||
* @returns void
|
||||
*/
|
||||
// TODO: should this be a useEffect or not?
|
||||
useEffect(() => {
|
||||
if (service) {
|
||||
service.setServiceImplementation({
|
||||
getState,
|
||||
setUser,
|
||||
getUser,
|
||||
reset,
|
||||
set,
|
||||
});
|
||||
}
|
||||
}, [getState, service, setUser, getUser, reset, set]);
|
||||
|
||||
// TODO: This may not be correct, but I think we need to set the implementation for the service
|
||||
// immediately when this runs, since otherwise the authentication redirects will fail.
|
||||
// (useEffect only runs after the child components - in this case, routing logic - has failed)
|
||||
if (service) {
|
||||
service.setServiceImplementation({
|
||||
getState,
|
||||
setUser,
|
||||
getUser,
|
||||
reset,
|
||||
set,
|
||||
});
|
||||
}
|
||||
|
||||
const api = {
|
||||
getState,
|
||||
setUser,
|
||||
getUser,
|
||||
getAuthorizationHeader: service.getAuthorizationHeader,
|
||||
handleUnauthenticated: service.handleUnauthenticated,
|
||||
reset,
|
||||
set,
|
||||
};
|
||||
|
||||
return (
|
||||
<UserAuthenticationContext.Provider value={[userAuthenticationState, api]}>
|
||||
{children}
|
||||
</UserAuthenticationContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export default UserAuthenticationProvider;
|
||||
|
||||
const UserAuthenticationConsumer = UserAuthenticationContext.Consumer;
|
||||
export { UserAuthenticationConsumer };
|
||||
|
||||
UserAuthenticationProvider.propTypes = {
|
||||
children: PropTypes.any,
|
||||
service: PropTypes.shape({
|
||||
setServiceImplementation: PropTypes.func,
|
||||
}).isRequired,
|
||||
};
|
||||
|
||||
export const useUserAuthentication = () => useContext(UserAuthenticationContext);
|
||||
@@ -0,0 +1,52 @@
|
||||
import React, { useState, createContext, useContext, useCallback, useEffect } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
const DEFAULT_STATE = {
|
||||
message: undefined,
|
||||
type: 'info', // "error" | "warning" | "info" | "success"
|
||||
actions: undefined, // array of { type, text, value }
|
||||
onSubmit: () => {
|
||||
console.log('btn value?');
|
||||
},
|
||||
onOutsideClick: () => {
|
||||
console.warn('default: onOutsideClick');
|
||||
},
|
||||
onDismiss: () => {
|
||||
console.log('dismiss? -1');
|
||||
},
|
||||
onKeyPress: () => {
|
||||
console.log('key pressed?');
|
||||
},
|
||||
};
|
||||
|
||||
const ViewportDialogContext = createContext(null);
|
||||
const { Provider } = ViewportDialogContext;
|
||||
|
||||
export const useViewportDialog = () => useContext(ViewportDialogContext);
|
||||
|
||||
const ViewportDialogProvider = ({ children, service }) => {
|
||||
const [viewportDialogState, setViewportDialogState] = useState(DEFAULT_STATE);
|
||||
const show = useCallback(
|
||||
params => setViewportDialogState({ ...viewportDialogState, ...params }),
|
||||
[viewportDialogState]
|
||||
);
|
||||
const hide = useCallback(() => setViewportDialogState(DEFAULT_STATE), []);
|
||||
|
||||
useEffect(() => {
|
||||
if (service) {
|
||||
service.setServiceImplementation({ hide, show });
|
||||
}
|
||||
}, [hide, service, show]);
|
||||
|
||||
return <Provider value={[viewportDialogState, { show, hide }]}>{children}</Provider>;
|
||||
};
|
||||
|
||||
ViewportDialogProvider.propTypes = {
|
||||
/** Children that will be wrapped with Modal Context */
|
||||
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.node), PropTypes.node]).isRequired,
|
||||
service: PropTypes.shape({
|
||||
setServiceImplementation: PropTypes.func,
|
||||
}),
|
||||
};
|
||||
|
||||
export default ViewportDialogProvider;
|
||||
@@ -3,6 +3,11 @@ import { ViewportGridContext, ViewportGridProvider, useViewportGrid } from './Vi
|
||||
import { ModalProvider, useModal } from './ModalProvider';
|
||||
import { DialogProvider, useDialog } from './DialogProvider';
|
||||
import ManagedDialog from './ManagedDialog';
|
||||
import ViewportDialogProvider from './ViewportDialogProvider';
|
||||
import { useViewportDialog } from './ViewportDialogProvider';
|
||||
import { UserAuthenticationProvider, useUserAuthentication } from './UserAuthenticationProvider';
|
||||
import { ImageViewerContext, ImageViewerProvider, useImageViewer } from './ImageViewerProvider';
|
||||
import DragAndDropProvider from './DragAndDropProvider';
|
||||
import CineProvider, { useCine } from './CineProvider';
|
||||
|
||||
export { useNotification, NotificationProvider };
|
||||
@@ -10,4 +15,8 @@ export { ViewportGridContext, ViewportGridProvider, useViewportGrid };
|
||||
export { ModalProvider, useModal };
|
||||
export { DialogProvider, useDialog };
|
||||
export { ManagedDialog };
|
||||
export { CineProvider, useCine };
|
||||
export { ViewportDialogProvider, useViewportDialog };
|
||||
export { UserAuthenticationProvider, useUserAuthentication };
|
||||
export { ImageViewerContext, ImageViewerProvider, useImageViewer };
|
||||
export { DragAndDropProvider };
|
||||
export { CineProvider, useCine };
|
||||
Reference in new issue
Block a user