From ccc65b0b06d3c9d05dc8a1698d3ed497b21e9df0 Mon Sep 17 00:00:00 2001 From: axolotle Date: Tue, 13 Aug 2024 00:19:22 +0200 Subject: [PATCH] refactor: rework async BackupCreate --- app/src/types/core/api.ts | 38 +++++ app/src/views/backup/BackupCreate.vue | 223 +++++++++----------------- app/src/views/backup/backupData.ts | 59 +++++++ 3 files changed, 174 insertions(+), 146 deletions(-) create mode 100644 app/src/views/backup/backupData.ts diff --git a/app/src/types/core/api.ts b/app/src/types/core/api.ts index 7ba15ea0..d5a5e57c 100644 --- a/app/src/types/core/api.ts +++ b/app/src/types/core/api.ts @@ -150,3 +150,41 @@ export type AppInfo = { } export type AppList = { apps: AppInfo[] } + +// BACKUP + +export type BackupHookDataKeys = + | 'data_xmpp' + | 'data_multimedia' + | 'data_mail' + | 'data_home' +export type BackupHookKeys = + | BackupHookDataKeys + | 'conf_ynh_settings' + | 'conf_ldap' + | 'conf_manually_modified_files' + | 'conf_ynh_certs' + +export type BackupHooksList = { + hooks: BackupHookKeys[] +} + +export type BackupAppList = { + apps: Pick< + AppInfo, + 'description' | 'name' | 'version' | 'domain_path' | 'id' + >[] +} + +export type BackupInfo = { + path: string + created_at: string + description: string + size: number + // TODO as array like everywhere else? + apps: Obj< + Pick & { size: number } + > + system: Record + from_yunohost_version: string +} diff --git a/app/src/views/backup/BackupCreate.vue b/app/src/views/backup/BackupCreate.vue index df8e8b76..6a77fa86 100644 --- a/app/src/views/backup/BackupCreate.vue +++ b/app/src/views/backup/BackupCreate.vue @@ -1,58 +1,34 @@ - + diff --git a/app/src/views/backup/backupData.ts b/app/src/views/backup/backupData.ts new file mode 100644 index 00000000..30e488ef --- /dev/null +++ b/app/src/views/backup/backupData.ts @@ -0,0 +1,59 @@ +import { toEntries } from '@/helpers/commons' +import type { + BackupHookDataKeys, + BackupHookKeys, + BackupHooksList, + BackupInfo, +} from '@/types/core/api' +import i18n from '@/i18n' + +type BackupSystem = Record< + BackupHookDataKeys | 'adminjs_group_configuration', + { + name: string + value: string[] + description: string + size?: number + } +> + +export function formatBackupSystem( + system: BackupHooksList['hooks'] | BackupInfo['system'], +) { + const t = i18n.global.t + const infos = ( + !Array.isArray(system) + ? toEntries(system).map(([key, { size }]) => [key, size]) + : system.map((key) => [key]) + ) as [BackupHookKeys, number | undefined][] + return infos.reduce((data, [key, size]) => { + const hookKey = key.startsWith('conf_') + ? 'adminjs_group_configuration' + : (key as BackupHookDataKeys) + if (hookKey in data) { + data[hookKey].value.push(key) + data[hookKey].description += ', ' + t('hook_' + key) + if (size) data[hookKey].size! += size + } else { + data[hookKey] = { + name: t('hook_' + hookKey), + value: [key], + description: t(hookKey === key ? `hook_${key}_desc` : 'hook_' + key), + size, + } + } + return data + }, {} as BackupSystem) +} + +export function parseBackupForm(selected: string[], system: BackupSystem) { + const data = { apps: [], system: [] } as { apps: string[]; system: string[] } + for (const key of selected) { + if (key in system) { + data.system.push(...system[key as keyof typeof system].value) + } else { + data.apps.push(key) + } + } + return data +}