From a9e885598c298d09022db0dc3f909df93e9867a8 Mon Sep 17 00:00:00 2001 From: Jonas Plum Date: Sun, 21 Jul 2024 04:07:23 +0200 Subject: [PATCH] feat: demo flags (#1084) --- app/app.go | 22 ++++++ app/flags.go | 18 +++++ app/flags_test.go | 14 ++++ ui/src/assets/main.css | 63 +++++++-------- ui/src/components/common/UserSelectList.vue | 5 +- .../components/form/JSONSchemaFormFields.vue | 6 +- ui/src/components/form/MultiSelect.vue | 5 +- .../components/reaction/ReactionDisplay.vue | 42 +++++----- ui/src/components/reaction/ReactionForm.vue | 78 +++++++++++++------ ui/src/components/reaction/ReactionList.vue | 8 +- ui/src/components/ticket/TicketList.vue | 5 +- ui/src/components/ticket/TicketNewDialog.vue | 15 ++-- ui/src/components/ticket/file/TicketFiles.vue | 16 ++-- .../ticket/timeline/TicketTimelineItem.vue | 11 ++- ui/src/views/LoginView.vue | 15 ++-- ui/src/views/ReactionView.vue | 2 +- 16 files changed, 217 insertions(+), 108 deletions(-) diff --git a/app/app.go b/app/app.go index 50bcfa3..a52cd09 100644 --- a/app/app.go +++ b/app/app.go @@ -1,10 +1,12 @@ package app import ( + "fmt" "os" "strings" "github.com/pocketbase/pocketbase" + "github.com/pocketbase/pocketbase/core" "github.com/SecurityBrewery/catalyst/migrations" "github.com/SecurityBrewery/catalyst/reaction" @@ -26,6 +28,14 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) { app.OnBeforeServe().Add(addRoutes()) + app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error { + if HasFlag(e.App, "demo") { + bindDemoHooks(e.App) + } + + return nil + }) + // Register additional commands app.RootCmd.AddCommand(fakeDataCmd(app)) app.RootCmd.AddCommand(setFeatureFlagsCmd(app)) @@ -41,6 +51,18 @@ func App(dir string, test bool) (*pocketbase.PocketBase, error) { return app, nil } +func bindDemoHooks(app core.App) { + app.OnRecordBeforeCreateRequest("files", "reactions").Add(func(e *core.RecordCreateEvent) error { + return fmt.Errorf("cannot create %s in demo mode", e.Record.Collection().Name) + }) + app.OnRecordBeforeUpdateRequest("files", "reactions").Add(func(e *core.RecordUpdateEvent) error { + return fmt.Errorf("cannot update %s in demo mode", e.Record.Collection().Name) + }) + app.OnRecordBeforeDeleteRequest("files", "reactions").Add(func(e *core.RecordDeleteEvent) error { + return fmt.Errorf("cannot delete %s in demo mode", e.Record.Collection().Name) + }) +} + func dev() bool { return strings.HasPrefix(os.Args[0], os.TempDir()) } diff --git a/app/flags.go b/app/flags.go index 9a4b749..5fc11d1 100644 --- a/app/flags.go +++ b/app/flags.go @@ -3,6 +3,7 @@ package app import ( "slices" + "github.com/pocketbase/dbx" "github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/models" "github.com/spf13/cobra" @@ -10,6 +11,23 @@ import ( "github.com/SecurityBrewery/catalyst/migrations" ) +func HasFlag(app core.App, flag string) bool { + records, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName, dbx.HashExp{"name": flag}) + if err != nil { + app.Logger().Error(err.Error()) + + return false + } + + for _, r := range records { + if r.GetString("name") == flag { + return true + } + } + + return false +} + func Flags(app core.App) ([]string, error) { records, err := app.Dao().FindRecordsByExpr(migrations.FeatureCollectionName) if err != nil { diff --git a/app/flags_test.go b/app/flags_test.go index c814f90..18d7d74 100644 --- a/app/flags_test.go +++ b/app/flags_test.go @@ -10,6 +10,20 @@ import ( catalystTesting "github.com/SecurityBrewery/catalyst/testing" ) +func TestHasFlag(t *testing.T) { + t.Parallel() + + catalystApp, _, cleanup := catalystTesting.App(t) + defer cleanup() + + // stage 1 + assert.False(t, app.HasFlag(catalystApp, "test")) + + // stage 2 + require.NoError(t, app.SetFlags(catalystApp, []string{"test"})) + assert.True(t, app.HasFlag(catalystApp, "test")) +} + func Test_flags(t *testing.T) { t.Parallel() diff --git a/ui/src/assets/main.css b/ui/src/assets/main.css index 0bd327d..fa8dd8e 100644 --- a/ui/src/assets/main.css +++ b/ui/src/assets/main.css @@ -5,32 +5,32 @@ @layer base { :root { --background: 0 0% 100%; - --foreground: 240 10% 3.9%; /* zinc-950 */ + --foreground: 20 14.3% 4.1%; --card: 0 0% 100%; - --card-foreground: 240 10% 3.9%; + --card-foreground: 20 14.3% 4.1%; --popover: 0 0% 100%; - --popover-foreground: 240 10% 3.9%; + --popover-foreground: 20 14.3% 4.1%; - --primary: 346.8 77.2% 49.8%; - --primary-foreground: 355.7 100% 97.3%; + --primary: 47.9 95.8% 53.1%; + --primary-foreground: 26 83.3% 14.1%; - --secondary: 240 4.8% 95.9%; - --secondary-foreground: 240 5.9% 10%; + --secondary: 60 4.8% 95.9%; + --secondary-foreground: 24 9.8% 10%; - --muted: 240 4.8% 95.9%; - --muted-foreground: 240 3.8% 46.1%; + --muted: 60 4.8% 95.9%; + --muted-foreground: 25 5.3% 44.7%; - --accent: 240 4.8% 95.9%; - --accent-foreground: 240 5.9% 10%; /* zinc-900 */ + --accent: 60 4.8% 95.9%; + --accent-foreground: 24 9.8% 10%; --destructive: 0 84.2% 60.2%; - --destructive-foreground: 0 0% 98%; + --destructive-foreground: 60 9.1% 97.8%; - --border: 240 5.9% 90%; - --input: 240 5.9% 90%; - --ring: 346.8 77.2% 49.8%; + --border: 20 5.9% 90%; + --input: 20 5.9% 90%; + --ring: 20 14.3% 4.1%; --radius: 0.5rem; --vis-tooltip-background-color: none !important; @@ -49,38 +49,39 @@ @media (prefers-color-scheme: dark) { :root { --background: 20 14.3% 4.1%; - --foreground: 0 0% 95%; + --foreground: 60 9.1% 97.8%; - --card: 24 9.8% 10%; - --card-foreground: 0 0% 95%; + --card: 20 14.3% 4.1%; + --card-foreground: 60 9.1% 97.8%; - --popover: 0 0% 9%; - --popover-foreground: 0 0% 95%; + --popover: 20 14.3% 4.1%; + --popover-foreground: 60 9.1% 97.8%; - --primary: 346.8 77.2% 49.8%; - --primary-foreground: 355.7 100% 97.3%; + --primary: 47.9 95.8% 53.1%; + --primary-foreground: 26 83.3% 14.1%; - --secondary: 240 3.7% 15.9%; - --secondary-foreground: 0 0% 98%; + --secondary: 12 6.5% 15.1%; + --secondary-foreground: 60 9.1% 97.8%; - --muted: 0 0% 15%; - --muted-foreground: 240 5% 64.9%; + --muted: 12 6.5% 15.1%; + --muted-foreground: 24 5.4% 63.9%; --accent: 12 6.5% 15.1%; - --accent-foreground: 0 0% 98%; + --accent-foreground: 60 9.1% 97.8%; --destructive: 0 62.8% 30.6%; - --destructive-foreground: 0 85.7% 97.3%; + --destructive-foreground: 60 9.1% 97.8%; - --border: 240 3.7% 15.9%; - --input: 240 3.7% 15.9%; - --ring: 346.8 77.2% 49.8%; + --border: 12 6.5% 15.1%; + --input: 12 6.5% 15.1%; + --ring: 35.5 91.7% 32.9%; } } * { @apply border-border; } + body { @apply bg-background text-foreground; font-feature-settings: diff --git a/ui/src/components/common/UserSelectList.vue b/ui/src/components/common/UserSelectList.vue index 2c2a410..94fbcb5 100644 --- a/ui/src/components/common/UserSelectList.vue +++ b/ui/src/components/common/UserSelectList.vue @@ -41,7 +41,10 @@ const { const searchUserDebounced = debounce(() => refetch(), 300) -watch(searchTerm, () => searchUserDebounced()) +watch( + () => searchTerm.value, + () => searchUserDebounced() +)