1 // Copyright 2020 Blue Static <https://www.bluestatic.org>
2 // This program is free software licensed under the GNU General Public License,
3 // version 3.0. The full text of the license can be found in LICENSE.txt.
4 // SPDX-License-Identifier: GPL-3.0-only
6 import { createEffect, createMemo, createState } from 'solid-js';
7 import { For, Show } from 'solid-js/dom';
8 import { TaxReturn, Form, Line } from 'ustaxlib/core';
9 import * as Trace from 'ustaxlib/core/Trace';
10 import { Edge } from 'ustaxlib/core/Trace';
11 import { graphviz } from 'd3-graphviz';
13 const S = require('./FormView.css');
20 export default function FormView(props: FormProps) {
21 const lines = createMemo(() => {
22 const keys = Object.keys(props.form.lines);
23 keys.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }));
24 return keys.map(k => props.form.lines[k]);
29 <h2 class={S.formName}>Form {props.form.name}</h2>
33 {line => <LineView tr={props.tr} line={line} />}
45 function LineView(props: LineProps) {
46 const { tr, line } = props;
48 const [ state, setState ] = createState({
49 value: undefined as any,
50 error: undefined as any,
51 trace: [] as readonly Edge[],
59 trace: [] as readonly Edge[]
63 newState.value = line.value(tr);
67 newState.trace = Trace.getLastTraceList();
71 const valueDisplay = createMemo(() => {
73 return <span class={S.error} title={state.error.stack}>{state.error.message}</span>;
75 return JSON.stringify(state.value, null, 1);
78 const toggleTrace = () => setState('showTrace', !state.showTrace);
83 <div class={S.id} onclick={toggleTrace}>{line.id}</div>
84 <div class={S.description}>
88 <div class={S.value}>{valueDisplay()}</div>
90 <Show when={state.showTrace}>
91 <TraceViewer line={line} trace={state.trace} onClose={() => setState('showTrace', false)} />
97 interface TraceProps {
99 trace: readonly Edge[];
104 function TraceViewer(props: TraceProps) {
105 const renderGraph = (ref) => {
107 for (const edge of props.trace) {
108 graph += `"${edge[1]}" -> "${edge[0]}"; `;
111 .zoomScaleExtent([0.1, 1])
112 .renderDot(`digraph { ${graph} }`, () => {
113 if (ref.querySelector('svg').clientWidth > ref.parentNode.clientWidth) {
114 ref.parentNode.classList.add(S.large);
119 <div class={S.traceViewer}>
120 <h2>Trace {props.line.id} <button class={S.close} onclick={props.onClose}>\u24E7</button></h2>
121 <div forwardRef={renderGraph}></div>