|
| 1 | +import type { ReactNode } from 'react' |
| 2 | +import { Loader2, Play, RefreshCw, Search, Square } from 'lucide-react' |
| 3 | +import { Button } from '@/components/ui/button' |
| 4 | +import { Input } from '@/components/ui/input' |
| 5 | +import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip' |
| 6 | +import { cn } from '@/lib/utils' |
| 7 | +import Timeline from '@/app/workspace/[workspaceId]/logs/components/filters/components/timeline' |
| 8 | + |
| 9 | +export function Controls({ |
| 10 | + searchQuery, |
| 11 | + setSearchQuery, |
| 12 | + isRefetching, |
| 13 | + resetToNow, |
| 14 | + live, |
| 15 | + setLive, |
| 16 | + viewMode, |
| 17 | + setViewMode, |
| 18 | + searchComponent, |
| 19 | + showExport = true, |
| 20 | + onExport, |
| 21 | +}: { |
| 22 | + searchQuery?: string |
| 23 | + setSearchQuery?: (v: string) => void |
| 24 | + isRefetching: boolean |
| 25 | + resetToNow: () => void |
| 26 | + live: boolean |
| 27 | + setLive: (v: (prev: boolean) => boolean) => void |
| 28 | + viewMode: string |
| 29 | + setViewMode: (mode: 'logs' | 'dashboard') => void |
| 30 | + searchComponent?: ReactNode |
| 31 | + showExport?: boolean |
| 32 | + onExport?: () => void |
| 33 | +}) { |
| 34 | + return ( |
| 35 | + <div className='mb-8 flex flex-col items-stretch justify-between gap-4 sm:flex-row sm:items-start'> |
| 36 | + {searchComponent ? ( |
| 37 | + searchComponent |
| 38 | + ) : ( |
| 39 | + <div className='relative w-full max-w-md'> |
| 40 | + <Search className='-translate-y-1/2 absolute top-1/2 left-3 h-[18px] w-[18px] text-muted-foreground' /> |
| 41 | + <Input |
| 42 | + type='text' |
| 43 | + placeholder='Search workflows...' |
| 44 | + value={searchQuery} |
| 45 | + onChange={(e) => setSearchQuery?.(e.target.value)} |
| 46 | + className='h-9 w-full rounded-[11px] border-[#E5E5E5] bg-[#FFFFFF] pr-10 pl-9 dark:border-[#414141] dark:bg-[var(--surface-elevated)]' |
| 47 | + /> |
| 48 | + {searchQuery && ( |
| 49 | + <button |
| 50 | + onClick={() => setSearchQuery?.('')} |
| 51 | + className='-translate-y-1/2 absolute top-1/2 right-3 text-muted-foreground hover:text-foreground' |
| 52 | + > |
| 53 | + <svg |
| 54 | + width='14' |
| 55 | + height='14' |
| 56 | + viewBox='0 0 16 16' |
| 57 | + fill='none' |
| 58 | + stroke='currentColor' |
| 59 | + strokeWidth='2' |
| 60 | + strokeLinecap='round' |
| 61 | + > |
| 62 | + <path d='M12 4L4 12M4 4l8 8' /> |
| 63 | + </svg> |
| 64 | + </button> |
| 65 | + )} |
| 66 | + </div> |
| 67 | + )} |
| 68 | + |
| 69 | + <div className='ml-auto flex flex-shrink-0 items-center gap-3'> |
| 70 | + <Tooltip> |
| 71 | + <TooltipTrigger asChild> |
| 72 | + <Button |
| 73 | + variant='ghost' |
| 74 | + size='icon' |
| 75 | + onClick={resetToNow} |
| 76 | + className='h-9 rounded-[11px] hover:bg-secondary' |
| 77 | + disabled={isRefetching} |
| 78 | + > |
| 79 | + {isRefetching ? ( |
| 80 | + <Loader2 className='h-5 w-5 animate-spin' /> |
| 81 | + ) : ( |
| 82 | + <RefreshCw className='h-5 w-5' /> |
| 83 | + )} |
| 84 | + <span className='sr-only'>Refresh</span> |
| 85 | + </Button> |
| 86 | + </TooltipTrigger> |
| 87 | + <TooltipContent>{isRefetching ? 'Refreshing...' : 'Refresh'}</TooltipContent> |
| 88 | + </Tooltip> |
| 89 | + |
| 90 | + {showExport && viewMode !== 'dashboard' && ( |
| 91 | + <Tooltip> |
| 92 | + <TooltipTrigger asChild> |
| 93 | + <Button |
| 94 | + variant='ghost' |
| 95 | + size='icon' |
| 96 | + onClick={onExport} |
| 97 | + className='h-9 rounded-[11px] hover:bg-secondary' |
| 98 | + aria-label='Export CSV' |
| 99 | + > |
| 100 | + <svg |
| 101 | + xmlns='http://www.w3.org/2000/svg' |
| 102 | + viewBox='0 0 24 24' |
| 103 | + fill='none' |
| 104 | + stroke='currentColor' |
| 105 | + strokeWidth='2' |
| 106 | + className='h-5 w-5' |
| 107 | + > |
| 108 | + <path d='M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4' /> |
| 109 | + <polyline points='7 10 12 15 17 10' /> |
| 110 | + <line x1='12' y1='15' x2='12' y2='3' /> |
| 111 | + </svg> |
| 112 | + <span className='sr-only'>Export CSV</span> |
| 113 | + </Button> |
| 114 | + </TooltipTrigger> |
| 115 | + <TooltipContent>Export CSV</TooltipContent> |
| 116 | + </Tooltip> |
| 117 | + )} |
| 118 | + |
| 119 | + <div className='inline-flex h-9 items-center rounded-[11px] border bg-card p-1 shadow-sm'> |
| 120 | + <Button |
| 121 | + variant='ghost' |
| 122 | + size='sm' |
| 123 | + onClick={() => setLive((v) => !v)} |
| 124 | + className={cn( |
| 125 | + 'h-7 rounded-[8px] px-3 font-normal text-xs', |
| 126 | + live ? 'bg-muted text-foreground' : 'text-muted-foreground hover:text-foreground' |
| 127 | + )} |
| 128 | + aria-pressed={live} |
| 129 | + > |
| 130 | + {live ? ( |
| 131 | + <> |
| 132 | + <Square className='mr-1.5 h-3 w-3 fill-current' /> |
| 133 | + Live |
| 134 | + </> |
| 135 | + ) : ( |
| 136 | + <> |
| 137 | + <Play className='mr-1.5 h-3 w-3' /> |
| 138 | + Live |
| 139 | + </> |
| 140 | + )} |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + |
| 144 | + <div className='inline-flex h-9 items-center rounded-[11px] border bg-card p-1 shadow-sm'> |
| 145 | + <Button |
| 146 | + variant='ghost' |
| 147 | + size='sm' |
| 148 | + onClick={() => setViewMode('logs')} |
| 149 | + className={cn( |
| 150 | + 'h-7 rounded-[8px] px-3 font-normal text-xs', |
| 151 | + (viewMode as string) !== 'dashboard' |
| 152 | + ? 'bg-muted text-foreground' |
| 153 | + : 'text-muted-foreground hover:text-foreground' |
| 154 | + )} |
| 155 | + aria-pressed={(viewMode as string) !== 'dashboard'} |
| 156 | + > |
| 157 | + Logs |
| 158 | + </Button> |
| 159 | + <Button |
| 160 | + variant='ghost' |
| 161 | + size='sm' |
| 162 | + onClick={() => setViewMode('dashboard')} |
| 163 | + className={cn( |
| 164 | + 'h-7 rounded-[8px] px-3 font-normal text-xs', |
| 165 | + (viewMode as string) === 'dashboard' |
| 166 | + ? 'bg-muted text-foreground' |
| 167 | + : 'text-muted-foreground hover:text-foreground' |
| 168 | + )} |
| 169 | + aria-pressed={(viewMode as string) === 'dashboard'} |
| 170 | + > |
| 171 | + Dashboard |
| 172 | + </Button> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + |
| 176 | + <div className='sm:hidden'> |
| 177 | + <TooltipProvider> |
| 178 | + <Timeline /> |
| 179 | + </TooltipProvider> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + ) |
| 183 | +} |
| 184 | + |
| 185 | +export default Controls |
0 commit comments