|
| 1 | +<script lang="ts" setup> |
| 2 | +import { |
| 3 | + TraktApiExtended, |
| 4 | + type TraktClientPagination, |
| 5 | +} from '@dvcol/trakt-http-client/models'; |
| 6 | +import { NButton, NIcon, NProgress } from 'naive-ui'; |
| 7 | +
|
| 8 | +import { reactive, ref } from 'vue'; |
| 9 | +
|
| 10 | +import type { CancellableWritePromise } from '~/utils/trakt-service.utils'; |
| 11 | +
|
| 12 | +import IconDownload from '~/components/icons/IconDownload.vue'; |
| 13 | +import IconLoadingDots from '~/components/icons/IconLoadingDots.vue'; |
| 14 | +import SettingsFormItem from '~/components/views/settings/SettingsFormItem.vue'; |
| 15 | +
|
| 16 | +import { NotificationService } from '~/services/notification.service'; |
| 17 | +import { TraktService } from '~/services/trakt.service'; |
| 18 | +import { |
| 19 | + isDefaultList, |
| 20 | + type ListEntity, |
| 21 | + ListType, |
| 22 | + type ListTypes, |
| 23 | + useListsStoreRefs, |
| 24 | +} from '~/stores/data/list.store'; |
| 25 | +import { logger } from '~/stores/settings/log.store'; |
| 26 | +import { useUserSettingsStoreRefs } from '~/stores/settings/user.store'; |
| 27 | +import { useI18n } from '~/utils/i18n.utils'; |
| 28 | +
|
| 29 | +const i18n = useI18n('settings', 'export'); |
| 30 | +const { user, userSetting } = useUserSettingsStoreRefs(); |
| 31 | +
|
| 32 | +const { lists } = useListsStoreRefs(); |
| 33 | +
|
| 34 | +const loading = reactive<Record<string | number, boolean>>({}); |
| 35 | +const cancelled = reactive<Record<string | number, boolean>>({}); |
| 36 | +const fetching = reactive< |
| 37 | + Record<string | number, CancellableWritePromise<unknown> | undefined> |
| 38 | +>({}); |
| 39 | +const pagination = reactive< |
| 40 | + Record<string | number, Partial<TraktClientPagination> | undefined> |
| 41 | +>({}); |
| 42 | +
|
| 43 | +type ExportScope<T extends Promise<unknown> = CancellableWritePromise<unknown>> = { |
| 44 | + label: string; |
| 45 | + name: string; |
| 46 | + click: () => T; |
| 47 | +}; |
| 48 | +const getOnClick = (scope: ExportScope, index: number) => async () => { |
| 49 | + loading[index] = true; |
| 50 | +
|
| 51 | + try { |
| 52 | + if (fetching[index]) { |
| 53 | + fetching[index]?.cancel(); |
| 54 | + cancelled[index] = true; |
| 55 | + await fetching[index]; |
| 56 | + NotificationService.message.warning(`${scope.name} ${i18n('export_aborted')}`); |
| 57 | + } else { |
| 58 | + fetching[index] = scope.click(); |
| 59 | + pagination[index] = fetching[index]?.pagination; |
| 60 | + await fetching[index]; |
| 61 | + if (cancelled[index]) { |
| 62 | + NotificationService.message.success(`${scope.name} ${i18n('export_success')}`); |
| 63 | + } |
| 64 | + } |
| 65 | + } catch (error) { |
| 66 | + logger.error(`Failed to export '${scope.name}'.`, error); |
| 67 | + NotificationService.error(`${scope.name} ${i18n('export_failure')}`, error); |
| 68 | + } finally { |
| 69 | + cancelled[index] = false; |
| 70 | + delete fetching[index]; |
| 71 | + delete pagination[index]; |
| 72 | + loading[index] = false; |
| 73 | + } |
| 74 | +}; |
| 75 | +
|
| 76 | +const fetchData = (type: ListTypes, name: string, entity?: ListEntity) => { |
| 77 | + const writer = { |
| 78 | + picker: { |
| 79 | + suggestedName: [ |
| 80 | + isDefaultList(type) || type === ListType.History |
| 81 | + ? i18n(`export_${name}_name`) |
| 82 | + : `${i18n(`export_${type}_name`)}_${name}`, |
| 83 | + user.value, |
| 84 | + new Date().toISOString(), |
| 85 | + ] |
| 86 | + .join('_') |
| 87 | + .replace(' ', '_') |
| 88 | + .toLowerCase(), |
| 89 | + }, |
| 90 | + separator: ',', |
| 91 | + }; |
| 92 | +
|
| 93 | + switch (type) { |
| 94 | + case 'history': |
| 95 | + return TraktService.export.history({ writer }); |
| 96 | + case 'watchlist': |
| 97 | + return TraktService.export.watchlist({ writer }); |
| 98 | + case 'collection': |
| 99 | + if (!entity?.scope) throw new Error('Entity scope is required for export.'); |
| 100 | + return TraktService.export.collection({ |
| 101 | + payload: { |
| 102 | + type: entity.scope, |
| 103 | + extended: TraktApiExtended.Full, |
| 104 | + }, |
| 105 | + writer, |
| 106 | + }); |
| 107 | + case 'favorites': |
| 108 | + return TraktService.export.favorites({ writer }); |
| 109 | + case 'list': |
| 110 | + if (!entity) throw new Error('List entity is required for export.'); |
| 111 | + return TraktService.export.list({ |
| 112 | + payload: { |
| 113 | + id: user.value, |
| 114 | + list_id: entity.id.toString(), |
| 115 | + extended: TraktApiExtended.Full, |
| 116 | + pagination: { limit: 1000 }, |
| 117 | + }, |
| 118 | + writer, |
| 119 | + }); |
| 120 | + default: |
| 121 | + throw new Error(`Unsupported list type: ${type}`); |
| 122 | + } |
| 123 | +}; |
| 124 | +
|
| 125 | +const scopes: ExportScope[] = [ |
| 126 | + { |
| 127 | + label: i18n('export_history'), |
| 128 | + name: i18n('export_history_name'), |
| 129 | + click: () => fetchData(ListType.History, ListType.History), |
| 130 | + }, |
| 131 | + ...(lists.value?.map(list => { |
| 132 | + const name = isDefaultList(list) ? i18n(list.name, 'list') : list.name; |
| 133 | + return { |
| 134 | + label: `${i18n('export_list')} ${name}`, |
| 135 | + name, |
| 136 | + click: () => fetchData(list.type, list.name, list), |
| 137 | + }; |
| 138 | + }) ?? []), |
| 139 | +]; |
| 140 | +
|
| 141 | +const exportScope: ExportScope<Promise<unknown>>[] = scopes.map((item, index) => ({ |
| 142 | + ...item, |
| 143 | + click: getOnClick(item, index), |
| 144 | +})); |
| 145 | +
|
| 146 | +const getProgress = (index: number) => { |
| 147 | + if (!pagination[index]?.pageCount) return; |
| 148 | + return [ |
| 149 | + i18n('export_progress_1'), |
| 150 | + pagination[index]?.page, |
| 151 | + i18n('export_progress_2'), |
| 152 | + pagination[index]?.pageCount, |
| 153 | + ].join(' '); |
| 154 | +}; |
| 155 | +
|
| 156 | +const getProgressPercentage = (index: number) => { |
| 157 | + if (!pagination[index]?.pageCount || !pagination[index]?.page) return; |
| 158 | + return (pagination[index]!.page! / pagination[index]!.pageCount!) * 100; |
| 159 | +}; |
| 160 | +
|
| 161 | +const container = ref(); |
| 162 | +</script> |
| 163 | + |
| 164 | +<template> |
| 165 | + <div ref="container" class="cache-container"> |
| 166 | + <!-- Scope export --> |
| 167 | + <SettingsFormItem |
| 168 | + v-for="({ label, click }, index) in exportScope" |
| 169 | + :key="index" |
| 170 | + :label="label" |
| 171 | + :warning="getProgress(index)" |
| 172 | + > |
| 173 | + <NButton |
| 174 | + class="export-button" |
| 175 | + :type="fetching[index] ? 'error' : 'info'" |
| 176 | + secondary |
| 177 | + :disabled="cancelled[index]" |
| 178 | + @click="click" |
| 179 | + > |
| 180 | + <span> |
| 181 | + {{ i18n(fetching[index] ? 'export_cancel' : 'export', 'common', 'button') }} |
| 182 | + </span> |
| 183 | + <template #icon> |
| 184 | + <NIcon :component="loading[index] ? IconLoadingDots : IconDownload" /> |
| 185 | + </template> |
| 186 | + </NButton> |
| 187 | + <NProgress |
| 188 | + v-if="pagination[index]?.pageCount" |
| 189 | + class="export-line" |
| 190 | + type="line" |
| 191 | + status="warning" |
| 192 | + :percentage="getProgressPercentage(index)" |
| 193 | + :show-indicator="false" |
| 194 | + :theme-overrides="{ |
| 195 | + railHeight: 'var(--rail-height)', |
| 196 | + }" |
| 197 | + /> |
| 198 | + </SettingsFormItem> |
| 199 | + </div> |
| 200 | +</template> |
| 201 | + |
| 202 | +<style lang="scss" scoped> |
| 203 | +.cache-container { |
| 204 | + display: flex; |
| 205 | + flex-direction: column; |
| 206 | + gap: 1.5rem; |
| 207 | +} |
| 208 | +
|
| 209 | +.export-button { |
| 210 | + i { |
| 211 | + margin-left: calc(0% - var(--n-icon-margin)); |
| 212 | + } |
| 213 | +} |
| 214 | +
|
| 215 | +.export-line { |
| 216 | + --rail-height: 2px; |
| 217 | +
|
| 218 | + position: absolute; |
| 219 | + bottom: calc(var(--rail-height) * -1); |
| 220 | + border-radius: 0; |
| 221 | +} |
| 222 | +
|
| 223 | +.form-select { |
| 224 | + min-width: 10rem; |
| 225 | +} |
| 226 | +</style> |
0 commit comments