|
| 1 | +import { defineComponent, ref, onMounted } from "vue"; |
| 2 | +import { Button, Section } from "@munet/ui"; |
| 3 | +import { theme } from "@munet/ui"; |
| 4 | +import { useI18n } from "vue-i18n"; |
| 5 | +import api from "@/client/api"; |
| 6 | +import { updateAll } from "@/store/refs"; |
| 7 | + |
| 8 | +export default defineComponent({ |
| 9 | + setup() { |
| 10 | + const { t } = useI18n(); |
| 11 | + |
| 12 | + const gamePath = ref(''); |
| 13 | + const switching = ref(false); |
| 14 | + const error = ref(''); |
| 15 | + const historyPaths = ref<string[]>([]); |
| 16 | + |
| 17 | + onMounted(async () => { |
| 18 | + try { |
| 19 | + const res = await api.GetGamePath(); |
| 20 | + gamePath.value = res.data || ''; |
| 21 | + } catch {} |
| 22 | + try { |
| 23 | + const res = await api.GetGamePathHistory(); |
| 24 | + historyPaths.value = res.data || []; |
| 25 | + } catch {} |
| 26 | + }); |
| 27 | + |
| 28 | + const handleChangeDirectory = async () => { |
| 29 | + error.value = ''; |
| 30 | + try { |
| 31 | + const res = await api.OpenFolderDialog(); |
| 32 | + if (!res.data) return; |
| 33 | + switching.value = true; |
| 34 | + await api.SetGamePath(res.data); |
| 35 | + await api.InitializeGameData(); |
| 36 | + await updateAll(); |
| 37 | + gamePath.value = res.data; |
| 38 | + if (!historyPaths.value.includes(res.data)) { |
| 39 | + historyPaths.value.push(res.data); |
| 40 | + } |
| 41 | + } catch (e: any) { |
| 42 | + error.value = t('settings.changeDirectoryFailed'); |
| 43 | + } finally { |
| 44 | + switching.value = false; |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + const handleSwitchToHistory = async (path: string) => { |
| 49 | + if (path === gamePath.value) return; |
| 50 | + error.value = ''; |
| 51 | + switching.value = true; |
| 52 | + try { |
| 53 | + await api.SetGamePath(path); |
| 54 | + await api.InitializeGameData(); |
| 55 | + await updateAll(); |
| 56 | + gamePath.value = path; |
| 57 | + } catch { |
| 58 | + error.value = t('settings.changeDirectoryFailed'); |
| 59 | + } finally { |
| 60 | + switching.value = false; |
| 61 | + } |
| 62 | + }; |
| 63 | + |
| 64 | + const handleDeleteHistory = async (path: string) => { |
| 65 | + try { |
| 66 | + await api.DeleteGamePathHistory(path); |
| 67 | + historyPaths.value = historyPaths.value.filter(p => p !== path); |
| 68 | + } catch {} |
| 69 | + }; |
| 70 | + |
| 71 | + return () => ( |
| 72 | + <div class="mb-6"> |
| 73 | + <div class="text-lg font-semibold mb-3 text-[var(--link-color)]">{t('settings.gameDirectory')}</div> |
| 74 | + <div class="rounded-xl bg-white/60 p-4 flex flex-col gap-4 border border-gray-200 border-solid"> |
| 75 | + <div class="flex items-center gap-3"> |
| 76 | + <span class="shrink-0 op-60">{t('settings.currentPath')}</span> |
| 77 | + <span class="text-sm break-all">{gamePath.value || '—'}</span> |
| 78 | + </div> |
| 79 | + <div class="flex items-center gap-3"> |
| 80 | + <Button |
| 81 | + disabled={switching.value} |
| 82 | + onClick={handleChangeDirectory} |
| 83 | + ing={switching.value} |
| 84 | + > |
| 85 | + {t('settings.changeDirectory')} |
| 86 | + </Button> |
| 87 | + <button |
| 88 | + onClick={()=>api.SwitchToSetMode()} |
| 89 | + > |
| 90 | + {t('settings.switchMode')} |
| 91 | + </button> |
| 92 | + {error.value && <span class="text-red-500 text-sm">{error.value}</span>} |
| 93 | + </div> |
| 94 | + {/* History */} |
| 95 | + <Section title={t('settings.historyPath')} icon="i-solar:history-linear"> |
| 96 | + {historyPaths.value.length === 0 |
| 97 | + ? <span class="text-sm op-50">{t('settings.noHistory')}</span> |
| 98 | + : historyPaths.value.map(path => ( |
| 99 | + <div |
| 100 | + class={[ |
| 101 | + 'flex items-center gap-2 px-3 py-1 rounded-lg cursor-pointer', |
| 102 | + theme.value.listItemHover, |
| 103 | + path === gamePath.value && theme.value.listItem, |
| 104 | + ]} |
| 105 | + key={path} |
| 106 | + onClick={() => handleSwitchToHistory(path)} |
| 107 | + > |
| 108 | + <div class="i-material-symbols:folder-outline-rounded text-lg op-70 shrink-0" /> |
| 109 | + <span class="text-sm break-all grow">{path}</span> |
| 110 | + <button |
| 111 | + onClick={(e: Event) => { e.stopPropagation(); handleDeleteHistory(path); }} |
| 112 | + > |
| 113 | + <div class="i-tabler:trash text-base op-80 text-sm" /> |
| 114 | + </button> |
| 115 | + </div> |
| 116 | + )) |
| 117 | + } |
| 118 | + </Section> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + ); |
| 122 | + }, |
| 123 | +}); |
0 commit comments