Skip to content

Commit bc1cd91

Browse files
authored
Merge pull request #294 from oasisprotocol/mz/secretTableRefactor
Extract add secret from SecretsTable component
2 parents b7803a8 + 4c24873 commit bc1cd91

5 files changed

Lines changed: 76 additions & 22 deletions

File tree

src/components/SecretsTable/AddSecret.tsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { type FC } from 'react'
2-
import { Button } from '@oasisprotocol/ui-library/src/components/ui/button'
32
import { z } from 'zod'
43
import { zodResolver } from '@hookform/resolvers/zod'
54
import { useForm } from 'react-hook-form'
6-
import { InputFormField } from '../InputFormField'
7-
import { Plus } from 'lucide-react'
5+
import { AddSecretFormContent } from './AddSecretFormContent'
86

97
const formSchema = z.object({
108
name: z.string().min(1, {
@@ -36,21 +34,7 @@ export const AddSecret: FC<AddSecretProps> = ({ handleAddSecret, disabled }) =>
3634

3735
return (
3836
<form onSubmit={form.handleSubmit(onSubmit)} className="flex gap-4 ">
39-
<div className="w-1/2">
40-
<InputFormField control={form.control} name="name" placeholder="Type Name" disabled={disabled} />
41-
</div>
42-
<div className="w-1/2">
43-
<InputFormField
44-
control={form.control}
45-
name="value"
46-
placeholder="Type Value"
47-
type="password"
48-
disabled={disabled}
49-
/>
50-
</div>
51-
<Button variant="secondary" size="icon" type="submit" disabled={disabled}>
52-
<Plus className="h-4 w-4" />
53-
</Button>
37+
<AddSecretFormContent formControl={form.control} disabled={disabled} />
5438
</form>
5539
)
5640
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Button } from '@oasisprotocol/ui-library/src/components/ui/button'
2+
import { InputFormField } from '../InputFormField'
3+
import { Plus } from 'lucide-react'
4+
import { Control, FieldValues, Path } from 'react-hook-form'
5+
6+
type AddSecretFormContentProps<T extends FieldValues> = {
7+
formControl: Control<T>
8+
disabled?: boolean
9+
onClick?: () => void
10+
}
11+
12+
export const AddSecretFormContent = <T extends FieldValues>({
13+
formControl,
14+
disabled,
15+
onClick,
16+
}: AddSecretFormContentProps<T>) => {
17+
return (
18+
<div className="flex gap-4 mt-4 w-full">
19+
<div className="w-1/2">
20+
<InputFormField
21+
control={formControl}
22+
name={'name' as Path<T>}
23+
placeholder="Type Name"
24+
disabled={disabled}
25+
/>
26+
</div>
27+
<div className="w-1/2">
28+
<InputFormField
29+
control={formControl}
30+
name={'value' as Path<T>}
31+
placeholder="Type Value"
32+
type="password"
33+
disabled={disabled}
34+
/>
35+
</div>
36+
<Button
37+
variant="secondary"
38+
size="icon"
39+
type={onClick ? 'button' : 'submit'}
40+
disabled={disabled}
41+
onClick={onClick}
42+
>
43+
<Plus className="h-4 w-4" />
44+
</Button>
45+
</div>
46+
)
47+
}

src/components/SecretsTable/EditSecretDialog.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,20 @@ export const EditSecretDialog: FC<EditSecretDialogProps> = ({
7272
onOpenChange(false)
7373
}
7474

75+
function handleFormSubmit(e: React.FormEvent<HTMLFormElement>) {
76+
e.preventDefault()
77+
e.stopPropagation()
78+
form.handleSubmit(onSubmit)(e)
79+
}
80+
7581
return (
7682
<Dialog open={open} onOpenChange={handleDialogOpenChange}>
7783
<DialogContent className="sm:max-w-[425px]">
7884
<DialogHeader>
7985
<DialogTitle>Edit secret</DialogTitle>
8086
</DialogHeader>
8187
<DialogDescription className="mb-6">Please provide a new secret value.</DialogDescription>
82-
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
88+
<form onSubmit={handleFormSubmit} className="space-y-6">
8389
<InputFormField control={form.control} name="name" label="Name" disabled />
8490

8591
<InputFormField control={form.control} name="value" label="Value" type="password" />

src/components/SecretsTable/index.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import {
2020
DropdownMenuItem,
2121
} from '@oasisprotocol/ui-library/src/components/ui/dropdown-menu'
2222
import { MoreVertical } from 'lucide-react'
23-
import { AddSecret } from './AddSecret'
2423

2524
type SecretsTableProps = {
2625
appSek: RoflApp['sek']
@@ -121,8 +120,6 @@ export const SecretsTable: FC<SecretsTableProps> = ({
121120
</Table>
122121
)}
123122

124-
<AddSecret disabled={!editEnabled} handleAddSecret={handleEditSecret} />
125-
126123
<EditSecretDialog
127124
open={secretDialogState.open}
128125
secret={secretDialogState.secretKey}

src/pages/Dashboard/AppDetails/index.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { useEffect, useState, type FC } from 'react'
2+
import * as oasis from '@oasisprotocol/client'
3+
import * as oasisRT from '@oasisprotocol/client-rt'
24
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@oasisprotocol/ui-library/src/components/ui/tabs'
35
import { AppStatusIcon } from '../../../components/AppStatusIcon'
46
import { AppMetadata } from './AppMetadata'
@@ -16,6 +18,7 @@ import { UnsavedChanges } from './UnsavedChanges'
1618
import { RemoveAppButton } from './RemoveAppButton'
1719
import { Dialog, DialogContent } from '@oasisprotocol/ui-library/src/components/ui/dialog'
1820
import { HelpWidget } from '../../CreateApp/HelpWidget'
21+
import { AddSecret } from '../../../components/SecretsTable/AddSecret'
1922

2023
function setDefaultMetadataViewState(metadata: RoflAppMetadata | undefined = {}): ViewMetadataState {
2124
return {
@@ -66,6 +69,22 @@ export const AppDetails: FC = () => {
6669

6770
const showBlockingOverlay = removeApp.isPending || updateApp.isPending
6871

72+
const handleAddSecret = (key: string, value: string) => {
73+
if (!roflApp?.sek) return
74+
75+
const secretValue = oasisRT.rofl.encryptSecret(
76+
key,
77+
oasis.misc.fromString(value),
78+
oasis.misc.fromBase64(roflApp.sek),
79+
)
80+
81+
const updatedSecrets = { ...viewSecretsState.secrets, [key]: secretValue }
82+
setViewSecretsState({
83+
isDirty: true,
84+
secrets: updatedSecrets,
85+
})
86+
}
87+
6988
useEffect(() => {
7089
if (roflApp) {
7190
setViewMetadataState({
@@ -183,6 +202,7 @@ export const AppDetails: FC = () => {
183202
setViewSecretsState={setViewSecretsState}
184203
editEnabled={editEnabled}
185204
/>
205+
<AddSecret disabled={!editEnabled} handleAddSecret={handleAddSecret} />
186206
</TabsContent>
187207
<TabsContent value="compose">
188208
<AppArtifacts

0 commit comments

Comments
 (0)