Replace the inline SVG icon set with the vuesax linear pack, add skeleton primitives for loading states, and keep page scroll inside the app shell. Co-authored-by: Cursor <cursoragent@cursor.com>
160 lines
5.2 KiB
Vue
160 lines
5.2 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue';
|
||
import TableColumnsMenu from '@/components/TableColumnsMenu.vue';
|
||
import { RouterLink } from 'vue-router';
|
||
import axios from 'axios';
|
||
import { api } from '@/lib/api';
|
||
import { formatJalaliNumeric } from '@/lib/jalali';
|
||
import { registerTableColumns, useTableColumns } from '@/lib/tableColumns';
|
||
import type { NoteAttachmentDto, NoteDto, PaginatedResponse } from '@mento/shared';
|
||
import {
|
||
Alert as AAlert,
|
||
Button as AButton,
|
||
Card as ACard,
|
||
Empty as AEmpty,
|
||
Flex as AFlex,
|
||
Table as ATable,
|
||
Typography,
|
||
} from 'ant-design-vue';
|
||
import type { TableColumnsType } from 'ant-design-vue';
|
||
import { DownloadOutlined } from '@/icons/ant-bridge';
|
||
import { CrmSkeleton, CrmSkeletonTable } from '@/components/skeleton';
|
||
|
||
const { Title: ATypographyTitle, Link: ATypographyLink } = Typography;
|
||
|
||
type Row = NoteAttachmentDto & {
|
||
noteId: string;
|
||
noteTitle: string;
|
||
};
|
||
|
||
const ATT_COL_LABELS = ['نام فایل', 'یادداشت', 'حجم', 'تاریخ'] as const;
|
||
|
||
registerTableColumns('attachments', [...ATT_COL_LABELS]);
|
||
const tableCols = useTableColumns(() => 'attachments');
|
||
|
||
const loading = ref(false);
|
||
const error = ref('');
|
||
const items = ref<Row[]>([]);
|
||
|
||
const listEmpty = computed(() => !loading.value && !items.value.length);
|
||
|
||
const columns = computed<TableColumnsType<Row>>(() => {
|
||
const all: TableColumnsType<Row> = [
|
||
{ title: 'نام فایل', key: 'نام فایل', dataIndex: 'fileName' },
|
||
{ title: 'یادداشت', key: 'یادداشت' },
|
||
{ title: 'حجم', key: 'حجم' },
|
||
{ title: 'تاریخ', key: 'تاریخ' },
|
||
{ title: '', key: 'عملیات', width: 110 },
|
||
];
|
||
return all.filter((col) => {
|
||
const key = String(col.key ?? '');
|
||
if (key === 'عملیات') return true;
|
||
if (!ATT_COL_LABELS.includes(key as (typeof ATT_COL_LABELS)[number])) return true;
|
||
return tableCols.isVisible(key);
|
||
});
|
||
});
|
||
|
||
function sizeLabel(bytes: number) {
|
||
if (bytes < 1024) return `${bytes} B`;
|
||
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||
}
|
||
|
||
function rowKey(row: Row) {
|
||
return `${row.noteId}-${row.id}`;
|
||
}
|
||
|
||
async function load() {
|
||
loading.value = true;
|
||
error.value = '';
|
||
try {
|
||
const { data } = await api.get<PaginatedResponse<NoteDto>>('/notes', {
|
||
params: { limit: 100 },
|
||
});
|
||
items.value = data.items.flatMap((note) =>
|
||
(note.attachments ?? []).map((att) => ({
|
||
...att,
|
||
noteId: note.id,
|
||
noteTitle: note.title?.trim() || note.body.slice(0, 40) || 'یادداشت',
|
||
})),
|
||
);
|
||
} catch (e) {
|
||
error.value = axios.isAxiosError(e)
|
||
? 'بارگذاری پیوستها ناموفق بود.'
|
||
: 'بارگذاری پیوستها ناموفق بود.';
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
async function download(row: Row) {
|
||
const { data } = await api.get<Blob>(
|
||
`/notes/${row.noteId}/attachments/${row.id}/download`,
|
||
{ responseType: 'blob' },
|
||
);
|
||
const url = URL.createObjectURL(data);
|
||
const a = document.createElement('a');
|
||
a.href = url;
|
||
a.download = row.fileName;
|
||
a.click();
|
||
URL.revokeObjectURL(url);
|
||
}
|
||
|
||
onMounted(load);
|
||
</script>
|
||
|
||
<template>
|
||
<a-flex vertical :gap="16">
|
||
<a-typography-title :level="3" :style="{ margin: 0 }">پیوستها</a-typography-title>
|
||
|
||
<a-alert v-if="error" type="error" show-icon :message="error" />
|
||
|
||
<CrmSkeleton :loading="loading">
|
||
<CrmSkeletonTable :rows="8" :columns="5">
|
||
<a-card v-if="listEmpty && !error" :bordered="false">
|
||
<a-empty description="پیوستی ثبت نشده." />
|
||
</a-card>
|
||
|
||
<a-card v-else-if="!error && items.length" :bordered="false">
|
||
<a-flex justify="flex-end" :style="{ marginBottom: '12px' }">
|
||
<TableColumnsMenu table-key="attachments" align="end" />
|
||
</a-flex>
|
||
|
||
<a-table
|
||
:columns="columns"
|
||
:data-source="items"
|
||
:pagination="false"
|
||
:row-key="rowKey"
|
||
:scroll="{ x: 640 }"
|
||
>
|
||
<template #bodyCell="{ column, record }">
|
||
<template v-if="column.key === 'نام فایل'">
|
||
{{ (record as Row).fileName }}
|
||
</template>
|
||
<template v-else-if="column.key === 'یادداشت'">
|
||
<RouterLink :to="{ name: 'notes' }" custom v-slot="{ href, navigate }">
|
||
<a-typography-link :href="href" @click="navigate">
|
||
{{ (record as Row).noteTitle }}
|
||
</a-typography-link>
|
||
</RouterLink>
|
||
</template>
|
||
<template v-else-if="column.key === 'حجم'">
|
||
{{ sizeLabel((record as Row).sizeBytes) }}
|
||
</template>
|
||
<template v-else-if="column.key === 'تاریخ'">
|
||
{{ formatJalaliNumeric(new Date((record as Row).createdAt)) }}
|
||
</template>
|
||
<template v-else-if="column.key === 'عملیات'">
|
||
<a-button @click="download(record as Row)">
|
||
<template #icon><DownloadOutlined /></template>
|
||
دانلود
|
||
</a-button>
|
||
</template>
|
||
</template>
|
||
</a-table>
|
||
</a-card>
|
||
</CrmSkeletonTable>
|
||
</CrmSkeleton>
|
||
</a-flex>
|
||
</template>
|