pleroma-fe/src/modules/drafts.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

import { storage } from 'src/lib/storage.js'
2023-03-10 11:20:06 -05:00
export const defaultState = {
2026-01-06 16:22:52 +02:00
drafts: {},
2023-03-10 11:20:06 -05:00
}
export const mutations = {
2026-01-06 16:22:52 +02:00
addOrSaveDraft(state, { draft }) {
2023-03-10 11:20:06 -05:00
state.drafts[draft.id] = draft
2023-03-10 12:39:08 -05:00
},
2026-01-06 16:22:52 +02:00
abandonDraft(state, { id }) {
2023-03-10 12:39:08 -05:00
delete state.drafts[id]
},
2026-01-06 16:22:52 +02:00
loadDrafts(state, data) {
state.drafts = data
2026-01-06 16:22:52 +02:00
},
2023-03-10 11:20:06 -05:00
}
const storageKey = 'pleroma-fe-drafts'
/*
* Note: we do not use the persist state plugin because
* it is not impossible for a user to have two windows at
* the same time. The persist state plugin is just overriding
* everything with the current state. This isn't good because
* if a draft is created in one window and another draft is
* created in another, the draft in the first window will just
* be overriden.
* Here, we can't guarantee 100% atomicity unless one uses
* different keys, which will just pollute the whole storage.
* It is indeed best to have backend support for this.
*/
2026-01-06 17:32:22 +02:00
const getStorageData = async () =>
(await storage.getItem(storageKey)) ||
{
/* no-op */
}
const saveDraftToStorage = async (draft) => {
const currentData = await getStorageData()
currentData[draft.id] = JSON.parse(JSON.stringify(draft))
await storage.setItem(storageKey, currentData)
}
const deleteDraftFromStorage = async (id) => {
const currentData = await getStorageData()
delete currentData[id]
await storage.setItem(storageKey, currentData)
}
2023-03-10 11:20:06 -05:00
export const actions = {
2026-01-06 16:22:52 +02:00
async addOrSaveDraft(store, { draft }) {
const id = draft.id || new Date().getTime().toString()
const draftWithId = { ...draft, id }
store.commit('addOrSaveDraft', { draft: draftWithId })
await saveDraftToStorage(draftWithId)
2023-03-10 11:20:06 -05:00
return id
2023-03-10 12:39:08 -05:00
},
2026-01-06 16:22:52 +02:00
async abandonDraft(store, { id }) {
2023-03-10 12:39:08 -05:00
store.commit('abandonDraft', { id })
await deleteDraftFromStorage(id)
},
2026-01-06 16:22:52 +02:00
async loadDrafts(store) {
const currentData = await getStorageData()
store.commit('loadDrafts', currentData)
2026-01-06 16:22:52 +02:00
},
2023-03-10 11:20:06 -05:00
}
export const getters = {
2026-01-06 16:22:52 +02:00
draftsByTypeAndRefId(state) {
2023-03-10 11:20:06 -05:00
return (type, refId) => {
2026-01-06 16:22:52 +02:00
return Object.values(state.drafts).filter(
(draft) => draft.type === type && draft.refId === refId,
)
2023-03-10 11:20:06 -05:00
}
2023-03-10 12:10:39 -05:00
},
2026-01-06 16:22:52 +02:00
draftsArray(state) {
2023-03-10 12:10:39 -05:00
return Object.values(state.drafts)
2023-03-10 20:22:41 -05:00
},
2026-01-06 16:22:52 +02:00
draftCount(state) {
2023-03-10 20:22:41 -05:00
return Object.values(state.drafts).length
2026-01-06 16:22:52 +02:00
},
2023-03-10 11:20:06 -05:00
}
const drafts = {
state: defaultState,
mutations,
getters,
2026-01-06 16:22:52 +02:00
actions,
2023-03-10 11:20:06 -05:00
}
export default drafts