-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
INN 2940 create select shared component (#1311)
* Add new status icons * Replace status icon in table cells * Add Select component * Change class order * Draft * Fix run status type errors * Simplify Select * Adjust styles * Fixes * Replace icon by remix * Remove fragment * Add styles * Remove code from next PR --------- Co-authored-by: Aaron Harper <aaron@inngest.com>
- Loading branch information
1 parent
3c0fb1a
commit 3b3e685
Showing
11 changed files
with
294 additions
and
22 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
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
40 changes: 40 additions & 0 deletions
40
...pp/(organization-active)/(dashboard)/env/[environmentSlug]/functions/[slug]/runs/utils.ts
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,40 @@ | ||
import { isFunctionRunStatus, type FunctionRunStatus } from '@inngest/components/types/functionRun'; | ||
|
||
import { FunctionRunStatus as FunctionRunStatusEnum } from '@/gql/graphql'; | ||
|
||
/** | ||
* Convert a run status union type into an enum. This is necessary because | ||
* TypeScript treats as enums as nominal types, which causes silly type errors. | ||
*/ | ||
function toRunStatus(status: FunctionRunStatus): FunctionRunStatusEnum { | ||
switch (status) { | ||
case 'CANCELLED': | ||
return FunctionRunStatusEnum.Cancelled; | ||
case 'COMPLETED': | ||
return FunctionRunStatusEnum.Completed; | ||
case 'FAILED': | ||
return FunctionRunStatusEnum.Failed; | ||
case 'QUEUED': | ||
return FunctionRunStatusEnum.Queued; | ||
case 'RUNNING': | ||
return FunctionRunStatusEnum.Running; | ||
} | ||
} | ||
|
||
/** | ||
* Convert a run status string array into an enum array. Unrecognized statuses | ||
* are logged and will not be in the returned array. | ||
*/ | ||
export function toRunStatuses(statuses: string[]): FunctionRunStatusEnum[] { | ||
const newValue: FunctionRunStatusEnum[] = []; | ||
|
||
for (const status of statuses) { | ||
if (isFunctionRunStatus(status)) { | ||
newValue.push(toRunStatus(status)); | ||
} else { | ||
console.error(`unexpected status: ${status}`); | ||
} | ||
} | ||
|
||
return newValue; | ||
} |
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,78 @@ | ||
import { RunStatusIcon, statusStyles } from '../FunctionRunStatusIcon/RunStatusIcons'; | ||
import { Select } from '../Select/Select'; | ||
import { | ||
functionRunStatuses, | ||
isFunctionRunStatus, | ||
type FunctionRunStatus, | ||
} from '../types/functionRun'; | ||
import { cn } from '../utils/classNames'; | ||
|
||
type StatusFilterProps = { | ||
selectedStatuses: FunctionRunStatus[]; | ||
onStatusesChange: (value: FunctionRunStatus[]) => void; | ||
}; | ||
|
||
export default function StatusFilter({ selectedStatuses, onStatusesChange }: StatusFilterProps) { | ||
const statusDots = selectedStatuses.map((status) => { | ||
const isSelected = selectedStatuses.includes(status); | ||
return ( | ||
<span | ||
key={status} | ||
className={cn( | ||
'inline-block h-[9px] w-[9px] flex-shrink-0 rounded-full border border-slate-50 bg-slate-50 ring-1 ring-inset ring-slate-300 group-hover:border-slate-100 [&:not(:first-child)]:-ml-1', | ||
isSelected && [statusStyles[status], 'ring-0'] | ||
)} | ||
aria-hidden="true" | ||
/> | ||
); | ||
}); | ||
|
||
return ( | ||
<Select | ||
defaultValue={selectedStatuses} | ||
onChange={(value: string[]) => { | ||
const newValue: FunctionRunStatus[] = []; | ||
value.forEach((status) => { | ||
if (isFunctionRunStatus(status)) { | ||
newValue.push(status); | ||
} else { | ||
console.error(`invalid status: ${status}`); | ||
} | ||
}); | ||
onStatusesChange(newValue); | ||
}} | ||
label="Status" | ||
> | ||
<Select.Button>{statusDots}</Select.Button> | ||
<Select.Options> | ||
{functionRunStatuses.map((option) => { | ||
return ( | ||
<Select.CustomOption | ||
key={option} | ||
value={option} | ||
className="ui-active:bg-blue-50 flex select-none items-center justify-between px-2 py-1.5 focus:outline-none" | ||
> | ||
{({ selected }: { selected: boolean }) => ( | ||
<span className="inline-flex items-center gap-2 lowercase"> | ||
<span className="inline-flex items-center gap-2 lowercase"> | ||
<input | ||
type="checkbox" | ||
id={option} | ||
checked={selected} | ||
readOnly | ||
className="h-[15px] w-[15px] rounded border-slate-300 text-indigo-500 drop-shadow-sm checked:border-indigo-500 checked:drop-shadow-none" | ||
/> | ||
<span className="flex items-center gap-1"> | ||
<RunStatusIcon status={option} className="h-2 w-2" /> | ||
<label className="text-sm first-letter:capitalize">{option}</label> | ||
</span> | ||
</span> | ||
</span> | ||
)} | ||
</Select.CustomOption> | ||
); | ||
})} | ||
</Select.Options> | ||
</Select> | ||
); | ||
} |
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
23 changes: 23 additions & 0 deletions
23
ui/packages/components/src/FunctionRunStatusIcon/RunStatusIcons.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,23 @@ | ||
import { type FunctionRunStatus } from '@inngest/components/types/functionRun'; | ||
|
||
import { cn } from '../utils/classNames'; | ||
|
||
export const statusStyles: Record<string, string> = { | ||
CANCELLED: 'bg-slate-300 border-slate-300', | ||
COMPLETED: 'bg-teal-500 border-teal-500', | ||
FAILED: 'bg-rose-500 border-rose-500', | ||
RUNNING: 'bg-blue-200 border-sky-500', | ||
QUEUED: 'bg-amber-100 border-amber-500', | ||
} as const satisfies { [key in FunctionRunStatus]: string }; | ||
|
||
type Props = { | ||
status: FunctionRunStatus; | ||
className?: string; | ||
}; | ||
|
||
export function RunStatusIcon({ status, className }: Props) { | ||
const style = statusStyles[status] ?? statusStyles['CANCELLED']; | ||
|
||
const title = 'Function ' + status.toLowerCase(); | ||
return <div className={cn('h-3.5 w-3.5 rounded-full border', style, className)} title={title} />; | ||
} |
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,82 @@ | ||
import { Listbox } from '@headlessui/react'; | ||
import { RiArrowDownSLine } from '@remixicon/react'; | ||
|
||
import { cn } from '../utils/classNames'; | ||
|
||
type SelectProps = { | ||
label?: string; | ||
defaultValue?: string[]; | ||
onChange: (value: string[]) => void; | ||
isLabelVisible?: boolean; | ||
children: React.ReactNode; | ||
}; | ||
|
||
export function Select({ | ||
defaultValue, | ||
label, | ||
isLabelVisible = true, | ||
children, | ||
onChange, | ||
}: SelectProps) { | ||
return ( | ||
<Listbox value={defaultValue} onChange={onChange} multiple> | ||
<span | ||
className={cn( | ||
isLabelVisible && 'divide-x divide-slate-300', | ||
'flex items-center rounded-md border border-slate-300 bg-slate-50 text-sm' | ||
)} | ||
> | ||
<Listbox.Label | ||
className={cn(!isLabelVisible && 'sr-only', 'rounded-l-[5px] px-2 py-2.5 text-slate-600')} | ||
> | ||
{label} | ||
</Listbox.Label> | ||
<span className="relative">{children}</span> | ||
</span> | ||
</Listbox> | ||
); | ||
} | ||
|
||
function Button({ | ||
children, | ||
isLabelVisible, | ||
}: React.PropsWithChildren<{ isLabelVisible?: boolean }>) { | ||
return ( | ||
<Listbox.Button | ||
className={cn( | ||
!isLabelVisible && 'rounded-l-[5px]', | ||
'flex items-center rounded-r-[5px] bg-white px-2 py-3' | ||
)} | ||
> | ||
{children} | ||
<RiArrowDownSLine className="h-4 w-4 text-slate-500" aria-hidden="true" /> | ||
</Listbox.Button> | ||
); | ||
} | ||
|
||
function Options({ children }: React.PropsWithChildren) { | ||
return ( | ||
<Listbox.Options className="absolute mt-1 min-w-max"> | ||
<div className="overflow-hidden rounded-md border border-slate-200 bg-white py-1 drop-shadow-lg"> | ||
{children} | ||
</div> | ||
</Listbox.Options> | ||
); | ||
} | ||
|
||
function Option({ children, option }: React.PropsWithChildren<{ option: string }>) { | ||
return ( | ||
<Listbox.Option | ||
className="ui-active:bg-blue-50 flex select-none items-center justify-between px-2 py-4 focus:outline-none" | ||
key={option} | ||
value={option} | ||
> | ||
{children} | ||
</Listbox.Option> | ||
); | ||
} | ||
|
||
Select.Button = Button; | ||
Select.Options = Options; | ||
Select.Option = Option; | ||
Select.CustomOption = Listbox.Option; |
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.