|
| 1 | +import { createComponent, Shade } from '@furystack/shades' |
| 2 | +import { |
| 3 | + Button, |
| 4 | + cssVariableTheme, |
| 5 | + Icon, |
| 6 | + icons, |
| 7 | + NotyService, |
| 8 | + ToggleButton, |
| 9 | + ToggleButtonGroup, |
| 10 | +} from '@furystack/shades-common-components' |
| 11 | +import type { ServiceView } from 'common' |
| 12 | + |
| 13 | +import { ServicesApiClient } from '../services/api-clients/services-api-client.js' |
| 14 | +import type { ServiceSummaryStatus } from '../utils/service-pipeline.js' |
| 15 | + |
| 16 | +type ServiceFiltersProps = { |
| 17 | + filteredServices: ServiceView[] |
| 18 | + searchText: string |
| 19 | + onSearchTextChange: (text: string) => void |
| 20 | + statusFilter: string |
| 21 | + onStatusFilterChange: (value: string) => void |
| 22 | +} |
| 23 | + |
| 24 | +const statusOptions: Array<{ value: ServiceSummaryStatus; label: string }> = [ |
| 25 | + { value: 'running', label: 'Running' }, |
| 26 | + { value: 'in-progress', label: 'In Progress' }, |
| 27 | + { value: 'error', label: 'Error' }, |
| 28 | + { value: 'pending', label: 'Pending' }, |
| 29 | +] |
| 30 | + |
| 31 | +export const ServiceFilters = Shade<ServiceFiltersProps>({ |
| 32 | + customElementName: 'shade-service-filters', |
| 33 | + render: ({ props, injector, useState }) => { |
| 34 | + const api = injector.getInstance(ServicesApiClient) |
| 35 | + const noty = injector.getInstance(NotyService) |
| 36 | + const [isUpdateLoading, setIsUpdateLoading] = useState('isUpdateLoading', false) |
| 37 | + |
| 38 | + const updatableServices = props.filteredServices.filter( |
| 39 | + (s) => s.cloneStatus === 'cloned' && s.commitsBehind && s.commitsBehind > 0, |
| 40 | + ) |
| 41 | + |
| 42 | + const updateAll = async () => { |
| 43 | + setIsUpdateLoading(true) |
| 44 | + const failures: string[] = [] |
| 45 | + for (const svc of updatableServices) { |
| 46 | + try { |
| 47 | + await api.call({ |
| 48 | + method: 'POST', |
| 49 | + action: '/services/:id/update' as '/services/:id/start', |
| 50 | + url: { id: svc.id }, |
| 51 | + }) |
| 52 | + } catch { |
| 53 | + failures.push(svc.displayName) |
| 54 | + } |
| 55 | + } |
| 56 | + if (failures.length > 0) { |
| 57 | + noty.emit('onNotyAdded', { |
| 58 | + title: 'Update failed', |
| 59 | + body: `Failed for: ${failures.join(', ')}`, |
| 60 | + type: 'error', |
| 61 | + }) |
| 62 | + } |
| 63 | + setIsUpdateLoading(false) |
| 64 | + } |
| 65 | + |
| 66 | + return ( |
| 67 | + <div style={{ display: 'flex', gap: '8px', alignItems: 'center' }}> |
| 68 | + <div style={{ position: 'relative', display: 'inline-flex', alignItems: 'center' }}> |
| 69 | + <Icon |
| 70 | + icon={icons.search} |
| 71 | + size={14} |
| 72 | + style={{ |
| 73 | + position: 'absolute', |
| 74 | + left: '8px', |
| 75 | + pointerEvents: 'none', |
| 76 | + opacity: '0.5', |
| 77 | + }} |
| 78 | + /> |
| 79 | + <input |
| 80 | + type="text" |
| 81 | + placeholder="Search..." |
| 82 | + value={props.searchText} |
| 83 | + oninput={(e: Event) => props.onSearchTextChange((e.target as HTMLInputElement).value)} |
| 84 | + style={{ |
| 85 | + padding: '4px 8px 4px 28px', |
| 86 | + borderRadius: cssVariableTheme.shape.borderRadius.sm, |
| 87 | + border: `1px solid ${cssVariableTheme.divider}`, |
| 88 | + background: cssVariableTheme.background.default, |
| 89 | + color: cssVariableTheme.text.primary, |
| 90 | + fontSize: cssVariableTheme.typography.fontSize.sm, |
| 91 | + width: '160px', |
| 92 | + outline: 'none', |
| 93 | + fontFamily: cssVariableTheme.typography.fontFamily, |
| 94 | + }} |
| 95 | + /> |
| 96 | + </div> |
| 97 | + |
| 98 | + <ToggleButtonGroup |
| 99 | + exclusive |
| 100 | + size="small" |
| 101 | + value={props.statusFilter} |
| 102 | + onValueChange={(value) => props.onStatusFilterChange(typeof value === 'string' ? value : '')} |
| 103 | + > |
| 104 | + {statusOptions.map((opt) => ( |
| 105 | + <ToggleButton value={opt.value}>{opt.label}</ToggleButton> |
| 106 | + ))} |
| 107 | + </ToggleButtonGroup> |
| 108 | + |
| 109 | + {updatableServices.length > 0 ? ( |
| 110 | + <Button |
| 111 | + variant="outlined" |
| 112 | + size="small" |
| 113 | + color="primary" |
| 114 | + loading={isUpdateLoading} |
| 115 | + onclick={() => void updateAll()} |
| 116 | + startIcon={<Icon icon={icons.download} size="small" />} |
| 117 | + > |
| 118 | + {`Update All (${updatableServices.length})`} |
| 119 | + </Button> |
| 120 | + ) : null} |
| 121 | + </div> |
| 122 | + ) |
| 123 | + }, |
| 124 | +}) |
0 commit comments