Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useScrollStates hook created replacing direct access to scrollTopState & scrollLeftState #4938

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useIcons } from 'twenty-ui';

import { FieldMetadata } from '@/object-record/record-field/types/FieldMetadata';
import { MOBILE_VIEWPORT } from '@/ui/theme/constants/MobileViewport';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftState';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftComponentState';

import { ColumnDefinition } from '../types/ColumnDefinition';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { useRecordTableStates } from '@/object-record/record-table/hooks/interna
import { RecordTableScope } from '@/object-record/record-table/scopes/RecordTableScope';
import { MOBILE_VIEWPORT } from '@/ui/theme/constants/MobileViewport';
import { RGBA } from '@/ui/theme/constants/Rgba';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftState';
import { scrollTopState } from '@/ui/utilities/scroll/states/scrollTopState';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftComponentState';
import { scrollTopState } from '@/ui/utilities/scroll/states/scrollTopComponentState';

const StyledTable = styled.table<{
freezeFirstColumns?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LightIconButton } from '@/ui/input/button/components/LightIconButton';
import { useTrackPointer } from '@/ui/utilities/pointer-event/hooks/useTrackPointer';
import { getSnapshotValue } from '@/ui/utilities/recoil-scope/utils/getSnapshotValue';
import { useIsMobile } from '@/ui/utilities/responsive/hooks/useIsMobile';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftState';
import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftComponentState';
import { mapArrayToObject } from '~/utils/array/mapArrayToObject';

import { ColumnHeadWithDropdown } from './ColumnHeadWithDropdown';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import styled from '@emotion/styled';
import { OverlayScrollbars } from 'overlayscrollbars';
import { useOverlayScrollbars } from 'overlayscrollbars-react';
import { useRecoilCallback } from 'recoil';

import { scrollLeftState } from '@/ui/utilities/scroll/states/scrollLeftState';
import { scrollTopState } from '@/ui/utilities/scroll/states/scrollTopState';

import 'overlayscrollbars/overlayscrollbars.css';
import { useScrollStates } from '@/ui/utilities/scroll/hooks/useScrollSates';

export const ScrollWrapperContext = createContext<RefObject<HTMLDivElement>>({
current: null,
Expand Down Expand Up @@ -37,6 +34,7 @@ export const ScrollWrapper = ({
hideY,
}: ScrollWrapperProps) => {
const scrollableRef = useRef<HTMLDivElement>(null);
const {scrollLeftState, scrollTopState} = useScrollStates()

const handleScroll = useRecoilCallback(
({ set }) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useAvailableScopeIdOrThrow } from "@/ui/utilities/recoil-scope/scopes-internal/hooks/useAvailableScopeId";
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 { ViewScopeInternalContext } from "@/views/scopes/scope-internal-context/ViewScopeInternalContext";

export const useScrollStates = (scrollWrapperId?:string) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in the filename that should be useScrollStates

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move this hook into a internal subfolder within hooks. internal hooks cannot be used outside of their component (here Scroll / ScrollWrapper).
This allows us to narrow down the public API of a component to enforce interface (easy maintenance).

We will need to create another useScrollTopValue(scrollWraperId) and useScrollLeftValue(scrollWraperId) hooks. These hooks will only expose what functions we want it to expose.

Here we would for example only want to expose the getter: useRecoilValue(scrollLeftState) (scrollLeftState being imported from useScrollStates)

Then from ColumnHead, Recordtable, RecordTableHeaderCell you can use useScrollTopValue and useScrollLeftValue

const componentId = useAvailableScopeIdOrThrow(
ViewScopeInternalContext,
scrollWrapperId,
);

return{
scrollLeftState: extractComponentState(
scrollLeftComponentState,
componentId
),
scrollTopState: extractComponentState(
scrollTopComponentState,
componentId
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createComponentState } from '@/ui/utilities/state/component-state/utils/createComponentState';

export const scrollLeftComponentState = createComponentState<number>({
key: 'scroll/scrollLeftState',
defaultValue: 0,
});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createComponentState } from '@/ui/utilities/state/component-state/utils/createComponentState';

export const scrollTopComponentState = createComponentState<number>({
key: 'scroll/scrollTopState',
defaultValue: 0,
});

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class EntityEventsToDbListener {
payload: ObjectRecordCreateEvent<any>,
operation: string,
) {

if (!payload.objectMetadata.isAuditLogged) {
return;
}
Expand Down