Skip to content

Commit

Permalink
admin: Switch array search functions to more efficient ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ThrRip committed Dec 19, 2024
1 parent 388dd3e commit 25d558b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/admin/app/components/playlist/MainList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ function modifyNext (currentTarget: ModifyTarget) {
const fields: Array<ModifyTarget['field']> = ['name', 'artist', 'payment_amount', 'language']
modifyingInput.value?.[0]?.blur()
modifyingTarget.value.id = currentTarget.id
modifyingTarget.value.field = fields[fields.findIndex(field => field === currentTarget.field) + 1] ?? null
modifyingTarget.value.field = fields[fields.indexOf(currentTarget.field) + 1] ?? null
nextTick(() => modifyingInput.value?.[0]?.focus())
}

Expand Down
24 changes: 14 additions & 10 deletions packages/admin/app/pages/playlist.vue
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ function backendPlaylistPublishChanges (changesIds: Set<Song['$id']>) {
else

// Modify song
if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length > 1) {
if (
backendPlaylist.value.findIndex(song => song.$id === changes.$id) !== -1 &&
Object.keys(changes).length > 1
) {
delete changes.$id
backendDatabases.updateDocument('home', 'playlist', changesId, changes)
.then(() => succeed(changesId), err => fail(changesId, err))
Expand All @@ -165,7 +168,10 @@ function backendPlaylistPublishChanges (changesIds: Set<Song['$id']>) {
else

// Delete song
if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length === 1) {
if (
backendPlaylist.value.findIndex(song => song.$id === changes.$id) !== -1 &&
Object.keys(changes).length === 1
) {
backendDatabases.deleteDocument('home', 'playlist', changesId)
.then(() => succeed(changesId), err => fail(changesId, err))
.finally(() => cleanup(changesId))
Expand Down Expand Up @@ -210,7 +216,7 @@ function viewPlaylistToggleSorting (column: PlaylistColumn) {
// Rotate between ordering options
else {
viewPlaylistSortingOrder.value = viewPlaylistSortingOrderOptions[column][
viewPlaylistSortingOrderOptions[column].findIndex(option => option === viewPlaylistSortingOrder.value) + 1
viewPlaylistSortingOrderOptions[column].indexOf(viewPlaylistSortingOrder.value as PlaylistSortingOrder) + 1
]
}
viewPlaylistDataUpdate()
Expand Down Expand Up @@ -282,10 +288,8 @@ async function viewPlaylistDataSort (data: Playlist, column: PlaylistColumn, ord
// Sort by language
if (column === 'language') {
const languageOrder = ['国语', '粤语', '日语']
playlist.sort((a, b) => {
return (languageOrder.findIndex(language => a.language === language) -
languageOrder.findIndex(language => b.language === language)) * orderModifier
})
// @ts-expect-error
playlist.sort((a, b) => ((languageOrder.indexOf(a.language) - languageOrder.indexOf(b.language)) * orderModifier))
}

return playlist
Expand Down Expand Up @@ -338,7 +342,7 @@ function viewPlaylistStageChanges (changes: Song) {
else

// Modify song
if (viewPlaylistData.value.find(song => song.$id === changes.$id) && Object.keys(changes).length > 1) {
if (viewPlaylistData.value.findIndex(song => song.$id === changes.$id) !== -1 && Object.keys(changes).length > 1) {
if (changesStaged) {
let diffFromStaged = false
for (const field in changes) {
Expand Down Expand Up @@ -393,7 +397,7 @@ function viewPlaylistStageChanges (changes: Song) {

// Delete song
if (
viewPlaylistData.value.find(song => song.$id === changes.$id) &&
viewPlaylistData.value.findIndex(song => song.$id === changes.$id) !== -1 &&
Object.keys(changes).length === 1 &&
changes.$id
) {
Expand All @@ -415,7 +419,7 @@ function viewPlaylistStageChanges (changes: Song) {
const viewPlaylistFurtherChangesIds = ref<Set<Song['$id']>>(new Set())
function viewPlaylistFurtherChangeCheck (changesId: Song['$id']) {
// If changes were made to a song with publishing history, mark for transition and clear its previous publishing state
if (backendPlaylistPublishChangesState.value.find(_ => _.$id === changesId)) {
if (backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) !== -1) {
viewPlaylistFurtherChangesIds.value.add(changesId)
setTimeout(() => viewPlaylistFurtherChangesIds.value.delete(changesId), 200)
backendPlaylistPublishChangesState.value.splice(
Expand Down
8 changes: 3 additions & 5 deletions packages/home/app/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ function viewPlaylistToggleSorting (column: PlaylistColumn) {
// Rotate between ordering options
else {
viewPlaylistSortingOrder.value = viewPlaylistSortingOrderOptions[column][
viewPlaylistSortingOrderOptions[column].findIndex(option => option === viewPlaylistSortingOrder.value) + 1
viewPlaylistSortingOrderOptions[column].indexOf(viewPlaylistSortingOrder.value as PlaylistSortingOrder) + 1
]
}
viewPlaylistDataSort(
Expand Down Expand Up @@ -411,10 +411,8 @@ async function viewPlaylistDataSort (data: Playlist, column: PlaylistColumn, ord
// Sort by language
if (column === 'language') {
const languageOrder = ['国语', '粤语', '日语']
playlist.sort((a, b) => {
return (languageOrder.findIndex(language => a.language === language) -
languageOrder.findIndex(language => b.language === language)) * orderModifier
})
// @ts-expect-error
playlist.sort((a, b) => ((languageOrder.indexOf(a.language) - languageOrder.indexOf(b.language)) * orderModifier))
}

return playlist
Expand Down

0 comments on commit 25d558b

Please sign in to comment.