feat: add profile reordering
This commit is contained in:
1 parent
8182fa09e7
commit
5ae9894ec5
4 files changed
+55
-8
No files matched your search
+15
-5
@@ -4,6 +4,7 @@ import { DEFAULT_INSTANCE_URL, instance } from '$lib/instance.js'
|
||||
import { getClient, site } from '$lib/lemmy.js'
|
||||
import { getInbox, getInboxItemPublished } from '$lib/lemmy/inbox.js'
|
||||
import { userSettings } from '$lib/settings.js'
|
||||
import { moveItem } from '$lib/util.js'
|
||||
import type { GetSiteResponse, MyUserInfo } from 'lemmy-js-client'
|
||||
import { get, writable } from 'svelte/store'
|
||||
|
||||
@@ -200,6 +201,20 @@ export function setUserID(id: number) {
|
||||
return prof
|
||||
}
|
||||
|
||||
export function moveProfile(id: number, up: boolean) {
|
||||
const pd = get(profileData)
|
||||
try {
|
||||
const index = pd.profiles.findIndex((i) => i.id == id)
|
||||
|
||||
profileData.set({
|
||||
...pd,
|
||||
profiles: moveItem(pd.profiles, index, index + (up ? -1 : 1)),
|
||||
})
|
||||
} catch (err) {
|
||||
// we dont care
|
||||
}
|
||||
}
|
||||
|
||||
const getNotificationCount = async (jwt: string, mod: boolean) => {
|
||||
const unreads = await getClient().getUnreadCount({
|
||||
auth: jwt,
|
||||
@@ -279,8 +294,3 @@ export async function getInboxNotifications(dontUpdate: boolean = false) {
|
||||
|
||||
localStorage.setItem('seenUntil', Date.now().toString())
|
||||
}
|
||||
|
||||
setInterval(
|
||||
async () => getInboxNotifications(),
|
||||
get(userSettings).notifications.notifRate || 30 * 1000
|
||||
)
|
||||
@@ -56,3 +56,28 @@ export const placeholders = {
|
||||
: ''
|
||||
},
|
||||
}
|
||||
|
||||
export function moveItem<T>(
|
||||
array: T[],
|
||||
currentIndex: number,
|
||||
newIndex: number
|
||||
): T[] {
|
||||
if (
|
||||
currentIndex < 0 ||
|
||||
currentIndex >= array.length ||
|
||||
newIndex < 0 ||
|
||||
newIndex >= array.length
|
||||
) {
|
||||
throw new Error('Invalid index')
|
||||
}
|
||||
|
||||
const newArray = [...array]
|
||||
|
||||
// Remove the item from the current index
|
||||
const [item] = newArray.splice(currentIndex, 1)
|
||||
|
||||
// Insert the item at the new index
|
||||
newArray.splice(newIndex, 0, item)
|
||||
|
||||
return newArray
|
||||
}
|
||||
Reference in new issue
Block a user