Files
modrinth/apps/frontend/src/services/moderation/checklist-storage.ts
T
chyz 5072c1d298 Moderation Changes Phase 2.-1 (#6833)
* 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
2026-08-07 23:40:11 +00:00

120 lines
3.5 KiB
TypeScript

import type { NodeState } from '@modrinth/moderation/src/types/node'
import { dbDelete, dbGet, dbPut, dbScan } from './db.ts'
export interface PersistedChecklistState {
savedAt: string
open?: boolean
reviewAnyway?: boolean
stage?: string
message?: string
state?: Record<string, Record<string, NodeState>>
activatedStages?: string[]
}
const STORE = 'checklist'
const CHECKLIST_STATE_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000
const CHECKLIST_CLEANUP_INTERVAL_MS = 24 * 60 * 60 * 1000
const saveChain = new Map<string, Promise<void>>()
let checklistCleanupPromise: Promise<void> | null = null
let checklistCleanupLastRunAt = 0
function isPersistedChecklistState(value: unknown): value is PersistedChecklistState {
if (!value || typeof value !== 'object') return false
const v = value as PersistedChecklistState
if (typeof v.savedAt !== 'string') return false
if (v.stage !== undefined && typeof v.stage !== 'string') return false
if (v.message !== undefined && typeof v.message !== 'string') return false
if (v.state !== undefined && typeof v.state !== 'object') return false
return true
}
function isStale(savedAt: string, now = Date.now()): boolean {
const time = Date.parse(savedAt)
return !Number.isNaN(time) && now - time > CHECKLIST_STATE_MAX_AGE_MS
}
async function cleanupStaleStates(now = Date.now()): Promise<void> {
const entries = await dbScan<unknown>(STORE)
const staleKeys = entries
.filter(({ value }) => {
if (!isPersistedChecklistState(value)) return false
return isStale(value.savedAt, now)
})
.map(({ key }) => key)
await Promise.all(staleKeys.map((key) => dbDelete(STORE, key)))
}
function scheduleStaleChecklistCleanup(): void {
if (!import.meta.client || checklistCleanupPromise) return
const now = Date.now()
if (now - checklistCleanupLastRunAt < CHECKLIST_CLEANUP_INTERVAL_MS) return
checklistCleanupLastRunAt = now
checklistCleanupPromise = cleanupStaleStates(now)
.catch((error) => {
console.debug('Failed to cleanup stale moderation checklist states from IndexedDB:', error)
})
.finally(() => {
checklistCleanupPromise = null
})
}
async function enqueueOp(projectId: string, op: () => Promise<void>): Promise<void> {
const result = (saveChain.get(projectId) ?? Promise.resolve()).then(op, op)
saveChain.set(
projectId,
result.then(
() => undefined,
() => undefined,
),
)
return result
}
export async function loadChecklistState(
projectId: string,
): Promise<PersistedChecklistState | null> {
if (!import.meta.client) return null
scheduleStaleChecklistCleanup()
try {
const raw = await dbGet<unknown>(STORE, projectId)
if (!isPersistedChecklistState(raw)) return null
if (isStale(raw.savedAt)) {
await clearChecklistState(projectId)
return null
}
return raw
} catch (error) {
console.debug('Failed to load checklist state from IndexedDB:', error)
return null
}
}
export async function saveChecklistState(
projectId: string,
state: Omit<PersistedChecklistState, 'savedAt'>,
): Promise<void> {
if (!import.meta.client) return
scheduleStaleChecklistCleanup()
const record: PersistedChecklistState = { ...state, savedAt: new Date().toISOString() }
try {
await enqueueOp(projectId, () => dbPut(STORE, projectId, record))
} catch (error) {
console.debug('Failed to save checklist state to IndexedDB:', error)
}
}
export async function clearChecklistState(projectId: string): Promise<void> {
if (!import.meta.client) return
try {
await enqueueOp(projectId, () => dbDelete(STORE, projectId))
} catch (error) {
console.debug('Failed to clear checklist state from IndexedDB:', error)
}
}