|
| 1 | +import { Component, Input, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core'; |
| 2 | +import { FormsModule } from '@angular/forms'; |
| 3 | +import { CommonModule } from '@angular/common'; |
| 4 | +import { BehaviorSubject, Observable, Subject, debounceTime, distinctUntilChanged, merge, switchMap, tap } from 'rxjs'; |
| 5 | +import { KvDataService, KvDataItem, KvDataListResponse } from '~/app/services/kv-data.service'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'app-kv-data-browser', |
| 9 | + standalone: true, |
| 10 | + imports: [CommonModule, FormsModule], |
| 11 | + templateUrl: './kv-data-browser.component.html', |
| 12 | + changeDetection: ChangeDetectionStrategy.OnPush |
| 13 | +}) |
| 14 | +export class KvDataBrowserComponent implements OnInit { |
| 15 | + @Input({ required: true }) namespaceId!: string; |
| 16 | + |
| 17 | + private readonly refresh$ = new BehaviorSubject<{ cursor?: string }>({}); |
| 18 | + private readonly search$ = new Subject<string>(); |
| 19 | + |
| 20 | + public readonly data$: Observable<KvDataListResponse>; |
| 21 | + public items: KvDataItem[] = []; |
| 22 | + public cursor: string | null = null; |
| 23 | + public hasMore = false; |
| 24 | + |
| 25 | + public searchPrefix = ''; |
| 26 | + public editingItem: { key: string; value: string; expiresIn: number | null; isNew: boolean } | null = null; |
| 27 | + public deleteConfirm: string | null = null; |
| 28 | + |
| 29 | + constructor( |
| 30 | + private kvDataService: KvDataService, |
| 31 | + private cdr: ChangeDetectorRef |
| 32 | + ) { |
| 33 | + const debouncedSearch$ = this.search$.pipe( |
| 34 | + debounceTime(300), |
| 35 | + distinctUntilChanged() |
| 36 | + ); |
| 37 | + |
| 38 | + this.data$ = merge( |
| 39 | + this.refresh$, |
| 40 | + debouncedSearch$.pipe(tap(() => this.cursor = null)) |
| 41 | + ).pipe( |
| 42 | + switchMap(() => |
| 43 | + this.kvDataService.list(this.namespaceId, { |
| 44 | + prefix: this.searchPrefix || undefined, |
| 45 | + cursor: this.cursor ?? undefined, |
| 46 | + limit: 50 |
| 47 | + }).pipe( |
| 48 | + tap((res) => { |
| 49 | + if (!this.cursor) { |
| 50 | + this.items = res.items; |
| 51 | + } else { |
| 52 | + this.items = [...this.items, ...res.items]; |
| 53 | + } |
| 54 | + |
| 55 | + this.cursor = res.cursor; |
| 56 | + this.hasMore = res.hasMore; |
| 57 | + this.cdr.markForCheck(); |
| 58 | + }) |
| 59 | + ) |
| 60 | + ) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + ngOnInit() { |
| 65 | + // Initial load triggered by template subscription |
| 66 | + } |
| 67 | + |
| 68 | + onSearchChange(value: string) { |
| 69 | + this.search$.next(value); |
| 70 | + } |
| 71 | + |
| 72 | + loadMore() { |
| 73 | + if (this.cursor) { |
| 74 | + this.refresh$.next({}); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + openNew() { |
| 79 | + this.editingItem = { key: '', value: '', expiresIn: null, isNew: true }; |
| 80 | + } |
| 81 | + |
| 82 | + openEdit(item: KvDataItem) { |
| 83 | + this.editingItem = { key: item.key, value: item.value, expiresIn: null, isNew: false }; |
| 84 | + } |
| 85 | + |
| 86 | + closeEdit() { |
| 87 | + this.editingItem = null; |
| 88 | + } |
| 89 | + |
| 90 | + save() { |
| 91 | + if (!this.editingItem) return; |
| 92 | + |
| 93 | + this.kvDataService.put( |
| 94 | + this.namespaceId, |
| 95 | + this.editingItem.key, |
| 96 | + this.editingItem.value, |
| 97 | + this.editingItem.expiresIn ?? undefined |
| 98 | + ).subscribe({ |
| 99 | + next: () => { |
| 100 | + this.closeEdit(); |
| 101 | + this.cursor = null; |
| 102 | + this.refresh$.next({}); |
| 103 | + }, |
| 104 | + error: (err) => { |
| 105 | + console.error('Failed to save:', err); |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + confirmDelete(key: string) { |
| 111 | + this.deleteConfirm = key; |
| 112 | + } |
| 113 | + |
| 114 | + cancelDelete() { |
| 115 | + this.deleteConfirm = null; |
| 116 | + } |
| 117 | + |
| 118 | + doDelete() { |
| 119 | + if (!this.deleteConfirm) return; |
| 120 | + |
| 121 | + this.kvDataService.delete(this.namespaceId, this.deleteConfirm).subscribe({ |
| 122 | + next: () => { |
| 123 | + this.deleteConfirm = null; |
| 124 | + this.cursor = null; |
| 125 | + this.refresh$.next({}); |
| 126 | + }, |
| 127 | + error: (err) => { |
| 128 | + console.error('Failed to delete:', err); |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + truncate(value: string, maxLength = 100): string { |
| 134 | + if (value.length <= maxLength) return value; |
| 135 | + return value.slice(0, maxLength) + '...'; |
| 136 | + } |
| 137 | + |
| 138 | + formatDate(date: string | null): string { |
| 139 | + if (!date) return '-'; |
| 140 | + return new Date(date).toLocaleString(); |
| 141 | + } |
| 142 | +} |
0 commit comments