-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy-up web-component JavaScript files
- Loading branch information
1 parent
988af7b
commit c6c18f7
Showing
19 changed files
with
242 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import "./web-components/chats/chat-controller.js"; | ||
import "./web-components/chats/chat-message.js"; | ||
import "./web-components/chats/chat-title.js"; | ||
import "./web-components/chats/document-selector.js"; | ||
import "./web-components/chats/feedback-buttons.js"; | ||
import "./web-components/markdown-converter.js"; | ||
import "./web-components/chats/message-input.js"; | ||
import "./web-components/chats/sources-list.js"; | ||
|
||
// Update URL when a new chat is created | ||
document.addEventListener("chat-response-end", (evt) => { | ||
const sessionId = /** @type{CustomEvent} */ (evt).detail.session_id; | ||
window.history.pushState({}, "", `/chats/${sessionId}`); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./web-components/markdown-converter.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,2 @@ | ||
// @ts-check | ||
|
||
|
||
class FileStatus extends HTMLElement { | ||
|
||
connectedCallback() { | ||
|
||
const checkStatus = async () => { | ||
|
||
// UPDATE THESE AS REQUIRED | ||
const FILE_STATUS_ENDPOINT = '/file-status'; | ||
const CHECK_INTERVAL_MS = 5000; | ||
|
||
// as we use different layouts for mobile and desktop - only request update if visible | ||
if (!this.checkVisibility()) { | ||
window.setTimeout(checkStatus, CHECK_INTERVAL_MS); | ||
return; | ||
} | ||
|
||
const response = await fetch(`${FILE_STATUS_ENDPOINT}?id=${this.dataset.id}`); | ||
const responseObj = await response.json(); | ||
this.textContent = responseObj.status; | ||
if (responseObj.status.toLowerCase() === 'complete') { | ||
const evt = new CustomEvent('doc-complete', { detail: this.closest('.iai-doc-list__item') }); | ||
document.body.dispatchEvent(evt); | ||
} else { | ||
window.setTimeout(checkStatus, CHECK_INTERVAL_MS); | ||
} | ||
|
||
}; | ||
|
||
checkStatus(); | ||
|
||
} | ||
|
||
} | ||
customElements.define('file-status', FileStatus); | ||
|
||
|
||
|
||
/** So completed docs can be added to this list */ | ||
class DocList extends HTMLElement { | ||
connectedCallback() { | ||
document.body.addEventListener('doc-complete', (evt) => { | ||
const completedDoc = /** @type{CustomEvent} */(evt).detail; | ||
this.querySelector('tbody')?.appendChild(completedDoc); | ||
}); | ||
} | ||
} | ||
customElements.define('doc-list', DocList); | ||
import "./web-components/documents/doc-list.js"; | ||
import "./web-components/documents/file-status.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import "./trusted-types.js"; | ||
import "./libs/govuk-frontend.min.js"; | ||
import "../../node_modules/i.ai-design-system/dist/iai-design-system.js"; |
56 changes: 56 additions & 0 deletions
56
django_app/frontend/src/js/web-components/chats/chat-controller.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// @ts-check | ||
|
||
class ChatController extends HTMLElement { | ||
connectedCallback() { | ||
const messageForm = this.closest("form"); | ||
const textArea = /** @type {HTMLInputElement | null} */ ( | ||
this.querySelector(".js-user-text") | ||
); | ||
const messageContainer = this.querySelector(".js-message-container"); | ||
const insertPosition = this.querySelector(".js-response-feedback"); | ||
const feedbackButtons = /** @type {HTMLElement | null} */ ( | ||
this.querySelector("feedback-buttons") | ||
); | ||
let selectedDocuments = []; | ||
|
||
messageForm?.addEventListener("submit", (evt) => { | ||
evt.preventDefault(); | ||
const userText = textArea?.value.trim(); | ||
if (!textArea || !userText) { | ||
return; | ||
} | ||
|
||
let userMessage = document.createElement("chat-message"); | ||
userMessage.setAttribute("data-text", userText); | ||
userMessage.setAttribute("data-role", "user"); | ||
messageContainer?.insertBefore(userMessage, insertPosition); | ||
|
||
let aiMessage = /** @type {ChatMessage} */ ( | ||
document.createElement("chat-message") | ||
); | ||
aiMessage.setAttribute("data-role", "ai"); | ||
messageContainer?.insertBefore(aiMessage, insertPosition); | ||
aiMessage.stream( | ||
userText, | ||
selectedDocuments, | ||
this.dataset.sessionId, | ||
this.dataset.streamUrl || "", | ||
this | ||
); | ||
/** @type {HTMLElement | null} */ ( | ||
aiMessage.querySelector(".iai-chat-bubble") | ||
)?.focus(); | ||
|
||
// reset UI | ||
if (feedbackButtons) { | ||
feedbackButtons.dataset.status = ""; | ||
} | ||
textArea.value = ""; | ||
}); | ||
|
||
document.body.addEventListener("selected-docs-change", (evt) => { | ||
selectedDocuments = /** @type{CustomEvent} */ (evt).detail; | ||
}); | ||
} | ||
} | ||
customElements.define("chat-controller", ChatController); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
31 changes: 31 additions & 0 deletions
31
django_app/frontend/src/js/web-components/chats/document-selector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @ts-check | ||
|
||
class DocumentSelector extends HTMLElement { | ||
connectedCallback() { | ||
const documents = /** @type {NodeListOf<HTMLInputElement>} */ ( | ||
this.querySelectorAll('input[type="checkbox"]') | ||
); | ||
|
||
const getSelectedDocuments = () => { | ||
let selectedDocuments = []; | ||
documents.forEach((document) => { | ||
if (document.checked) { | ||
selectedDocuments.push(document.value); | ||
} | ||
}); | ||
const evt = new CustomEvent("selected-docs-change", { | ||
detail: selectedDocuments, | ||
}); | ||
document.body.dispatchEvent(evt); | ||
}; | ||
|
||
// update on page load | ||
getSelectedDocuments(); | ||
|
||
// update on any selection change | ||
documents.forEach((document) => { | ||
document.addEventListener("change", getSelectedDocuments); | ||
}); | ||
} | ||
} | ||
customElements.define("document-selector", DocumentSelector); |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.