fix: partially revert changes - reduce scope of pr

This commit is contained in:
Krrish Dholakia 2025-04-25 12:07:19 -07:00
parent 10849a8b6d
commit edf6064775
3 changed files with 87 additions and 91 deletions

View file

@ -94,12 +94,6 @@ const TeamFilter = ({
* The team selector and filtering have been removed so that all keys are shown.
*/
export interface FilterState {
'Team ID': string;
'Organization ID': string;
'Key Alias': string;
[key: string]: string;
}
export function AllKeysTable({
keys,
@ -123,9 +117,6 @@ export function AllKeysTable({
const [selectedKeyId, setSelectedKeyId] = useState<string | null>(null);
const [userList, setUserList] = useState<UserResponse[]>([]);
const lastSearchTimestamp = useRef(0);
const [filteredKeys, setFilteredKeys] = useState<KeyResponse[]>([]);
const [allTeams, setAllTeams] = useState<Team[]>([]);
const [allOrganizations, setAllOrganizations] = useState<Organization[]>([]);
// Use the filter logic hook
useEffect(() => {
@ -152,62 +143,25 @@ export function AllKeysTable({
}
}, [accessToken]);
const debouncedSearch = useCallback(
debounce(async (filters: FilterState) => {
if (!accessToken || !userRole || !userID) {
return;
}
const currentTimestamp = Date.now();
lastSearchTimestamp.current = currentTimestamp;
try {
// Make the API call using userListCall with all filter parameters
const data = await keyListCall(
accessToken,
filters["Organization ID"] || null,
filters["Team ID"] || null,
filters["Key Alias"] || null,
filters["User ID"] || null,
1, // Reset to first page when searching
defaultPageSize
);
// Only update state if this is the most recent search
if (currentTimestamp === lastSearchTimestamp.current) {
if (data) {
setFilteredKeys(data.keys);
console.log("called from debouncedSearch filters:", JSON.stringify(filters));
console.log("called from debouncedSearch data:", JSON.stringify(data));
}
}
} catch (error) {
console.error("Error searching users:", error);
}
}, 300),
[accessToken, userRole, userID]
);
const [filters, setFilters] = useState<FilterState>({
'Team ID': '',
'Organization ID': '',
'Key Alias': ''
const {
filters,
filteredKeys,
allKeyAliases,
allTeams,
allOrganizations,
handleFilterChange,
handleFilterReset
} = useFilterLogic({
keys,
teams,
organizations,
accessToken,
setSelectedTeam,
setCurrentOrg,
setSelectedKeyAlias
});
const handleFilterChange = (key: keyof FilterState, value: string) => {
const newFilters = { ...filters, [key]: value };
setFilters(newFilters);
console.log("called from handleFilterChange - newFilters:", JSON.stringify(newFilters));
debouncedSearch(newFilters);
};
const handleFilterReset = () => {
const resetFilters = Object.keys(filters).reduce((acc, key) => {
acc[key] = '';
return acc;
}, {} as FilterState);
setFilters(resetFilters);
};
useEffect(() => {
if (accessToken) {

View file

@ -1,38 +1,41 @@
import { keyListCall, teamListCall, organizationListCall } from '../networking';
import { Team } from './key_list';
import { Organization } from '../networking';
import { keyListCall, teamListCall, organizationListCall } from "../networking";
import { Team } from "./key_list";
import { Organization } from "../networking";
/**
* Fetches all key aliases across all pages
* @param accessToken The access token for API authentication
* @returns Array of all unique key aliases
*/
export const fetchAllKeyAliases = async (accessToken: string | null): Promise<string[]> => {
export const fetchAllKeyAliases = async (
accessToken: string | null
): Promise<string[]> => {
if (!accessToken) return [];
try {
// Fetch all pages of keys to extract aliases
let allAliases: string[] = [];
let currentPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const response = await keyListCall(
accessToken,
null, // organization_id
"", // team_id
null, // selectedKeyAlias
null, // user_id
currentPage,
100 // larger page size to reduce number of requests
);
// Extract aliases from this page
const pageAliases = response.keys
.map((key: any) => key.key_alias)
.filter(Boolean) as string[];
allAliases = [...allAliases, ...pageAliases];
// Check if there are more pages
if (currentPage < response.total_pages) {
currentPage++;
@ -40,7 +43,7 @@ export const fetchAllKeyAliases = async (accessToken: string | null): Promise<st
hasMorePages = false;
}
}
// Remove duplicates
return Array.from(new Set(allAliases));
} catch (error) {
@ -55,24 +58,27 @@ export const fetchAllKeyAliases = async (accessToken: string | null): Promise<st
* @param organizationId Optional organization ID to filter teams
* @returns Array of all teams
*/
export const fetchAllTeams = async (accessToken: string | null, organizationId?: string | null): Promise<Team[]> => {
export const fetchAllTeams = async (
accessToken: string | null,
organizationId?: string | null
): Promise<Team[]> => {
if (!accessToken) return [];
try {
let allTeams: Team[] = [];
let currentPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const response = await teamListCall(
accessToken,
organizationId || null,
null,
null
);
// Add teams from this page
allTeams = [...allTeams, ...response.teams];
allTeams = [...allTeams, ...response];
// Check if there are more pages
if (currentPage < response.total_pages) {
currentPage++;
@ -80,7 +86,7 @@ export const fetchAllTeams = async (accessToken: string | null, organizationId?:
hasMorePages = false;
}
}
return allTeams;
} catch (error) {
console.error("Error fetching all teams:", error);
@ -93,22 +99,22 @@ export const fetchAllTeams = async (accessToken: string | null, organizationId?:
* @param accessToken The access token for API authentication
* @returns Array of all organizations
*/
export const fetchAllOrganizations = async (accessToken: string | null): Promise<Organization[]> => {
export const fetchAllOrganizations = async (
accessToken: string | null
): Promise<Organization[]> => {
if (!accessToken) return [];
try {
let allOrganizations: Organization[] = [];
let currentPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const response = await organizationListCall(
accessToken
);
const response = await organizationListCall(accessToken);
// Add organizations from this page
allOrganizations = [...allOrganizations, ...response.organizations];
allOrganizations = [...allOrganizations, ...response];
// Check if there are more pages
if (currentPage < response.total_pages) {
currentPage++;
@ -116,7 +122,7 @@ export const fetchAllOrganizations = async (accessToken: string | null): Promise
hasMorePages = false;
}
}
return allOrganizations;
} catch (error) {
console.error("Error fetching all organizations:", error);

View file

@ -14,6 +14,42 @@ export interface FilterState {
[key: string]: string;
}
// const debouncedSearch = useCallback(
// debounce(async (filters: FilterState) => {
// if (!accessToken || !userRole || !userID) {
// return;
// }
// const currentTimestamp = Date.now();
// lastSearchTimestamp.current = currentTimestamp;
// try {
// // Make the API call using userListCall with all filter parameters
// const data = await keyListCall(
// accessToken,
// filters["Organization ID"] || null,
// filters["Team ID"] || null,
// filters["Key Alias"] || null,
// filters["User ID"] || null,
// 1, // Reset to first page when searching
// defaultPageSize
// );
// // Only update state if this is the most recent search
// if (currentTimestamp === lastSearchTimestamp.current) {
// if (data) {
// setFilteredKeys(data.keys);
// console.log("called from debouncedSearch filters:", JSON.stringify(filters));
// console.log("called from debouncedSearch data:", JSON.stringify(data));
// }
// }
// } catch (error) {
// console.error("Error searching users:", error);
// }
// }, 300),
// [accessToken, userRole, userID]
// );
export function useFilterLogic({
keys,
teams,