forked from Mirrors/elk
refactor(settings): replace file input with browser-fs-access
This commit is contained in:
parent
4460d0f59d
commit
f1f2449559
1 changed files with 28 additions and 37 deletions
|
@ -1,8 +1,7 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
|
import { fileOpen } from 'browser-fs-access'
|
||||||
import type { UserLogin } from '~/types'
|
import type { UserLogin } from '~/types'
|
||||||
|
|
||||||
const { lg } = breakpoints
|
|
||||||
|
|
||||||
const loggedInUsers = useUsers()
|
const loggedInUsers = useUsers()
|
||||||
|
|
||||||
async function exportTokens() {
|
async function exportTokens() {
|
||||||
|
@ -22,43 +21,35 @@ async function exportTokens() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function importTokens() {
|
async function importTokens() {
|
||||||
const input = document.createElement('input') as HTMLInputElement
|
const file = await fileOpen({
|
||||||
input.type = 'file'
|
description: 'Token File',
|
||||||
input.accept = 'application/json'
|
mimeTypes: ['application/json'],
|
||||||
input.multiple = false
|
|
||||||
|
|
||||||
input.addEventListener('change', async (e) => {
|
|
||||||
const file = (e.target as any)?.files?.[0] as File
|
|
||||||
if (!file)
|
|
||||||
return
|
|
||||||
|
|
||||||
try {
|
|
||||||
const content = await file.text()
|
|
||||||
const data = JSON.parse(content)
|
|
||||||
if (data.version !== 1)
|
|
||||||
throw new Error('Invalid version')
|
|
||||||
const users = data.users as UserLogin[]
|
|
||||||
const newUsers: UserLogin[] = []
|
|
||||||
for (const user of users) {
|
|
||||||
if (loggedInUsers.value.some(u => u.server === user.server && u.account.id === user.account.id))
|
|
||||||
continue
|
|
||||||
newUsers.push(user)
|
|
||||||
}
|
|
||||||
if (newUsers.length === 0) {
|
|
||||||
alert('No new users found')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if (!confirm(`Found ${newUsers.length} new users, are you sure you want to import them?`))
|
|
||||||
return
|
|
||||||
loggedInUsers.value = [...loggedInUsers.value, ...newUsers]
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
alert('Invalid Elk tokens file')
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
input.click()
|
try {
|
||||||
|
const content = await file.text()
|
||||||
|
const data = JSON.parse(content)
|
||||||
|
if (data.version !== 1)
|
||||||
|
throw new Error('Invalid version')
|
||||||
|
const users = data.users as UserLogin[]
|
||||||
|
const newUsers: UserLogin[] = []
|
||||||
|
for (const user of users) {
|
||||||
|
if (loggedInUsers.value.some(u => u.server === user.server && u.account.id === user.account.id))
|
||||||
|
continue
|
||||||
|
newUsers.push(user)
|
||||||
|
}
|
||||||
|
if (newUsers.length === 0) {
|
||||||
|
alert('No new users found')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!confirm(`Found ${newUsers.length} new users, are you sure you want to import them?`))
|
||||||
|
return
|
||||||
|
loggedInUsers.value = [...loggedInUsers.value, ...newUsers]
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
alert('Invalid Elk tokens file')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue