Real doc ID updating

This commit is contained in:
shamoon 2025-04-25 23:43:28 -07:00
parent b8ff611bd0
commit 0f730ee0a9
No known key found for this signature in database
2 changed files with 42 additions and 11 deletions

View File

@ -20,8 +20,9 @@
<form class="chat-input"> <form class="chat-input">
<div class="input-group"> <div class="input-group">
<input <input
#inputField #chatInput
class="form-control form-control-sm" name="chatInput" type="text" placeholder="Ask about this document..." class="form-control form-control-sm" name="chatInput" type="text"
[placeholder]="placeholder"
[disabled]="loading" [disabled]="loading"
[(ngModel)]="input" [(ngModel)]="input"
(keydown)="searchInputKeyDown($event)" (keydown)="searchInputKeyDown($event)"

View File

@ -1,7 +1,9 @@
import { Component, ElementRef, ViewChild } from '@angular/core' import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { FormsModule, ReactiveFormsModule } from '@angular/forms' import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { NavigationEnd, Router } from '@angular/router'
import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap' import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap'
import { NgxBootstrapIconsModule } from 'ngx-bootstrap-icons' import { NgxBootstrapIconsModule } from 'ngx-bootstrap-icons'
import { filter, map } from 'rxjs'
import { ChatMessage, ChatService } from 'src/app/services/chat.service' import { ChatMessage, ChatService } from 'src/app/services/chat.service'
@Component({ @Component({
@ -15,24 +17,52 @@ import { ChatMessage, ChatService } from 'src/app/services/chat.service'
templateUrl: './chat.component.html', templateUrl: './chat.component.html',
styleUrl: './chat.component.scss', styleUrl: './chat.component.scss',
}) })
export class ChatComponent { export class ChatComponent implements OnInit {
messages: ChatMessage[] = [] public messages: ChatMessage[] = []
loading = false public loading = false
documentId = 295 // Replace this with actual doc ID logic public input: string = ''
input: string = '' public documentId!: number
@ViewChild('scrollAnchor') scrollAnchor!: ElementRef<HTMLDivElement> @ViewChild('scrollAnchor') scrollAnchor!: ElementRef<HTMLDivElement>
@ViewChild('inputField') inputField!: ElementRef<HTMLInputElement> @ViewChild('chatInput') chatInput!: ElementRef<HTMLInputElement>
private typewriterBuffer: string[] = [] private typewriterBuffer: string[] = []
private typewriterActive = false private typewriterActive = false
constructor(private chatService: ChatService) {} public get placeholder(): string {
return this.documentId
? $localize`Ask a question about this document...`
: $localize`Ask a question about a document...`
}
constructor(
private chatService: ChatService,
private router: Router
) {}
ngOnInit(): void {
this.updateDocumentId(this.router.url)
this.router.events
.pipe(
filter((event) => event instanceof NavigationEnd),
map(() => this.router.url)
)
.subscribe((url) => {
this.updateDocumentId(url)
})
}
private updateDocumentId(url: string): void {
const docIdRe = url.match(/^\/documents\/(\d+)/)
this.documentId = docIdRe ? +docIdRe[1] : undefined
}
sendMessage(): void { sendMessage(): void {
if (!this.input.trim()) return if (!this.input.trim()) return
const userMessage: ChatMessage = { role: 'user', content: this.input } const userMessage: ChatMessage = { role: 'user', content: this.input }
this.messages.push(userMessage) this.messages.push(userMessage)
this.scrollToBottom()
const assistantMessage: ChatMessage = { const assistantMessage: ChatMessage = {
role: 'assistant', role: 'assistant',
@ -98,7 +128,7 @@ export class ChatComponent {
public onOpenChange(open: boolean): void { public onOpenChange(open: boolean): void {
if (open) { if (open) {
setTimeout(() => { setTimeout(() => {
this.inputField.nativeElement.focus() this.chatInput.nativeElement.focus()
}, 10) }, 10)
} }
} }