Skip to content

Commit 33aa1fb

Browse files
committed
feat: save file and bump version
1 parent fe7c17f commit 33aa1fb

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "next-whois-ui",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"private": true,
55
"scripts": {
66
"dev": "next dev",

src/lib/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const VERSION = "0.0.2";
1+
export const VERSION = "0.1.0";
22

33
export const HISTORY_LIMIT: number = intEnv("NEXT_PUBLIC_HISTORY_LIMIT", 6);
44
// The maximum number of history items to keep in the local storage

src/lib/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ export function isEnter(e: React.KeyboardEvent) {
1212
return e.key === "Enter" && e.keyCode !== 229;
1313
}
1414

15+
export function saveAsFile(filename: string, content: string) {
16+
/**
17+
* Save text as file
18+
* @param filename Filename
19+
* @param content File content
20+
* @example
21+
* saveAsFile("hello.txt", "Hello world!");
22+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Blob
23+
*/
24+
25+
const a = document.createElement("a");
26+
a.href = URL.createObjectURL(new Blob([content]));
27+
a.download = filename;
28+
a.click();
29+
}
30+
1531
async function copyClipboard(text: string) {
1632
if (navigator.clipboard && navigator.clipboard.writeText) {
1733
return await navigator.clipboard.writeText(text);
@@ -51,6 +67,19 @@ export function useClipboard() {
5167
};
5268
}
5369

70+
export function useSaver() {
71+
return (filename: string, content: string) => {
72+
try {
73+
saveAsFile(filename, content);
74+
toast("Saved!");
75+
} catch (e) {
76+
console.error(e);
77+
78+
toast(`Failed to save: ${toErrorMessage(e)}`);
79+
}
80+
};
81+
}
82+
5483
export function toSearchURI(query: string) {
5584
const q = query.trim();
5685
return q ? `/${encodeURIComponent(q)}` : "/";

src/pages/[query].tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
toReadableISODate,
66
toSearchURI,
77
useClipboard,
8+
useSaver,
89
} from "@/lib/utils";
910
import { NextPageContext } from "next";
1011
import { Input } from "@/components/ui/input";
@@ -14,6 +15,7 @@ import {
1415
CircleX,
1516
CopyIcon,
1617
CornerDownRight,
18+
DownloadIcon,
1719
ExternalLink,
1820
Link2,
1921
Loader2,
@@ -379,6 +381,7 @@ function ResultTable({ result, target }: ResultTableProps) {
379381

380382
function ResultComp({ data, target }: Props) {
381383
const copy = useClipboard();
384+
const save = useSaver();
382385
const { status, result, error, time } = data;
383386

384387
return (
@@ -416,10 +419,23 @@ function ResultComp({ data, target }: Props) {
416419
<Button
417420
variant={`outline`}
418421
size={`icon-sm`}
422+
className={`mr-1`}
419423
onClick={() => copy(result?.rawWhoisContent || "")}
420424
>
421425
<CopyIcon className={`w-3 h-3`} />
422426
</Button>
427+
<Button
428+
variant={`outline`}
429+
size={`icon-sm`}
430+
onClick={() =>
431+
save(
432+
`${target.replace(/\./g, "-")}-whois.txt`,
433+
result?.rawWhoisContent || "",
434+
)
435+
}
436+
>
437+
<DownloadIcon className={`w-3 h-3`} />
438+
</Button>
423439
</div>
424440
<TextArea
425441
rows={10}

0 commit comments

Comments
 (0)