|
1 |
| -import { getLastCommit } from '@/lib/api/github'; |
| 1 | +import { getLastCommit, getPathContents } from '@/lib/api/github'; |
2 | 2 | import { useRepoStore } from '@/stores/repo.store';
|
3 | 3 | import { Content } from '@/types/github';
|
4 | 4 | import { useQuery } from '@tanstack/react-query';
|
| 5 | +import { useCallback, useMemo, useState } from 'react'; |
5 | 6 | import { useStableSession } from './use-stable-session';
|
6 | 7 |
|
7 | 8 | export default function useRepoContents(repo: string) {
|
8 | 9 | const { session } = useStableSession();
|
9 | 10 | const { config } = useRepoStore((state) => state);
|
10 | 11 |
|
11 |
| - async function getRootContent(path: string): Promise<Content> { |
12 |
| - const lastCommit = await getLastCommit({ |
| 12 | + const [history, setHistory] = useState<string[]>(['<root>']); |
| 13 | + const currentPath = history[history.length - 1]; |
| 14 | + const canGoBack = history.length > 1; |
| 15 | + |
| 16 | + const fetchParams = useMemo( |
| 17 | + () => ({ |
13 | 18 | accessToken: session?.accessToken,
|
14 | 19 | username: session?.user?.username,
|
15 | 20 | repo,
|
16 |
| - path, |
17 |
| - }); |
| 21 | + }), |
| 22 | + [session, repo], |
| 23 | + ); |
18 | 24 |
|
19 |
| - return { |
20 |
| - lastCommit, |
21 |
| - name: path, |
22 |
| - path, |
23 |
| - type: 'dir', |
24 |
| - }; |
25 |
| - } |
| 25 | + const _getContentDetails = useCallback( |
| 26 | + async (path: string, type: Content['type']): Promise<Content> => { |
| 27 | + const lastCommit = await getLastCommit({ |
| 28 | + ...fetchParams, |
| 29 | + path, |
| 30 | + }); |
26 | 31 |
|
27 |
| - const { data: contents, isLoading } = useQuery<Content[]>({ |
28 |
| - queryKey: ['repoContents', repo], |
29 |
| - queryFn: async () => { |
30 |
| - if (!config) return []; |
| 32 | + return { |
| 33 | + lastCommit, |
| 34 | + name: path.split('/').pop() ?? path, |
| 35 | + path, |
| 36 | + type, |
| 37 | + }; |
| 38 | + }, |
| 39 | + [fetchParams], |
| 40 | + ); |
| 41 | + |
| 42 | + const _getRootContents = useCallback(async (): Promise<Content[]> => { |
| 43 | + if (!config) return []; |
| 44 | + |
| 45 | + const paths = [...Object.values(config.contentTypes).map((item) => item.path), '.gitloom']; |
| 46 | + return Promise.all(paths.map((path) => _getContentDetails(path, 'dir'))); |
| 47 | + }, [config, _getContentDetails]); |
| 48 | + |
| 49 | + const _getPathContents = useCallback( |
| 50 | + async (path: string): Promise<Content[]> => { |
| 51 | + const contents = await getPathContents({ |
| 52 | + ...fetchParams, |
| 53 | + path, |
| 54 | + }); |
31 | 55 |
|
32 |
| - const paths = [...Object.values(config.contentTypes).map((item) => item.path), '.gitloom']; |
33 |
| - return Promise.all(paths.map((path) => getRootContent(path))); |
| 56 | + return Promise.all(contents.map((item) => _getContentDetails(item.path, item.type))); |
34 | 57 | },
|
| 58 | + [fetchParams, _getContentDetails], |
| 59 | + ); |
| 60 | + |
| 61 | + const { data: contents, isLoading } = useQuery<Content[]>({ |
| 62 | + queryKey: ['repoContents', repo, currentPath], |
| 63 | + queryFn: async () => |
| 64 | + currentPath === '<root>' ? await _getRootContents() : await _getPathContents(currentPath), |
35 | 65 | enabled: !!config,
|
36 | 66 | });
|
37 | 67 |
|
38 |
| - return { contents, isLoading }; |
| 68 | + const navigateTo = useCallback((path: string) => { |
| 69 | + setHistory((prev) => [...prev, path]); |
| 70 | + }, []); |
| 71 | + |
| 72 | + const navigateBack = useCallback(() => { |
| 73 | + setHistory((prev) => prev.slice(0, prev.length - 1)); |
| 74 | + }, []); |
| 75 | + |
| 76 | + const navigateBackTo = useCallback((path: string) => { |
| 77 | + setHistory((prev) => { |
| 78 | + const targetIdx = prev.findIndex((p) => p === path); |
| 79 | + if (targetIdx === -1) return prev; |
| 80 | + |
| 81 | + return prev.slice(0, targetIdx + 1); |
| 82 | + }); |
| 83 | + }, []); |
| 84 | + |
| 85 | + return { |
| 86 | + contents, |
| 87 | + isLoading, |
| 88 | + currentPath, |
| 89 | + navigateTo, |
| 90 | + navigateBack, |
| 91 | + navigateBackTo, |
| 92 | + canGoBack, |
| 93 | + }; |
39 | 94 | }
|
0 commit comments