mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-28 00:32:42 +00:00
WIP Update
This commit is contained in:
@@ -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 <div className='dropdown'>
|
||||
{_.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 <div className='dropdown'>
|
||||
// {_.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 <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>;
|
||||
// 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 <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(){
|
||||
@@ -199,7 +204,7 @@ const Snippetbar = createClass({
|
||||
return <div className='editors'>
|
||||
<div className={`editorTool snippetGroup history ${this.state.historyExists ? 'active' : ''}`} >
|
||||
<i className='fas fa-clock-rotate-left' />
|
||||
{this.state.historyExists && this.renderHistoryItems() }
|
||||
{/* { this.renderHistoryItems() } */}
|
||||
</div>
|
||||
<div className={`editorTool undo ${this.props.historySize.undo ? 'active' : ''}`}
|
||||
onClick={this.props.undo} >
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user