mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-22 06:26:00 +00:00
openNewTab refactor
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import uuidv1 from 'uuid/v1';
|
||||
import localforage from 'localforage';
|
||||
|
||||
export class LoadingToken {
|
||||
constructor() {
|
||||
@@ -15,21 +13,6 @@ export function sleep(milliseconds) {
|
||||
return new Promise((resolve) => window.setTimeout(() => resolve(null), milliseconds));
|
||||
}
|
||||
|
||||
export async function openNewTab(setOpenedTabs, newTab, initialData = undefined) {
|
||||
const tabid = uuidv1();
|
||||
if (initialData) {
|
||||
await localforage.setItem(`tabdata_${tabid}`, initialData);
|
||||
}
|
||||
setOpenedTabs((files) => [
|
||||
...(files || []).map((x) => ({ ...x, selected: false })),
|
||||
{
|
||||
tabid,
|
||||
selected: true,
|
||||
...newTab,
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
export function changeTab(tabid, setOpenedTabs, changeFunc) {
|
||||
setOpenedTabs((files) => files.map((tab) => (tab.tabid == tabid ? changeFunc(tab) : tab)));
|
||||
}
|
||||
|
||||
28
packages/web/src/utility/useOpenNewTab.js
Normal file
28
packages/web/src/utility/useOpenNewTab.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import uuidv1 from 'uuid/v1';
|
||||
import React from 'react';
|
||||
import localforage from 'localforage';
|
||||
import { useSetOpenedTabs } from './globalState';
|
||||
|
||||
export default function useOpenNewTab() {
|
||||
const setOpenedTabs = useSetOpenedTabs();
|
||||
|
||||
const openNewTab = React.useCallback(
|
||||
async (newTab, initialData = undefined) => {
|
||||
const tabid = uuidv1();
|
||||
if (initialData) {
|
||||
await localforage.setItem(`tabdata_${tabid}`, initialData);
|
||||
}
|
||||
setOpenedTabs((files) => [
|
||||
...(files || []).map((x) => ({ ...x, selected: false })),
|
||||
{
|
||||
tabid,
|
||||
selected: true,
|
||||
...newTab,
|
||||
},
|
||||
]);
|
||||
},
|
||||
[setOpenedTabs]
|
||||
);
|
||||
|
||||
return openNewTab;
|
||||
}
|
||||
Reference in New Issue
Block a user