Skip to content

feat(site): add new filter to audit logs #7878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add ilters
  • Loading branch information
BrunoQuaresma committed Jun 6, 2023
commit 529ef8cda37c3beba8e41c02db31039828fa1cf5
65 changes: 64 additions & 1 deletion site/src/pages/AuditPage/AuditFilter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuditActions } from "api/typesGenerated"
import { AuditActions, ResourceTypes } from "api/typesGenerated"
import { UserFilterMenu, UserMenu } from "components/Filter/UserFilter"
import {
Filter,
Expand All @@ -22,6 +22,7 @@ export const AuditFilter = ({
menus: {
user: UserFilterMenu
action: ActionFilterMenu
resourceType: ResourceTypeFilterMenu
}
}) => {
return (
Expand All @@ -31,6 +32,7 @@ export const AuditFilter = ({
error={error}
options={
<>
<ResourceTypeMenu {...menus.resourceType} />
<ActionMenu {...menus.action} />
<UserMenu {...menus.user} />
</>
Expand All @@ -40,6 +42,7 @@ export const AuditFilter = ({
<SearchFieldSkeleton />
<MenuSkeleton />
<MenuSkeleton />
<MenuSkeleton />
</>
}
/>
Expand Down Expand Up @@ -83,3 +86,63 @@ const ActionMenu = (menu: ActionFilterMenu) => {
</FilterMenu>
)
}

export const useResourceTypeFilterMenu = ({
value,
onChange,
}: Pick<UseFilterMenuOptions<BaseOption>, "value" | "onChange">) => {
const actionOptions: BaseOption[] = ResourceTypes.map((type) => {
let label = capitalize(type)

if (type === "api_key") {
label = "API Key"
}

if (type === "git_ssh_key") {
label = "Git SSH Key"
}

if (type === "template_version") {
label = "Template Version"
}

if (type === "workspace_build") {
label = "Workspace Build"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine! We could probably shorten with a map if we wanted to, but not a blocker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I just went to what Copilot suggested to me 😆 since this is not complex code I think it is ok to let to refactor this if we need to extract it into a function to be reused anywhere else.


return {
value: type,
label,
}
})
return useFilterMenu({
onChange,
value,
id: "resourceType",
getSelectedOption: async () =>
actionOptions.find((option) => option.value === value) ?? null,
getOptions: async () => actionOptions,
})
}

export type ResourceTypeFilterMenu = ReturnType<
typeof useResourceTypeFilterMenu
>

const ResourceTypeMenu = (menu: ResourceTypeFilterMenu) => {
return (
<FilterMenu
id="resource-type-menu"
menu={menu}
label={
menu.selectedOption ? (
<OptionItem option={menu.selectedOption} />
) : (
"All resource types"
)
}
>
{(itemProps) => <OptionItem {...itemProps} />}
</FilterMenu>
)
}
11 changes: 10 additions & 1 deletion site/src/pages/AuditPage/AuditPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useDashboard } from "components/Dashboard/DashboardProvider"
import { usePagination } from "hooks"
import { useQuery } from "@tanstack/react-query"
import { getAuditLogs } from "api/api"
import { useActionFilterMenu } from "./AuditFilter"
import { useActionFilterMenu, useResourceTypeFilterMenu } from "./AuditFilter"

const AuditPage: FC = () => {
const dashboard = useDashboard()
Expand Down Expand Up @@ -39,6 +39,14 @@ const AuditPage: FC = () => {
action: option?.value,
}),
})
const resourceTypeMenu = useResourceTypeFilterMenu({
value: filter.values["resource_type"],
onChange: (option) =>
filter.update({
...filter.values,
resource_type: option?.value,
}),
})
const { audit_log: isAuditLogVisible } = useFeatureVisibility()
const { data, error } = useQuery({
queryKey: ["auditLogs", filter.query, pagination.page],
Expand Down Expand Up @@ -72,6 +80,7 @@ const AuditPage: FC = () => {
menus: {
user: userMenu,
action: actionMenu,
resourceType: resourceTypeMenu,
},
}
: {
Expand Down