Skip to content

Commit

Permalink
refacto stats layout
Browse files Browse the repository at this point in the history
  • Loading branch information
zapaz committed May 1, 2024
1 parent f95631e commit badd002
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 109 deletions.
61 changes: 36 additions & 25 deletions svelte/src/components/Global/Navigation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,48 @@

<nav>
<ul>
<li class="active">
<p>
<a href="." on:click|preventDefault={toggle}>
<i class="fas {onMainNet ? 'fa-flask' : 'fa-road'}" /><br />
{@html onMainNet ? "testnet" : "mainnet"}<br />
networks
</a>
</p>
</li>
{#if !onMainNet}
<li class="active">
<ConfigModal />
</li>
{/if}
{#if !onProd}
{#if chainId > 0}
<li class="active">
<p>
<a href={config.base}>
<i class="fas fa-columns" /><br />
stable<br />
version
<a href="." on:click|preventDefault={toggle}>
<i class="fas {onMainNet ? 'fa-flask' : 'fa-road'}" /><br />
{@html onMainNet ? "testnet" : "mainnet"}<br />
networks
</a>
</p>
</li>
<!-- experimental -->
{#if !onProd}
<!-- only on testnets -->
{#if !onMainNet}
<li class="active">
<ConfigModal />
</li>
{/if}
<li class="active">
<p>
<a href={config.base}>
<i class="fas fa-columns" /><br />
stable<br />
version
</a>
</p>
</li>
<li class="active">
<a href="/stats">
<i class="fas fa-database"></i><br />
stats
</a>
</li>
{/if}
{:else}
<li class="active">
<a href="/">
<i class="fas fa-home"></i><br />
home
</a>
</li>
{/if}
<li class="active">
<a href="/stats">
<i class="fas fa-database"></i><br />
stats
</a>
</li>
</ul>
</nav>
</div>
Expand Down
89 changes: 89 additions & 0 deletions sveltekit/src/lib/components/Stats.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<script lang="ts">
import HomeLayout from "@kredeum/svelte/src/components/Global/HomeLayout.svelte";
import Navigation from "@kredeum/svelte/src/components/Global/Navigation.svelte";
import StatsNetworks from "./StatsNetworks.svelte";
import type { NetworkType } from "@kredeum/common/src/common/types";
import { networks } from "@kredeum/common/src/common/networks";
// Manage StatsNetworks Svelte components in Tabs with 2 states:
// - active (i.e. displayed) or not (only one at a time)
// - mounted or not (mounted one time, can't be unmounted)
// To avoid re-calling onchain data each time tab is reactivated
// To avoid calling onchain data on a tab never activated
type TabsMounted = { [key: string]: boolean };
let tabsMounted: TabsMounted = {
Mainnets: true,
OPnets: false,
Testnets: false,
Inactives: false
};
let tabActive = "Mainnets";
const getNetworks = (tab: string): NetworkType[] => {
if (tab === "OPnets") return networks.getAllOpMainnets();
if (tab === "Testnets") return networks.getAllTestnets();
if (tab === "Inactives") return networks.getAllInactive();
return networks.getAllMainnets();
};
$: console.log(tabsMounted);
</script>

<HomeLayout>
<span slot="nav">
<Navigation chainId={0} />
</span>

<span slot="header">
<h1>Kredeum NFTs Factory - Statistics</h1>
</span>

<span slot="content">
<div class="stats">
<div class="tabs">
{#each Object.keys(tabsMounted) as tabKey}
<button
class={tabActive === tabKey ? "active" : ""}
on:click={() => {
console.log("click", tabKey, tabsMounted);
tabActive = tabKey;
tabsMounted[tabKey] = true;
}}>{tabKey}</button
>
{/each}
</div>

{#each Object.entries(tabsMounted) as [tabKey, tabMounted]}
{#if tabMounted}
<span class={tabActive === tabKey ? "" : "hidden"}>
<StatsNetworks networks={getNetworks(tabKey)} />
</span>
{/if}
{/each}
</div>
</span>
</HomeLayout>

<style>
.stats {
margin-left: 20px;
}
.tabs {
display: flex;
gap: 10px;
}
.tabs button {
padding: 10px;
border: none;
cursor: pointer;
background-color: #aaa;
}
.tabs button.active {
background-color: #eee;
}
</style>
File renamed without changes.
75 changes: 2 additions & 73 deletions sveltekit/src/routes/stats/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,76 +1,5 @@
<script lang="ts">
import Addresses from "./Addresses.svelte";
import type { NetworkType } from "@kredeum/common/src/common/types";
import { networks } from "@kredeum/common/src/common/networks";
// Manage Addresses Svelte components in Tabs with 2 states:
// - active (i.e. displayed) or not (only one at a time)
// - mounted or not (mounted one time, can't be unmounted)
// To avoid re-calling onchain data each time tab is reactivated
// To avoid calling onchain data on a tab never activated
type TabsMounted = { [key: string]: boolean };
let tabsMounted: TabsMounted = {
Mainnets: true,
OPnets: false,
Testnets: false,
Inactives: false
};
let tabActive = "Mainnets";
const getNetworks = (tab: string): NetworkType[] => {
if (tab === "OPnets") return networks.getAllOpMainnets();
if (tab === "Testnets") return networks.getAllTestnets();
if (tab === "Inactives") return networks.getAllInactive();
return networks.getAllMainnets();
};
$: console.log(tabsMounted);
import Stats from "$lib/components/Stats.svelte";
</script>

<div class="stats">
<h1>Kredeum NFTs Factory - Statistics</h1>

<div class="tabs">
{#each Object.keys(tabsMounted) as tabKey}
<button
class={tabActive === tabKey ? "active" : ""}
on:click={() => {
console.log("click", tabKey, tabsMounted);
tabActive = tabKey;
tabsMounted[tabKey] = true;
}}>{tabKey}</button
>
{/each}
</div>

{#each Object.entries(tabsMounted) as [tabKey, tabMounted]}
{#if tabMounted}
<span class={tabActive === tabKey ? "" : "hidden"}>
<Addresses networks={getNetworks(tabKey)} />
</span>
{/if}
{/each}
</div>

<style>
.stats {
margin-left: 20px;
}
.tabs {
display: flex;
gap: 10px;
}
.tabs button {
padding: 10px;
border: none;
cursor: pointer;
background-color: #aaa;
}
.tabs button.active {
background-color: #eee;
}
</style>
<Stats />
11 changes: 0 additions & 11 deletions sveltekit/src/routes/stats/mainnets/+page.svelte

This file was deleted.

0 comments on commit badd002

Please sign in to comment.