Skip to content

Commit

Permalink
feat: save file and bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Jul 30, 2024
1 parent fe7c17f commit 33aa1fb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-whois-ui",
"version": "0.0.2",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const VERSION = "0.0.2";
export const VERSION = "0.1.0";

export const HISTORY_LIMIT: number = intEnv("NEXT_PUBLIC_HISTORY_LIMIT", 6);
// The maximum number of history items to keep in the local storage
Expand Down
29 changes: 29 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ export function isEnter(e: React.KeyboardEvent) {
return e.key === "Enter" && e.keyCode !== 229;
}

export function saveAsFile(filename: string, content: string) {
/**
* Save text as file
* @param filename Filename
* @param content File content
* @example
* saveAsFile("hello.txt", "Hello world!");
* @see https://developer.mozilla.org/en-US/docs/Web/API/Blob
*/

const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([content]));
a.download = filename;
a.click();
}

async function copyClipboard(text: string) {
if (navigator.clipboard && navigator.clipboard.writeText) {
return await navigator.clipboard.writeText(text);
Expand Down Expand Up @@ -51,6 +67,19 @@ export function useClipboard() {
};
}

export function useSaver() {
return (filename: string, content: string) => {
try {
saveAsFile(filename, content);
toast("Saved!");
} catch (e) {
console.error(e);

toast(`Failed to save: ${toErrorMessage(e)}`);
}
};
}

export function toSearchURI(query: string) {
const q = query.trim();
return q ? `/${encodeURIComponent(q)}` : "/";
Expand Down
16 changes: 16 additions & 0 deletions src/pages/[query].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
toReadableISODate,
toSearchURI,
useClipboard,
useSaver,
} from "@/lib/utils";
import { NextPageContext } from "next";
import { Input } from "@/components/ui/input";
Expand All @@ -14,6 +15,7 @@ import {
CircleX,
CopyIcon,
CornerDownRight,
DownloadIcon,
ExternalLink,
Link2,
Loader2,
Expand Down Expand Up @@ -379,6 +381,7 @@ function ResultTable({ result, target }: ResultTableProps) {

function ResultComp({ data, target }: Props) {
const copy = useClipboard();
const save = useSaver();
const { status, result, error, time } = data;

return (
Expand Down Expand Up @@ -416,10 +419,23 @@ function ResultComp({ data, target }: Props) {
<Button
variant={`outline`}
size={`icon-sm`}
className={`mr-1`}
onClick={() => copy(result?.rawWhoisContent || "")}
>
<CopyIcon className={`w-3 h-3`} />
</Button>
<Button
variant={`outline`}
size={`icon-sm`}
onClick={() =>
save(
`${target.replace(/\./g, "-")}-whois.txt`,
result?.rawWhoisContent || "",
)
}
>
<DownloadIcon className={`w-3 h-3`} />
</Button>
</div>
<TextArea
rows={10}
Expand Down

0 comments on commit 33aa1fb

Please sign in to comment.