DEV: Update form-template-field/upload uppy usage (#863)

This commit is contained in:
David Taylor 2024-10-24 15:21:13 +01:00 committed by GitHub
parent fc6f0a6560
commit c1fa84ad29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 27 additions and 22 deletions

View File

@ -3,32 +3,44 @@ import Component, { Input } from "@ember/component";
import { fn } from "@ember/helper"; import { fn } from "@ember/helper";
import { on } from "@ember/modifier"; import { on } from "@ember/modifier";
import { action } from "@ember/object"; import { action } from "@ember/object";
import { getOwner } from "@ember/owner";
import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { inject as service } from "@ember/service"; import { inject as service } from "@ember/service";
import DButton from "discourse/components/d-button"; import DButton from "discourse/components/d-button";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import UppyUploadMixin from "discourse/mixins/uppy-upload"; import UppyUpload from "discourse/lib/uppy/uppy-upload";
import icon from "discourse-common/helpers/d-icon"; import icon from "discourse-common/helpers/d-icon";
import discourseDebounce from "discourse-common/lib/debounce"; import discourseDebounce from "discourse-common/lib/debounce";
import I18n from "discourse-i18n"; import I18n from "discourse-i18n";
import RagUploadProgress from "./rag-upload-progress"; import RagUploadProgress from "./rag-upload-progress";
export default class RagUploader extends Component.extend(UppyUploadMixin) { export default class RagUploader extends Component {
@service appEvents; @service appEvents;
@tracked term = null; @tracked term = null;
@tracked filteredUploads = null; @tracked filteredUploads = null;
@tracked ragIndexingStatuses = null; @tracked ragIndexingStatuses = null;
@tracked ragUploads = null; @tracked ragUploads = null;
id = "discourse-ai-rag-uploader";
maxFiles = 20; uppyUpload = new UppyUpload(getOwner(this), {
uploadUrl = "/admin/plugins/discourse-ai/rag-document-fragments/files/upload"; id: "discourse-ai-rag-uploader",
preventDirectS3Uploads = true; maxFiles: 20,
uploadUrl:
"/admin/plugins/discourse-ai/rag-document-fragments/files/upload",
preventDirectS3Uploads: true,
uploadDone: (uploadedFile) => {
const newUpload = uploadedFile.upload;
newUpload.status = "uploaded";
newUpload.statusText = I18n.t("discourse_ai.rag.uploads.uploaded");
this.ragUploads.pushObject(newUpload);
this.debouncedSearch();
},
});
didReceiveAttrs() { didReceiveAttrs() {
super.didReceiveAttrs(...arguments); super.didReceiveAttrs(...arguments);
if (this.inProgressUploads?.length > 0) { if (this.uppyUpload.inProgressUploads?.length > 0) {
this._uppyInstance?.cancelAll(); this.uppyUpload.cancelAllUploads();
} }
this.ragUploads = this.target?.rag_uploads || []; this.ragUploads = this.target?.rag_uploads || [];
@ -45,7 +57,7 @@ export default class RagUploader extends Component.extend(UppyUploadMixin) {
} }
this.appEvents.on( this.appEvents.on(
`upload-mixin:${this.id}:all-uploads-complete`, `upload-mixin:${this.uppyUpload.config.id}:all-uploads-complete`,
this, this,
"_updateTargetWithUploads" "_updateTargetWithUploads"
); );
@ -54,7 +66,7 @@ export default class RagUploader extends Component.extend(UppyUploadMixin) {
willDestroy() { willDestroy() {
super.willDestroy(...arguments); super.willDestroy(...arguments);
this.appEvents.off( this.appEvents.off(
`upload-mixin:${this.id}:all-uploads-complete`, `upload-mixin:${this.uppyUpload.config}:all-uploads-complete`,
this, this,
"_updateTargetWithUploads" "_updateTargetWithUploads"
); );
@ -64,22 +76,14 @@ export default class RagUploader extends Component.extend(UppyUploadMixin) {
this.updateUploads(this.ragUploads); this.updateUploads(this.ragUploads);
} }
uploadDone(uploadedFile) {
const newUpload = uploadedFile.upload;
newUpload.status = "uploaded";
newUpload.statusText = I18n.t("discourse_ai.rag.uploads.uploaded");
this.ragUploads.pushObject(newUpload);
this.debouncedSearch();
}
@action @action
submitFiles() { submitFiles() {
this.fileInputEl.click(); this.uppyUpload.openPicker();
} }
@action @action
cancelUploading(upload) { cancelUploading(upload) {
this.appEvents.trigger(`upload-mixin:${this.id}:cancel-upload`, { this.uppyUpload.cancelSingleUpload({
fileId: upload.id, fileId: upload.id,
}); });
} }
@ -152,7 +156,7 @@ export default class RagUploader extends Component.extend(UppyUploadMixin) {
</td> </td>
</tr> </tr>
{{/each}} {{/each}}
{{#each this.inProgressUploads as |upload|}} {{#each this.uppyUpload.inProgressUploads as |upload|}}
<tr> <tr>
<td><span class="rag-uploader__rag-file-icon">{{icon <td><span class="rag-uploader__rag-file-icon">{{icon
"file" "file"
@ -177,6 +181,7 @@ export default class RagUploader extends Component.extend(UppyUploadMixin) {
</table> </table>
<input <input
{{didInsert this.uppyUpload.setup}}
class="hidden-upload-field" class="hidden-upload-field"
disabled={{this.uploading}} disabled={{this.uploading}}
type="file" type="file"