Skip to content

Commit

Permalink
fix: flickering of loading state for datasection component
Browse files Browse the repository at this point in the history
  • Loading branch information
johangirod committed Apr 22, 2024
1 parent b85a630 commit ade164c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions components/section/data-section/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export function DataSectionClient<T>({
...props
}: IDataSectionClientProps<T>) {
const showLoadingState = useShowLoadingState(data);

if (isAPILoading(data) && !showLoadingState) {
return <div style={{ minHeight: '100px' }} />;
}
Expand All @@ -49,19 +50,19 @@ export function DataSectionClient<T>({

/** Contains the logic that prevents flickering of UI */
function useShowLoadingState<T>(data: IAPILoading | T): data is IAPILoading {
const after100ms = useTimeout(100);
const before100ms = !useTimeout(100);
const before800ms = !useTimeout(800);
const [dataLoadedBefore100ms, setDataLoadedBefore100ms] = useState(
!isAPILoading(data)
before100ms && !isAPILoading(data)
);

useEffect(() => {
if (!isAPILoading(data) && !after100ms) {
if (!isAPILoading(data) && before100ms) {
setDataLoadedBefore100ms(true);
}
}, [data, after100ms]);
}, [data]);

if (dataLoadedBefore100ms) {
if (before100ms || dataLoadedBefore100ms) {
return false;
}
if (before800ms) {
Expand Down

0 comments on commit ade164c

Please sign in to comment.