leptos_query_devtools 0.1.3

Devtools for Leptos Query
Documentation
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
use leptos::*;
use leptos_query::{
    cache_observer::{
        CacheEvent, CacheObserver, CreatedQuery, ObserverAdded, QueryCacheKey, SerializedQuery,
    },
    *,
};
use std::{collections::HashMap, time::Duration};

use crate::component::*;
use crate::timeout::{time_until_stale, use_timeout};

#[component]
pub(crate) fn InnerDevtools() -> impl IntoView {
    let mounted = create_rw_signal(false);

    create_effect(move |_| {
        mounted.set(true);
    });

    move || {
        if mounted.get() {
            view! { <DevtoolsClient/> }
        } else {
            ().into_view()
        }
    }
}

#[component]
fn DevtoolsClient() -> impl IntoView {
    let client = leptos_query::use_query_client();
    let state = DevtoolsContext::new();
    client.register_cache_observer(state.clone());
    provide_context(state);

    // Ensure that selected query is closed if it is evicted.
    create_effect({
        move |_| {
            let context = use_devtools_context();

            if let Some(key) = context
                .selected_query
                .with(|maybe| maybe.as_ref().map(|q| q.key.clone()))
            {
                let cache = context.query_state.get();

                if cache.get(&key).is_none() {
                    context.selected_query.set(None);
                }
            }
        }
    });

    view! {
        <Portal>
            <style>{include_str!("./styles.css")}</style>
            <div class="leptos-query-devtools lq-font-mono">
                <Devtools/>
            </div>
        </Portal>
    }
}

#[derive(Clone)]
struct DevtoolsContext {
    owner: Owner,
    query_state: RwSignal<HashMap<QueryCacheKey, QueryCacheEntry>>,
    open: RwSignal<bool>,
    filter: RwSignal<String>,
    sort: RwSignal<SortOption>,
    order_asc: RwSignal<bool>,
    selected_query: RwSignal<Option<QueryCacheEntry>>,
}

#[derive(Debug, Clone, Copy)]
enum SortOption {
    Time,
    Ascii,
}

impl SortOption {
    fn as_str(&self) -> &str {
        match self {
            SortOption::Time => "Time",
            SortOption::Ascii => "Ascii",
        }
    }
    fn from_string(s: &str) -> Self {
        match s {
            "Ascii" => SortOption::Ascii,
            "Time" => SortOption::Time,
            _ => SortOption::Time,
        }
    }
}

#[derive(Clone)]
struct QueryCacheEntry {
    key: QueryCacheKey,
    state: RwSignal<QueryState<String>>,
    observer_count: RwSignal<usize>,
    gc_time: RwSignal<SettingTime>,
    stale_time: RwSignal<SettingTime>,
    is_stale: Signal<bool>,
    mark_invalid: std::rc::Rc<dyn Fn() -> bool>,
}

fn use_devtools_context() -> DevtoolsContext {
    use_context::<DevtoolsContext>().expect("Devtools Context to be present.")
}

impl DevtoolsContext {
    fn new() -> Self {
        DevtoolsContext {
            owner: Owner::current().expect("Owner to be present"),
            query_state: create_rw_signal(HashMap::new()),
            open: create_rw_signal(false),
            filter: create_rw_signal("".to_string()),
            sort: create_rw_signal(SortOption::Time),
            order_asc: create_rw_signal(false),
            selected_query: create_rw_signal(None),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum SettingTime {
    // No time set.
    None,
    // Some gc time set.
    Some(Duration),
    // Never expires.
    Infinity,
}

impl SettingTime {
    fn max(&self, other: Self) -> Self {
        match (self, other) {
            (SettingTime::None, other) => other,
            (current, SettingTime::None) => *current,
            (SettingTime::Some(current), SettingTime::Some(other)) if other > *current => {
                SettingTime::Some(other)
            }
            (SettingTime::Some(current), SettingTime::Some(_)) => SettingTime::Some(*current),
            (SettingTime::Infinity, _) => SettingTime::Infinity,
            (_, SettingTime::Infinity) => SettingTime::Infinity,
        }
    }

    fn min(&self, other: Self) -> Self {
        match (self, other) {
            (SettingTime::None, other) => other,
            (current, SettingTime::None) => *current,
            (SettingTime::Some(current), SettingTime::Some(other)) if other < *current => {
                SettingTime::Some(other)
            }
            (SettingTime::Some(current), SettingTime::Some(_)) => SettingTime::Some(*current),
            (SettingTime::Infinity, other) => other,
            (current, SettingTime::Infinity) => *current,
        }
    }

    fn from_option(duration: Option<Duration>) -> Self {
        match duration {
            Some(duration) => SettingTime::Some(duration),
            None => SettingTime::Infinity,
        }
    }

    fn to_expiration(self) -> Option<Duration> {
        match self {
            SettingTime::None => None,
            SettingTime::Infinity => None,
            SettingTime::Some(duration) => Some(duration),
        }
    }
}

impl std::fmt::Display for SettingTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SettingTime::None => write!(f, "None"),
            SettingTime::Some(duration) => write!(f, "{}ms", duration.as_millis()),
            SettingTime::Infinity => write!(f, "Infinity"),
        }
    }
}

impl CacheObserver for DevtoolsContext {
    fn process_cache_event(&self, event: CacheEvent) {
        match event {
            CacheEvent::Created(CreatedQuery {
                key,
                state,
                mark_invalid,
            }) => {
                // Need to create signals with root owner, or else they will be disposed of.
                let entry = with_owner(self.owner, || {
                    let stale_time = create_rw_signal(SettingTime::None);
                    let state = create_rw_signal(state);

                    let is_stale = {
                        let (stale, set_stale) = create_signal(false);

                        let updated_at = Signal::derive(move || state.with(|s| s.updated_at()));

                        use_timeout(move || {
                            match (updated_at.get(), stale_time.get().to_expiration()) {
                                (Some(updated_at), Some(stale_time)) => {
                                    let duration = time_until_stale(updated_at, stale_time);
                                    if duration.is_zero() {
                                        set_stale.set(true);
                                        None
                                    } else {
                                        set_stale.set(false);
                                        set_timeout_with_handle(
                                            move || {
                                                set_stale.set(true);
                                            },
                                            duration,
                                        )
                                        .ok()
                                    }
                                }
                                _ => None,
                            }
                        });

                        stale.into()
                    };

                    QueryCacheEntry {
                        key: key.clone(),
                        state,
                        stale_time,
                        gc_time: create_rw_signal(SettingTime::None),
                        observer_count: create_rw_signal(0),
                        is_stale,
                        mark_invalid,
                    }
                });

                self.query_state.update(|map| {
                    map.insert(key, entry);
                })
            }
            CacheEvent::Removed(key) => self.query_state.update(|map| {
                map.remove(&key);
            }),
            // TODO: Fix this borrow error when using signal update.
            CacheEvent::Updated(SerializedQuery { key, state }) => {
                let map = self.query_state.get_untracked();
                if let Some(entry) = map.get(&key) {
                    entry.state.set(state);
                }
                self.query_state.set(map);
            }
            CacheEvent::ObserverAdded(observer) => {
                let ObserverAdded { key, options } = observer;
                let QueryOptions {
                    stale_time,
                    gc_time,
                    ..
                } = options;
                self.query_state.update(|map| {
                    if let Some(entry) = map.get_mut(&key) {
                        entry.observer_count.update(|c| *c += 1);
                        {
                            let current_gc = entry.gc_time.get_untracked();
                            let setting_gc = SettingTime::from_option(gc_time);

                            let new_gc = current_gc.max(setting_gc);
                            entry.gc_time.set(new_gc);
                        }
                        {
                            let current_stale = entry.stale_time.get_untracked();
                            let setting_stale = SettingTime::from_option(stale_time);
                            let new_stale = current_stale.min(setting_stale);
                            entry.stale_time.set(new_stale);
                        }
                    }
                });
            }
            CacheEvent::ObserverRemoved(key) => {
                self.query_state.update(|map| {
                    if let Some(entry) = map.get_mut(&key) {
                        entry.observer_count.update(|c| {
                            if *c > 0 {
                                *c -= 1
                            }
                        });
                    }
                });
            }
        }
    }
}

#[component]
fn Devtools() -> impl IntoView {
    let DevtoolsContext {
        open,
        query_state,
        selected_query,
        filter,
        sort,
        order_asc,
        ..
    } = use_devtools_context();

    let query_state = Signal::derive(move || {
        let filter = filter.get().to_ascii_lowercase();

        // Filtered
        let mut query_state = query_state.with(|map| {
            map.iter()
                .filter(|(key, _)| key.0.to_ascii_lowercase().contains(&filter))
                .map(|(_, q)| q)
                .cloned()
                .collect::<Vec<_>>()
        });

        match sort.get() {
            SortOption::Ascii => query_state.sort_by(|a, b| a.key.0.cmp(&b.key.0)),
            SortOption::Time => {
                query_state.sort_by(|a, b| {
                    let a_updated = a.state.with(|s| s.updated_at()).unwrap_or(Instant::now());
                    let b_updated = b.state.with(|s| s.updated_at()).unwrap_or(Instant::now());
                    a_updated.cmp(&b_updated)
                });
            }
        };

        if !order_asc.get() {
            query_state.reverse();
        }

        query_state
    });

    let container_ref = leptos::create_node_ref::<leptos::html::Div>();

    let height_signal = create_rw_signal(500);

    #[cfg(not(feature = "csr"))]
    let handle_drag_start = move |_| ();

    // Drag start handler
    #[cfg(feature = "csr")]
    let handle_drag_start = move |event: web_sys::MouseEvent| {
        use wasm_bindgen::closure::Closure;
        use wasm_bindgen::JsCast;

        let bounding = container_ref
            .get()
            .expect("container to be mounted")
            .get_bounding_client_rect();

        let height = bounding.height();

        let start_y = event.client_y() as f64;

        let move_closure = Closure::wrap(Box::new(move |move_event: web_sys::MouseEvent| {
            move_event.prevent_default();

            let val_to_add = start_y - move_event.client_y() as f64;

            let new_height = (height + val_to_add).max(200.0);

            height_signal.set(new_height as i32);
        }) as Box<dyn FnMut(_)>)
        .into_js_value();

        // Register the move event listener
        if let Some(window) = web_sys::window() {
            let end = std::rc::Rc::new(std::cell::Cell::new(None::<Closure<dyn FnMut()>>));
            let end_closure = Closure::wrap({
                let window = window.clone();
                let move_closure = move_closure.clone();
                Box::new(move || {
                    window
                        .remove_event_listener_with_callback(
                            "mousemove",
                            move_closure.as_ref().unchecked_ref(),
                        )
                        .unwrap();

                    if let Some(end) = end.take() {
                        let _ = window.remove_event_listener_with_callback(
                            "mouseup",
                            end.as_ref().unchecked_ref(),
                        );
                    }
                }) as Box<dyn FnMut()>
            })
            .into_js_value();

            window
                .add_event_listener_with_callback(
                    "mousemove",
                    move_closure.as_ref().clone().unchecked_ref(),
                )
                .unwrap();

            window
                .add_event_listener_with_callback("mouseup", end_closure.as_ref().unchecked_ref())
                .unwrap();
        }
    };

    view! {
        <Show
            when=move || open.get()
            fallback=move || {
                view! {
                    <button
                        on:click=move |_| open.set(true)
                        class="lq-bg-zinc-200 text-lq-foreground lq-fixed lq-bottom-3 lq-right-3 lq-rounded-full lq-w-12 lq-h-12 hover:-lq-translate-y-1 hover:lq-bg-zinc-300 lq-transition-all lq-duration-200"
                        inner_html=include_str!("logo.svg")
                    ></button>
                }
            }
        >

            <div
                class="lq-bg-lq-background lq-text-lq-foreground lq-px-0 lq-fixed lq-bottom-0 lq-left-0 lq-right-0 lq-z-[1000]"
                style:height=move || format!("{}px", height_signal.get())
                ref=container_ref
            >
                <div
                    class="lq-w-full lq-py-1 lq-bg-lq-background lq-cursor-ns-resize lq-transition-colors hover:lq-bg-lq-border"
                    on:mousedown=handle_drag_start
                ></div>
                <div class="lq-h-full lq-flex lq-flex-col lq-relative">
                    <div class="lq-flex-1 lq-overflow-hidden lq-flex">
                        <div class="lq-flex lq-flex-col lq-flex-1  lq-overflow-x-hidden">
                            <div class="lq-flex-none">
                                <Header/>
                                <div class="lq-py-1 lq-px-2 lq-border-lq-border lq-border-b lq-flex lq-items-center lq-w-full lq-justify-between lq-max-w-full lq-overflow-x-auto lq-gap-2 lq-no-scrollbar">
                                    <div class="lq-flex lq-items-center lq-gap-2">
                                        <SearchInput/>
                                        <SetSort/>
                                        <SetSortOrder/>
                                    </div>
                                    <div class="lq-flex lq-items-center">
                                        <ClearCache/>
                                    </div>
                                </div>
                            </div>

                            <ul class="lq-flex lq-flex-col lq-gap-1 lq-overflow-y-auto">
                                <For each=move || query_state.get() key=|q| q.key.clone() let:entry>
                                    <QueryRow entry=entry/>
                                </For>

                            </ul>
                        </div>
                        <Show when=move || {
                            selected_query.get().is_some()
                        }>
                            {move || {
                                selected_query.get().map(|q| view! { <SelectedQuery query=q/> })
                            }}

                        </Show>
                    </div>
                    <div class="lq-absolute -lq-top-6 lq-right-2">
                        <CloseButton/>
                    </div>
                </div>
            </div>
        </Show>
    }
}

#[component]
fn CloseButton() -> impl IntoView {
    let DevtoolsContext { open, .. } = use_devtools_context();

    view! {
        <button
            on:click=move |_| open.set(false)
            class="lq-bg-lq-background lq-text-lq-foreground lq-rounded-t-sm lq-w-6 lq-h-6 lq-p-1 lq-transition-colors lq-hover:bg-lq-accent"
        >
            <svg
                width="100%"
                height="100%"
                viewBox="0 0 15 15"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
            >
                <path
                    d="M12.8536 2.85355C13.0488 2.65829 13.0488 2.34171 12.8536 2.14645C12.6583 1.95118 12.3417 1.95118 12.1464 2.14645L7.5 6.79289L2.85355 2.14645C2.65829 1.95118 2.34171 1.95118 2.14645 2.14645C1.95118 2.34171 1.95118 2.65829 2.14645 2.85355L6.79289 7.5L2.14645 12.1464C1.95118 12.3417 1.95118 12.6583 2.14645 12.8536C2.34171 13.0488 2.65829 13.0488 2.85355 12.8536L7.5 8.20711L12.1464 12.8536C12.3417 13.0488 12.6583 13.0488 12.8536 12.8536C13.0488 12.6583 13.0488 12.3417 12.8536 12.1464L8.20711 7.5L12.8536 2.85355Z"
                    fill="currentColor"
                    fill-rule="evenodd"
                    clip-rule="evenodd"
                ></path>
            </svg>
        </button>
    }
}

#[component]
fn Header() -> impl IntoView {
    let DevtoolsContext { query_state, .. } = use_devtools_context();

    let num_loaded = Signal::derive(move || {
        query_state
            .get()
            .values()
            .map(|q| q.state)
            .filter(|s| s.with(|s| matches!(s, QueryState::Loaded(_))))
            .count()
    });

    let num_fetching = Signal::derive(move || {
        query_state
            .get()
            .values()
            .map(|q| q.state)
            .filter(|s| s.with(|s| matches!(s, QueryState::Fetching(_) | QueryState::Loading)))
            .count()
    });

    let invalid = Signal::derive(move || {
        query_state
            .get()
            .values()
            .map(|q| q.state)
            .filter(|s| s.with(|s| matches!(s, QueryState::Invalid(_))))
            .count()
    });

    let total = Signal::derive(move || query_state.get().len());

    let label_class = "lq-hidden lg:lq-inline-block";
    view! {
        <div class="lq-flex-none lq-flex lq-justify-between lq-w-full lq-overflow-y-hidden lq-items-center lq-border-b lq-border-lq-border lq-pb-2 lq-px-1">
            <h3 class="lq-pl-2 lq-tracking-tighter lq-text-lg lq-italic lq-text-transparent lq-bg-clip-text lq-font-bold lq-bg-gradient-to-r lq-from-red-800 lq-to-orange-400">
                Leptos Query
            </h3>

            <div class="lq-flex lq-gap-2 lq-px-2">
                <DotBadge color=ColorOption::Blue>
                    <span class=label_class>Fetching</span>
                    <span>{num_fetching}</span>
                </DotBadge>

                <DotBadge color=ColorOption::Green>
                    <span class=label_class>Loaded</span>
                    <span>{num_loaded}</span>
                </DotBadge>

                <DotBadge color=ColorOption::Red>
                    <span class=label_class>Invalid</span>
                    <span>{invalid}</span>
                </DotBadge>

                <DotBadge color=ColorOption::Gray>
                    <span class=label_class>Total</span>
                    <span>{total}</span>
                </DotBadge>
            </div>
        </div>
    }
}

#[component]
fn SearchInput() -> impl IntoView {
    let DevtoolsContext { filter, .. } = use_devtools_context();

    view! {
        <div class="lq-relative lq-w-64">
            <div class="lq-pointer-events-none lq-absolute lq-inset-y-0 lq-left-0 lq-flex lq-items-center lq-pl-3 lq-text-zinc-400">
                <svg
                    class="lq-h-4 lq-w-4"
                    viewBox="0 0 20 20"
                    fill="currentColor"
                    aria-hidden="true"
                >
                    <path
                        fill-rule="evenodd"
                        d="M9 3.5a5.5 5.5 0 100 11 5.5 5.5 0 000-11zM2 9a7 7 0 1112.452 4.391l3.328 3.329a.75.75 0 11-1.06 1.06l-3.329-3.328A7 7 0 012 9z"
                        clip-rule="evenodd"
                    ></path>
                </svg>
            </div>
            <input
                id="search"
                class="lq-form-input lq-block lq-w-full lq-rounded-md lq-bg-lq-input lq-py-0 lq-pl-10 lq-pr-3 lq-text-lq-input-foreground lq-text-xs lq-leading-6 lq-placeholder-lq-input-foreground lq-border lq-border-lq-border"
                placeholder="Search"
                name="search"
                autocomplete="off"
                type="search"
                on:input=move |ev| {
                    let value = event_target_value(&ev);
                    filter.set(value);
                }

                prop:value=filter
            />
        </div>
    }
}

#[component]
fn SetSort() -> impl IntoView {
    let DevtoolsContext { sort, .. } = use_devtools_context();

    view! {
        <select
            id="countries"
            class="lq-form-select lq-border-lq-border lq-border lq-text-xs lq-rounded-md lq-block lq-w-48 lq-py-1 lq-px-2 lq-bg-lq-input lq-text-lq-input-foreground lq-line-clamp-1"
            value=move || sort.get().as_str().to_string()
            on:change=move |ev| {
                let new_value = event_target_value(&ev);
                let option = SortOption::from_string(&new_value);
                sort.set(option);
            }
        >

            <option value=SortOption::Time.as_str()>Sort by last updated</option>
            <option value=SortOption::Ascii.as_str()>Sort by query key</option>
        </select>
    }
}

#[component]
fn SetSortOrder() -> impl IntoView {
    let DevtoolsContext { order_asc, .. } = use_devtools_context();

    view! {
        <button
            class="lq-bg-lq-input lq-text-lq-input-foreground lq-rounded-md lq-px-2 lq-py-1 lq-text-xs lq-inline-flex lq-items-center lq-gap-1 lq-border lq-border-lq-border"
            on:click=move |_| {
                order_asc.set(!order_asc.get());
            }
        >

            <span class="w-8">{move || { if order_asc.get() { "Asc " } else { "Desc" } }}</span>
            {move || {
                if order_asc.get() {
                    view! {
                        <svg
                            width="15"
                            height="15"
                            viewBox="0 0 15 15"
                            fill="none"
                            xmlns="http://www.w3.org/2000/svg"
                        >
                            <path
                                d="M7.14645 2.14645C7.34171 1.95118 7.65829 1.95118 7.85355 2.14645L11.8536 6.14645C12.0488 6.34171 12.0488 6.65829 11.8536 6.85355C11.6583 7.04882 11.3417 7.04882 11.1464 6.85355L8 3.70711L8 12.5C8 12.7761 7.77614 13 7.5 13C7.22386 13 7 12.7761 7 12.5L7 3.70711L3.85355 6.85355C3.65829 7.04882 3.34171 7.04882 3.14645 6.85355C2.95118 6.65829 2.95118 6.34171 3.14645 6.14645L7.14645 2.14645Z"
                                fill="currentColor"
                                fill-rule="evenodd"
                                clip-rule="evenodd"
                            ></path>
                        </svg>
                    }
                } else {
                    view! {
                        <svg
                            width="15"
                            height="15"
                            viewBox="0 0 15 15"
                            fill="none"
                            xmlns="http://www.w3.org/2000/svg"
                        >
                            <path
                                d="M7.5 2C7.77614 2 8 2.22386 8 2.5L8 11.2929L11.1464 8.14645C11.3417 7.95118 11.6583 7.95118 11.8536 8.14645C12.0488 8.34171 12.0488 8.65829 11.8536 8.85355L7.85355 12.8536C7.75979 12.9473 7.63261 13 7.5 13C7.36739 13 7.24021 12.9473 7.14645 12.8536L3.14645 8.85355C2.95118 8.65829 2.95118 8.34171 3.14645 8.14645C3.34171 7.95118 3.65829 7.95118 3.85355 8.14645L7 11.2929L7 2.5C7 2.22386 7.22386 2 7.5 2Z"
                                fill="currentColor"
                                fill-rule="evenodd"
                                clip-rule="evenodd"
                            ></path>
                        </svg>
                    }
                }
            }}

        </button>
    }
}

#[component]
fn ClearCache() -> impl IntoView {
    let cache = leptos_query::use_query_client();

    view! {
        <button
            class="lq-bg-lq-input lq-text-lq-input-foreground lq-rounded-md lq-px-2 lq-py-1 lq-text-xs lq-inline-flex lq-items-center lq-gap-1 lq-border lq-border-lq-border"
            on:click=move |_| {
                cache.clear();
            }
        >

            <svg
                width="15"
                height="15"
                viewBox="0 0 15 15"
                fill="none"
                xmlns="http://www.w3.org/2000/svg"
            >
                <path
                    d="M5.5 1C5.22386 1 5 1.22386 5 1.5C5 1.77614 5.22386 2 5.5 2H9.5C9.77614 2 10 1.77614 10 1.5C10 1.22386 9.77614 1 9.5 1H5.5ZM3 3.5C3 3.22386 3.22386 3 3.5 3H5H10H11.5C11.7761 3 12 3.22386 12 3.5C12 3.77614 11.7761 4 11.5 4H11V12C11 12.5523 10.5523 13 10 13H5C4.44772 13 4 12.5523 4 12V4L3.5 4C3.22386 4 3 3.77614 3 3.5ZM5 4H10V12H5V4Z"
                    fill="currentColor"
                    fill-rule="evenodd"
                    clip-rule="evenodd"
                ></path>
            </svg>
        </button>
    }
}

#[component]
fn QueryRow(entry: QueryCacheEntry) -> impl IntoView {
    let selected_query = use_devtools_context().selected_query;
    let QueryCacheEntry {
        key,
        state,
        observer_count,
        is_stale,
        ..
    } = entry.clone();
    let observer = move || {
        let count = observer_count.get();
        if count == 0 {
            view! {
                <span class="lq-inline-flex lq-items-center lq-gap-x-1.5 lq-rounded-md lq-bg-gray-100 lq-px-2 lq-py-1 lq-text-xs lq-font-medium lq-text-gray-700">
                    {count}
                </span>
            }
        } else {
            view! {
                <span class="lq-inline-flex lq-items-center lq-gap-x-1.5 lq-rounded-md lq-bg-green-100 lq-px-2 lq-py-1 lq-text-xs lq-font-medium lq-text-green-700">
                    {count}
                </span>
            }
        }
    };
    view! {
        <li
            class="hover:lq-bg-lq-accent lq-transition-colors lq-flex lq-w-full lq-gap-4 lq-items-center lq-border-lq-border lq-border-b lq-p-1"
            on:click={
                let key = key.clone();
                move |_| {
                    if selected_query.get_untracked().map_or(false, |q| q.key == key) {
                        selected_query.set(None);
                    } else {
                        selected_query.set(Some(entry.clone()))
                    }
                }
            }
        >

            {observer}
            <span class="lq-w-[4.5rem]">
                <RowStateLabel state=state.into() is_stale/>
            </span>
            <span class="lq-text-sm">{key.0}</span>
        </li>
    }
}

#[component]
fn RowStateLabel(state: Signal<QueryState<String>>, is_stale: Signal<bool>) -> impl IntoView {
    let state_label = Signal::derive(move || {
        let is_stale = is_stale.get();
        match state.get() {
            QueryState::Created => "Created",
            QueryState::Loading => "Loading",
            QueryState::Fetching(_) => "Fetching",
            QueryState::Loaded(_) if is_stale => "Stale",
            QueryState::Loaded(_) => "Loaded",
            QueryState::Invalid(_) => "Invalid",
        }
    });

    let badge = Signal::derive(move || {
        let is_stale = is_stale.get();
        match state.get() {
            QueryState::Created | QueryState::Loading | QueryState::Fetching(_) => {
                ColorOption::Blue
            }
            QueryState::Loaded(_) if is_stale => ColorOption::Yellow,
            QueryState::Loaded(_) => ColorOption::Green,
            QueryState::Invalid(_) => ColorOption::Red,
        }
    });

    move || {
        view! {
            <DotBadge color=badge.get() dot=false>
                {state_label}
            </DotBadge>
        }
    }
}

#[component]
fn SelectedQuery(query: QueryCacheEntry) -> impl IntoView {
    let QueryCacheEntry {
        key: query_key,
        state: query_state,
        is_stale,
        observer_count,
        mark_invalid,
        stale_time,
        gc_time,
    } = query;

    #[cfg(feature = "csr")]
    let last_update = Signal::derive(move || {
        use wasm_bindgen::JsValue;
        query_state.get().updated_at().map(|i| {
            let time = JsValue::from_f64(i.0.as_millis() as f64);
            let date = js_sys::Date::new(&time);
            let hours = date.get_hours();
            let minutes = date.get_minutes();
            let seconds = date.get_seconds();
            format!("{:02}:{:02}:{:02}", hours, minutes, seconds)
        })
    });

    #[cfg(not(feature = "csr"))]
    let last_update = Signal::derive(move || query_state.get().updated_at().map(|i| i.to_string()));

    // Pretty print the JSON
    #[cfg(feature = "csr")]
    let value: Signal<Option<String>> = Signal::derive(move || {
        use wasm_bindgen::JsValue;
        let value = query_state.get().data().cloned()?;
        let json = js_sys::JSON::parse(value.as_str()).ok()?;
        let result = js_sys::JSON::stringify_with_replacer_and_space(
            &json,
            &JsValue::NULL,
            &JsValue::from_f64(2.0),
        )
        .ok()
        .map(|r| r.as_string())
        // If value is not json, just present value.
        .unwrap_or(Some(value));

        result
    });

    #[cfg(not(feature = "csr"))]
    let value: Signal<Option<String>> = Signal::derive(move || query_state.get().data().cloned());

    let section_class = "lq-px-2 lq-py-1 lq-flex lq-flex-col lq-items-center lq-gap-1 lq-w-full";
    let entry_class =
        "lq-flex lq-items-center lq-justify-between lq-text-xs lq-font-medium lq-w-full";

    let stale_time = Signal::derive(move || stale_time.get().to_string());

    let gc_time = Signal::derive(move || gc_time.get().to_string());

    view! {
        <div class="lq-w-1/2 lq-overflow-y-scroll lq-max-h-full lq-border-black lq-border-l-4">
            <div class="lq-flex lq-flex-col lq-w-full lq-h-full lq-items-center">
                <div class="lq-w-full">
                    <div class="lq-text-sm lq-text-lq-foreground lq-p-1 lq-bg-lq-accent">
                        Query Details
                    </div>
                    <dl class=section_class>
                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">Status</dt>
                            <dd class="lq-text-zinc-200">
                                <RowStateLabel state=query_state.into() is_stale/>
                            </dd>
                        </div>
                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">Key</dt>
                            <dd class="lq-text-zinc-200">{query_key.0}</dd>
                        </div>
                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">Last Update</dt>
                            <dd class="lq-text-zinc-200">{last_update}</dd>
                        </div>
                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">Active Observers</dt>
                            <dd class="lq-text-zinc-200">{observer_count}</dd>
                        </div>

                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">Stale Time</dt>
                            <dd class="text-zinc-200">{stale_time}</dd>
                        </div>
                        <div class=entry_class>
                            <dt class="lq-text-zinc-100">GC Time</dt>
                            <dd class="lq-text-zinc-200">{gc_time}</dd>
                        </div>
                    </dl>
                </div>
                <div class="lq-w-full">
                    <div class="lq-text-sm lq-text-lq-foreground lq-p-1 lq-bg-lq-accent">
                        Query Actions
                    </div>
                    <div class="lq-flex lq-items-center lq-gap-2 lq-p-1">
                        <Button
                            color=ColorOption::Red
                            on:click=move |_| {
                                mark_invalid();
                            }
                        >

                            Invalidate
                        </Button>
                    </div>
                </div>
                <div class="lq-text-sm lq-text-lq-foreground lq-p-1 lq-bg-lq-accent lq-w-full">
                    Query Data
                </div>
                <div class="lq-flex-1 lq-flex lq-p-2 lq-w-full">
                    <div class="lq-flex-1 lq-p-4 lq-rounded-md lq-bg-zinc-800 lq-shadow-md lq-w-11/12 lq-text-xs lq-overflow-hidden">
                        <pre class="lq-whitespace-pre-wrap lq-break-words">
                            {move || value.get().unwrap_or_default()}
                        </pre>
                    </div>
                </div>
            </div>
        </div>
    }
}