Skip to content

Commit

Permalink
chore: Use getFileInfo helper from utils (chatwoot#10819)
Browse files Browse the repository at this point in the history
# Pull Request Template

## Description
The PR includes the usage of `getFileInfo` helper from utils
chatwoot/utils#40.

Fixes
chatwoot#10806 (comment)

## How Has This Been Tested?

**Screenshot**
<img width="490" alt="image"
src="https://github.com/user-attachments/assets/f0788e89-b670-47da-b0ca-3765eb424be0"
/>



## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [ ] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
iamsivin authored and Diego Santos committed Mar 5, 2025
1 parent 6246918 commit af0d2f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 38 deletions.
42 changes: 10 additions & 32 deletions app/javascript/dashboard/components-next/message/chips/File.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup>
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { getFileInfo } from '@chatwoot/utils';
import FileIcon from 'next/icon/FileIcon.vue';
import Icon from 'next/icon/Icon.vue';
Expand All @@ -14,43 +15,20 @@ const { attachment } = defineProps({
const { t } = useI18n();
const fileName = computed(() => {
const url = attachment.dataUrl;
if (!url) return t('CONVERSATION.UNKNOWN_FILE_TYPE');
try {
const encodedFilename = url.substring(url.lastIndexOf('/') + 1);
return decodeURIComponent(encodedFilename);
} catch {
return t('CONVERSATION.UNKNOWN_FILE_TYPE');
}
});
const fileType = computed(() => fileName.value.split('.').pop()?.toLowerCase());
const fileNameWithoutExt = computed(() => {
const parts = fileName.value.split('.');
// If there's no extension (no dots in filename)
if (parts.length === 1) {
return fileName.value.trim();
}
// Take all parts except the last one (extension)
const nameWithoutExt = parts.slice(0, -1).join('.');
return nameWithoutExt.trim();
const fileDetails = computed(() => {
return getFileInfo(attachment?.dataUrl || '');
});
const displayFileName = computed(() => {
const name = fileNameWithoutExt.value;
const { base, type } = fileDetails.value;
const truncatedName = (str, maxLength, hasExt) =>
str.length > maxLength
? `${str.substring(0, maxLength).trimEnd()}${hasExt ? '..' : '...'}`
: str;
return fileType.value
? `${truncatedName(name, 14, true)}.${fileType.value}`
: truncatedName(name, 16, false);
return type
? `${truncatedName(base, 12, true)}.${type}`
: truncatedName(base, 14, false);
});
const textColorClass = computed(() => {
Expand All @@ -73,18 +51,18 @@ const textColorClass = computed(() => {
zip: 'dark:text-[#EDEEF0] text-[#2F265F]',
};
return colorMap[fileType.value] || 'text-n-slate-12';
return colorMap[fileDetails.value.type] || 'text-n-slate-12';
});
</script>
<template>
<div
class="h-9 bg-n-alpha-white gap-2 overflow-hidden items-center flex px-2 rounded-lg border border-n-container"
>
<FileIcon class="flex-shrink-0" :file-type="fileType" />
<FileIcon class="flex-shrink-0" :file-type="fileDetails.type" />
<span
class="flex-1 min-w-0 text-sm max-w-36"
:title="fileName"
:title="fileDetails.name"
:class="textColorClass"
>
{{ displayFileName }}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"@breezystack/lamejs": "^1.2.7",
"@chatwoot/ninja-keys": "1.2.3",
"@chatwoot/prosemirror-schema": "1.1.1-next",
"@chatwoot/utils": "^0.0.33",
"@chatwoot/utils": "^0.0.35",
"@formkit/core": "^1.6.7",
"@formkit/vue": "^1.6.7",
"@hcaptcha/vue3-hcaptcha": "^1.3.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af0d2f0

Please sign in to comment.