mirror of
https://github.com/modrinth/code.git
synced 2026-08-25 09:04:55 +00:00
* better default state handling
* make next stage button work for going to next project
* fix skill issue
* actually fix initial values this time,
make single line inputs with varying lengths (text input and dropdown) use their widest possible length instead of whatever the hell they used to be
* loaders in progress
* loaders and versions test (probably dont use them as the messages suck and idrk what they should do quick fix/message wise)
* WIP the wip
* good enough for now slug taken improvements
* trim all those extra newlines
* viewedness
* batch prefetch queue + technically slightly faster checklist init
* show all valid alternative project types regardless of current project type
* Hide moderation permissions stage when it should be, and I'm sure this won't cause any issues
* Hide moderation permissions stage when it should be, and I'm sure this won't cause any issues
* gimme them versions
* better moderation permission stage shown check?
* betterer moderation permission stage shown check?
* bettererer moderation permission stage shown check? + also began deleting legacy checklist types
* fix certain stages being able to be considered viewed before being viewed
* more removals + i forgot to remove an import last commit
* changed nothing™️
* remove severities + bandaid post-approval message priorities + dependencies
* fix message preview tooltips
* next stage = next project when done
* insufficient description custom fix
* fix that thing coolbot told me was broken I forgot what it was oops
* oh there was a second bug there
* make alternate versions and incorrect project type required cuz they are
* re-navigate button also damn that's a lot of other stuff that pnpm fix... fixed?
* you cant just quick fix a slug into a taken one
* maybe lets not just try and set a slug to something entirely invalid?
* what if we just didn't drop ur queue :smart:
* more queue upgrades
* tweaks
* I love unbreaking things
* un-nuked renavigate button
* prepr
* prepr worked this time i think
* undo more mistakes
* i love intellij refactoring
* ok i think we're good
95 lines
2.8 KiB
TypeScript
95 lines
2.8 KiB
TypeScript
const DB_NAME = 'modrinth-moderation'
|
|
const DB_VERSION = 2
|
|
|
|
function hasIndexedDb(): boolean {
|
|
return typeof window !== 'undefined' && typeof indexedDB !== 'undefined'
|
|
}
|
|
|
|
function openDatabase(): Promise<IDBDatabase> {
|
|
return new Promise((resolve, reject) => {
|
|
const request = indexedDB.open(DB_NAME, DB_VERSION)
|
|
|
|
request.onupgradeneeded = (event) => {
|
|
const db = request.result
|
|
if (event.oldVersion < 2 && db.objectStoreNames.contains('kv')) {
|
|
db.deleteObjectStore('kv')
|
|
}
|
|
if (!db.objectStoreNames.contains('checklist')) {
|
|
db.createObjectStore('checklist')
|
|
}
|
|
if (!db.objectStoreNames.contains('queue')) {
|
|
db.createObjectStore('queue')
|
|
}
|
|
}
|
|
|
|
request.onsuccess = () => resolve(request.result)
|
|
request.onerror = () => reject(request.error ?? new Error('Failed to open IndexedDB'))
|
|
request.onblocked = () => reject(new Error('IndexedDB open request blocked'))
|
|
})
|
|
}
|
|
|
|
function requestToPromise<T>(request: IDBRequest<T>): Promise<T> {
|
|
return new Promise((resolve, reject) => {
|
|
request.onsuccess = () => resolve(request.result)
|
|
request.onerror = () => reject(request.error ?? new Error('IndexedDB request failed'))
|
|
})
|
|
}
|
|
|
|
export async function dbGet<T>(store: string, key: IDBValidKey): Promise<T | null> {
|
|
if (!hasIndexedDb()) return null
|
|
const db = await openDatabase()
|
|
try {
|
|
const tx = db.transaction(store, 'readonly')
|
|
const result = await requestToPromise<T | undefined>(tx.objectStore(store).get(key))
|
|
return result ?? null
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export async function dbPut<T>(store: string, key: IDBValidKey, value: T): Promise<void> {
|
|
if (!hasIndexedDb()) return
|
|
const db = await openDatabase()
|
|
try {
|
|
const tx = db.transaction(store, 'readwrite')
|
|
tx.objectStore(store).put(value, key)
|
|
await new Promise<void>((resolve, reject) => {
|
|
tx.oncomplete = () => resolve()
|
|
tx.onerror = () => reject(tx.error ?? new Error('IndexedDB transaction failed'))
|
|
})
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export async function dbDelete(store: string, key: IDBValidKey): Promise<void> {
|
|
if (!hasIndexedDb()) return
|
|
const db = await openDatabase()
|
|
try {
|
|
const tx = db.transaction(store, 'readwrite')
|
|
tx.objectStore(store).delete(key)
|
|
await new Promise<void>((resolve, reject) => {
|
|
tx.oncomplete = () => resolve()
|
|
tx.onerror = () => reject(tx.error ?? new Error('IndexedDB transaction failed'))
|
|
})
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|
|
|
|
export async function dbScan<T>(store: string): Promise<{ key: IDBValidKey; value: T }[]> {
|
|
if (!hasIndexedDb()) return []
|
|
const db = await openDatabase()
|
|
try {
|
|
const tx = db.transaction(store, 'readonly')
|
|
const s = tx.objectStore(store)
|
|
const [keys, values] = await Promise.all([
|
|
requestToPromise(s.getAllKeys()),
|
|
requestToPromise(s.getAll()),
|
|
])
|
|
return keys.map((key, i) => ({ key, value: values[i] as T }))
|
|
} finally {
|
|
db.close()
|
|
}
|
|
}
|