feat(app): sign-out warns before losing editor work, local copies purged
This commit is contained in:
1 parent
8b6adcb7df
commit
bd4ba338af
12 files changed
+417
-16
No files matched your search
@@ -12,7 +12,8 @@ export const COPY_PREFIX = 'osteo:sheet-draft:'
|
||||
export const MAX_OTHER_COPIES = 2
|
||||
|
||||
const DocumentObject = z.custom<Record<string, unknown>>(
|
||||
(value) => typeof value === 'object' && value !== null && !Array.isArray(value),
|
||||
(value) =>
|
||||
typeof value === 'object' && value !== null && !Array.isArray(value),
|
||||
)
|
||||
|
||||
const CopySchema = z.object({
|
||||
@@ -55,7 +56,11 @@ function storage(): Storage | null {
|
||||
}
|
||||
}
|
||||
|
||||
function parseCopy(raw: string, userId: string, versionId: string): RecoveryCopy | null {
|
||||
function parseCopy(
|
||||
raw: string,
|
||||
userId: string,
|
||||
versionId: string,
|
||||
): RecoveryCopy | null {
|
||||
try {
|
||||
const parsed = CopySchema.safeParse(JSON.parse(raw))
|
||||
const matches = parsed.success && parsed.data.user_id === userId
|
||||
@@ -90,7 +95,12 @@ function keysOf(store: Storage, prefix: string): string[] {
|
||||
return keys.sort()
|
||||
}
|
||||
|
||||
function infoOf(store: Storage, key: string, userId: string, versionId: string) {
|
||||
function infoOf(
|
||||
store: Storage,
|
||||
key: string,
|
||||
userId: string,
|
||||
versionId: string,
|
||||
) {
|
||||
const copy = parseCopy(store.getItem(key) ?? '', userId, versionId)
|
||||
return {
|
||||
versionId,
|
||||
@@ -101,7 +111,10 @@ function infoOf(store: Storage, key: string, userId: string, versionId: string)
|
||||
}
|
||||
|
||||
/** The account's copies of other versions; another account's keys never match the prefix. */
|
||||
export function listOtherCopies(userId: string, versionId: string): StoredCopyInfo[] {
|
||||
export function listOtherCopies(
|
||||
userId: string,
|
||||
versionId: string,
|
||||
): StoredCopyInfo[] {
|
||||
const store = storage()
|
||||
if (!store) return []
|
||||
const prefix = `${COPY_PREFIX}${userId}:`
|
||||
@@ -124,3 +137,27 @@ export function writeCopy(copy: RecoveryCopy): WriteResult {
|
||||
export function removeCopy(userId: string, versionId: string): void {
|
||||
writeOrRemove(copyKey(userId, versionId), null)
|
||||
}
|
||||
|
||||
/** Whether this tab holds at least one copy of the account's own. */
|
||||
export function hasUserCopies(userId: string): boolean {
|
||||
const store = storage()
|
||||
return store !== null && keysOf(store, `${COPY_PREFIX}${userId}:`).length > 0
|
||||
}
|
||||
|
||||
function removeKeys(keys: string[]): void {
|
||||
keys.forEach((key) => writeOrRemove(key, null))
|
||||
}
|
||||
|
||||
/** Voluntary sign-out: every copy of the account goes, other keys stay (plan R24). */
|
||||
export function removeUserCopies(userId: string): void {
|
||||
const store = storage()
|
||||
if (store) removeKeys(keysOf(store, `${COPY_PREFIX}${userId}:`))
|
||||
}
|
||||
|
||||
/** Another account signed in: the previous accounts' copies go, the account's own stay. */
|
||||
export function removeOtherUsersCopies(userId: string): void {
|
||||
const store = storage()
|
||||
if (!store) return
|
||||
const own = `${COPY_PREFIX}${userId}:`
|
||||
removeKeys(keysOf(store, COPY_PREFIX).filter((key) => !key.startsWith(own)))
|
||||
}
|
||||
Reference in new issue
Block a user