From c9241e30914d2ab0e8a4c1222ba9223a54006752 Mon Sep 17 00:00:00 2001 From: "G.Ambatte" Date: Mon, 7 Oct 2024 18:22:06 +1300 Subject: [PATCH] WIP Update --- .../homebrew/editor/snippetbar/snippetbar.jsx | 61 ++++++++++--------- client/homebrew/utils/versionHistory.js | 52 +++++++--------- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/client/homebrew/editor/snippetbar/snippetbar.jsx b/client/homebrew/editor/snippetbar/snippetbar.jsx index 12658cd41..3a7b48916 100644 --- a/client/homebrew/editor/snippetbar/snippetbar.jsx +++ b/client/homebrew/editor/snippetbar/snippetbar.jsx @@ -5,7 +5,7 @@ const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); -import { getHistoryItems, historyExists } from '../../utils/versionHistory.js'; +import { historyCheck, loadHistory } from '../../utils/versionHistory.js'; //Import all themes const ThemeSnippets = {}; @@ -50,30 +50,31 @@ const Snippetbar = createClass({ renderer : this.props.renderer, themeSelector : false, snippets : [], - historyExists : false + historyExists : false, + showHistory : false }; }, - componentDidMount : async function() { + componentDidMount : async function(prevState) { const snippets = this.compileSnippets(); this.setState({ snippets : snippets }); }, - componentDidUpdate : async function(prevProps) { + componentDidUpdate : async function(prevProps, prevState) { if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme || prevProps.snippetBundle != this.props.snippetBundle) { this.setState({ snippets : this.compileSnippets() }); }; - if(historyExists(this.props.brew) != this.state.historyExists){ + const checkHistoryExists = (await historyCheck(this.props.brew)) ?? false; + if(checkHistoryExists != prevState.historyExists){ this.setState({ - historyExists : !this.state.historyExists + historyExists : checkHistoryExists }); - }; - + } }, mergeCustomizer : function(oldValue, newValue, key) { @@ -151,30 +152,34 @@ const Snippetbar = createClass({ return this.props.updateBrew(item); }, - renderHistoryItems : function() { - const historyItems = getHistoryItems(this.props.brew); + renderHistoryItems : async function() { + if(!this.state.historyExists || !this.state.showHistory) return; - return
- {_.map(historyItems, (item, index)=>{ - if(!item.savedAt) return; + const historyItems = await loadHistory(this.props.brew); + console.log('renderHistoryItems', historyItems); + return; - const saveTime = new Date(item.savedAt); - const diffMs = new Date() - saveTime; - const diffSecs = Math.floor(diffMs / 1000); + // return
+ // {_.map(historyItems, (item, index)=>{ + // if(!item[1].savedAt) return; - let diffString = `about ${diffSecs} seconds ago`; + // const saveTime = new Date(item[1].savedAt); + // const diffMs = new Date() - saveTime; + // const diffSecs = Math.floor(diffMs / 1000); - if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; - if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; - if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; - if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; + // let diffString = `about ${diffSecs} seconds ago`; - return
{this.replaceContent(item);}} > - - v{item.version} : {diffString} -
; - })} -
; + // if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`; + // if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`; + // if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`; + // if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`; + + // return
{this.replaceContent(item);}} > + // + // v{item.version} : {diffString} + //
; + // })} + //
; }, renderEditorButtons : function(){ @@ -199,7 +204,7 @@ const Snippetbar = createClass({ return
- {this.state.historyExists && this.renderHistoryItems() } + {/* { this.renderHistoryItems() } */}
diff --git a/client/homebrew/utils/versionHistory.js b/client/homebrew/utils/versionHistory.js index b86dc66e0..b72ab6db6 100644 --- a/client/homebrew/utils/versionHistory.js +++ b/client/homebrew/utils/versionHistory.js @@ -21,20 +21,23 @@ const GARBAGE_COLLECT_DELAY = global.config?.historyData?.GARBAGE_COLLECT_DELAY function getKeyBySlot(brew, slot){ + // Return a string representing the key for this brew and history slot return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`; }; -function getVersionBySlot(brew, slot){ - // Read stored brew data - // - If it exists, parse data to object - // - If it doesn't exist, pass default object - const key = getKeyBySlot(brew, slot); - const storedVersion = IDB.get(key); - const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; - return output; -}; +// function getVersionBySlot(brew, slot){ +// // Read stored brew data +// // - If it exists, parse data to object +// // - If it doesn't exist, pass default object +// const key = getKeyBySlot(brew, slot); +// const storedVersion = IDB.get(key); +// const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; +// return output; +// }; function parseBrewForStorage(brew, slot = 0) { + // Strip out unneeded object properties + // Returns an array of [ key, brew ] const archiveBrew = { title : brew.title, text : brew.text, @@ -53,8 +56,8 @@ function parseBrewForStorage(brew, slot = 0) { } -export async function historyExists(brew){ - console.log('HISTORY CHECK'); +export async function historyCheck(brew){ + if(!IDB) return false; const historyExists = await IDB.keys() .then((keys)=>{ return [...keys].some((key)=>{ @@ -62,7 +65,8 @@ export async function historyExists(brew){ }); }) .catch(()=>{return false;}); - console.log('HISTORY STATE:', historyExists); + + return historyExists; } export async function loadHistory(brew){ @@ -70,12 +74,13 @@ export async function loadHistory(brew){ const historyKeys = []; - // Load data from local storage to History object + // Create array of all history keys for (let i = 1; i <= HISTORY_SLOTS; i++){ historyKeys.push(getKeyBySlot(brew, i)); }; const history = []; + // Load all keys from IDB at once await IDB.getMany(historyKeys) .then((dataArray)=>{ return dataArray.forEach((data)=>{ @@ -94,22 +99,20 @@ export async function loadHistory(brew){ export async function updateHistory(brew) { const history = await loadHistory(brew); - console.log('DATA:', history); - // Walk each version position for (let slot = HISTORY_SLOTS - 1; slot >= 0; slot--){ - console.log('SLOT #:', slot, history[slot]); const storedVersion = history[slot]; // If slot has expired, update all lower slots and break if(new Date() >= new Date(storedVersion.expireAt)){ + + // Create array of arrays : [ [key1, value1], [key2, value2], ..., [keyN, valueN] ] + // to pass to IDB.setMany const historyUpdate = []; - for (let updateSlot = slot - 1; updateSlot > 0; updateSlot--){ - console.log('CHECK DATA IN SLOT #:', updateSlot); + for (let updateSlot = slot; updateSlot > 0; updateSlot--){ // Move data from updateSlot to updateSlot + 1 if(!history[updateSlot - 1]?.noData) { - console.log('UPDATE SLOT #:', updateSlot - 1, '>', updateSlot); historyUpdate.push(parseBrewForStorage(history[updateSlot - 1], updateSlot + 1)); } }; @@ -117,7 +120,6 @@ export async function updateHistory(brew) { // Update the most recent brew historyUpdate.push(parseBrewForStorage(brew, 1)); - console.log('HISTORY UPDATE OBJECT:', historyUpdate); IDB.setMany(historyUpdate); // Break out of data checks because we found an expired value @@ -126,16 +128,6 @@ export async function updateHistory(brew) { }; }; -export function getHistoryItems(brew){ - const historyArray = []; - - for (let i = 1; i <= HISTORY_SLOTS; i++){ - historyArray.push(getVersionBySlot(brew, i)); - } - - return historyArray; -}; - export function versionHistoryGarbageCollection(){ console.log('Version History Garbage Collection'); // Object.keys(IDB)