Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
ehconitin committed Aug 18, 2024
1 parent ccca96b commit 4cab78d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useOverlayScrollbars } from 'overlayscrollbars-react';
import { useEffect, useRef } from 'react';
import { useSetRecoilState } from 'recoil';

import { getContextByProviderName } from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import {
ContextProviderName,
getContextByProviderName,
} from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import { useScrollStates } from '@/ui/utilities/scroll/hooks/internal/useScrollStates';
import { overlayScrollbarsState } from '@/ui/utilities/scroll/states/overlayScrollbarsState';

Expand All @@ -25,7 +28,7 @@ export type ScrollWrapperProps = {
className?: string;
hideY?: boolean;
hideX?: boolean;
contextProviderName: string;
contextProviderName: ContextProviderName;
};

export const ScrollWrapper = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { createContext, RefObject } from 'react';

// Define the new interface for the context value
interface ScrollWrapperContextValue {
type ScrollWrapperContextValue = {
ref: RefObject<HTMLDivElement>;
id: string;
}
};

export type ContextProviderName =
| 'eventList'
| 'commandMenu'
| 'recordBoard'
| 'recordTableWithWrappers'
| 'settingsPageContainer'
| 'dropdownMenuItemsContainer'
| 'showPageContainer'
| 'showPageLeftContainer'
| 'tabList'
| 'releases'
| 'test';

// Helper function to create a context with a unique ID
const createScrollWrapperContext = (id: string) =>
createContext<ScrollWrapperContextValue>({
ref: { current: null },
id,
});

// Define all context providers
export const EventListScrollWrapperContext =
createScrollWrapperContext('eventList');
export const CommandMenuScrollWrapperContext =
Expand All @@ -34,9 +44,11 @@ export const TabListScrollWrapperContext =
createScrollWrapperContext('tabList');
export const ReleasesScrollWrapperContext =
createScrollWrapperContext('releases');
export const TestScrollWrapperContext = createScrollWrapperContext('test');

// Function to get the context based on provider name
export const getContextByProviderName = (contextProviderName: string) => {
export const getContextByProviderName = (
contextProviderName: ContextProviderName,
) => {
switch (contextProviderName) {
case 'eventList':
return EventListScrollWrapperContext;
Expand All @@ -58,7 +70,9 @@ export const getContextByProviderName = (contextProviderName: string) => {
return TabListScrollWrapperContext;
case 'releases':
return ReleasesScrollWrapperContext;
case 'test':
return TestScrollWrapperContext;
default:
return createScrollWrapperContext('awdawdawd');
throw new Error('Context Provider not available');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jest.mock('react', () => {

describe('useScrollWrapperScopedRef', () => {
it('should return the scrollWrapperRef if available', () => {
const { result } = renderHook(() => useScrollWrapperScopedRef());
const { result } = renderHook(() => useScrollWrapperScopedRef('test'));

expect(result.current).toBeDefined();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { getContextByProviderName } from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import {
ContextProviderName,
getContextByProviderName,
} from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import { scrollLeftComponentState } from '@/ui/utilities/scroll/states/scrollLeftComponentState';
import { scrollTopComponentState } from '@/ui/utilities/scroll/states/scrollTopComponentState';

import { extractComponentState } from '@/ui/utilities/state/component-state/utils/extractComponentState';

import { useContext } from 'react';

export const useScrollStates = (contextProviderName: string) => {
export const useScrollStates = (contextProviderName: ContextProviderName) => {
const Context = getContextByProviderName(contextProviderName);
const context = useContext(Context);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { ContextProviderName } from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import { useScrollStates } from '@/ui/utilities/scroll/hooks/internal/useScrollStates';
import { useRecoilValue } from 'recoil';

export const useScrollLeftValue = (contextProviderName: string) => {
export const useScrollLeftValue = (
contextProviderName: ContextProviderName,
) => {
const { scrollLeftComponentState } = useScrollStates(contextProviderName);
return useRecoilValue(scrollLeftComponentState);
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ContextProviderName } from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import { useScrollStates } from '@/ui/utilities/scroll/hooks/internal/useScrollStates';
import { useRecoilValue } from 'recoil';

export const useScrollTopValue = (contextProviderName: string) => {
export const useScrollTopValue = (contextProviderName: ContextProviderName) => {
const { scrollTopComponentState } = useScrollStates(contextProviderName);
return useRecoilValue(scrollTopComponentState);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import { useContext } from 'react';

import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';

import { getContextByProviderName } from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';
import {
ContextProviderName,
getContextByProviderName,
} from '@/ui/utilities/scroll/contexts/ScrollWrapperContexts';

export const useScrollWrapperScopedRef = (contextProviderName: string) => {
export const useScrollWrapperScopedRef = (
contextProviderName: ContextProviderName,
) => {
const Context = getContextByProviderName(contextProviderName);
const scrollWrapperRef = useContext(Context);

Expand Down

0 comments on commit 4cab78d

Please sign in to comment.