Skip to content

Commit 9484f43

Browse files
authored
refactor: return required info from stable-session hook (#40)
refactor: return necessary information from use-stable-session
1 parent faec813 commit 9484f43

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

src/app/(app)/[repo]/_components/repo-contents.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { getRepoConfig } from '@/lib/api/github';
88
import datetime from '@/lib/date-time';
99
import { useRepoStore } from '@/stores/repo.store';
1010
import { Folder, Plus, Search } from 'lucide-react';
11-
import { useSession } from 'next-auth/react';
1211
import { useCallback, useEffect } from 'react';
1312
import { toast } from 'sonner';
1413

@@ -18,17 +17,16 @@ interface Props {
1817
}
1918

2019
export default function RepoContents({ repo, setIsConfigDialogOpen }: Props) {
21-
const { status } = useSession();
22-
const stableSession = useStableSession();
20+
const { session, status } = useStableSession();
2321
const { setConfig, setIsValid } = useRepoStore((state) => state);
2422
const { contents, isLoading } = useRepoContents(repo);
2523

2624
const loadConfigFilePromise = useCallback(
2725
() =>
2826
new Promise<void>(async (resolve, reject) => {
2927
const _config = await getRepoConfig({
30-
accessToken: stableSession?.accessToken,
31-
username: stableSession?.user?.username,
28+
accessToken: session?.accessToken,
29+
username: session?.user?.username,
3230
repo,
3331
});
3432

@@ -42,11 +40,11 @@ export default function RepoContents({ repo, setIsConfigDialogOpen }: Props) {
4240
setIsValid(true);
4341
}
4442
}),
45-
[stableSession, repo, setIsConfigDialogOpen, setIsValid, setConfig],
43+
[session, repo, setIsConfigDialogOpen, setIsValid, setConfig],
4644
);
4745

4846
useEffect(() => {
49-
if (!stableSession) return;
47+
if (!session) return;
5048

5149
// call toast to init config
5250
// and show some feedback
@@ -61,15 +59,15 @@ export default function RepoContents({ repo, setIsConfigDialogOpen }: Props) {
6159
description: 'Failed to load repo config file.',
6260
},
6361
});
64-
}, [stableSession, loadConfigFilePromise]);
62+
}, [session, loadConfigFilePromise]);
6563

6664
return (
6765
<div className="col-span-2 flex flex-col gap-2">
6866
<div className="flex items-center gap-4">
6967
<div className="flex items-center gap-2">
7068
<Avatar className="size-5">
71-
<AvatarImage src={stableSession?.user?.image ?? undefined} />
72-
<AvatarFallback>{stableSession?.user?.name?.[0]}</AvatarFallback>
69+
<AvatarImage src={session?.user?.image ?? undefined} />
70+
<AvatarFallback>{session?.user?.name?.[0]}</AvatarFallback>
7371
</Avatar>
7472
<span className="text-muted-foreground/50 text-xl">/</span>
7573
<Button variant={'ghost'} className="px-2 font-bold" asChild>
@@ -116,7 +114,7 @@ export default function RepoContents({ repo, setIsConfigDialogOpen }: Props) {
116114
<button className="text-sm hover:underline">{content.name}</button>
117115
</div>
118116
<a
119-
href={`https://github.com/${stableSession?.user?.username}/${repo}/commit/${content.lastCommit.sha}`}
117+
href={`https://github.com/${session?.user?.username}/${repo}/commit/${content.lastCommit.sha}`}
120118
target="_blank"
121119
rel="noreferrer noopener"
122120
className="text-muted-foreground col-span-2 text-sm hover:underline"

src/hooks/use-repo-contents.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { useQuery } from '@tanstack/react-query';
55
import { useStableSession } from './use-stable-session';
66

77
export default function useRepoContents(repo: string) {
8-
const stableSession = useStableSession();
8+
const { session } = useStableSession();
99
const { config } = useRepoStore((state) => state);
1010

1111
async function getRootContent(path: string): Promise<Content> {
1212
const lastCommit = await getLastCommit({
13-
accessToken: stableSession?.accessToken,
14-
username: stableSession?.user?.username,
13+
accessToken: session?.accessToken,
14+
username: session?.user?.username,
1515
repo,
1616
path,
1717
});

src/hooks/use-stable-session.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { useSession } from 'next-auth/react';
22
import { useMemo } from 'react';
33

44
export function useStableSession() {
5-
const { data: session } = useSession();
5+
const { data, status } = useSession();
66
// session gets synced everytime
77
// which re-triggers all checks
88
// eslint-disable-next-line react-hooks/exhaustive-deps
9-
const stableSession = useMemo(() => session, [session?.accessToken]);
9+
const session = useMemo(() => data, [data?.accessToken]);
1010

11-
return stableSession;
11+
return { session, status };
1212
}

0 commit comments

Comments
 (0)