From 5476df5fe98d5ee6ef82cbd0b8e9d424ab55f7f6 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Sat, 8 Jul 2023 13:07:55 +0200 Subject: [PATCH] Preserve state between tabs (cc #37) (fixes #56) --- frontend/src/lib/stores/hosttree.js | 1 + .../connection/collection/aggregate.svelte | 4 +- .../connection/collection/find.svelte | 6 +- .../connection/collection/index.svelte | 69 ++++++++----------- .../connection/collection/indexes.svelte | 8 ++- .../connection/collection/insert.svelte | 4 +- .../connection/collection/remove.svelte | 4 +- .../connection/database/index.svelte | 35 +++++----- .../organisms/connection/host/index.svelte | 39 +++++------ .../src/organisms/connection/host/logs.svelte | 7 ++ .../src/organisms/connection/index.svelte | 35 +++++----- .../src/organisms/connection/shell.svelte | 4 ++ 12 files changed, 106 insertions(+), 110 deletions(-) diff --git a/frontend/src/lib/stores/hosttree.js b/frontend/src/lib/stores/hosttree.js index b54a608..41c0e4b 100644 --- a/frontend/src/lib/stores/hosttree.js +++ b/frontend/src/lib/stores/hosttree.js @@ -95,6 +95,7 @@ async function refresh() { collection.key = collKey; collection.dbKey = dbKey; collection.hostKey = hostKey; + collection.viewKey = 'list'; collection.indexes = collection.indexes || []; delete collection.new; diff --git a/frontend/src/organisms/connection/collection/aggregate.svelte b/frontend/src/organisms/connection/collection/aggregate.svelte index 8a9d8b2..ece20f6 100644 --- a/frontend/src/organisms/connection/collection/aggregate.svelte +++ b/frontend/src/organisms/connection/collection/aggregate.svelte @@ -68,7 +68,8 @@ diff --git a/frontend/src/organisms/connection/collection/find.svelte b/frontend/src/organisms/connection/collection/find.svelte index 732637b..e0e4136 100644 --- a/frontend/src/organisms/connection/collection/find.svelte +++ b/frontend/src/organisms/connection/collection/find.svelte @@ -11,9 +11,9 @@ import { convertLooseJson } from '$lib/strings'; import { FindItems, RemoveItemById, UpdateFoundDocument } from '$wails/go/app/App'; import { EJSON } from 'bson'; - import { onMount } from 'svelte'; export let collection; + export let visible = false; const defaults = { query: '{}', @@ -42,7 +42,7 @@ } async function submitQuery() { - if (querying) { + if (querying || !visible) { return; } @@ -159,7 +159,7 @@ } $: collection && refresh(); - onMount(refresh); + $: visible && refresh();
diff --git a/frontend/src/organisms/connection/collection/index.svelte b/frontend/src/organisms/connection/collection/index.svelte index 6ab9c37..e0018ec 100644 --- a/frontend/src/organisms/connection/collection/index.svelte +++ b/frontend/src/organisms/connection/collection/index.svelte @@ -1,7 +1,6 @@
{#if collection} - {#key collection} - + -
- {#if tab === 'stats'} - {:else if tab === 'find'} - {:else if tab === 'insert'} - {:else if tab === 'update'} - {:else if tab === 'remove'} - {:else if tab === 'indexes'} - {:else if tab === 'aggregate'} - {:else if tab === 'shell'} - {/if} + {#each Object.values(tabs) as view} +
+
- {/key} + {/each} {:else} {/if} @@ -92,6 +74,9 @@ min-height: 0; min-width: 0; } + .container.hidden { + display: none; + } .container > :global(*) { width: 100%; } diff --git a/frontend/src/organisms/connection/collection/indexes.svelte b/frontend/src/organisms/connection/collection/indexes.svelte index cc29229..49ed505 100644 --- a/frontend/src/organisms/connection/collection/indexes.svelte +++ b/frontend/src/organisms/connection/collection/indexes.svelte @@ -1,9 +1,9 @@
diff --git a/frontend/src/organisms/connection/collection/insert.svelte b/frontend/src/organisms/connection/collection/insert.svelte index 669c044..d32b0c8 100644 --- a/frontend/src/organisms/connection/collection/insert.svelte +++ b/frontend/src/organisms/connection/collection/insert.svelte @@ -14,6 +14,7 @@ import Form from './components/form.svelte'; export let collection; + export let visible = false; const dispatch = createEventDispatcher(); const formValidity = {}; @@ -98,6 +99,8 @@ views.openConfig(collection); } + $: visible && editor.focus(); + onMount(() => { if (collection.viewKey === 'list') { editor.dispatch({ @@ -110,7 +113,6 @@ anchor: 3, }, }); - editor.focus(); } }); diff --git a/frontend/src/organisms/connection/collection/remove.svelte b/frontend/src/organisms/connection/collection/remove.svelte index d7bb300..06703d5 100644 --- a/frontend/src/organisms/connection/collection/remove.svelte +++ b/frontend/src/organisms/connection/collection/remove.svelte @@ -6,6 +6,7 @@ import { onMount } from 'svelte'; export let collection; + export let visible = false; let json = ''; let many = true; @@ -23,6 +24,8 @@ ); } + $: visible && editor.focus(); + onMount(() => { editor.dispatch({ changes: { @@ -34,7 +37,6 @@ anchor: 3, }, }); - editor.focus(); }); diff --git a/frontend/src/organisms/connection/database/index.svelte b/frontend/src/organisms/connection/database/index.svelte index b3d37eb..2ee1327 100644 --- a/frontend/src/organisms/connection/database/index.svelte +++ b/frontend/src/organisms/connection/database/index.svelte @@ -7,17 +7,15 @@ import Stats from './stats.svelte'; export let database; - export let hostKey; - export let dbKey; export let tab = 'stats'; - $: if (database) { - database.hostKey = hostKey; - database.dbKey = dbKey; - } + const tabs = { + 'stats': { icon: 'chart', title: 'Database stats', component: Stats }, + 'shell': { icon: 'shell', title: 'Shell', component: Shell }, + }; - $: if (hostKey || dbKey) { - tab = 'stats'; + for (const key of Object.keys(tabs)) { + tabs[key].key = key; } EventsOn('OpenStatsTab', name => (tab = name || tab)); @@ -26,17 +24,13 @@
{#if database} {#key database} - -
- {#if tab === 'stats'} - {:else if tab === 'shell'} - {/if} -
+ + + {#each Object.values(tabs) as view} +
+ +
+ {/each} {/key} {:else} @@ -61,6 +55,9 @@ min-height: 0; min-width: 0; } + .container.hidden { + display: none; + } .container > :global(*) { width: 100%; } diff --git a/frontend/src/organisms/connection/host/index.svelte b/frontend/src/organisms/connection/host/index.svelte index 3da1b4a..cdb57e5 100644 --- a/frontend/src/organisms/connection/host/index.svelte +++ b/frontend/src/organisms/connection/host/index.svelte @@ -9,15 +9,17 @@ import SystemInfo from './systeminfo.svelte'; export let host; - export let hostKey; export let tab = 'status'; - $: if (host) { - host.hostKey = hostKey; - } + const tabs = { + 'status': { icon: 'chart', title: 'Host status', component: Status }, + 'shell': { icon: 'shell', title: 'Shell', component: Shell }, + 'logs': { icon: 'doc', title: 'Logs', component: Logs }, + 'systemInfo': { icon: 'server', title: 'System info', component: SystemInfo }, + }; - $: if (hostKey) { - tab = 'status'; + for (const key of Object.keys(tabs)) { + tabs[key].key = key; } EventsOn('OpenStatusTab', name => (tab = name || tab)); @@ -26,23 +28,13 @@
{#if host} {#key host} - + -
- {#if tab === 'status'} - {:else if tab === 'logs'} - {:else if tab === 'systemInfo'} - {:else if tab === 'shell'} - {/if} -
+ {#each Object.values(tabs) as view} +
+ +
+ {/each} {/key} {:else} @@ -67,6 +59,9 @@ min-height: 0; min-width: 0; } + .container.hidden { + display: none; + } .container > :global(*) { width: 100%; } diff --git a/frontend/src/organisms/connection/host/logs.svelte b/frontend/src/organisms/connection/host/logs.svelte index 6b105e1..ea61acf 100644 --- a/frontend/src/organisms/connection/host/logs.svelte +++ b/frontend/src/organisms/connection/host/logs.svelte @@ -8,6 +8,7 @@ import { onDestroy } from 'svelte'; export let host; + export let visible = false; const autoReloadIntervals = [ 1, 2, 5, 10, 30, 60 ]; let filter = 'global'; @@ -31,6 +32,10 @@ } async function refresh() { + if (!visible) { + return; + } + let _logs = []; ({ logs: _logs, total, error } = await host.getLogs(filter)); logs = []; @@ -64,6 +69,8 @@ setTimeout(() => copySucceeded = false, 1500); } + $: visible && !logs && refresh(); + onDestroy(() => { if (interval) { clearInterval(interval); diff --git a/frontend/src/organisms/connection/index.svelte b/frontend/src/organisms/connection/index.svelte index a730686..277d5cd 100644 --- a/frontend/src/organisms/connection/index.svelte +++ b/frontend/src/organisms/connection/index.svelte @@ -9,10 +9,18 @@ import HostTree from './hosttree.svelte'; let path = []; + let prevPath = ''; let hostTab = ''; let dbTab = ''; let collTab = ''; + $: if (path.join('.') !== prevPath) { + hostTab = 'status'; + dbTab = 'stats'; + collTab = 'stats'; + prevPath = path.join('.'); + } + $: activeHostKey = path[0]; $: activeDbKey = path[1]; $: activeCollKey = path[2]; @@ -55,26 +63,17 @@
{#if activeCollKey} - + {#key activeCollKey} + + {/key} {:else if activeDbKey} - + {#key activeDbKey} + + {/key} {:else if activeHostKey} - + {#key activeHostKey} + + {/key} {/if}