aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2022-05-10 17:38:07 +0200
committerArmand Philippot <git@armandphilippot.com>2022-05-13 15:46:01 +0200
commit9c8921db92d16b07ffc2a63ff3c80c4dcdd9ff9d (patch)
tree52e87fa8e758ec51cfbf7aa200982e0a6f5ab1ca /src/utils/hooks
parent0d59a6d2995b4119865271ed1908ede0bb96497c (diff)
chore: add Project single pages
Diffstat (limited to 'src/utils/hooks')
-rw-r--r--src/utils/hooks/use-github-api.tsx30
-rw-r--r--src/utils/hooks/use-headings-tree.tsx2
2 files changed, 31 insertions, 1 deletions
diff --git a/src/utils/hooks/use-github-api.tsx b/src/utils/hooks/use-github-api.tsx
new file mode 100644
index 0000000..edff974
--- /dev/null
+++ b/src/utils/hooks/use-github-api.tsx
@@ -0,0 +1,30 @@
+import { SWRResult } from '@ts/types/swr';
+import useSWR, { Fetcher } from 'swr';
+
+export type RepoData = {
+ created_at: string;
+ updated_at: string;
+ stargazers_count: number;
+};
+
+const fetcher: Fetcher<RepoData, string> = (...args) =>
+ fetch(...args).then((res) => res.json());
+
+/**
+ * Retrieve data from Github API.
+ *
+ * @param repo - The Github repo (`owner/repo-name`).
+ * @returns The repository data.
+ */
+const useGithubApi = (repo: string): SWRResult<RepoData> => {
+ const apiUrl = repo ? `https://api.github.com/repos/${repo}` : null;
+ const { data, error } = useSWR<RepoData>(apiUrl, fetcher);
+
+ return {
+ data,
+ isLoading: !error && !data,
+ isError: error,
+ };
+};
+
+export default useGithubApi;
diff --git a/src/utils/hooks/use-headings-tree.tsx b/src/utils/hooks/use-headings-tree.tsx
index 5506e8b..4646b4a 100644
--- a/src/utils/hooks/use-headings-tree.tsx
+++ b/src/utils/hooks/use-headings-tree.tsx
@@ -1,4 +1,4 @@
-import { slugify } from '@utils/helpers/slugify';
+import { slugify } from '@utils/helpers/strings';
import { useCallback, useEffect, useMemo, useState } from 'react';
export type Heading = {