local storage garbage collector + delete older tabs

This commit is contained in:
Jan Prochazka 2020-10-10 18:25:14 +02:00
parent a07d99c731
commit 74d3407048
3 changed files with 45 additions and 1 deletions

View File

@ -2,6 +2,22 @@ import React from 'react';
import _ from 'lodash';
import moment from 'moment';
import { getIconImage } from '../icons';
import { DropDownMenuItem } from '../modals/DropDownMenu';
function Menu({ data, setOpenedTabs }) {
const handleDelete = () => {
setOpenedTabs((tabs) => tabs.filter((x) => x.tabid != data.tabid));
};
const handleDeleteOlder = () => {
setOpenedTabs((tabs) => tabs.filter((x) => !x.closedTime || x.closedTime >= data.closedTime));
};
return (
<>
<DropDownMenuItem onClick={handleDelete}>Delete</DropDownMenuItem>
<DropDownMenuItem onClick={handleDeleteOlder}>Delete older</DropDownMenuItem>
</>
);
}
const closedTabAppObject = () => ({ tabid, props, selected, icon, title, closedTime, busy }, { setOpenedTabs }) => {
const key = tabid;
@ -18,7 +34,7 @@ const closedTabAppObject = () => ({ tabid, props, selected, icon, title, closedT
);
};
return { title: `${title} ${moment(closedTime).fromNow()}`, key, Icon, isBold, onClick, isBusy: busy };
return { title: `${title} ${moment(closedTime).fromNow()}`, key, Icon, isBold, onClick, isBusy: busy, Menu };
};
export default closedTabAppObject;

View File

@ -13,10 +13,13 @@ import 'ace-builds/src-noconflict/mode-javascript';
import 'ace-builds/src-noconflict/theme-github';
import 'ace-builds/src-noconflict/ext-searchbox';
import 'ace-builds/src-noconflict/ext-language_tools';
import localStorageGarbageCollector from './utility/localStorageGarbageCollector';
// import 'ace-builds/src-noconflict/snippets/sqlserver';
// import 'ace-builds/src-noconflict/snippets/pgsql';
// import 'ace-builds/src-noconflict/snippets/mysql';
localStorageGarbageCollector();
ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change

View File

@ -0,0 +1,25 @@
import moment from 'moment';
export default function localStorageGarbageCollector() {
const openedTabsJson = localStorage.getItem('openedTabs');
const savedSqlFilesJson = localStorage.getItem('savedSqlFiles');
let openedTabs = openedTabsJson ? JSON.parse(openedTabsJson) : [];
const closeLimit = moment().add(-7, 'day').valueOf();
openedTabs = openedTabs.filter((x) => !x.closedTime || x.closedTime > closeLimit);
localStorage.setItem('openedTabs', JSON.stringify(openedTabs));
const savedSqlFiles = savedSqlFilesJson ? JSON.parse(savedSqlFilesJson) : [];
const toRemove = [];
for (const key in localStorage) {
if (!key.startsWith('tabdata_')) continue;
if (savedSqlFiles.find((x) => x.storageKey == key)) continue;
if (openedTabs.find((x) => key.endsWith('_' + x.tabid))) continue;
toRemove.push(key);
}
for (const key of toRemove) {
localStorage.removeItem(key);
}
}