Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

fix(alerts): memoize selecting alerts #231

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import {
AlertConversation,
V1GetWorkspaceAlertsResponse,
} from "@/api/generated";
import { filterAlertsCritical } from "../lib/filter-alerts-critical";
import { isAlertMalicious } from "../lib/is-alert-malicious";
import { useQueryGetWorkspaceAlerts } from "./use-query-get-workspace-alerts";

// NOTE: This needs to be a stable function reference to enable memo-isation of
// the select operation on each React re-render.
function select(data: V1GetWorkspaceAlertsResponse): AlertConversation[] {
return filterAlertsCritical(data).filter(isAlertMalicious);
}

export function useQueryGetWorkspaceAlertsMaliciousPkg() {
return useQueryGetWorkspaceAlerts({
select: (data) => filterAlertsCritical(data).filter(isAlertMalicious),
select,
});
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import {
V1GetWorkspaceAlertsResponse,
AlertConversation,
} from "@/api/generated";
import { filterAlertsCritical } from "../lib/filter-alerts-critical";
import { isAlertSecret } from "../lib/is-alert-secret";
import { useQueryGetWorkspaceAlerts } from "./use-query-get-workspace-alerts";

// NOTE: This needs to be a stable function reference to enable memo-isation of
// the select operation on each React re-render.
function select(data: V1GetWorkspaceAlertsResponse): AlertConversation[] {
return filterAlertsCritical(data).filter(isAlertSecret);
}

export function useQueryGetWorkspaceAlertSecrets() {
return useQueryGetWorkspaceAlerts({
select: (data) => filterAlertsCritical(data).filter(isAlertSecret),
select,
});
}
9 changes: 8 additions & 1 deletion src/features/workspace/hooks/use-active-workspace-name.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ListActiveWorkspacesResponse } from "@/api/generated";
import { useActiveWorkspaces } from "./use-active-workspaces";

// NOTE: This needs to be a stable function reference to enable memo-isation of
// the select operation on each React re-render.
function select(data: ListActiveWorkspacesResponse | undefined): string | null {
return data?.workspaces?.[0]?.name ?? null;
}

export function useActiveWorkspaceName() {
return useActiveWorkspaces({
select: (d) => d?.workspaces?.[0]?.name ?? null,
select,
});
}
2 changes: 2 additions & 0 deletions src/hooks/useAlertsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export const useFilteredAlerts = () => {
const { isMaliciousFilterActive, search } = useAlertSearch();

return useAlertsData({
// NOTE: Inlined select will run on every render, we'll remove this over
// time though - AMG
select: (
data: Exclude<ReturnType<typeof useAlertsData>["data"], undefined>,
) => {
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/usePromptsData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
import { v1GetWorkspaceMessagesOptions } from "@/api/generated/@tanstack/react-query.gen";
import { useActiveWorkspaceName } from "@/features/workspace/hooks/use-active-workspace-name";

const selectConversations = (
data: V1GetWorkspaceMessagesResponse,
): Conversation[] => {
// NOTE: This needs to be a stable function reference to enable memo-isation of
// the select operation on each React re-render.
function select(data: V1GetWorkspaceMessagesResponse): Conversation[] {
return data.filter((prompt) =>
prompt.question_answers?.every((item) => item.answer && item.question),
);
};
}

export const usePromptsData = () => {
const { data: activeWorkspaceName } = useActiveWorkspaceName();
Expand All @@ -26,6 +26,6 @@ export const usePromptsData = () => {

return useQuery({
...v1GetWorkspaceMessagesOptions(options),
select: selectConversations,
select: select,
});
};
Loading