11"use client" ;
22
33import { Alert , AlertDescription , AlertTitle } from "@/components/ui/alert" ;
4- import { Check , InfoIcon , Pencil , ShieldCheck , ShieldOff , X } from "lucide-react" ;
4+ import { ShieldCheck , ShieldOff } from "lucide-react" ;
55import { useResourceContext } from "@app/hooks/useResourceContext" ;
66import CopyToClipboard from "@app/components/CopyToClipboard" ;
77import {
@@ -14,167 +14,25 @@ import { useTranslations } from "next-intl";
1414import CertificateStatus from "@app/components/private/CertificateStatus" ;
1515import { toUnicode } from "punycode" ;
1616import { useEnvContext } from "@app/hooks/useEnvContext" ;
17- import { Button } from "./ui/button" ;
18- import { Input } from "./ui/input" ;
19- import { createApiClient , formatAxiosError } from "@app/lib/api" ;
20- import { useState } from "react" ;
21- import { useToast } from "@app/hooks/useToast" ;
22- import { UpdateResourceResponse } from "@server/routers/resource" ;
23- import { AxiosResponse } from "axios" ;
24- import { useRouter , usePathname } from "next/navigation" ;
2517
2618type ResourceInfoBoxType = { } ;
2719
2820export default function ResourceInfoBox ( { } : ResourceInfoBoxType ) {
2921 const { resource, authInfo, updateResource } = useResourceContext ( ) ;
3022 const { env } = useEnvContext ( ) ;
31- const api = createApiClient ( useEnvContext ( ) ) ;
32- const router = useRouter ( ) ;
33- const pathname = usePathname ( ) ;
34-
35- const [ isEditing , setIsEditing ] = useState ( false ) ;
36- const [ niceId , setNiceId ] = useState ( resource . niceId ) ;
37- const [ tempNiceId , setTempNiceId ] = useState ( resource . niceId ) ;
38- const [ isLoading , setIsLoading ] = useState ( false ) ;
39- const { toast } = useToast ( ) ;
4023
4124 const t = useTranslations ( ) ;
4225
4326 const fullUrl = `${ resource . ssl ? "https" : "http" } ://${ toUnicode ( resource . fullDomain || "" ) } ` ;
4427
4528
46- const handleEdit = ( ) => {
47- setTempNiceId ( niceId ) ;
48- setIsEditing ( true ) ;
49- } ;
50-
51- const handleCancel = ( ) => {
52- setTempNiceId ( niceId ) ;
53- setIsEditing ( false ) ;
54- } ;
55-
56- const handleSave = async ( ) => {
57- if ( tempNiceId . trim ( ) === "" ) {
58- toast ( {
59- variant : "destructive" ,
60- title : t ( "error" ) ,
61- description : t ( "niceIdCannotBeEmpty" )
62- } ) ;
63- return ;
64- }
65-
66- if ( tempNiceId === niceId ) {
67- setIsEditing ( false ) ;
68- return ;
69- }
70-
71- setIsLoading ( true ) ;
72-
73- try {
74- const res = await api
75- . post < AxiosResponse < UpdateResourceResponse > > (
76- `resource/${ resource ?. resourceId } ` ,
77- {
78- niceId : tempNiceId . trim ( )
79- }
80- ) ;
81-
82- setNiceId ( tempNiceId . trim ( ) ) ;
83- setIsEditing ( false ) ;
84-
85- updateResource ( {
86- niceId : tempNiceId . trim ( )
87- } ) ;
88-
89- // update the URL to reflect the new niceId
90- const newPath = pathname . replace ( `/resources/${ niceId } ` , `/resources/${ tempNiceId . trim ( ) } ` ) ;
91- router . replace ( newPath ) ;
92-
93- toast ( {
94- title : t ( "niceIdUpdated" ) ,
95- description : t ( "niceIdUpdatedSuccessfully" )
96- } ) ;
97- } catch ( e : any ) {
98- toast ( {
99- variant : "destructive" ,
100- title : t ( "niceIdUpdateError" ) ,
101- description : formatAxiosError (
102- e ,
103- t ( "niceIdUpdateErrorDescription" )
104- )
105- } ) ;
106- } finally {
107- setIsLoading ( false ) ;
108- }
109- } ;
110-
111-
112- const handleKeyDown = ( e : React . KeyboardEvent ) => {
113- if ( e . key === "Enter" ) {
114- handleSave ( ) ;
115- } else if ( e . key === "Escape" ) {
116- handleCancel ( ) ;
117- }
118- } ;
119-
12029 return (
12130 < Alert >
12231 < AlertDescription >
12332 { /* 4 cols because of the certs */ }
12433 < InfoSections
125- cols = { resource . http && env . flags . usePangolinDns ? 5 : 4 }
34+ cols = { resource . http && env . flags . usePangolinDns ? 4 : 3 }
12635 >
127- < InfoSection >
128- < InfoSectionTitle >
129- { t ( "niceId" ) }
130- </ InfoSectionTitle >
131- < InfoSectionContent >
132- < div className = "flex items-center gap-2" >
133- { isEditing ? (
134- < >
135- < Input
136- value = { tempNiceId }
137- onChange = { ( e ) => setTempNiceId ( e . target . value ) }
138- onKeyDown = { handleKeyDown }
139- disabled = { isLoading }
140- className = "flex-1"
141- autoFocus
142- />
143- < Button
144- size = "icon"
145- variant = "ghost"
146- onClick = { handleSave }
147- disabled = { isLoading }
148- className = "h-8 w-8"
149- >
150- < Check className = "h-4 w-4" />
151- </ Button >
152- < Button
153- size = "icon"
154- variant = "ghost"
155- onClick = { handleCancel }
156- disabled = { isLoading }
157- className = "h-8 w-8"
158- >
159- < X className = "h-4 w-4" />
160- </ Button >
161- </ >
162- ) : (
163- < >
164- < span > { niceId } </ span >
165- < Button
166- size = "icon"
167- variant = "ghost"
168- onClick = { handleEdit }
169- className = "h-8 w-8"
170- >
171- < Pencil className = "h-4 w-4" />
172- </ Button >
173- </ >
174- ) }
175- </ div >
176- </ InfoSectionContent >
177- </ InfoSection >
17836 { resource . http ? (
17937 < >
18038 < InfoSection >
0 commit comments