1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-04 11:17:02 +02:00
unleash.unleash/frontend/src/hooks/useLoading.ts
Thomas Heartman 3bc9fe9a9a
[wip] add data to ui (#8710)
Hooks up the project status lifecycle data to the UI. Adds some minor
refactoring as part of that effort.

## Other files

There's been some small changes to
`frontend/src/component/feature/FeatureView/FeatureOverview/FeatureLifecycle/FeatureLifecycleStageIcon.tsx`
and `frontend/src/hooks/useLoading.ts` as well to accommodate their
usage here and to remove unused stuff. The inline comments mention the
same thing but for posterity (especially after this is merged), the
comments are:

For
`frontend/src/component/feature/FeatureView/FeatureOverview/FeatureLifecycle/FeatureLifecycleStageIcon.tsx`:

> The icon only needs the name to pick.
https://github.com/Unleash/unleash/pull/7049 deliberately changed the
logic so that the completed stage gets the same icon regardless of its
status. As such, to make the icon easier to use other places (such as in
the lifecycle widget), we'll only require the name.

For `frontend/src/hooks/useLoading.ts`:
> There's no reason we should only be able to put refs on divs, as far
as I'm aware. TS was complaining that that a `ul` couldn't hold a div
reference, so I gave it a type parameter that defaults to the old
version.
2024-11-12 11:35:42 +01:00

26 lines
699 B
TypeScript

import { createRef, useLayoutEffect } from 'react';
const useLoading = <T extends HTMLElement = HTMLDivElement>(
loading: boolean,
selector = '[data-loading=true]',
) => {
const ref = createRef<T>();
useLayoutEffect(() => {
if (ref.current) {
const elements = ref.current.querySelectorAll(selector);
elements.forEach((element) => {
if (loading) {
element.classList.add('skeleton');
} else {
setTimeout(() => element.classList.remove('skeleton'), 10);
}
});
}
}, [loading, selector, ref]);
return ref;
};
export default useLoading;