forked from keephq/keep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Feat/runbook' into feat/runbook-1780-main-final
Signed-off-by: Rajesh Jonnalagadda <[email protected]>
- Loading branch information
Showing
15 changed files
with
674 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
keep-ui/app/incidents/incident-table-filters-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import {Dispatch, SetStateAction, useCallback, useContext, useEffect} from 'react'; | ||
|
||
import { createContext, useState, FC, PropsWithChildren } from "react"; | ||
import { useSearchParams, useRouter, usePathname } from "next/navigation"; | ||
import {useIncidentsMeta} from "../../utils/hooks/useIncidents"; | ||
import {IncidentsMetaDto} from "./models"; | ||
|
||
interface IIncidentFilterContext { | ||
meta: IncidentsMetaDto | undefined; | ||
|
||
statuses: string[]; | ||
severities: string[]; | ||
assignees: string[]; | ||
services: string[]; | ||
sources: string[]; | ||
|
||
setStatuses: (value: string[]) => void; | ||
setSeverities: (value: string[]) => void; | ||
setAssignees: (value: string[]) => void; | ||
setServices: (value: string[]) => void; | ||
setSources: (value: string[]) => void; | ||
} | ||
|
||
const IncidentFilterContext = createContext<IIncidentFilterContext | null>(null); | ||
|
||
export const IncidentFilterContextProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const router = useRouter(); | ||
const pathname = usePathname(); | ||
const searchParams = useSearchParams(); | ||
|
||
const {data: incidentsMeta, isLoading} = useIncidentsMeta(); | ||
|
||
const setFilterValue = (filterName: string) => { | ||
return () => { | ||
if (incidentsMeta === undefined) return []; | ||
|
||
const values = searchParams?.get(filterName); | ||
const valuesArray = values?.split(',').filter( | ||
value => incidentsMeta[filterName as keyof IncidentsMetaDto]?.includes(value) | ||
); | ||
|
||
return (valuesArray || []) as string[]; | ||
} | ||
} | ||
|
||
const [statuses, setStatuses] = useState<string[]>(setFilterValue("statuses")); | ||
const [severities, setSeverities] = useState<string[]>(setFilterValue("severities")); | ||
const [assignees, setAssignees] = useState<string[]>(setFilterValue("assignees")); | ||
const [services, setServices] = useState<string[]>(setFilterValue("services")); | ||
const [sources, setSources] = useState<string[]>(setFilterValue("sources")); | ||
|
||
useEffect(() => { | ||
if (!isLoading) { | ||
setStatuses(setFilterValue("statuses")); | ||
setSeverities(setFilterValue("severities")); | ||
setAssignees(setFilterValue("assignees")); | ||
setServices(setFilterValue("services")); | ||
setSources(setFilterValue("sources")); | ||
} | ||
}, [isLoading]) | ||
|
||
const createQueryString = useCallback( | ||
(name: string, value: string[]) => { | ||
const params = new URLSearchParams(searchParams?.toString()) | ||
if (value.length == 0) { | ||
params.delete(name); | ||
} else { | ||
params.set(name, value.join(",")); | ||
} | ||
|
||
|
||
return params.toString(); | ||
}, | ||
[searchParams] | ||
) | ||
|
||
const filterSetter = (filterName: string, stateSetter: Dispatch<SetStateAction<string[]>>) => { | ||
return (value: string[]) => { | ||
router.push(pathname + '?' + createQueryString(filterName, value)); | ||
stateSetter(value); | ||
} | ||
} | ||
|
||
const contextValue: IIncidentFilterContext = { | ||
meta: incidentsMeta, | ||
statuses, | ||
severities, | ||
assignees, | ||
services, | ||
sources, | ||
|
||
setStatuses: filterSetter("statuses", setStatuses), | ||
setSeverities: filterSetter("severities", setSeverities), | ||
setAssignees: filterSetter("assignees", setAssignees), | ||
setServices: filterSetter("services", setServices), | ||
setSources: filterSetter("sources", setSources), | ||
} | ||
|
||
return <IncidentFilterContext.Provider value={contextValue}>{children}</IncidentFilterContext.Provider> | ||
} | ||
|
||
export const useIncidentFilterContext = (): IIncidentFilterContext => { | ||
const filterContext = useContext(IncidentFilterContext); | ||
|
||
if (!filterContext) { | ||
throw new ReferenceError('Usage of useIncidentFilterContext outside of IncidentFilterContext provider is forbidden'); | ||
} | ||
|
||
return filterContext; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import type { FC } from "react"; | ||
import { MultiSelect, MultiSelectItem } from "@tremor/react"; | ||
import { useIncidentFilterContext } from "./incident-table-filters-context"; | ||
import { capitalize } from "@/utils/helpers"; | ||
|
||
export const IncidentTableFilters: FC = (props) => { | ||
const { | ||
meta, | ||
statuses, | ||
severities, | ||
assignees, | ||
services, | ||
sources, | ||
setStatuses, | ||
setSeverities, | ||
setAssignees, | ||
setServices, | ||
setSources, | ||
} = useIncidentFilterContext(); | ||
|
||
return ( | ||
<div className="flex flex-col md:flex-row gap-2"> | ||
{/* TODO: use copy-and-paste multiselect component to be able to control the width */} | ||
<MultiSelect | ||
onValueChange={setStatuses} | ||
value={statuses} | ||
placeholder="Status" | ||
> | ||
{meta?.statuses.map((value) => ( | ||
<MultiSelectItem key={value} value={value}> | ||
{capitalize(value)} | ||
</MultiSelectItem> | ||
))} | ||
</MultiSelect> | ||
|
||
<MultiSelect | ||
onValueChange={setSeverities} | ||
value={severities} | ||
placeholder="Severity" | ||
> | ||
{meta?.severities.map((value) => ( | ||
<MultiSelectItem key={value} value={value}> | ||
{capitalize(value)} | ||
</MultiSelectItem> | ||
))} | ||
</MultiSelect> | ||
|
||
<MultiSelect | ||
onValueChange={setAssignees} | ||
value={assignees} | ||
placeholder="Assignee" | ||
> | ||
{meta?.assignees.map((value) => ( | ||
<MultiSelectItem key={value} value={value}> | ||
{capitalize(value)} | ||
</MultiSelectItem> | ||
))} | ||
</MultiSelect> | ||
|
||
<MultiSelect | ||
onValueChange={setServices} | ||
value={services} | ||
placeholder="Service" | ||
> | ||
{meta?.services.map((value) => ( | ||
<MultiSelectItem key={value} value={value}> | ||
{capitalize(value)} | ||
</MultiSelectItem> | ||
))} | ||
</MultiSelect> | ||
|
||
<MultiSelect | ||
onValueChange={setSources} | ||
value={sources} | ||
placeholder="Source" | ||
> | ||
{meta?.sources.map((value) => ( | ||
<MultiSelectItem key={value} value={value}> | ||
{capitalize(value)} | ||
</MultiSelectItem> | ||
))} | ||
</MultiSelect> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use client"; | ||
import {IncidentFilterContextProvider} from "./incident-table-filters-context"; | ||
|
||
export default function Layout({ children }: { children: any }) { | ||
return <IncidentFilterContextProvider>{children}</IncidentFilterContextProvider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.