fix: isolate Today summary drafts by account
This commit is contained in:
1 parent
b3a359760c
commit
abc736d32e
2 files changed
+40
-5
No files matched your search
@@ -1,16 +1,27 @@
|
||||
function createTodaySummary({ storage = null, getLogin = () => '', share = null, copy = null } = {}) {
|
||||
let draft = null;
|
||||
let draftKey = '';
|
||||
|
||||
const storageKey = () => {
|
||||
const login = String(getLogin() || '').trim().toLowerCase();
|
||||
return login ? 'stackchain.today-summary-draft.v1.' + login : '';
|
||||
};
|
||||
const syncAccount = () => {
|
||||
const key = storageKey();
|
||||
if (key !== draftKey) {
|
||||
draft = null;
|
||||
draftKey = key;
|
||||
}
|
||||
};
|
||||
const copyRows = rows => rows.map(row => ({ ...row }));
|
||||
const snapshot = () => draft ? {
|
||||
...draft,
|
||||
worked:copyRows(draft.worked),
|
||||
tomorrow:copyRows(draft.tomorrow),
|
||||
} : null;
|
||||
const snapshot = () => {
|
||||
syncAccount();
|
||||
return draft ? {
|
||||
...draft,
|
||||
worked:copyRows(draft.worked),
|
||||
tomorrow:copyRows(draft.tomorrow),
|
||||
} : null;
|
||||
};
|
||||
const persist = () => {
|
||||
const key = storageKey();
|
||||
if (storage && key && draft) storage.setItem(key, JSON.stringify(draft));
|
||||
@@ -58,6 +69,7 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||
|
||||
return {
|
||||
begin(worked, tomorrow) {
|
||||
draftKey = storageKey();
|
||||
draft = {
|
||||
worked:(worked || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, true)),
|
||||
tomorrow:(tomorrow || []).filter(row => row?.identity).slice(0, 20).map(row => cleanRow(row, false)),
|
||||
@@ -69,10 +81,12 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||
},
|
||||
snapshot,
|
||||
restore() {
|
||||
draftKey = storageKey();
|
||||
draft = load();
|
||||
return Boolean(draft);
|
||||
},
|
||||
choose(section, identity, include) {
|
||||
syncAccount();
|
||||
const row = draft?.[section]?.find(candidate => candidate.identity === identity);
|
||||
if (!row || typeof include !== 'boolean') return false;
|
||||
row.include = include;
|
||||
@@ -80,12 +94,14 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||
return true;
|
||||
},
|
||||
includeActuals(include) {
|
||||
syncAccount();
|
||||
if (!draft || typeof include !== 'boolean') return false;
|
||||
draft.include_actuals = include;
|
||||
persist();
|
||||
return true;
|
||||
},
|
||||
setNote(note) {
|
||||
syncAccount();
|
||||
if (!draft) return false;
|
||||
draft.note = String(note || '').trim().slice(0, 1000);
|
||||
persist();
|
||||
@@ -93,6 +109,7 @@ function createTodaySummary({ storage = null, getLogin = () => '', share = null,
|
||||
},
|
||||
discard,
|
||||
text() {
|
||||
syncAccount();
|
||||
if (!draft) return '';
|
||||
const sections = [];
|
||||
const worked = selectedLines(draft.worked, draft.include_actuals);
|
||||
|
||||
Reference in new issue
Block a user