File tree Expand file tree Collapse file tree 4 files changed +47
-2
lines changed Expand file tree Collapse file tree 4 files changed +47
-2
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " next-whois-ui" ,
3
- "version" : " 0.0.2 " ,
3
+ "version" : " 0.1.0 " ,
4
4
"private" : true ,
5
5
"scripts" : {
6
6
"dev" : " next dev" ,
Original file line number Diff line number Diff line change 1
- export const VERSION = "0.0.2 " ;
1
+ export const VERSION = "0.1.0 " ;
2
2
3
3
export const HISTORY_LIMIT : number = intEnv ( "NEXT_PUBLIC_HISTORY_LIMIT" , 6 ) ;
4
4
// The maximum number of history items to keep in the local storage
Original file line number Diff line number Diff line change @@ -12,6 +12,22 @@ export function isEnter(e: React.KeyboardEvent) {
12
12
return e . key === "Enter" && e . keyCode !== 229 ;
13
13
}
14
14
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
+
15
31
async function copyClipboard ( text : string ) {
16
32
if ( navigator . clipboard && navigator . clipboard . writeText ) {
17
33
return await navigator . clipboard . writeText ( text ) ;
@@ -51,6 +67,19 @@ export function useClipboard() {
51
67
} ;
52
68
}
53
69
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
+
54
83
export function toSearchURI ( query : string ) {
55
84
const q = query . trim ( ) ;
56
85
return q ? `/${ encodeURIComponent ( q ) } ` : "/" ;
Original file line number Diff line number Diff line change 5
5
toReadableISODate ,
6
6
toSearchURI ,
7
7
useClipboard ,
8
+ useSaver ,
8
9
} from "@/lib/utils" ;
9
10
import { NextPageContext } from "next" ;
10
11
import { Input } from "@/components/ui/input" ;
@@ -14,6 +15,7 @@ import {
14
15
CircleX ,
15
16
CopyIcon ,
16
17
CornerDownRight ,
18
+ DownloadIcon ,
17
19
ExternalLink ,
18
20
Link2 ,
19
21
Loader2 ,
@@ -379,6 +381,7 @@ function ResultTable({ result, target }: ResultTableProps) {
379
381
380
382
function ResultComp ( { data, target } : Props ) {
381
383
const copy = useClipboard ( ) ;
384
+ const save = useSaver ( ) ;
382
385
const { status, result, error, time } = data ;
383
386
384
387
return (
@@ -416,10 +419,23 @@ function ResultComp({ data, target }: Props) {
416
419
< Button
417
420
variant = { `outline` }
418
421
size = { `icon-sm` }
422
+ className = { `mr-1` }
419
423
onClick = { ( ) => copy ( result ?. rawWhoisContent || "" ) }
420
424
>
421
425
< CopyIcon className = { `w-3 h-3` } />
422
426
</ 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 >
423
439
</ div >
424
440
< TextArea
425
441
rows = { 10 }
You can’t perform that action at this time.
0 commit comments