mirror of
https://github.com/freedbygrace/DynamoDNS.git
synced 2026-08-13 19:46:53 +00:00
Implement initial application structure and UI.
Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9111ef36-26c8-4085-84ca-a35dc1fec1b5 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/7083d608-d6d3-4a6a-9a27-6286c5109627/54918520-d42c-44cd-b4f4-9e5a42eb5be1.jpg
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { createContext, useContext, useState, useEffect, ReactNode } from "react";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Organization } from "@shared/schema";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
|
||||
type OrganizationContextType = {
|
||||
organizations: Organization[];
|
||||
currentOrganization: Organization | null;
|
||||
setCurrentOrganization: (organization: Organization) => void;
|
||||
isLoading: boolean;
|
||||
};
|
||||
|
||||
const OrganizationContext = createContext<OrganizationContextType | undefined>(undefined);
|
||||
|
||||
export function OrganizationProvider({ children }: { children: ReactNode }) {
|
||||
const { user } = useAuth();
|
||||
const [currentOrganization, setCurrentOrganization] = useState<Organization | null>(null);
|
||||
|
||||
const { data: organizations = [], isLoading } = useQuery<Organization[]>({
|
||||
queryKey: ["/api/organizations"],
|
||||
// Only fetch if user is logged in
|
||||
enabled: !!user,
|
||||
});
|
||||
|
||||
// Set default organization when organizations are loaded
|
||||
useEffect(() => {
|
||||
if (organizations.length > 0 && !currentOrganization) {
|
||||
// If user has an organization ID, try to find it in the list
|
||||
if (user?.organizationId) {
|
||||
const userOrg = organizations.find(org => org.id === user.organizationId);
|
||||
if (userOrg) {
|
||||
setCurrentOrganization(userOrg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, use the first organization
|
||||
setCurrentOrganization(organizations[0]);
|
||||
}
|
||||
}, [organizations, currentOrganization, user]);
|
||||
|
||||
return (
|
||||
<OrganizationContext.Provider
|
||||
value={{ organizations, currentOrganization, setCurrentOrganization, isLoading }}
|
||||
>
|
||||
{children}
|
||||
</OrganizationContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useOrganization() {
|
||||
const context = useContext(OrganizationContext);
|
||||
|
||||
if (context === undefined) {
|
||||
throw new Error("useOrganization must be used within an OrganizationProvider");
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user