From 0cfeaece8694b46b2a46395a12c305d196dd5755 Mon Sep 17 00:00:00 2001 From: Amogh-Bharadwaj Date: Tue, 6 Feb 2024 20:23:41 +0530 Subject: [PATCH] change field to file upload --- ui/app/peers/create/[peerType]/helpers/pg.ts | 19 +++++++-- ui/components/PeerForms/PostgresForm.tsx | 43 +++++++++++++++++++- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/ui/app/peers/create/[peerType]/helpers/pg.ts b/ui/app/peers/create/[peerType]/helpers/pg.ts index 122b92be2e..a6bdd0b956 100644 --- a/ui/app/peers/create/[peerType]/helpers/pg.ts +++ b/ui/app/peers/create/[peerType]/helpers/pg.ts @@ -49,8 +49,18 @@ export const postgresSetting: PeerSetting[] = [ }, ]; -type sshSetter = Dispatch>; -export const sshSetting = [ +export type sshSetter = Dispatch>; +export interface SSHSetting { + label: string; + stateHandler: (value: string, setter: sshSetter) => void; + type?: string; + optional?: boolean; + tips?: string; + helpfulLink?: string; + default?: string | number; +} + +export const sshSetting: SSHSetting[] = [ { label: 'Host', stateHandler: (value: string, setter: sshSetter) => @@ -80,11 +90,12 @@ export const sshSetting = [ tips: 'Password associated with the user you provided.', }, { - label: 'BASE64 Private Key', + label: 'Private Key', stateHandler: (value: string, setter: sshSetter) => setter((curr) => ({ ...curr, privateKey: value })), optional: true, - tips: 'Private key as a BASE64 string for authentication in order to SSH into your machine.', + type: 'file', + tips: 'Private key for authentication in order to SSH into your machine.', }, { label: 'Host Key', diff --git a/ui/components/PeerForms/PostgresForm.tsx b/ui/components/PeerForms/PostgresForm.tsx index 1d76286db0..dda2e4b3d6 100644 --- a/ui/components/PeerForms/PostgresForm.tsx +++ b/ui/components/PeerForms/PostgresForm.tsx @@ -2,7 +2,9 @@ import { PeerSetter } from '@/app/dto/PeersDTO'; import { PeerSetting } from '@/app/peers/create/[peerType]/helpers/common'; import { + SSHSetting, blankSSHConfig, + sshSetter, sshSetting, } from '@/app/peers/create/[peerType]/helpers/pg'; import { SSHConfig } from '@/grpc_generated/peers'; @@ -22,6 +24,27 @@ export default function PostgresForm({ settings, setter }: ConfigProps) { const [showSSH, setShowSSH] = useState(false); const [sshConfig, setSSHConfig] = useState(blankSSHConfig); + const handleFile = ( + file: File, + setFile: (value: string, configSetter: sshSetter) => void + ) => { + if (file) { + const reader = new FileReader(); + reader.readAsText(file); + reader.onload = () => { + const fileContents = reader.result as string; + const base64EncodedContents = Buffer.from( + fileContents, + 'utf-8' + ).toString('base64'); + setFile(base64EncodedContents, setSSHConfig); + }; + reader.onerror = (error) => { + console.log(error); + }; + } + }; + const handleChange = ( e: React.ChangeEvent, setting: PeerSetting @@ -29,6 +52,17 @@ export default function PostgresForm({ settings, setter }: ConfigProps) { setting.stateHandler(e.target.value, setter); }; + const handleSSHParam = ( + e: React.ChangeEvent, + setting: SSHSetting + ) => { + if (setting.type === 'file') { + if (e.target.files) handleFile(e.target.files[0], setting.stateHandler); + } else { + setting.stateHandler(e.target.value, setSSHConfig); + } + }; + useEffect(() => { setter((prev) => { return { @@ -130,12 +164,17 @@ export default function PostgresForm({ settings, setter }: ConfigProps) { ) => - sshParam.stateHandler(e.target.value, setSSHConfig) + handleSSHParam(e, sshParam) + } + style={ + sshParam.type === 'file' + ? { border: 'none', height: 'auto' } + : { border: 'auto' } } type={sshParam.type} defaultValue={ (sshConfig as SSHConfig)[ - sshParam.label === 'BASE64 Private Key' + sshParam.label === 'Private Key' ? 'privateKey' : sshParam.label === 'Host Key' ? 'hostKey'