Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(ui): added org context and user context

akhilmhdh8dc47110a0a9
diff --git a/frontend/src/context/OrganizationContext/OrganizationContext.tsx b/frontend/src/context/OrganizationContext/OrganizationContext.tsx
new file mode 100644
index 0000000000..6f5a1562e3
--- /dev/null
+++ b/frontend/src/context/OrganizationContext/OrganizationContext.tsx
@@ -0,0 +1,46 @@
+import { createContext, ReactNode, useContext, useMemo } from 'react';
+
+import { useGetOrganization } from '@app/hooks/api';
+import { Organization } from '@app/hooks/api/types';
+
+import { useWorkspace } from '../WorkspaceContext';
+
+type TOrgContext = {
+ orgs?: Organization[];
+ currentOrg?: Organization;
+ isLoading: boolean;
+};
+
+const OrgContext = createContext<TOrgContext | null>(null);
+
+type Props = {
+ children: ReactNode;
+};
+
+export const OrgProvider = ({ children }: Props): JSX.Element => {
+ const { currentWorkspace } = useWorkspace();
+ const { data: userOrgs, isLoading } = useGetOrganization();
+
+ const currentWsOrgID = currentWorkspace?.organization;
+
+ // memorize the workspace details for the context
+ const value = useMemo<TOrgContext>(
+ () => ({
+ org: userOrgs,
+ currentOrg: (userOrgs || []).find(({ _id }) => _id === currentWsOrgID),
+ isLoading
+ }),
+ [currentWsOrgID, userOrgs, isLoading]
+ );
+
+ return <OrgContext.Provider value={value}>{children}</OrgContext.Provider>;
+};
+
+export const useOrgnization = () => {
+ const ctx = useContext(OrgContext);
+ if (!ctx) {
+ throw new Error('useOrgnization to be used within <OrgContext.Provider>');
+ }
+
+ return ctx;
+};
diff --git a/frontend/src/context/OrganizationContext/index.tsx b/frontend/src/context/OrganizationContext/index.tsx
new file mode 100644
index 0000000000..8f21078cbf
--- /dev/null
+++ b/frontend/src/context/OrganizationContext/index.tsx
@@ -0,0 +1 @@
+export { OrgProvider, useOrgnization } from './OrganizationContext';
diff --git a/frontend/src/context/UserContext/UserContext.tsx b/frontend/src/context/UserContext/UserContext.tsx
new file mode 100644
index 0000000000..0c6ce95495
--- /dev/null
+++ b/frontend/src/context/UserContext/UserContext.tsx
@@ -0,0 +1,38 @@
+import { createContext, ReactNode, useContext, useMemo } from 'react';
+
+import { useGetUser } from '@app/hooks/api';
+import { User } from '@app/hooks/api/types';
+
+type TUserContext = {
+ user: User;
+ isLoading: boolean;
+};
+
+const UserContext = createContext<TUserContext | null>(null);
+
+type Props = {
+ children: ReactNode;
+};
+
+export const UserProvider = ({ children }: Props): JSX.Element => {
+ const { data, isLoading } = useGetUser();
+
+ // memorize the workspace details for the context
+ const value = useMemo<TUserContext>(() => {
+ return {
+ user: data!,
+ isLoading
+ };
+ }, [data, isLoading]);
+
+ return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
+};
+
+export const useUser = () => {
+ const ctx = useContext(UserContext);
+ if (!ctx) {
+ throw new Error('useUser has to be used within <UserContext.Provider>');
+ }
+
+ return ctx;
+};
diff --git a/frontend/src/context/UserContext/index.tsx b/frontend/src/context/UserContext/index.tsx
new file mode 100644
index 0000000000..44aa26a3b3
--- /dev/null
+++ b/frontend/src/context/UserContext/index.tsx
@@ -0,0 +1 @@
+export { UserProvider, useUser } from './UserContext';
diff --git a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx
index 4edef8e6ab..f2876d9962 100644
--- a/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx
+++ b/frontend/src/context/WorkspaceContext/WorkspaceContext.tsx
@@ -23,9 +23,10 @@ export const WorkspaceProvider = ({ children }: Props): JSX.Element => {
// memorize the workspace details for the context
const value = useMemo<TWorkspaceContext>(() => {
+ const wsId = workspaceId || localStorage.getItem('projectData.id');
return {
workspaces: ws || [],
- currentWorkspace: (ws || []).find(({ _id: id }) => id === workspaceId),
+ currentWorkspace: (ws || []).find(({ _id: id }) => id === wsId),
isLoading
};
}, [ws, workspaceId, isLoading]);
diff --git a/frontend/src/context/index.tsx b/frontend/src/context/index.tsx
index 1bf59f9ac4..31c7f7985e 100644
--- a/frontend/src/context/index.tsx
+++ b/frontend/src/context/index.tsx
@@ -1,2 +1,5 @@
+export { AuthProvider } from './AuthContext';
+export { OrgProvider, useOrgnization } from './OrganizationContext';
export { SubscriptionProvider, useSubscription } from './SubscriptionContext';
+export { UserProvider, useUser } from './UserContext';
export { useWorkspace, WorkspaceProvider } from './WorkspaceContext';