Skip to content

Commit c113cb9

Browse files
committed
chore(clerk-js): Move useInView inside /hooks
+ Add default values in useLoadingStatus
1 parent 1cc9862 commit c113cb9

File tree

4 files changed

+51
-46
lines changed

4 files changed

+51
-46
lines changed

packages/clerk-js/src/ui/components/OrganizationSwitcher/OtherOrganizationActions.tsx

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { OrganizationResource, UserOrganizationInvitationResource } from '@clerk/types';
2-
import React, { useCallback, useRef, useState } from 'react';
32

43
import { Plus, SwitchArrows } from '../../../ui/icons';
54
import {
@@ -18,6 +17,7 @@ import {
1817
useCardState,
1918
withCardStateProvider,
2019
} from '../../elements';
20+
import { useInView } from '../../hooks';
2121
import { common } from '../../styledSystem';
2222
import { handleError } from '../../utils';
2323

@@ -34,50 +34,6 @@ export interface IntersectionOptions extends IntersectionObserverInit {
3434
onChange?: (inView: boolean, entry: IntersectionObserverEntry) => void;
3535
}
3636

37-
const useInView = (params: IntersectionOptions) => {
38-
const [inView, setInView] = useState(false);
39-
const observerRef = useRef<IntersectionObserver | null>(null);
40-
const thresholds = Array.isArray(params.threshold) ? params.threshold : [params.threshold || 0];
41-
const internalOnChange = React.useRef<IntersectionOptions['onChange']>();
42-
43-
internalOnChange.current = params.onChange;
44-
45-
const ref = useCallback((element: HTMLElement | null) => {
46-
if (!element) {
47-
if (observerRef.current) {
48-
observerRef.current.disconnect();
49-
}
50-
return;
51-
}
52-
53-
observerRef.current = new IntersectionObserver(
54-
entries => {
55-
entries.forEach(entry => {
56-
const _inView = entry.isIntersecting && thresholds.some(threshold => entry.intersectionRatio >= threshold);
57-
58-
setInView(_inView);
59-
60-
if (internalOnChange.current) {
61-
internalOnChange.current(_inView, entry);
62-
}
63-
});
64-
},
65-
{
66-
root: params.root,
67-
rootMargin: params.rootMargin,
68-
threshold: thresholds,
69-
},
70-
);
71-
72-
observerRef.current.observe(element);
73-
}, []);
74-
75-
return {
76-
inView,
77-
ref,
78-
};
79-
};
80-
8137
export const OrganizationActionList = (props: OrganizationActionListProps) => {
8238
const { onCreateOrganizationClick, onPersonalWorkspaceClick, onOrganizationClick } = props;
8339
const { organizationList, userInvitations } = useCoreOrganizationList({

packages/clerk-js/src/ui/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './useWindowEventListener';
44
export * from './useMagicLink';
55
export * from './useClipboard';
66
export * from './useEnabledThirdPartyProviders';
7+
export * from './useInView';
78
export * from './useLoadingStatus';
89
export * from './usePassword';
910
export * from './usePasswordComplexity';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { useCallback, useRef, useState } from 'react';
2+
3+
import type { IntersectionOptions } from '../components/OrganizationSwitcher/OtherOrganizationActions';
4+
5+
export const useInView = (params: IntersectionOptions) => {
6+
const [inView, setInView] = useState(false);
7+
const observerRef = useRef<IntersectionObserver | null>(null);
8+
const thresholds = Array.isArray(params.threshold) ? params.threshold : [params.threshold || 0];
9+
const internalOnChange = React.useRef<IntersectionOptions['onChange']>();
10+
11+
internalOnChange.current = params.onChange;
12+
13+
const ref = useCallback((element: HTMLElement | null) => {
14+
if (!element) {
15+
if (observerRef.current) {
16+
observerRef.current.disconnect();
17+
}
18+
return;
19+
}
20+
21+
observerRef.current = new IntersectionObserver(
22+
entries => {
23+
entries.forEach(entry => {
24+
const _inView = entry.isIntersecting && thresholds.some(threshold => entry.intersectionRatio >= threshold);
25+
26+
setInView(_inView);
27+
28+
if (internalOnChange.current) {
29+
internalOnChange.current(_inView, entry);
30+
}
31+
});
32+
},
33+
{
34+
root: params.root,
35+
rootMargin: params.rootMargin,
36+
threshold: thresholds,
37+
},
38+
);
39+
40+
observerRef.current.observe(element);
41+
}, []);
42+
43+
return {
44+
inView,
45+
ref,
46+
};
47+
};

packages/clerk-js/src/ui/hooks/useLoadingStatus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import { useSafeState } from './useSafeState';
22

33
type Status = 'idle' | 'loading' | 'error';
44

5-
export const useLoadingStatus = <Metadata>() => {
5+
export const useLoadingStatus = <Metadata>(initialState?: { status: Status; metadata?: Metadata | undefined }) => {
66
const [state, setState] = useSafeState<{ status: Status; metadata?: Metadata | undefined }>({
77
status: 'idle',
88
metadata: undefined,
9+
...initialState,
910
});
1011

1112
return {

0 commit comments

Comments
 (0)