Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions ui/src/components/ai-chat/component/chat-input-operate/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
@TouchEnd="TouchEnd"
:time="recorderTime"
:start="recorderStatus === 'START'"
:disabled="loading"
:disabled="localLoading"
/>
<el-input
v-else
Expand Down Expand Up @@ -199,7 +199,7 @@
</span>
<span class="flex align-center" v-else>
<el-button
:disabled="loading"
:disabled="localLoading"
text
@click="startRecording"
v-if="recorderStatus === 'STOP'"
Expand Down Expand Up @@ -252,7 +252,7 @@
}}:{{ getAcceptList().replace(/\./g, '').replace(/,/g, '、').toUpperCase() }}
</div>
</template>
<el-button text :disabled="checkMaxFilesLimit() || loading" class="mt-4">
<el-button text :disabled="checkMaxFilesLimit() || localLoading" class="mt-4">
<el-icon><Paperclip /></el-icon>
</el-button>
</el-tooltip>
Expand All @@ -268,11 +268,11 @@
<el-button
text
class="sent-button"
:disabled="isDisabledChat || loading"
:disabled="isDisabledChat || localLoading"
@click="sendChatHandle"
>
<img v-show="isDisabledChat || loading" src="@/assets/icon_send.svg" alt="" />
<SendIcon v-show="!isDisabledChat && !loading" />
<img v-show="isDisabledChat || localLoading" src="@/assets/icon_send.svg" alt="" />
<SendIcon v-show="!isDisabledChat && !localLoading" />
</el-button>
</template>
</div>
Expand Down Expand Up @@ -339,9 +339,13 @@ const chatId_context = computed({
emit('update:chatId', v)
}
})
const uploadLoading = computed(() => {
return Object.values(filePromisionDict.value).length > 0
})

const localLoading = computed({
get: () => {
return props.loading
return props.loading || uploadLoading.value
},
set: (v) => {
emit('update:loading', v)
Expand Down Expand Up @@ -393,7 +397,7 @@ const checkMaxFilesLimit = () => {
uploadOtherList.value.length
)
}

const filePromisionDict: any = ref<any>({})
const uploadFile = async (file: any, fileList: any) => {
const { maxFiles, fileLimit } = props.applicationDetails.file_upload_setting
// 单次上传文件数量限制
Expand All @@ -414,7 +418,7 @@ const uploadFile = async (file: any, fileList: any) => {
fileList.splice(0, fileList.length)
return
}

filePromisionDict.value[file.uid] = false
const formData = new FormData()
formData.append('file', file.raw, file.name)
//
Expand Down Expand Up @@ -454,6 +458,7 @@ const uploadFile = async (file: any, fileList: any) => {
fileList.splice(0, fileList.length)
file.url = response.data[0].url
file.file_id = response.data[0].file_id
delete filePromisionDict.value[file.uid]
})
}
// 粘贴处理
Expand Down Expand Up @@ -798,7 +803,7 @@ function sendChatHandle(event?: any) {
if (!event?.ctrlKey && !event?.shiftKey && !event?.altKey && !event?.metaKey) {
// 如果没有按下组合键,则会阻止默认事件
event?.preventDefault()
if (!isDisabledChat.value && !props.loading && !event?.isComposing) {
if (!isDisabledChat.value && !localLoading.value && !event?.isComposing) {
if (
inputValue.value.trim() ||
uploadImageList.value.length > 0 ||
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Analysis

Regularities and Issues

  1. Variable Naming Consistency: The variable name uploadLoading is used inconsistently throughout the codebase without explanation. This might lead to confusion or maintenance errors.
  2. Conditionality on Local Loading:
    • In many places, conditions like :disabled="loading" are replaced with :disabled="localLoading", which should be consistent throughout.
  3. Function Calls in Conditional Statements:
    • A few function calls (getAcceptList()) seem to depend on state that isn't guaranteed to be up-to-date due to asynchronous operations.

Optimization Suggestions

  1. Consistent Variable Naming Convention:

    • It's recommended to consistently use either loading or localLoading based on context to avoid naming inconsistencies.
  2. Use of $nextTick for Asynchronous State Updates:

    • If checkMaxFilesLimit() depends on dynamic data, consider using Vue.js' $nextTick to ensure reactivity when it updates.
  3. Avoid Overwriting State Directly:

    • Instead of setting the entire object value in filePromisionDict.value=file.uid=false;, only update the specific property needed:
      filePromisionDict.value[file.uid] = false;
  4. Ensure Proper Dependency Tracking For Asynchronous Operations:

    • Always make sure dependency tracking is handled correctly in functions dependent on asynchronous operations like uploading files.

Here's a revised version of the key parts with these considerations:

// Example revisions

<div>
  <!-- TouchEnd Event -->
  <RecordButton
    @TouchEnd="TouchEnd"
    :time="recorderTime"
    :start="recorderStatus === 'START'"
    :disabled="localLoading"
  />

  <!-- Else Block -->
  <el-input v-else></el-input>

  <!-- Recording Button Logic (Start/Stop) -->
  <el-button
    :disabled="(recorderStatus !== 'STOP') ? loading || localLoading : true"
    text
    @click="startRecording"
    v-if="recorderStatus === 'STOP'"
  ></el-button>

  <!-- File Upload Template -->
  ...
</div>

<!-- Computed Properties Revised -->
const chatId_context = computed(() => {
  return props.chatId
})

const uploadLoading = computed(() => {
  return Object.values(filePromisionDict.value).some(value => !value);
})

...

// Upload File Function Updated
async function uploadFile(file, fileList) {
  // Existing logic but modified for better consistency

  // After successful upload
  filePromisionDict.value[file.uid] = true;  
}

These changes aim to improve clarity, maintainability, and responsiveness of the component.

Expand Down
Loading