mirror of
https://github.com/Unleash/unleash.git
synced 2025-04-19 01:17:18 +02:00
This PR adds support for projects as a first class citizen, and toggling features on in different environments.
37 lines
887 B
TypeScript
37 lines
887 B
TypeScript
import useSWR, { mutate } from 'swr';
|
|
import { useState, useEffect } from 'react';
|
|
import { formatApiPath } from '../../../../utils/format-path';
|
|
|
|
import { IProjectCard } from '../../../../interfaces/project';
|
|
|
|
const useProjects = () => {
|
|
const fetcher = () => {
|
|
const path = formatApiPath(`api/admin/projects`);
|
|
return fetch(path, {
|
|
method: 'GET',
|
|
}).then(res => res.json());
|
|
};
|
|
|
|
const KEY = `api/admin/projects`;
|
|
|
|
const { data, error } = useSWR<{ projects: IProjectCard[] }>(KEY, fetcher);
|
|
const [loading, setLoading] = useState(!error && !data);
|
|
|
|
const refetch = () => {
|
|
mutate(KEY);
|
|
};
|
|
|
|
useEffect(() => {
|
|
setLoading(!error && !data);
|
|
}, [data, error]);
|
|
|
|
return {
|
|
projects: data?.projects || [],
|
|
error,
|
|
loading,
|
|
refetch,
|
|
};
|
|
};
|
|
|
|
export default useProjects;
|