1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-19 13:27:58 +00:00
rolens/frontend/src/app.svelte

141 lines
3.9 KiB
Svelte
Raw Normal View History

2023-01-10 16:28:27 +00:00
<script>
import { onMount } from 'svelte';
2023-01-14 20:09:21 +00:00
import { DropCollection, DropDatabase, Hosts, OpenCollection, OpenConnection, OpenDatabase } from '../wailsjs/go/app/App';
2023-01-10 16:28:27 +00:00
import AddressBar from './organisms/addressbar/index.svelte';
import Grid from './components/grid.svelte';
import CollectionDetail from './organisms/collection-detail/index.svelte';
2023-01-14 19:38:39 +00:00
import { busy, contextMenu } from './stores';
import ContextMenu from './components/contextmenu.svelte';
2023-01-10 16:28:27 +00:00
const connections = {};
let hosts = {};
let activeHostKey = '';
let activeDbKey = '';
let activeCollKey = '';
2023-01-11 19:41:15 +00:00
let addressBarModalOpen = true;
2023-01-10 16:28:27 +00:00
$: host = hosts[activeHostKey];
$: connection = connections[activeHostKey];
$: database = connection?.databases[activeDbKey];
$: collection = database?.collections?.[activeCollKey];
async function openConnection(hostKey) {
2023-01-14 20:09:21 +00:00
busy.start();
2023-01-10 16:28:27 +00:00
const databases = await OpenConnection(hostKey);
if (databases) {
connections[hostKey] = { databases: {} };
databases.forEach(dbKey => {
connections[hostKey].databases[dbKey] = { collections: {} };
});
activeHostKey = hostKey;
addressBarModalOpen = false;
window.runtime.WindowSetTitle(`${host.name} - Mongodup`);
}
2023-01-14 20:09:21 +00:00
busy.end();
2023-01-10 16:28:27 +00:00
}
async function openDatabase(dbKey) {
2023-01-14 20:09:21 +00:00
busy.start();
2023-01-10 16:28:27 +00:00
const collections = await OpenDatabase(activeHostKey, dbKey);
for (const collKey of collections || []) {
connections[activeHostKey].databases[dbKey].collections[collKey] = {};
}
2023-01-14 20:09:21 +00:00
busy.end();
2023-01-10 16:28:27 +00:00
}
2023-01-14 19:38:39 +00:00
async function dropDatabase(dbKey) {
2023-01-14 20:09:21 +00:00
busy.start();
2023-01-14 19:38:39 +00:00
await DropDatabase(activeHostKey, dbKey);
await openConnection(activeHostKey);
2023-01-14 20:09:21 +00:00
busy.end();
2023-01-14 19:38:39 +00:00
}
2023-01-10 16:28:27 +00:00
async function openCollection(collKey) {
2023-01-14 20:09:21 +00:00
busy.start();
2023-01-10 16:28:27 +00:00
const stats = await OpenCollection(activeHostKey, activeDbKey, collKey);
connections[activeHostKey].databases[activeDbKey].collections[collKey].stats = stats;
2023-01-14 20:09:21 +00:00
busy.end();
}
async function dropCollection(dbKey, collKey) {
busy.start();
await DropCollection(activeHostKey, dbKey, collKey);
await openConnection(activeHostKey);
2023-01-14 20:11:35 +00:00
await openDatabase(dbKey);
2023-01-14 20:09:21 +00:00
busy.end();
2023-01-10 16:28:27 +00:00
}
2023-01-14 19:38:39 +00:00
async function reload() {
activeHostKey && await openConnection(activeHostKey);
activeDbKey && await openDatabase(activeDbKey);
}
2023-01-10 16:28:27 +00:00
onMount(() => {
Hosts().then(h => hosts = h);
});
</script>
<main>
<AddressBar {hosts} bind:activeHostKey on:select={e => openConnection(e.detail)} bind:modalOpen={addressBarModalOpen} />
2023-01-11 19:41:15 +00:00
{#if host && connection}
2023-01-14 19:38:39 +00:00
<div class="databaselist">
2023-01-11 19:41:15 +00:00
<Grid
columns={[ { key: 'id' }, { key: 'collCount', right: true } ]}
2023-01-14 20:09:21 +00:00
items={Object.keys(connection.databases).map(dbKey => ({
id: dbKey,
collCount: Object.keys(connection.databases[dbKey].collections || {}).length,
children: Object.keys(connection.databases[dbKey].collections).map(collKey => ({
id: collKey,
menu: [ { label: `Drop ${collKey}`, fn: () => dropCollection(dbKey, collKey) } ],
})) || [],
menu: [ { label: `Drop ${dbKey}`, fn: () => dropDatabase(dbKey) } ],
2023-01-11 19:41:15 +00:00
}))}
2023-01-14 19:38:39 +00:00
actions={[
{ icon: 'reload', fn: reload },
{ icon: '+' },
{ icon: '-' },
]}
2023-01-11 19:41:15 +00:00
bind:activeKey={activeDbKey}
bind:activeChildKey={activeCollKey}
on:select={e => openDatabase(e.detail)}
on:selectChild={e => openCollection(e.detail)}
/>
</div>
<div class="collection">
<CollectionDetail
{collection}
hostKey={activeHostKey}
dbKey={activeDbKey}
collectionKey={activeCollKey}
/>
</div>
{/if}
2023-01-10 16:28:27 +00:00
</main>
2023-01-14 19:38:39 +00:00
{#key $contextMenu}
<ContextMenu {...$contextMenu} on:close={contextMenu.hide} />
{/key}
2023-01-10 16:28:27 +00:00
<style>
main {
height: 100vh;
2023-01-11 19:41:15 +00:00
display: grid;
grid-template: 3rem auto / 250px 1fr;
gap: 0.5rem;
padding: 0.5rem;
2023-01-10 16:28:27 +00:00
}
2023-01-11 19:41:15 +00:00
main > :global(.addressbar) {
grid-column: 1 / 3;
2023-01-10 16:28:27 +00:00
}
2023-01-14 19:38:39 +00:00
.databaselist {
2023-01-10 19:10:39 +00:00
overflow: scroll;
2023-01-10 16:28:27 +00:00
}
</style>