File size: 3,140 Bytes
43a06dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<script lang="ts">
    import { onDestroy, onMount } from "svelte";
    import { goto } from "$app/navigation";
    import { version } from "$lib/version";
    import { device, app } from "$lib/device";
    import { defaultNavPage } from "$lib/subnav";
    import settings, { storedSettings } from "$lib/state/settings";
    import SectionHeading from "$components/misc/SectionHeading.svelte";
    import { type Readable, type Unsubscriber } from "svelte/store";

    const stateSubscribers: Record<string, Unsubscriber> = {};
    let states: Record<string, unknown> = {};

    $: sections = [
        { title: "device", data: device },
        { title: "app", data: app },
        { title: "settings", data: $storedSettings },
        { title: "version", data: $version },
        { title: "states", data: states }
    ];

    const loadStates = () => {
        const modules = import.meta.glob("/src/lib/*/*.ts");
        const excluded = new Set(['translations.translations', 'settings']);

        Object.entries(modules).map(async ([ name, _import ]) => {
            const moduleName = name.split('/').pop()?.split('.').shift();

            const module = await _import() as Record<string, unknown>;
            for (const key in module) {
                const _export = module[key] as unknown as Readable<unknown>;
                if (typeof _export === 'object' && 'subscribe' in _export) {
                    const name = moduleName + (key === 'default' ? '' : `.${key}`);
                    if (excluded.has(name)) continue;

                    stateSubscribers[name] = _export.subscribe((value) => {
                        states = {
                            ...states,
                            [name]: value
                        }
                    });
                }
            }
        });
    }

    onMount(() => {
        if (!$settings.advanced.debug) {
            goto(defaultNavPage("settings"), { replaceState: true });
        }

        loadStates();
    });

    onDestroy(() => {
        Object.values(stateSubscribers).map(unsub => unsub());
    })
</script>

{#if $settings.advanced.debug}
    <div id="debug-page">
        {#each sections as { title, data }, i}
            <div class="debug-section">
                <SectionHeading
                    sectionId={title}
                    {title}
                    copyData={JSON.stringify(data)}
                />
                <div class="json-block subtext">
                    {JSON.stringify(data, null, 2)}
                </div>
            </div>
        {/each}
    </div>
{/if}

<style>
    #debug-page {
        display: flex;
        flex-direction: column;
        padding: calc(var(--subnav-padding) / 2);
        gap: var(--padding);
    }

    .debug-section {
        display: flex;
        flex-direction: column;
        gap: var(--padding);
    }

    .json-block {
        display: flex;
        flex-direction: column;
        line-break: anywhere;
        border-radius: var(--border-radius);
        background: var(--button);
        padding: var(--padding);
        white-space: pre-wrap;
    }
</style>