Skip to content

Commit

Permalink
Update index.mjs
Browse files Browse the repository at this point in the history
  • Loading branch information
steve02081504 committed Feb 1, 2025
1 parent cba718e commit 33a2224
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/public/home/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,35 @@ async function setLocale(locale) {
throw new Error(data.message)
}

async function updateCharList() {
const currentChars = Array.from(roleContainer.children).map(
(element) => element.dataset.charname
)
const newCharList = await getCharList()

// Remove characters that no longer exist
for (const char of currentChars)
if (!newCharList.includes(char)) {
const elementToRemove = roleContainer.querySelector(
`[data-charname="${char}"]`
)
if (elementToRemove)
roleContainer.removeChild(elementToRemove)
}

// Append new characters in order
for (const char of newCharList)
if (!currentChars.includes(char)) {
const charDetails = await getCharDetails(char)
const roleElement = await renderCharView(charDetails, char)
roleElement.dataset.charname = char
roleContainer.appendChild(roleElement)
}
}

async function displayCharList() {
const charList = await getCharList()
roleContainer.innerHTML = ''

for (const char of charList) {
const charDetails = await getCharDetails(char)
const roleElement = await renderCharView(charDetails, char)
roleContainer.appendChild(roleElement)
}
await updateCharList()
}

// 添加功能按钮
Expand Down Expand Up @@ -164,4 +184,9 @@ async function initializeApp() {
displayCharList()
}

// Add event listener for window focus
window.addEventListener('focus', async () => {
await updateCharList()
})

initializeApp()

0 comments on commit 33a2224

Please sign in to comment.