fix(frontend): resolve lint blockers for gitea frontend-ci
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext, useEffect, useState, ReactNode } from 'react';
|
||||
import { createContext, useContext, useEffect, useState, useMemo, ReactNode } from 'react';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
@@ -22,10 +22,31 @@ type TenantContextType = {
|
||||
|
||||
const TenantContext = createContext<TenantContextType | undefined>(undefined);
|
||||
|
||||
function getInitialClubId(): string | null {
|
||||
if (typeof window === 'undefined') return null;
|
||||
return localStorage.getItem('activeClubId');
|
||||
}
|
||||
|
||||
function determineActiveClub(clubs: Club[], currentActiveId: string | null): string | null {
|
||||
if (!clubs.length) return null;
|
||||
|
||||
const stored = getInitialClubId();
|
||||
if (stored && clubs.find(c => c.id === stored)) {
|
||||
return stored;
|
||||
}
|
||||
|
||||
if (currentActiveId && clubs.find(c => c.id === currentActiveId)) {
|
||||
return currentActiveId;
|
||||
}
|
||||
|
||||
return clubs[0].id;
|
||||
}
|
||||
|
||||
export function TenantProvider({ children }: { children: ReactNode }) {
|
||||
const { data: session, status } = useSession();
|
||||
const [activeClubId, setActiveClubId] = useState<string | null>(null);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const [activeClubId, setActiveClubId] = useState<string | null>(getInitialClubId);
|
||||
|
||||
const { data: clubs = [], isLoading: clubsLoading, error: clubsError } = useQuery<Club[]>({
|
||||
queryKey: ['my-clubs', session?.accessToken],
|
||||
@@ -43,25 +64,19 @@ export function TenantProvider({ children }: { children: ReactNode }) {
|
||||
retryDelay: (attemptIndex) => Math.min(1000 * Math.pow(2, attemptIndex), 10000),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (status === 'authenticated' && clubs.length > 0) {
|
||||
const stored = localStorage.getItem('activeClubId');
|
||||
if (stored && clubs.find(c => c.id === stored)) {
|
||||
setActiveClubId(stored);
|
||||
} else if (!activeClubId) {
|
||||
setActiveClubId(clubs[0].id);
|
||||
}
|
||||
}
|
||||
const computedActiveClubId = useMemo(() => {
|
||||
if (status !== 'authenticated' || !clubs.length) return activeClubId;
|
||||
return determineActiveClub(clubs, activeClubId);
|
||||
}, [status, clubs, activeClubId]);
|
||||
|
||||
useEffect(() => {
|
||||
if (activeClubId) {
|
||||
const selectedClub = clubs.find(c => c.id === activeClubId);
|
||||
if (computedActiveClubId) {
|
||||
const selectedClub = clubs.find(c => c.id === computedActiveClubId);
|
||||
if (selectedClub) {
|
||||
document.cookie = `X-Tenant-Id=${selectedClub.tenantId}; path=/; max-age=86400`;
|
||||
}
|
||||
}
|
||||
}, [activeClubId, clubs]);
|
||||
}, [computedActiveClubId, clubs]);
|
||||
|
||||
const handleSetActiveClub = (clubId: string) => {
|
||||
setActiveClubId(clubId);
|
||||
@@ -73,10 +88,10 @@ export function TenantProvider({ children }: { children: ReactNode }) {
|
||||
queryClient.invalidateQueries();
|
||||
};
|
||||
|
||||
const userRole = activeClubId && session?.user?.clubs ? session.user.clubs[activeClubId] || null : null;
|
||||
const userRole = computedActiveClubId && session?.user?.clubs ? session.user.clubs[computedActiveClubId] || null : null;
|
||||
|
||||
return (
|
||||
<TenantContext.Provider value={{ activeClubId, setActiveClub: handleSetActiveClub, userRole, clubs, clubsLoading, clubsError: clubsError || null }}>
|
||||
<TenantContext.Provider value={{ activeClubId: computedActiveClubId, setActiveClub: handleSetActiveClub, userRole, clubs, clubsLoading, clubsError: clubsError || null }}>
|
||||
{children}
|
||||
</TenantContext.Provider>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user