Skip to content

Commit

Permalink
Tree json collapse all (#3684)
Browse files Browse the repository at this point in the history
# Motivation

Add collapse all functionality to json tree view.
- [x] Related pr #3692

# Changes

- removed unused var `_collapsed`
- add collapse all button

# Tests

![Screen Recording 2023-11-07 at 09 36
19](https://github.com/dfinity/nns-dapp/assets/98811342/5c75d3e2-559e-4d5e-8b8b-4ae536f017d0)

# Todos

- [x] Add entry to changelog (if necessary).
  • Loading branch information
mstrasinskis authored Nov 8, 2023
1 parent d831865 commit ca96e75
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ proposal is successful, the changes it released will be moved from this file to
* Show the maximum participation of the Neurons' Fund when present.
* A list of exceptional (not-rendered, zero value) transactions.
* Add a link to the ICP Dashboard in the project detail page.
* Add collapse-all functionality to json tree view.

#### Changed

Expand Down
49 changes: 33 additions & 16 deletions frontend/src/lib/components/common/JsonPreview.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,54 @@
import { i18n } from "$lib/stores/i18n";
import { expandObject, getObjMaxDepth } from "$lib/utils/utils";
import { isNullish } from "@dfinity/utils";
import { IconExpandAll } from "@dfinity/gix-components";
import { IconCollapseAll, IconExpandAll } from "@dfinity/gix-components";
import TreeJson from "$lib/components/common/TreeJson.svelte";
import RawJson from "$lib/components/common/RawJson.svelte";
import { fade } from "svelte/transition";
import { jsonRepresentationModeStore } from "$lib/derived/json-representation.derived";
const DEFAULT_EXPANDED_LEVEL = 1;
export let json: unknown | undefined = undefined;
let expandedData: unknown;
$: expandedData = isNullish(json)
? json
: expandObject(json as Record<string, unknown>);
const DEFAULT_EXPANDED_LEVEL = 1;
let expandAll = false;
$: expandAll =
getObjMaxDepth(expandedData) <= DEFAULT_EXPANDED_LEVEL ? true : expandAll;
const toggleExpanded = () => (expandAll = !expandAll);
let isExpandedAllVisible = false;
$: isExpandedAllVisible =
getObjMaxDepth(expandedData) > DEFAULT_EXPANDED_LEVEL;
let isAllExpanded: boolean | undefined = undefined;
const toggleExpanded = () => {
isAllExpanded = !isAllExpanded;
};
</script>

<div class="content-cell-island markdown-container">
{#if $jsonRepresentationModeStore === "tree"}
<div class="json" data-tid="json-wrapper" in:fade>
{#if !expandAll}
<button class="ghost expand-all" on:click={toggleExpanded}
><IconExpandAll /><span class="expand-all-label"
>{$i18n.core.expand_all}</span
></button
>
{#if isExpandedAllVisible}
<button class="ghost expand-all" on:click={toggleExpanded}>
{#if isAllExpanded}
<div in:fade>
<IconCollapseAll />
<span class="expand-all-label">{$i18n.core.collapse_all}</span>
</div>
{:else}
<div in:fade>
<IconExpandAll />
<span class="expand-all-label">{$i18n.core.expand_all}</span>
</div>
{/if}
</button>
{/if}
<TreeJson
json={expandedData}
defaultExpandedLevel={expandAll ? Number.MAX_SAFE_INTEGER : 1}
defaultExpandedLevel={isAllExpanded
? Number.MAX_SAFE_INTEGER
: DEFAULT_EXPANDED_LEVEL}
/>
</div>
{:else}
Expand All @@ -53,9 +68,11 @@
right: var(--padding-0_5x);
top: var(--padding-0_5x);
display: flex;
align-items: center;
gap: var(--padding-0_5x);
div {
display: flex;
align-items: center;
gap: var(--padding-0_5x);
}
.expand-all-label {
display: none;
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/lib/components/common/TreeJson.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
export let defaultExpandedLevel = Infinity;
export let _key: string | undefined = undefined;
export let _level = 0;
export let _collapsed: boolean | undefined = undefined;
let keyLabel: string;
let children: [string, unknown][];
Expand All @@ -34,8 +33,7 @@
}
let collapsed = true;
$: collapsed =
_collapsed === undefined ? _level >= defaultExpandedLevel : _collapsed;
$: collapsed = _level >= defaultExpandedLevel;
let keyIsIndex = false;
$: keyIsIndex = !isNaN(Number(_key));
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/lib/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"do_not_close": "Please do not close your browser tab",
"finish": "Finish",
"unknown": "Unknown",
"expand_all": "Expand All"
"expand_all": "Expand All",
"collapse_all": "Collapse All"
},
"error": {
"auth_sync": "There was an unexpected issue while syncing the status of your authentication. Try to refresh your browser.",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/types/i18n.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface I18nCore {
finish: string;
unknown: string;
expand_all: string;
collapse_all: string;
}

interface I18nError {
Expand Down

0 comments on commit ca96e75

Please sign in to comment.