123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <script setup lang="ts">
- import type { ConfigBackup } from '@/api/config'
- import type { GetListResponse } from '@/api/curd'
- import type { Key } from 'ant-design-vue/es/_util/type'
- import config from '@/api/config'
- import StdPagination from '@/components/StdDesign/StdDataDisplay/StdPagination.vue'
- import { message } from 'ant-design-vue'
- import { datetime } from '../StdDesign/StdDataDisplay/StdTableTransformer'
- import DiffViewer from './DiffViewer.vue'
- // Define props for the component
- const props = defineProps<{
- filepath: string
- currentContent?: string
- }>()
- // Define modal props using defineModel with boolean type
- const visible = defineModel<boolean>('visible')
- const loading = ref(false)
- const records = ref<ConfigBackup[]>([])
- const showDiffViewer = ref(false)
- const pagination = ref({
- total: 0,
- per_page: 10,
- current_page: 1,
- total_pages: 0,
- })
- const selectedRowKeys = ref<Key[]>([])
- const selectedRecords = ref<ConfigBackup[]>([])
- // Watch for changes in modal visibility and filepath to fetch data
- watch(() => [visible.value, props.filepath], ([newVisible, newPath]) => {
- if (newVisible && newPath) {
- fetchHistoryList()
- }
- }, { immediate: true })
- // Table column definitions
- const columns = [
- {
- title: () => $gettext('Created At'),
- dataIndex: 'created_at',
- key: 'created_at',
- customRender: datetime,
- },
- ]
- // Fetch history records list
- async function fetchHistoryList() {
- if (!props.filepath)
- return
- loading.value = true
- try {
- const response = await config.get_history(props.filepath)
- const data = response as GetListResponse<ConfigBackup>
- records.value = data.data || []
- if (data.pagination) {
- pagination.value = data.pagination
- }
- }
- catch (error) {
- message.error($gettext('Failed to load history records'))
- console.error('Failed to fetch config backup list:', error)
- }
- finally {
- loading.value = false
- }
- }
- // Handle pagination changes
- function changePage(page: number, pageSize: number) {
- pagination.value.current_page = page
- pagination.value.per_page = pageSize
- fetchHistoryList()
- }
- // Row selection handler
- const rowSelection = computed(() => ({
- selectedRowKeys: selectedRowKeys.value,
- onChange: (keys: Key[], selectedRows: ConfigBackup[]) => {
- // Limit to maximum of two records
- if (keys.length > 2) {
- return
- }
- selectedRowKeys.value = keys
- selectedRecords.value = selectedRows
- },
- getCheckboxProps: (record: ConfigBackup) => ({
- disabled: selectedRowKeys.value.length >= 2 && !selectedRowKeys.value.includes(record.id as Key),
- }),
- }))
- // Compare selected records
- function compareSelected() {
- if (selectedRecords.value.length > 0) {
- showDiffViewer.value = true
- }
- }
- // Close modal and reset selection
- function handleClose() {
- showDiffViewer.value = false
- selectedRowKeys.value = []
- selectedRecords.value = []
- visible.value = false
- }
- // Dynamic button text based on selection count
- const compareButtonText = computed(() => {
- if (selectedRowKeys.value.length === 0)
- return $gettext('Compare')
- if (selectedRowKeys.value.length === 1)
- return $gettext('Compare with Current')
- return $gettext('Compare Selected')
- })
- </script>
- <template>
- <div>
- <AModal
- v-model:open="visible"
- :title="$gettext('Configuration History')"
- :footer="null"
- @cancel="handleClose"
- >
- <div class="history-container">
- <ATable
- :loading="loading"
- :columns="columns"
- :data-source="records"
- :row-selection="rowSelection"
- row-key="id"
- size="small"
- :pagination="false"
- />
- <div class="history-footer">
- <StdPagination
- :pagination="pagination"
- :loading="loading"
- @change="changePage"
- />
- <div class="actions">
- <AButton
- type="primary"
- :disabled="selectedRowKeys.length === 0"
- @click="compareSelected"
- >
- {{ compareButtonText }}
- </AButton>
- <AButton @click="handleClose">
- {{ $gettext('Close') }}
- </AButton>
- </div>
- </div>
- </div>
- </AModal>
- <DiffViewer
- v-model:visible="showDiffViewer"
- :records="selectedRecords"
- :current-content="currentContent"
- />
- </div>
- </template>
- <style lang="less" scoped>
- .history-container {
- display: flex;
- flex-direction: column;
- height: 100%;
- }
- .history-footer {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-top: 16px;
- }
- .actions {
- display: flex;
- gap: 8px;
- }
- </style>
|