2026-09-11 14:47:01 +03:30
|
|
|
|
<script setup lang="ts">
|
2026-11-04 10:01:11 +03:30
|
|
|
|
import { computed, onMounted, ref } from 'vue';
|
2026-10-05 11:56:49 +03:30
|
|
|
|
import TableColumnsMenu from '@/components/TableColumnsMenu.vue';
|
2026-09-11 14:47:01 +03:30
|
|
|
|
import { RouterLink } from 'vue-router';
|
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
|
import { api } from '@/lib/api';
|
|
|
|
|
|
import { formatJalaliNumeric } from '@/lib/jalali';
|
2026-11-04 10:01:11 +03:30
|
|
|
|
import { registerTableColumns, useTableColumns } from '@/lib/tableColumns';
|
2026-09-11 14:47:01 +03:30
|
|
|
|
import type { NoteAttachmentDto, NoteDto, PaginatedResponse } from '@mento/shared';
|
2026-11-04 10:01:11 +03:30
|
|
|
|
import {
|
|
|
|
|
|
Alert as AAlert,
|
|
|
|
|
|
Button as AButton,
|
|
|
|
|
|
Card as ACard,
|
|
|
|
|
|
Empty as AEmpty,
|
|
|
|
|
|
Flex as AFlex,
|
|
|
|
|
|
Spin as ASpin,
|
|
|
|
|
|
Table as ATable,
|
|
|
|
|
|
Typography,
|
|
|
|
|
|
} from 'ant-design-vue';
|
|
|
|
|
|
import type { TableColumnsType } from 'ant-design-vue';
|
|
|
|
|
|
import { DownloadOutlined } from '@ant-design/icons-vue';
|
|
|
|
|
|
|
|
|
|
|
|
const { Title: ATypographyTitle, Text: ATypographyText, Link: ATypographyLink } = Typography;
|
2026-09-11 14:47:01 +03:30
|
|
|
|
|
|
|
|
|
|
type Row = NoteAttachmentDto & {
|
|
|
|
|
|
noteId: string;
|
|
|
|
|
|
noteTitle: string;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
|
const ATT_COL_LABELS = ['نام فایل', 'یادداشت', 'حجم', 'تاریخ'] as const;
|
|
|
|
|
|
|
|
|
|
|
|
registerTableColumns('attachments', [...ATT_COL_LABELS]);
|
|
|
|
|
|
const tableCols = useTableColumns(() => 'attachments');
|
|
|
|
|
|
|
2026-09-11 14:47:01 +03:30
|
|
|
|
const loading = ref(false);
|
|
|
|
|
|
const error = ref('');
|
|
|
|
|
|
const items = ref<Row[]>([]);
|
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
|
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);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-09-11 14:47:01 +03:30
|
|
|
|
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`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
|
function rowKey(row: Row) {
|
|
|
|
|
|
return `${row.noteId}-${row.id}`;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-11 14:47:01 +03:30
|
|
|
|
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>
|
2026-11-04 10:01:11 +03:30
|
|
|
|
<a-flex vertical :gap="16">
|
|
|
|
|
|
<a-typography-title :level="3" :style="{ margin: 0 }">پیوستها</a-typography-title>
|
2026-09-11 14:47:01 +03:30
|
|
|
|
|
2026-11-04 10:01:11 +03:30
|
|
|
|
<a-alert v-if="error" type="error" show-icon :message="error" />
|
|
|
|
|
|
|
|
|
|
|
|
<a-spin :spinning="loading">
|
|
|
|
|
|
<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>
|
|
|
|
|
|
</a-spin>
|
|
|
|
|
|
</a-flex>
|
|
|
|
|
|
</template>
|