Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
annavik committed Dec 16, 2024
1 parent 29c3274 commit 0c53c30
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
16 changes: 12 additions & 4 deletions ui/src/components/error-state/error-state.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AlertCircleIcon } from 'lucide-react'
import { useMemo } from 'react'

interface ErrorStateProps {
error?: any
Expand All @@ -7,16 +8,23 @@ interface ErrorStateProps {
export const ErrorState = ({ error }: ErrorStateProps) => {
const title = error?.message ?? 'Unknown error'
const data = error?.response?.data
const description = data ? Object.values(data)?.[0] : undefined

const description = useMemo(() => {
const entries = data ? Object.entries(data) : undefined

if (entries?.length) {
const [key, value] = entries[0]

return `${key}: ${value}`
}
}, [error])

return (
<div className="flex flex-col items-center py-24">
<AlertCircleIcon className="w-8 h-8 text-destructive mb-8" />
<span className="body-large font-medium mb-2">{title}</span>
{description ? (
<span className="body-base text-muted-foreground">
{description as string}
</span>
<span className="body-base text-muted-foreground">{description}</span>
) : null}
</div>
)
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/filtering/filters/date-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export const DateFilter = ({ isValid, onAdd, onClear, value }: FilterProps) => {
const selected = value ? new Date(value) : undefined

const triggerLabel = (() => {
if (!selected) {
if (!value) {
return 'Select a date'
}

return dateToLabel(selected)
return value
})()

return (
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/filtering/filters/type-filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Select } from 'nova-ui-kit'
import { FilterProps } from './types'

const OPTIONS = SERVER_JOB_TYPES.map((key) => {
const typrInfo = Job.getJobTypeInfo(key)
const typeInfo = Job.getJobTypeInfo(key)

return {
...typrInfo,
...typeInfo,
}
})

Expand Down
14 changes: 8 additions & 6 deletions ui/src/utils/useFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const AVAILABLE_FILTERS: {
}

if (!isValid(new Date(value))) {
return 'Date has not a valid format.'
return 'Date is not valid'
}

const dateEnd = filters?.find(
Expand Down Expand Up @@ -112,7 +112,7 @@ export const AVAILABLE_FILTERS: {
export const useFilters = (defaultFilters?: { [field: string]: string }) => {
const [searchParams, setSearchParams] = useSearchParams()

const filters = AVAILABLE_FILTERS.map(({ field, ...rest }) => {
const _filters = AVAILABLE_FILTERS.map(({ field, ...rest }) => {
const value = searchParams.get(field) ?? defaultFilters?.[field]

return {
Expand All @@ -122,17 +122,19 @@ export const useFilters = (defaultFilters?: { [field: string]: string }) => {
}
})

const validatedFilters = filters.map(({ validate, value, ...rest }) => {
const error = validate ? validate(value, filters) : undefined
const filters = _filters.map(({ validate, value, ...rest }) => {
const error = validate ? validate(value, _filters) : undefined
const isValid = !error

return {
...rest,
value,
isValid,
error,
}
})

const activeFilters = filters.filter((filter) => filter.value?.length)
const activeFilters = filters.filter((filter) => !!filter.value?.length)

const addFilter = (field: string, value: string) => {
if (AVAILABLE_FILTERS.some((filter) => filter.field === field)) {
Expand All @@ -152,6 +154,6 @@ export const useFilters = (defaultFilters?: { [field: string]: string }) => {
activeFilters,
addFilter,
clearFilter,
filters: validatedFilters,
filters,
}
}

0 comments on commit 0c53c30

Please sign in to comment.