-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
133 lines (120 loc) · 3.82 KB
/
page.tsx
File metadata and controls
133 lines (120 loc) · 3.82 KB
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
"use client";
import { useSearchParams } from "next/navigation";
import { Fragment, useState, useEffect } from "react";
import { diffLines } from "diff";
import lz from "lz-string";
import { createShareUrl } from "@/create-share-url";
type DiffResult = {
added?: boolean;
removed?: boolean;
value: string;
};
export default function Home() {
const searchParams = useSearchParams();
const data = searchParams.get("data");
const { textA, textB } = data
? (JSON.parse(lz.decompressFromEncodedURIComponent(data)) as {
textA: string;
textB: string;
})
: { textA: "", textB: "" };
const [text1, setText1] = useState<string>(textA);
const [text2, setText2] = useState<string>(textB);
const [diffs, setDiffs] = useState<DiffResult[]>(diffLines(textA, textB));
const [copyButtonText, setCopyButtonText] = useState<string>("Copy");
const [currentURL, setCurrentURL] = useState<string>("");
useEffect(() => {
setCurrentURL(window.location.href);
}, []);
const calculateDiff = (a: string, b: string) => {
setDiffs(diffLines(a, b));
};
const copyUrl = () => {
navigator.clipboard.writeText(
createShareUrl(currentURL, text1, text2)
);
};
return (
<main className="p-5">
<h1 className="text-4xl font-bold p-5">Diff Viewer</h1>
<div className="p-4">
<div className="flex mb-4 space-x-4">
<textarea
className="border p-2 flex-1 h-96"
value={text1}
onChange={(e) => {
setText1(e.target.value);
calculateDiff(e.target.value, text2);
}}
placeholder="Text 1"
/>
<textarea
className="border p-2 flex-1 h-96"
value={text2}
onChange={(e) => {
setText2(e.target.value);
calculateDiff(text1, e.target.value);
}}
placeholder="Text 2"
/>
</div>
<pre className="border mt-4 p-4 whitespace-pre-wrap">
{diffs.map((part, index) => {
const lines = part.value
.replace(/\n$/, "")
.split("\n")
.map((line, lineIndex) => {
if (part.added) {
return (
<div
key={`${index}-${lineIndex}`}
className="bg-green-100 text-green-700"
>
+ {line}
</div>
);
}
if (part.removed) {
return (
<div
key={`${index}-${lineIndex}`}
className="bg-red-100 text-red-700"
>
- {line}
</div>
);
}
return (
<div key={`${index}-${lineIndex}`} className="">
{line}
</div>
);
});
return <Fragment key={index}>{lines}</Fragment>;
})}
</pre>
</div>
<div className="p-4 flex space-x-4">
<input
type="text"
className="bg-gray-100 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-100 focus:border-blue-100 block w-full p-2.5"
value={createShareUrl(currentURL, text1, text2)}
readOnly
/>
<button
type="button"
className="text-white bg-blue-500 hover:bg-blue-600 focus:outline-none focus:ring-4 focus:ring-blue-300 rounded-lg px-3 py-3"
onClick={() => {
copyUrl();
setCopyButtonText("Copied");
setTimeout(() => {
setCopyButtonText("Copy");
}, 1000);
}}
>
{copyButtonText}
</button>
</div>
</main>
);
}