mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-10 01:16:39 +02:00
* fix: wait for api call before refetching * fix: set active environment from feature instead of cache * fix: remove console logs * fix: add permission icon button to project card * fix: remove project button * fix: empty tooltip if it is not passed * fix: add refresh interval * fix: permission buttons * fix: project permission buttons * fix: remove unused imports * fix: add projectId
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
import useSWR, { mutate, SWRConfiguration } from 'swr';
|
|
import { useState, useEffect } from 'react';
|
|
import { getProjectFetcher } from './getProjectFetcher';
|
|
import { IProject } from '../../../../interfaces/project';
|
|
import { fallbackProject } from './fallbackProject';
|
|
import useSort from '../../../useSort';
|
|
|
|
const useProject = (id: string, options: SWRConfiguration = {}) => {
|
|
const { KEY, fetcher } = getProjectFetcher(id);
|
|
const [sort] = useSort();
|
|
|
|
const { data, error } = useSWR<IProject>(KEY, fetcher, options);
|
|
const [loading, setLoading] = useState(!error && !data);
|
|
|
|
const refetch = () => {
|
|
mutate(KEY);
|
|
};
|
|
|
|
useEffect(() => {
|
|
setLoading(!error && !data);
|
|
}, [data, error]);
|
|
|
|
const sortedData = (data: IProject | undefined): IProject => {
|
|
if (data) {
|
|
return { ...data, features: sort(data.features || []) };
|
|
}
|
|
return fallbackProject;
|
|
};
|
|
|
|
return {
|
|
project: sortedData(data),
|
|
error,
|
|
loading,
|
|
refetch,
|
|
};
|
|
};
|
|
|
|
export default useProject;
|