Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

NetworkLatencyConfigurator.tsx

101 lines3.5 KB

1import { GlobeIcon } from "lucide-react";
2import { useEffect, useState } from "react";
3import {
4 NETWORK_LATENCY_LOCALSTORAGE_KEY,
5 networkLatencyInMsSchema,
6} from "@/client/pgliteHelpers";
7import { cn } from "@/lib/utils";
8import { HighlightWrapper } from "@/utils/highlight-collection-related-info";
9import { Button } from "./ui/button";
10import { Popover, PopoverContent, PopoverTrigger } from "./ui/popover";
11import { Separator } from "./ui/separator";
12​
13type NetworkLatency = 0 | 1000 | 2000 | 5000;
14​
15type NetworkLatencyOption = {
16 label: string;
17 value: NetworkLatency;
18 color: string;
19};
20​
21const networkLatencyOptions: NetworkLatencyOption[] = [
22 { label: "No latency", value: 0, color: "green" },
23 { label: "1s", value: 1_000, color: "lime" },
24 { label: "2s", value: 2_000, color: "orange" },
25 { label: "5s", value: 5_000, color: "red" },
26];
27​
28export function NetworkLatencyConfigurator() {
29 const [isOpen, setIsOpen] = useState(false);
30 const [selectedLatency, setSelectedLatency] = useState<NetworkLatency>(1_000);
31​
32 function handleSelectLatency(option: NetworkLatencyOption) {
33 localStorage.setItem(
34 NETWORK_LATENCY_LOCALSTORAGE_KEY,
35 option.value.toString(),
36 );
37 setSelectedLatency(option.value);
38 setIsOpen(false);
39 }
40​
41 useEffect(() => {
42 // get the saved latency from localStorage
43 const savedLatency = localStorage.getItem(NETWORK_LATENCY_LOCALSTORAGE_KEY);
44 const latency = networkLatencyInMsSchema.parse(savedLatency);
45​
46 setSelectedLatency(latency as NetworkLatency);
47 }, []);
48​
49 return (
50 <HighlightWrapper highlightId="networkLatencyCofigurator">
51 <Popover open={isOpen} onOpenChange={setIsOpen}>
52 <PopoverTrigger asChild>
53 <Button variant="outline" size="icon">
54 <GlobeIcon
55 className={cn(
56 selectedLatency === 0
57 ? "text-green-500 dark:text-green-300"
58 : selectedLatency === 2000
59 ? "text-orange-400 dark:text-orange-300"
60 : selectedLatency === 5000
61 ? "text-destructive dark:text-destructive"
62 : "text-foreground",
63 )}
64 />
65 </Button>
66 </PopoverTrigger>
67 <PopoverContent className="w-fit max-w-80 flex flex-col gap-2">
68 <p className="text-sm text-foreground font-bold">
69 Select the network latency you want to emulate.
70 </p>
71 <p className="text-xs text-muted-foreground">
72 Note that requests won't show up in the Network tab of the DevTools
73 until they are completed (no PENDING state).
74 </p>
75 <p className="text-xs text-muted-foreground">
76 This is because currently they are handled locally by a service
77 worker and this implementation comes with this limitation.
78 </p>
79 <Separator orientation="horizontal" />
80 {networkLatencyOptions.map((option) => (
81 <div key={option.value} className="">
82 <Button
83 key={option.value}
84 variant={
85 selectedLatency === option.value ? "secondary" : "ghost"
86 }
87 size="sm"
88 onClick={() => {
89 handleSelectLatency(option);
90 }}
91 className="w-full"
92 >
93 {option.label}
94 </Button>
95 </div>
96 ))}
97 </PopoverContent>
98 </Popover>
99 </HighlightWrapper>
100 );