]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Real doc ID updating
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sat, 26 Apr 2025 06:43:28 +0000 (23:43 -0700)
committershamoon <4887959+shamoon@users.noreply.github.com>
Wed, 2 Jul 2025 18:03:57 +0000 (11:03 -0700)
src-ui/src/app/components/chat/chat/chat.component.html
src-ui/src/app/components/chat/chat/chat.component.ts

index 54753c88f1056084fb2a86b97be56e97766a9da9..f0b61b805f4761777a798dc5b5f0cdb9049df57c 100644 (file)
@@ -20,8 +20,9 @@
       <form class="chat-input">
         <div class="input-group">
           <input
-            #inputField
-            class="form-control form-control-sm" name="chatInput" type="text" placeholder="Ask about this document..."
+            #chatInput
+            class="form-control form-control-sm" name="chatInput" type="text"
+            [placeholder]="placeholder"
             [disabled]="loading"
             [(ngModel)]="input"
             (keydown)="searchInputKeyDown($event)"
index 750edd9376b35bf7519c96e0ca15a47b8444e017..0c97abc4fbee60343b17a8a3af7fd3249565ee3c 100644 (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 { NavigationEnd, Router } from '@angular/router'
 import { NgbDropdownModule } from '@ng-bootstrap/ng-bootstrap'
 import { NgxBootstrapIconsModule } from 'ngx-bootstrap-icons'
+import { filter, map } from 'rxjs'
 import { ChatMessage, ChatService } from 'src/app/services/chat.service'
 
 @Component({
@@ -15,24 +17,52 @@ import { ChatMessage, ChatService } from 'src/app/services/chat.service'
   templateUrl: './chat.component.html',
   styleUrl: './chat.component.scss',
 })
-export class ChatComponent {
-  messages: ChatMessage[] = []
-  loading = false
-  documentId = 295 // Replace this with actual doc ID logic
-  input: string = ''
+export class ChatComponent implements OnInit {
+  public messages: ChatMessage[] = []
+  public loading = false
+  public input: string = ''
+  public documentId!: number
+
   @ViewChild('scrollAnchor') scrollAnchor!: ElementRef<HTMLDivElement>
-  @ViewChild('inputField') inputField!: ElementRef<HTMLInputElement>
+  @ViewChild('chatInput') chatInput!: ElementRef<HTMLInputElement>
 
   private typewriterBuffer: string[] = []
   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 {
     if (!this.input.trim()) return
 
     const userMessage: ChatMessage = { role: 'user', content: this.input }
     this.messages.push(userMessage)
+    this.scrollToBottom()
 
     const assistantMessage: ChatMessage = {
       role: 'assistant',
@@ -98,7 +128,7 @@ export class ChatComponent {
   public onOpenChange(open: boolean): void {
     if (open) {
       setTimeout(() => {
-        this.inputField.nativeElement.focus()
+        this.chatInput.nativeElement.focus()
       }, 10)
     }
   }