From bdca1326f418759fcc71ae945ae779b611a14155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Tue, 28 May 2024 12:24:01 +0000 Subject: [PATCH] wip: nkey component --- internal/utils/strings.go | 14 ++++++ internal/web/adapters/db/accounts.go | 9 +++- internal/web/components/nkeys/nkey.go | 57 ++++++++++++++++++++++ internal/web/controllers/accounts/show.go | 50 +++++++------------ internal/web/controllers/operators/show.go | 33 ++----------- 5 files changed, 101 insertions(+), 62 deletions(-) create mode 100644 internal/utils/strings.go create mode 100644 internal/web/components/nkeys/nkey.go diff --git a/internal/utils/strings.go b/internal/utils/strings.go new file mode 100644 index 00000000..4dee5fa5 --- /dev/null +++ b/internal/utils/strings.go @@ -0,0 +1,14 @@ +package utils + +// FirstN returns the first n characters of a string. +func FirstN(s string, n int) string { + i := 0 + for j := range s { + if i == n { + return s[:j] + } + i++ + } + + return s +} diff --git a/internal/web/adapters/db/accounts.go b/internal/web/adapters/db/accounts.go index 3e43a899..af3176ab 100644 --- a/internal/web/adapters/db/accounts.go +++ b/internal/web/adapters/db/accounts.go @@ -19,7 +19,14 @@ func (db *database) CreateAccount(ctx context.Context, account *models.Account) // GetAccount ... func (db *database) GetAccount(ctx context.Context, account *models.Account) error { - return db.conn.WithContext(ctx).Preload("SigningKeyGroups").Preload("SigningKeyGroups.Key").Preload("Key").Preload("Token").First(account).Error + return db.conn.WithContext(ctx). + Preload("SigningKeyGroups"). + Preload("SigningKeyGroups.Key"). + Preload("Key"). + Preload("Token"). + Preload("Operator"). + Preload("Operator.Key"). + First(account).Error } // UpdateAccount ... diff --git a/internal/web/components/nkeys/nkey.go b/internal/web/components/nkeys/nkey.go new file mode 100644 index 00000000..40d8abc1 --- /dev/null +++ b/internal/web/components/nkeys/nkey.go @@ -0,0 +1,57 @@ +package nkeys + +import ( + htmx "github.com/zeiss/fiber-htmx" + "github.com/zeiss/fiber-htmx/components/icons" + "github.com/zeiss/fiber-htmx/components/tooltips" + "github.com/zeiss/typhoon/internal/utils" +) + +// NKeyProps are the properties of the NKey display component. +type NKeyProps struct { + ClassNames htmx.ClassNames + PublicKey string + Title string +} + +// NKey renders the NKey display component. +func NKey(props NKeyProps, children ...htmx.Node) htmx.Node { + return htmx.Div( + htmx.Merge( + htmx.ClassNames{ + "flex": true, + "flex-col": true, + "py-2": true, + }, + props.ClassNames, + ), + htmx.H4( + htmx.ClassNames{ + "text-neutral-content": true, + "flex": true, + "items-center": true, + }, + htmx.Text(props.Title), + tooltips.Tooltip( + tooltips.TooltipProps{ + ClassNames: htmx.ClassNames{}, + DataTip: "Public NKey is the public key of the operator", + }, + icons.InformationCircleOutline( + icons.IconProps{}, + ), + ), + ), + htmx.H3( + tooltips.Tooltip( + tooltips.TooltipProps{ + ClassNames: htmx.ClassNames{}, + DataTip: props.PublicKey, + }, + htmx.Text( + utils.FirstN(props.PublicKey, 8), + ), + ), + ), + ) +} diff --git a/internal/web/controllers/accounts/show.go b/internal/web/controllers/accounts/show.go index beca5580..7f9901fb 100644 --- a/internal/web/controllers/accounts/show.go +++ b/internal/web/controllers/accounts/show.go @@ -3,18 +3,19 @@ package accounts import ( "fmt" + "github.com/zeiss/typhoon/internal/api/models" + "github.com/zeiss/typhoon/internal/utils" + "github.com/zeiss/typhoon/internal/web/components" + "github.com/zeiss/typhoon/internal/web/components/accounts" + "github.com/zeiss/typhoon/internal/web/components/nkeys" + "github.com/zeiss/typhoon/internal/web/ports" + "github.com/google/uuid" htmx "github.com/zeiss/fiber-htmx" "github.com/zeiss/fiber-htmx/components/buttons" "github.com/zeiss/fiber-htmx/components/cards" "github.com/zeiss/fiber-htmx/components/dropdowns" "github.com/zeiss/fiber-htmx/components/icons" - "github.com/zeiss/fiber-htmx/components/tooltips" - "github.com/zeiss/typhoon/internal/api/models" - "github.com/zeiss/typhoon/internal/utils" - "github.com/zeiss/typhoon/internal/web/components" - "github.com/zeiss/typhoon/internal/web/components/accounts" - "github.com/zeiss/typhoon/internal/web/ports" ) // ShowAccountControllerImpl ... @@ -159,34 +160,17 @@ func (l *ShowAccountControllerImpl) Get() error { cards.TitleProps{}, htmx.Text("Details"), ), - htmx.Div( - htmx.ClassNames{ - "flex": true, - "flex-col": true, - "py-2": true, + nkeys.NKey( + nkeys.NKeyProps{ + Title: "ID", + PublicKey: acc.Key.ID, + }, + ), + nkeys.NKey( + nkeys.NKeyProps{ + Title: "Issuer", + PublicKey: acc.Operator.Key.ID, }, - htmx.H4( - htmx.ClassNames{ - "text-gray-500": true, - }, - htmx.Text("Public NKey"), - tooltips.Tooltip( - tooltips.TooltipProps{ - ClassNames: htmx.ClassNames{ - "tooltip-right": true, - }, - DataTip: "Public NKey is the public key of the account", - }, - icons.InformationCircleOutline( - icons.IconProps{}, - ), - ), - ), - htmx.H3( - htmx.Text( - acc.Key.ID, - ), - ), ), ), ), diff --git a/internal/web/controllers/operators/show.go b/internal/web/controllers/operators/show.go index d978ec97..b20ccb4e 100644 --- a/internal/web/controllers/operators/show.go +++ b/internal/web/controllers/operators/show.go @@ -6,6 +6,7 @@ import ( "github.com/zeiss/typhoon/internal/api/models" "github.com/zeiss/typhoon/internal/utils" "github.com/zeiss/typhoon/internal/web/components" + "github.com/zeiss/typhoon/internal/web/components/nkeys" "github.com/zeiss/typhoon/internal/web/components/operators" "github.com/zeiss/typhoon/internal/web/ports" @@ -15,7 +16,6 @@ import ( "github.com/zeiss/fiber-htmx/components/dropdowns" "github.com/zeiss/fiber-htmx/components/forms" "github.com/zeiss/fiber-htmx/components/icons" - "github.com/zeiss/fiber-htmx/components/tooltips" ) // ShowOperatorControllerImpl ... @@ -210,34 +210,11 @@ func (l *ShowOperatorControllerImpl) Get() error { cards.TitleProps{}, htmx.Text("Details"), ), - htmx.Div( - htmx.ClassNames{ - "flex": true, - "flex-col": true, - "py-2": true, + nkeys.NKey( + nkeys.NKeyProps{ + Title: "ID", + PublicKey: op.Key.ID, }, - htmx.H4( - htmx.ClassNames{ - "text-gray-500": true, - }, - htmx.Text("Public NKey"), - tooltips.Tooltip( - tooltips.TooltipProps{ - ClassNames: htmx.ClassNames{ - "tooltip-right": true, - }, - DataTip: "Public NKey is the public key of the operator", - }, - icons.InformationCircleOutline( - icons.IconProps{}, - ), - ), - ), - htmx.H3( - htmx.Text( - op.Key.ID, - ), - ), ), ), ),