mirror of
https://github.com/RayLabsHQ/gitea-mirror.git
synced 2025-12-12 22:46:46 +03:00
feat: Add Gitea configuration hook and enhance repository list with Gitea links
This commit is contained in:
@@ -145,3 +145,10 @@ export function useConfigStatus(): ConfigStatus {
|
||||
export function invalidateConfigCache() {
|
||||
configCache = { data: null, timestamp: 0, userId: null };
|
||||
}
|
||||
|
||||
// Export function to get cached config data for other hooks
|
||||
export function getCachedConfig(): ConfigApiResponse | null {
|
||||
const now = Date.now();
|
||||
const isCacheValid = configCache.data && (now - configCache.timestamp) < CACHE_DURATION;
|
||||
return isCacheValid ? configCache.data : null;
|
||||
}
|
||||
|
||||
73
src/hooks/useGiteaConfig.ts
Normal file
73
src/hooks/useGiteaConfig.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { useAuth } from './useAuth';
|
||||
import { apiRequest } from '@/lib/utils';
|
||||
import type { ConfigApiResponse, GiteaConfig } from '@/types/config';
|
||||
import { getCachedConfig } from './useConfigStatus';
|
||||
|
||||
interface GiteaConfigHook {
|
||||
giteaConfig: GiteaConfig | null;
|
||||
isLoading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to get Gitea configuration data
|
||||
* Uses the same cache as useConfigStatus to prevent duplicate API calls
|
||||
*/
|
||||
export function useGiteaConfig(): GiteaConfigHook {
|
||||
const { user } = useAuth();
|
||||
const [giteaConfigState, setGiteaConfigState] = useState<GiteaConfigHook>({
|
||||
giteaConfig: null,
|
||||
isLoading: true,
|
||||
error: null,
|
||||
});
|
||||
|
||||
const fetchGiteaConfig = useCallback(async () => {
|
||||
if (!user?.id) {
|
||||
setGiteaConfigState({
|
||||
giteaConfig: null,
|
||||
isLoading: false,
|
||||
error: 'User not authenticated',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to get from cache first
|
||||
const cachedConfig = getCachedConfig();
|
||||
if (cachedConfig) {
|
||||
setGiteaConfigState({
|
||||
giteaConfig: cachedConfig.giteaConfig || null,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
setGiteaConfigState(prev => ({ ...prev, isLoading: true, error: null }));
|
||||
|
||||
const configResponse = await apiRequest<ConfigApiResponse>(
|
||||
`/config?userId=${user.id}`,
|
||||
{ method: 'GET' }
|
||||
);
|
||||
|
||||
setGiteaConfigState({
|
||||
giteaConfig: configResponse?.giteaConfig || null,
|
||||
isLoading: false,
|
||||
error: null,
|
||||
});
|
||||
} catch (error) {
|
||||
setGiteaConfigState({
|
||||
giteaConfig: null,
|
||||
isLoading: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to fetch Gitea configuration',
|
||||
});
|
||||
}
|
||||
}, [user?.id]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchGiteaConfig();
|
||||
}, [fetchGiteaConfig]);
|
||||
|
||||
return giteaConfigState;
|
||||
}
|
||||
Reference in New Issue
Block a user