2022-09-20 18:14:24 +00:00
|
|
|
import { ComponentType } from 'react';
|
|
|
|
|
2022-11-13 08:10:18 +00:00
|
|
|
import { UserProvider } from '@/react/hooks/useUser';
|
2022-09-20 18:14:24 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
import { withReactQuery } from './withReactQuery';
|
|
|
|
|
2022-09-20 18:14:24 +00:00
|
|
|
export function withCurrentUser<T>(
|
2023-05-14 09:24:37 +00:00
|
|
|
WrappedComponent: ComponentType<T & JSX.IntrinsicAttributes>
|
|
|
|
): ComponentType<T & JSX.IntrinsicAttributes> {
|
2022-09-20 18:14:24 +00:00
|
|
|
// Try to create a nice displayName for React Dev Tools.
|
|
|
|
const displayName =
|
|
|
|
WrappedComponent.displayName || WrappedComponent.name || 'Component';
|
|
|
|
|
2023-05-14 09:24:37 +00:00
|
|
|
function WrapperComponent(props: T & JSX.IntrinsicAttributes) {
|
2022-09-20 18:14:24 +00:00
|
|
|
return (
|
|
|
|
<UserProvider>
|
|
|
|
<WrappedComponent {...props} />
|
|
|
|
</UserProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
WrapperComponent.displayName = `withCurrentUser(${displayName})`;
|
2022-09-20 18:14:24 +00:00
|
|
|
|
2023-01-26 02:10:05 +00:00
|
|
|
// User provider makes a call to the API to get the current user.
|
|
|
|
// We need to wrap it with React Query to make that call.
|
|
|
|
return withReactQuery(WrapperComponent);
|
2022-09-20 18:14:24 +00:00
|
|
|
}
|