0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-09 18:02:39 +00:00

WIP Update

This commit is contained in:
G.Ambatte
2024-10-07 18:22:06 +13:00
parent 77f162f7a4
commit c9241e3091
2 changed files with 55 additions and 58 deletions

View File

@@ -5,7 +5,7 @@ const createClass = require('create-react-class');
const _ = require('lodash'); const _ = require('lodash');
const cx = require('classnames'); const cx = require('classnames');
import { getHistoryItems, historyExists } from '../../utils/versionHistory.js'; import { historyCheck, loadHistory } from '../../utils/versionHistory.js';
//Import all themes //Import all themes
const ThemeSnippets = {}; const ThemeSnippets = {};
@@ -50,30 +50,31 @@ const Snippetbar = createClass({
renderer : this.props.renderer, renderer : this.props.renderer,
themeSelector : false, themeSelector : false,
snippets : [], snippets : [],
historyExists : false historyExists : false,
showHistory : false
}; };
}, },
componentDidMount : async function() { componentDidMount : async function(prevState) {
const snippets = this.compileSnippets(); const snippets = this.compileSnippets();
this.setState({ this.setState({
snippets : snippets 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) { if(prevProps.renderer != this.props.renderer || prevProps.theme != this.props.theme || prevProps.snippetBundle != this.props.snippetBundle) {
this.setState({ this.setState({
snippets : this.compileSnippets() 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({ this.setState({
historyExists : !this.state.historyExists historyExists : checkHistoryExists
}); });
}; }
}, },
mergeCustomizer : function(oldValue, newValue, key) { mergeCustomizer : function(oldValue, newValue, key) {
@@ -151,30 +152,34 @@ const Snippetbar = createClass({
return this.props.updateBrew(item); return this.props.updateBrew(item);
}, },
renderHistoryItems : function() { renderHistoryItems : async function() {
const historyItems = getHistoryItems(this.props.brew); if(!this.state.historyExists || !this.state.showHistory) return;
return <div className='dropdown'> const historyItems = await loadHistory(this.props.brew);
{_.map(historyItems, (item, index)=>{ console.log('renderHistoryItems', historyItems);
if(!item.savedAt) return; return;
const saveTime = new Date(item.savedAt); // return <div className='dropdown'>
const diffMs = new Date() - saveTime; // {_.map(historyItems, (item, index)=>{
const diffSecs = Math.floor(diffMs / 1000); // 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`; // let diffString = `about ${diffSecs} seconds 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 <div className='snippet' key={index} onClick={()=>{this.replaceContent(item);}} > // if(diffSecs > 60) diffString = `about ${Math.floor(diffSecs / 60)} minutes ago`;
<i className={`fas fa-${index+1}`} /> // if(diffSecs > (60 * 60)) diffString = `about ${Math.floor(diffSecs / (60 * 60))} hours ago`;
<span className='name' title={saveTime.toISOString()}>v{item.version} : {diffString}</span> // if(diffSecs > (24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (24 * 60 * 60))} days ago`;
</div>; // if(diffSecs > (7 * 24 * 60 * 60)) diffString = `about ${Math.floor(diffSecs / (7 * 24 * 60 * 60))} weeks ago`;
})}
</div>; // return <div className='snippet' key={index} onClick={()=>{this.replaceContent(item);}} >
// <i className={`fas fa-${index+1}`} />
// <span className='name' title={saveTime.toISOString()}>v{item.version} : {diffString}</span>
// </div>;
// })}
// </div>;
}, },
renderEditorButtons : function(){ renderEditorButtons : function(){
@@ -199,7 +204,7 @@ const Snippetbar = createClass({
return <div className='editors'> return <div className='editors'>
<div className={`editorTool snippetGroup history ${this.state.historyExists ? 'active' : ''}`} > <div className={`editorTool snippetGroup history ${this.state.historyExists ? 'active' : ''}`} >
<i className='fas fa-clock-rotate-left' /> <i className='fas fa-clock-rotate-left' />
{this.state.historyExists && this.renderHistoryItems() } {/* { this.renderHistoryItems() } */}
</div> </div>
<div className={`editorTool undo ${this.props.historySize.undo ? 'active' : ''}`} <div className={`editorTool undo ${this.props.historySize.undo ? 'active' : ''}`}
onClick={this.props.undo} > onClick={this.props.undo} >

View File

@@ -21,20 +21,23 @@ const GARBAGE_COLLECT_DELAY = global.config?.historyData?.GARBAGE_COLLECT_DELAY
function getKeyBySlot(brew, slot){ function getKeyBySlot(brew, slot){
// Return a string representing the key for this brew and history slot
return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`; return `${HISTORY_PREFIX}-${brew.shareId}-${slot}`;
}; };
function getVersionBySlot(brew, slot){ // function getVersionBySlot(brew, slot){
// Read stored brew data // // Read stored brew data
// - If it exists, parse data to object // // - If it exists, parse data to object
// - If it doesn't exist, pass default object // // - If it doesn't exist, pass default object
const key = getKeyBySlot(brew, slot); // const key = getKeyBySlot(brew, slot);
const storedVersion = IDB.get(key); // const storedVersion = IDB.get(key);
const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true }; // const output = storedVersion ? storedVersion : { expireAt: '2000-01-01T00:00:00.000Z', shareId: brew.shareId, noData: true };
return output; // return output;
}; // };
function parseBrewForStorage(brew, slot = 0) { function parseBrewForStorage(brew, slot = 0) {
// Strip out unneeded object properties
// Returns an array of [ key, brew ]
const archiveBrew = { const archiveBrew = {
title : brew.title, title : brew.title,
text : brew.text, text : brew.text,
@@ -53,8 +56,8 @@ function parseBrewForStorage(brew, slot = 0) {
} }
export async function historyExists(brew){ export async function historyCheck(brew){
console.log('HISTORY CHECK'); if(!IDB) return false;
const historyExists = await IDB.keys() const historyExists = await IDB.keys()
.then((keys)=>{ .then((keys)=>{
return [...keys].some((key)=>{ return [...keys].some((key)=>{
@@ -62,7 +65,8 @@ export async function historyExists(brew){
}); });
}) })
.catch(()=>{return false;}); .catch(()=>{return false;});
console.log('HISTORY STATE:', historyExists);
return historyExists;
} }
export async function loadHistory(brew){ export async function loadHistory(brew){
@@ -70,12 +74,13 @@ export async function loadHistory(brew){
const historyKeys = []; const historyKeys = [];
// Load data from local storage to History object // Create array of all history keys
for (let i = 1; i <= HISTORY_SLOTS; i++){ for (let i = 1; i <= HISTORY_SLOTS; i++){
historyKeys.push(getKeyBySlot(brew, i)); historyKeys.push(getKeyBySlot(brew, i));
}; };
const history = []; const history = [];
// Load all keys from IDB at once
await IDB.getMany(historyKeys) await IDB.getMany(historyKeys)
.then((dataArray)=>{ .then((dataArray)=>{
return dataArray.forEach((data)=>{ return dataArray.forEach((data)=>{
@@ -94,22 +99,20 @@ export async function loadHistory(brew){
export async function updateHistory(brew) { export async function updateHistory(brew) {
const history = await loadHistory(brew); const history = await loadHistory(brew);
console.log('DATA:', history);
// Walk each version position // Walk each version position
for (let slot = HISTORY_SLOTS - 1; slot >= 0; slot--){ for (let slot = HISTORY_SLOTS - 1; slot >= 0; slot--){
console.log('SLOT #:', slot, history[slot]);
const storedVersion = history[slot]; const storedVersion = history[slot];
// If slot has expired, update all lower slots and break // If slot has expired, update all lower slots and break
if(new Date() >= new Date(storedVersion.expireAt)){ if(new Date() >= new Date(storedVersion.expireAt)){
// Create array of arrays : [ [key1, value1], [key2, value2], ..., [keyN, valueN] ]
// to pass to IDB.setMany
const historyUpdate = []; const historyUpdate = [];
for (let updateSlot = slot - 1; updateSlot > 0; updateSlot--){ for (let updateSlot = slot; updateSlot > 0; updateSlot--){
console.log('CHECK DATA IN SLOT #:', updateSlot);
// Move data from updateSlot to updateSlot + 1 // Move data from updateSlot to updateSlot + 1
if(!history[updateSlot - 1]?.noData) { if(!history[updateSlot - 1]?.noData) {
console.log('UPDATE SLOT #:', updateSlot - 1, '>', updateSlot);
historyUpdate.push(parseBrewForStorage(history[updateSlot - 1], updateSlot + 1)); historyUpdate.push(parseBrewForStorage(history[updateSlot - 1], updateSlot + 1));
} }
}; };
@@ -117,7 +120,6 @@ export async function updateHistory(brew) {
// Update the most recent brew // Update the most recent brew
historyUpdate.push(parseBrewForStorage(brew, 1)); historyUpdate.push(parseBrewForStorage(brew, 1));
console.log('HISTORY UPDATE OBJECT:', historyUpdate);
IDB.setMany(historyUpdate); IDB.setMany(historyUpdate);
// Break out of data checks because we found an expired value // 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(){ export function versionHistoryGarbageCollection(){
console.log('Version History Garbage Collection'); console.log('Version History Garbage Collection');
// Object.keys(IDB) // Object.keys(IDB)