|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <h3 :class="$style.header">キャッシュの削除</h3> |
| 4 | + <div :class="$style.content"> |
| 5 | + <p v-if="cacheData && cacheData.usage" :class="$style.usage"> |
| 6 | + <template v-if="cacheData.usageDetails"> |
| 7 | + <label v-for="(usage, key) in cacheData.usageDetails" :key="key"> |
| 8 | + <input v-model="selectedCaches" type="checkbox" :value="key" /> |
| 9 | + {{ key }} {{ prettifyFileSize(usage) }} |
| 10 | + </label> |
| 11 | + </template> |
| 12 | + <template v-else> |
| 13 | + {{ prettifyFileSize(cacheData.usage) }} |
| 14 | + </template> |
| 15 | + </p> |
| 16 | + <div v-for="(cache, index) in selectedCaches" :key="index"> |
| 17 | + {{ cache }} |
| 18 | + </div> |
| 19 | + <form-button |
| 20 | + :class="$style.button" |
| 21 | + label="キャンセル" |
| 22 | + @click="closeModal" |
| 23 | + /> |
| 24 | + <form-button |
| 25 | + :class="$style.button" |
| 26 | + label="削除する" |
| 27 | + is-danger |
| 28 | + @click="clearCache" |
| 29 | + /> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | +</template> |
| 33 | + |
| 34 | +<script lang="ts"> |
| 35 | +import { onMounted, ref } from 'vue' |
| 36 | +import { useToastStore } from '/@/store/ui/toast' |
| 37 | +import { wait } from '/@/lib/basic/timer' |
| 38 | +import { checkStorageManagerSupport } from '/@/lib/dom/browser' |
| 39 | +import { prettifyFileSize } from '/@/lib/basic/file' |
| 40 | +import { useStampsStore } from '/@/store/entities/stamps' |
| 41 | +
|
| 42 | +declare global { |
| 43 | + interface StorageEstimate { |
| 44 | + usageDetails: Record<string, number> |
| 45 | + } |
| 46 | +} |
| 47 | +
|
| 48 | +const isStorageManagerSupported = checkStorageManagerSupport() |
| 49 | +const getStorageUsage = async () => { |
| 50 | + if (!isStorageManagerSupported) return null |
| 51 | +
|
| 52 | + return navigator.storage.estimate() |
| 53 | +} |
| 54 | +
|
| 55 | +const confirmClear = () => window.confirm('本当に削除しますか?') |
| 56 | +
|
| 57 | +/* CacheStorageのnameはsw.jsを参照 */ |
| 58 | +const clearCacheStorage = (cacheName: string) => window.caches.delete(cacheName) |
| 59 | +</script> |
| 60 | + |
| 61 | +<script lang="ts" setup> |
| 62 | +import FormButton from '/@/components/UI/FormButton.vue' |
| 63 | +
|
| 64 | +const { fetchStamps } = useStampsStore() |
| 65 | +const { addSuccessToast } = useToastStore() |
| 66 | +const showToast = (extraMesage?: string) => { |
| 67 | + addSuccessToast(`削除に成功しました${extraMesage ? `: ${extraMesage}` : ''}`) |
| 68 | +} |
| 69 | +
|
| 70 | +const cacheData = ref<StorageEstimate | null>(null) |
| 71 | +const setCacheData = async () => { |
| 72 | + cacheData.value = await getStorageUsage() |
| 73 | +} |
| 74 | +onMounted(setCacheData) |
| 75 | +
|
| 76 | +const selectedCaches = ref<Array<string>>([]) |
| 77 | +
|
| 78 | +const closeModal = () => { |
| 79 | + return |
| 80 | +} |
| 81 | +
|
| 82 | +const clearCache = async () => { |
| 83 | + if (!confirmClear()) return |
| 84 | + for (const key of selectedCaches.value) { |
| 85 | + switch (key) { |
| 86 | + case 'caches': { |
| 87 | + const names = await window.caches.keys() |
| 88 | + await Promise.all( |
| 89 | + names |
| 90 | + .filter(name => name.startsWith('traQ_S-precache')) |
| 91 | + .map(name => clearCacheStorage(name)) |
| 92 | + ) |
| 93 | + break |
| 94 | + } |
| 95 | + case 'indexedDB': { |
| 96 | + await clearCacheStorage('files-cache') |
| 97 | + break |
| 98 | + } |
| 99 | + case 'serviceWorkerRegistrations': { |
| 100 | + await clearCacheStorage('thumbnail-cache') |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + const registration = await navigator.serviceWorker?.getRegistration() |
| 106 | + if (registration) { |
| 107 | + registration.unregister() |
| 108 | + showToast('1秒後にリロードします') |
| 109 | + setCacheData() |
| 110 | + await wait(1000) |
| 111 | + window.location.reload() |
| 112 | + } else { |
| 113 | + showToast() |
| 114 | + } |
| 115 | +} |
| 116 | +</script> |
| 117 | + |
| 118 | +<style lang="scss" module> |
| 119 | +.header { |
| 120 | + margin-bottom: 8px; |
| 121 | +} |
| 122 | +.content { |
| 123 | + margin-left: 12px; |
| 124 | +} |
| 125 | +.usage { |
| 126 | + margin-bottom: 8px; |
| 127 | +} |
| 128 | +.button { |
| 129 | + margin-top: 8px; |
| 130 | + margin-right: 8px; |
| 131 | +} |
| 132 | +</style> |
0 commit comments