Skip to content

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

feat(sub-org): migrate SubOrgsView to server-side search, ordering, and pagination

Igor Correa3fe8e7738eea
diff --git a/frontend/src/hooks/api/subOrganizations/queries.tsx b/frontend/src/hooks/api/subOrganizations/queries.tsx
index 99ccb77702..2bf2c1819f 100644
--- a/frontend/src/hooks/api/subOrganizations/queries.tsx
+++ b/frontend/src/hooks/api/subOrganizations/queries.tsx
@@ -12,17 +12,20 @@ export const subOrganizationsQuery = {
queryOptions({
queryKey: subOrganizationsQuery.listKey(params),
queryFn: async () => {
- const { data } = await apiRequest.get<{ organizations: TSubOrganization[] }>(
- "/api/v1/sub-organizations",
- {
- params: {
- limit: params.limit,
- offset: params.offset,
- isAccessible: params.isAccessible
- }
+ const { data } = await apiRequest.get<{
+ organizations: TSubOrganization[];
+ totalCount: number;
+ }>("/api/v1/sub-organizations", {
+ params: {
+ limit: params.limit,
+ offset: params.offset,
+ search: params.search,
+ orderBy: params.orderBy,
+ orderDirection: params.orderDirection,
+ isAccessible: params.isAccessible
}
- );
- return data.organizations;
+ });
+ return data;
}
})
};
diff --git a/frontend/src/hooks/api/subOrganizations/types.ts b/frontend/src/hooks/api/subOrganizations/types.ts
index 4eb4c255a0..7f28810e20 100644
--- a/frontend/src/hooks/api/subOrganizations/types.ts
+++ b/frontend/src/hooks/api/subOrganizations/types.ts
@@ -15,6 +15,9 @@ export type TCreateSubOrganizationDTO = {
export type TListSubOrganizationsDTO = {
limit?: number;
offset?: number;
+ search?: string;
+ orderBy?: "name";
+ orderDirection?: "asc" | "desc";
isAccessible?: boolean;
};
diff --git a/frontend/src/layouts/OrganizationLayout/components/NavBar/Navbar.tsx b/frontend/src/layouts/OrganizationLayout/components/NavBar/Navbar.tsx
index e7f31dfbbd..b66394dac3 100644
--- a/frontend/src/layouts/OrganizationLayout/components/NavBar/Navbar.tsx
+++ b/frontend/src/layouts/OrganizationLayout/components/NavBar/Navbar.tsx
@@ -214,7 +214,8 @@ export const Navbar = () => {
const subOrgQuery = subOrganizationsQuery.list({ limit: 500, isAccessible: true });
const { data: subOrganizations = [] } = useQuery({
...subOrgQuery,
- enabled: Boolean(subscription.subOrganization)
+ enabled: Boolean(subscription.subOrganization),
+ select: (data) => data.organizations
});
const isCardDeclined = Boolean(subscription?.cardDeclined);
diff --git a/frontend/src/pages/organization/ProjectsPage/components/SubOrgsView.tsx b/frontend/src/pages/organization/ProjectsPage/components/SubOrgsView.tsx
index d35097efb7..8bbddcec12 100644
--- a/frontend/src/pages/organization/ProjectsPage/components/SubOrgsView.tsx
+++ b/frontend/src/pages/organization/ProjectsPage/components/SubOrgsView.tsx
@@ -47,7 +47,7 @@ import {
PreferenceKey,
setUserTablePreference
} from "@app/helpers/userTablePreferences";
-import { usePagination, useResetPageHelper, useToggle } from "@app/hooks";
+import { useDebounce, usePagination, useResetPageHelper, useToggle } from "@app/hooks";
import { authKeys, selectOrganization } from "@app/hooks/api/auth/queries";
import { MfaMethod } from "@app/hooks/api/auth/types";
import { OrderByDirection } from "@app/hooks/api/generic/types";
@@ -118,6 +118,7 @@ export const SubOrgsView = () => {
const [listView, setListView] = useState<SubOrgListView>(SubOrgListView.MySubOrgs);
const effectiveListView = hasDirectAccess ? listView : SubOrgListView.AllSubOrgs;
const [searchFilter, setSearchFilter] = useState("");
+ const [debouncedSearch] = useDebounce(searchFilter);
const [selectedSubOrg, setSelectedSubOrg] = useState<TSubOrganization | null>(null);
const { popUp, handlePopUpOpen, handlePopUpToggle, handlePopUpClose } = usePopUp([
@@ -144,22 +145,46 @@ export const SubOrgsView = () => {
setUserTablePreference("subOrgsTable", PreferenceKey.PerPage, newPerPage);
};
- const { data: allSubOrgs = [], isPending: isAllLoading } = useQuery(
- subOrganizationsQuery.list({ limit: 500 })
- );
+ const isAllSubOrgsView = hasDirectAccess && effectiveListView === SubOrgListView.AllSubOrgs;
+ const orderDir = orderDirection === OrderByDirection.ASC ? "asc" : "desc";
+
+ const { data: allSubOrgsData, isPending: isAllLoading } = useQuery({
+ ...subOrganizationsQuery.list({
+ search: debouncedSearch || undefined,
+ orderBy: "name",
+ orderDirection: orderDir,
+ limit,
+ offset
+ }),
+ enabled: isAllSubOrgsView
+ });
- const { data: mySubOrgs = [], isPending: isMyLoading } = useQuery(
- subOrganizationsQuery.list({ limit: 500, isAccessible: true })
+ const { data: mySubOrgsData, isPending: isMyLoading } = useQuery(
+ subOrganizationsQuery.list({
+ search: debouncedSearch || undefined,
+ orderBy: "name",
+ orderDirection: orderDir,
+ limit,
+ offset,
+ isAccessible: true
+ })
);
- const isLoading = isAllLoading || isMyLoading;
+ // Used only to determine isMember flag when showing the full list; unaffected by search/pagination.
+ const { data: mySubOrgIds = new Set<string>() } = useQuery({
+ ...subOrganizationsQuery.list({ limit: 500, isAccessible: true }),
+ enabled: isAllSubOrgsView,
+ select: (data) => new Set(data.organizations.map((o) => o.id))
+ });
- const mySubOrgIds = new Set(mySubOrgs.map((o) => o.id));
+ const isLoading = isAllSubOrgsView ? isAllLoading : isMyLoading;
+ const activeData = isAllSubOrgsView ? allSubOrgsData : mySubOrgsData;
+ const totalCount = activeData?.totalCount ?? 0;
+ const activeOrgs = activeData?.organizations ?? [];
- const subOrgsWithMember: SubOrgWithMember[] =
- !hasDirectAccess || effectiveListView === SubOrgListView.MySubOrgs
- ? mySubOrgs.map((o) => ({ ...o, isMember: true }))
- : allSubOrgs.map((o) => ({ ...o, isMember: mySubOrgIds.has(o.id) }));
+ const paginatedSubOrgs: SubOrgWithMember[] = isAllSubOrgsView
+ ? activeOrgs.map((o) => ({ ...o, isMember: mySubOrgIds.has(o.id) }))
+ : activeOrgs.map((o) => ({ ...o, isMember: true }));
const { mutateAsync: updateSubOrg, isPending: isUpdating } = useUpdateSubOrganization();
const { mutateAsync: deleteSubOrg } = useDeleteSubOrganization();
@@ -174,17 +199,7 @@ export const SubOrgsView = () => {
resolver: zodResolver(editSubOrgSchema)
});
- const filteredSubOrgs = subOrgsWithMember
- .filter((org) => org.name.toLowerCase().includes(searchFilter.toLowerCase()))
- .sort((a, b) =>
- orderDirection === OrderByDirection.ASC
- ? a.name.toLowerCase().localeCompare(b.name.toLowerCase())
- : b.name.toLowerCase().localeCompare(a.name.toLowerCase())
- );
-
- useResetPageHelper({ setPage, offset, totalCount: filteredSubOrgs.length });
-
- const paginatedSubOrgs = filteredSubOrgs.slice(offset, limit * page);
+ useResetPageHelper({ setPage, offset, totalCount });
const handleLoginSubOrg = async (subOrgId: string) => {
const { token, isMfaEnabled, mfaMethod } = await selectOrganization({
@@ -408,7 +423,7 @@ export const SubOrgsView = () => {
{paginatedSubOrgs.map((subOrg, index) => renderListItem(subOrg, index))}
</div>
);
- } else if (filteredSubOrgs.length === 0 && searchFilter) {
+ } else if (totalCount === 0 && searchFilter) {
content = (
<div className="mt-4 w-full rounded-md border border-mineshaft-700 bg-mineshaft-800 px-4 py-6 text-base text-mineshaft-300">
<FontAwesomeIcon
@@ -547,7 +562,7 @@ export const SubOrgsView = () => {
{content}
- {!isLoading && filteredSubOrgs.length > 0 && (
+ {!isLoading && totalCount > 0 && (
<Pagination
className={
viewMode === SubOrgsViewMode.GRID
@@ -556,7 +571,7 @@ export const SubOrgsView = () => {
}
perPage={perPage}
perPageList={[12, 24, 48, 96]}
- count={filteredSubOrgs.length}
+ count={totalCount}
page={page}
onChangePage={setPage}
onChangePerPage={handlePerPageChange}