feat(T02-T11): IHF Phase 1 schema, controllers, views, and helpers

- Schema: hubs, widgets, widget_versions, interaction_events (append-only
  trigger), annotations, users — single migration file
- Web layer: Types, Routes, FrontController with auth + AutoRefresh layout
- Controllers: Hubs (CRUD), Widgets (CRUD + versioning), InteractionEvents
  (JSON capture, canonical event_type validation), Annotations (threaded,
  append-only)
- Sessions controller for IHP auth
- Views: Hubs (index/show/new/edit), Widgets (index/show/new/edit),
  Annotations (index/new), Sessions (login)
- widgetEnvelope helper with full data-* governance attributes
- Integration tests: Hub CRUD, Widget versioning, event capture, append-only
  guard, annotation threading, validation

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-27 01:42:43 +00:00
parent ff11913d5c
commit c560e541c7
26 changed files with 1591 additions and 12 deletions

View File

@@ -0,0 +1,48 @@
module Web.Controller.Annotations where
import Web.Types
import Web.View.Annotations.Index
import Web.View.Annotations.New
import Generated.Types
import IHP.Prelude
import IHP.ControllerPrelude
validCategories :: [Text]
validCategories = ["friction", "defect", "wish", "policy_concern", "doc_gap", "trust", "other"]
instance Controller AnnotationsController where
beforeAction = ensureIsUser
action WidgetAnnotationsAction { widgetId } = do
widget <- fetch widgetId
annotations <- query @Annotation
|> filterWhere (#widgetId, widgetId)
|> orderByAsc #createdAt
|> fetch
render IndexView { widget, annotations }
action NewAnnotationAction { widgetId } = do
widget <- fetch widgetId
let annotation = newRecord @Annotation
render NewView { widget, annotation }
action CreateAnnotationAction { widgetId } = do
widget <- fetch widgetId
mUser <- currentUserOrNothing
let actorId = fmap (.id) mUser
actorType = maybe "anonymous" (const "user") mUser
let annotation = newRecord @Annotation
annotation
|> fill @'["body", "category", "parentId", "widgetStateRef"]
|> set #widgetId widgetId
|> set #actorId (fmap (Id . unId) actorId)
|> set #actorType actorType
|> validateField #body nonEmpty
|> validateField #category (`elem` validCategories)
|> ifValid \case
Left annotation -> render NewView { widget, annotation }
Right annotation -> do
createRecord annotation
setSuccessMessage "Annotation added"
redirectTo WidgetAnnotationsAction { widgetId }

74
Web/Controller/Hubs.hs Normal file
View File

@@ -0,0 +1,74 @@
module Web.Controller.Hubs where
import Web.Types
import Web.View.Hubs.Index
import Web.View.Hubs.Show
import Web.View.Hubs.New
import Web.View.Hubs.Edit
import Generated.Types
import IHP.Prelude
import IHP.ControllerPrelude
instance Controller HubsController where
beforeAction = ensureIsUser
action HubsAction = do
hubs <- query @Hub |> orderByAsc #createdAt |> fetch
render IndexView { hubs }
action NewHubAction = do
let hub = newRecord @Hub
render NewView { hub }
action ShowHubAction { hubId } = autoRefresh do
hub <- fetch hubId
widgets <- query @Widget
|> filterWhere (#hubId, hubId)
|> orderByAsc #name
|> fetch
widgetIds <- pure (map (.id) widgets)
recentEvents <- sqlQuery
"SELECT * FROM interaction_events WHERE widget_id = ANY(?) ORDER BY occurred_at DESC LIMIT 50"
(Only (PGArray widgetIds))
recentAnnotations <- sqlQuery
"SELECT * FROM annotations WHERE widget_id = ANY(?) ORDER BY created_at DESC LIMIT 20"
(Only (PGArray widgetIds))
render ShowView { hub, widgets, recentEvents, recentAnnotations }
action CreateHubAction = do
let hub = newRecord @Hub
hub
|> fill @'["slug", "name", "domain"]
|> validateField #slug nonEmpty
|> validateField #name nonEmpty
|> validateField #domain nonEmpty
|> ifValid \case
Left hub -> render NewView { hub }
Right hub -> do
hub <- createRecord hub
setSuccessMessage "Hub created"
redirectTo ShowHubAction { hubId = hub.id }
action EditHubAction { hubId } = do
hub <- fetch hubId
render EditView { hub }
action UpdateHubAction { hubId } = do
hub <- fetch hubId
hub
|> fill @'["slug", "name", "domain"]
|> validateField #slug nonEmpty
|> validateField #name nonEmpty
|> validateField #domain nonEmpty
|> ifValid \case
Left hub -> render EditView { hub }
Right hub -> do
updateRecord hub
setSuccessMessage "Hub updated"
redirectTo ShowHubAction { hubId = hub.id }
action DeleteHubAction { hubId } = do
hub <- fetch hubId
deleteRecord hub
setSuccessMessage "Hub deleted"
redirectTo HubsAction

View File

@@ -0,0 +1,55 @@
module Web.Controller.InteractionEvents where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ControllerPrelude
import Data.Aeson (object, (.=))
import qualified Data.Text as T
-- Valid canonical event types
validEventTypes :: [Text]
validEventTypes =
[ "viewed", "clicked", "submitted", "abandoned", "retried", "failed"
, "commented", "flagged_confusing", "flagged_helpful"
, "blocked_by_policy", "escalated"
, "accepted_recommendation", "rejected_recommendation"
]
instance Controller InteractionEventsController where
action CreateInteractionEventAction { widgetId } = do
eventType <- param @Text "event_type"
unless (eventType `elem` validEventTypes) do
respondJson (object ["error" .= ("unknown event_type" :: Text), "valid" .= validEventTypes])
-- IHP stops here; the above respondJson sends 200 but we need 422
-- Use renderWithStatus for proper 422:
setStatus 422
respondJson (object ["error" .= ("unknown event_type" :: Text)])
mUser <- currentUserOrNothing
let actorId = fmap (.id) mUser
actorType = maybe "anonymous" (const "user") mUser
actorTypeParam <- paramOrDefault @Text actorType "actor_type"
viewContextRef <- paramOrNothing @Text "view_context_ref"
metadataRaw <- paramOrDefault @Text "{}" "metadata"
let metadata = case readMay @Value (cs metadataRaw) of
Just v -> v
Nothing -> object []
event <- newRecord @InteractionEvent
|> set #widgetId widgetId
|> set #eventType eventType
|> set #actorId (fmap (Id . unId) actorId)
|> set #actorType actorTypeParam
|> set #viewContextRef viewContextRef
|> set #metadata metadata
|> createRecord
respondJson (object
[ "id" .= event.id
, "widget_id" .= event.widgetId
, "event_type" .= event.eventType
, "occurred_at".= event.occurredAt
])

View File

@@ -0,0 +1,29 @@
module Web.Controller.Sessions where
import Web.Types
import Web.View.Sessions.New
import Generated.Types
import IHP.LoginSupport.Helper.Controller
import IHP.Prelude
import IHP.ControllerPrelude
instance Controller SessionsController where
action NewSessionAction = do
let user = newRecord @User
render NewView { user }
action CreateSessionAction = do
(user, token) <- authenticate @User
case user of
Just user -> do
setSession "userId" (show user.id)
redirectTo HubsAction
Nothing -> do
setErrorMessage "Invalid email or password"
redirectTo NewSessionAction
action DeleteSessionAction = do
unsetSession "userId"
redirectTo NewSessionAction
instance SessionsControllerConfig User

106
Web/Controller/Widgets.hs Normal file
View File

@@ -0,0 +1,106 @@
module Web.Controller.Widgets where
import Web.Types
import Web.View.Widgets.Index
import Web.View.Widgets.Show
import Web.View.Widgets.New
import Web.View.Widgets.Edit
import Generated.Types
import IHP.Prelude
import IHP.ControllerPrelude
import Data.Aeson (toJSON, object, (.=))
instance Controller WidgetsController where
beforeAction = ensureIsUser
action WidgetsAction = do
widgets <- query @Widget |> orderByAsc #name |> fetch
hubs <- query @Hub |> fetch
render IndexView { widgets, hubs }
action NewWidgetAction = do
let widget = newRecord @Widget
hubs <- query @Hub |> fetch
render NewView { widget, hubs }
action ShowWidgetAction { widgetId } = do
widget <- fetch widgetId
hub <- fetch widget.hubId
versions <- query @WidgetVersion
|> filterWhere (#widgetId, widgetId)
|> orderByDesc #version
|> fetch
events <- query @InteractionEvent
|> filterWhere (#widgetId, widgetId)
|> orderByDesc #occurredAt
|> limit 20
|> fetch
annotations <- query @Annotation
|> filterWhere (#widgetId, widgetId)
|> orderByAsc #createdAt
|> fetch
render ShowView { widget, hub, versions, events, annotations }
action CreateWidgetAction = do
let widget = newRecord @Widget
hubs <- query @Hub |> fetch
widget
|> fill @'["name", "widgetType", "hubId", "capabilityRef", "viewContext", "policyScope", "status"]
|> validateField #name nonEmpty
|> validateField #widgetType nonEmpty
|> ifValid \case
Left widget -> render NewView { widget, hubs }
Right widget -> do
widget <- createRecord widget
let snapshot = object
[ "name" .= widget.name
, "widget_type" .= widget.widgetType
, "hub_id" .= widget.hubId
, "capability_ref" .= widget.capabilityRef
, "view_context" .= widget.viewContext
, "policy_scope" .= widget.policyScope
, "status" .= widget.status
, "version" .= widget.version
]
newRecord @WidgetVersion
|> set #widgetId widget.id
|> set #version 1
|> set #schemaSnapshot snapshot
|> createRecord
setSuccessMessage "Widget registered"
redirectTo ShowWidgetAction { widgetId = widget.id }
action EditWidgetAction { widgetId } = do
widget <- fetch widgetId
hubs <- query @Hub |> fetch
render EditView { widget, hubs }
action UpdateWidgetAction { widgetId } = do
widget <- fetch widgetId
hubs <- query @Hub |> fetch
widget
|> fill @'["name", "widgetType", "hubId", "capabilityRef", "viewContext", "policyScope", "status"]
|> validateField #name nonEmpty
|> validateField #widgetType nonEmpty
|> ifValid \case
Left widget -> render EditView { widget, hubs }
Right widget -> do
let newVersion = widget.version + 1
widget <- widget |> set #version newVersion |> updateRecord
let snapshot = object
[ "name" .= widget.name
, "widget_type" .= widget.widgetType
, "hub_id" .= widget.hubId
, "capability_ref" .= widget.capabilityRef
, "view_context" .= widget.viewContext
, "policy_scope" .= widget.policyScope
, "status" .= widget.status
, "version" .= newVersion
]
newRecord @WidgetVersion
|> set #widgetId widget.id
|> set #version newVersion
|> set #schemaSnapshot snapshot
|> createRecord
setSuccessMessage "Widget updated"
redirectTo ShowWidgetAction { widgetId = widget.id }

57
Web/FrontController.hs Normal file
View File

@@ -0,0 +1,57 @@
module Web.FrontController where
import IHP.RouterPrelude
import IHP.LoginSupport.Middleware
import Generated.Types
import Web.Types
import Web.Routes ()
-- Controllers
import Web.Controller.Hubs ()
import Web.Controller.Widgets ()
import Web.Controller.InteractionEvents ()
import Web.Controller.Annotations ()
import Web.Controller.Sessions ()
instance FrontController WebApplication where
controllers =
[ parseRoute @SessionsController
, parseRoute @HubsController
, parseRoute @WidgetsController
, parseRoute @InteractionEventsController
, parseRoute @AnnotationsController
]
instance InitControllerContext WebApplication where
initContext = do
setLayout defaultLayout
initAuthentication @User
defaultLayout :: Layout
defaultLayout inner = [hsx|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>inter-hub</title>
{autoRefreshMeta}
<link rel="stylesheet" href="/app.css" />
<script src="/vendor/morphdom.js"></script>
<script src="/vendor/ihp-auto-refresh.js"></script>
</head>
<body class="bg-gray-50 text-gray-900">
<nav class="bg-white border-b border-gray-200 px-6 py-3 flex items-center gap-6">
<a href={HubsAction} class="font-semibold text-indigo-600">inter-hub</a>
<a href={HubsAction} class="text-sm text-gray-600 hover:text-gray-900">Hubs</a>
<a href={WidgetsAction} class="text-sm text-gray-600 hover:text-gray-900">Widgets</a>
<div class="ml-auto">
<a href={DeleteSessionAction} class="text-sm text-gray-500 hover:text-gray-700">Sign out</a>
</div>
</nav>
<main class="max-w-5xl mx-auto px-6 py-8">
{inner}
</main>
</body>
</html>
|]

20
Web/Routes.hs Normal file
View File

@@ -0,0 +1,20 @@
module Web.Routes where
import IHP.RouterPrelude
import Generated.Types
import Web.Types
-- Hubs
instance AutoRoute HubsController
-- Widgets
instance AutoRoute WidgetsController
-- Interaction Events (POST /widgets/:widgetId/events)
instance AutoRoute InteractionEventsController
-- Annotations (scoped to widget: /widgets/:widgetId/annotations/)
instance AutoRoute AnnotationsController
-- Sessions
instance AutoRoute SessionsController

51
Web/Types.hs Normal file
View File

@@ -0,0 +1,51 @@
module Web.Types where
import IHP.Prelude
import IHP.ModelSupport
import IHP.LoginSupport.Types
import Generated.Types
-- | Authentication type alias
type CurrentUserRecord = User
instance HasNewSessionUrl User where
newSessionUrl _ = "/NewSession"
-- Controllers
data WebApplication = WebApplication deriving (Eq, Show)
data HubsController
= HubsAction
| NewHubAction
| ShowHubAction { hubId :: !(Id Hub) }
| CreateHubAction
| EditHubAction { hubId :: !(Id Hub) }
| UpdateHubAction { hubId :: !(Id Hub) }
| DeleteHubAction { hubId :: !(Id Hub) }
deriving (Eq, Show, Data)
data WidgetsController
= WidgetsAction
| NewWidgetAction
| ShowWidgetAction { widgetId :: !(Id Widget) }
| CreateWidgetAction
| EditWidgetAction { widgetId :: !(Id Widget) }
| UpdateWidgetAction { widgetId :: !(Id Widget) }
deriving (Eq, Show, Data)
data InteractionEventsController
= CreateInteractionEventAction { widgetId :: !(Id Widget) }
deriving (Eq, Show, Data)
data AnnotationsController
= WidgetAnnotationsAction { widgetId :: !(Id Widget) }
| NewAnnotationAction { widgetId :: !(Id Widget) }
| CreateAnnotationAction { widgetId :: !(Id Widget) }
deriving (Eq, Show, Data)
data SessionsController
= NewSessionAction
| CreateSessionAction
| DeleteSessionAction
deriving (Eq, Show, Data)

View File

@@ -0,0 +1,61 @@
module Web.View.Annotations.Index where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data IndexView = IndexView
{ widget :: !Widget
, annotations :: ![Annotation]
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="mb-6 flex items-center gap-2 text-sm text-gray-500">
<a href={WidgetsAction} class="hover:text-gray-700">Widgets</a>
<span>/</span>
<a href={ShowWidgetAction { widgetId = widget.id }} class="hover:text-gray-700">{widget.name}</a>
<span>/</span>
<span>Annotations</span>
</div>
<div class="flex items-center justify-between mb-4">
<h1 class="text-2xl font-semibold">Annotations for {widget.name}</h1>
<a href={NewAnnotationAction { widgetId = widget.id }}
class="bg-indigo-600 text-white text-sm font-medium px-4 py-2 rounded hover:bg-indigo-700">
Add Annotation
</a>
</div>
<div class="space-y-3">
{forEach rootAnnotations (renderAnnotation childrenOf)}
</div>
|]
where
rootAnnotations = filter (\a -> isNothing a.parentId) annotations
childrenOf parent = filter (\a -> a.parentId == Just parent.id) annotations
renderAnnotation :: (Annotation -> [Annotation]) -> Annotation -> Html
renderAnnotation childrenOf a = [hsx|
<div class="bg-white rounded-lg border border-gray-200 px-4 py-3">
<div class="flex items-center gap-2 mb-2">
<span class="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded font-medium">
{a.category}
</span>
<span class="text-xs text-gray-400">{a.actorType}</span>
{if isJust a.retractedAt
then [hsx|<span class="text-xs text-red-400 italic">retracted</span>|]
else mempty}
<span class="ml-auto text-xs text-gray-300">{show a.createdAt}</span>
</div>
<p class="text-sm text-gray-700">{a.body}</p>
<div class="mt-2 flex gap-2">
<a href={NewAnnotationAction { widgetId = a.widgetId }}
class="text-xs text-indigo-500 hover:text-indigo-700">Reply</a>
</div>
<div class="ml-6 mt-3 space-y-3">
{forEach (childrenOf a) (renderAnnotation childrenOf)}
</div>
</div>
|]

View File

@@ -0,0 +1,44 @@
module Web.View.Annotations.New where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data NewView = NewView
{ widget :: !Widget
, annotation :: !Annotation
}
instance View NewView where
html NewView { .. } = [hsx|
<div class="max-w-lg">
<div class="flex items-center gap-2 text-sm text-gray-500 mb-4">
<a href={ShowWidgetAction { widgetId = widget.id }} class="hover:text-gray-700">{widget.name}</a>
<span>/</span>
<a href={WidgetAnnotationsAction { widgetId = widget.id }} class="hover:text-gray-700">Annotations</a>
<span>/</span>
<span>New</span>
</div>
<h1 class="text-2xl font-semibold mb-6">Add Annotation</h1>
{renderForm annotation widget.id}
</div>
|]
renderForm :: Annotation -> Id Widget -> Html
renderForm annotation widgetId = formFor annotation [hsx|
{(textareaField #body) { fieldLabel = "Comment" }}
{selectField #category categoryOptions}
{submitButton}
|]
categoryOptions :: [(Text, Text)]
categoryOptions =
[ ("Friction", "friction")
, ("Defect", "defect")
, ("Wish", "wish")
, ("Policy Concern", "policy_concern")
, ("Documentation Gap", "doc_gap")
, ("Trust", "trust")
, ("Other", "other")
]

31
Web/View/Hubs/Edit.hs Normal file
View File

@@ -0,0 +1,31 @@
module Web.View.Hubs.Edit where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data EditView = EditView { hub :: !Hub }
instance View EditView where
html EditView { .. } = [hsx|
<div class="max-w-lg">
<div class="flex items-center gap-2 text-sm text-gray-500 mb-4">
<a href={HubsAction} class="hover:text-gray-700">Hubs</a>
<span>/</span>
<a href={ShowHubAction { hubId = hub.id }} class="hover:text-gray-700">{hub.name}</a>
<span>/</span>
<span>Edit</span>
</div>
<h1 class="text-2xl font-semibold mb-6">Edit Hub</h1>
{renderForm hub}
</div>
|]
renderForm :: Hub -> Html
renderForm hub = formFor hub [hsx|
{textField #name}
{(textField #slug) { helpText = "Lowercase, URL-safe identifier" }}
{textField #domain}
{submitButton}
|]

56
Web/View/Hubs/Index.hs Normal file
View File

@@ -0,0 +1,56 @@
module Web.View.Hubs.Index where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data IndexView = IndexView { hubs :: ![Hub] }
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="flex items-center justify-between mb-6">
<h1 class="text-2xl font-semibold">Hubs</h1>
<a href={NewHubAction}
class="bg-indigo-600 text-white text-sm font-medium px-4 py-2 rounded
hover:bg-indigo-700">
New Hub
</a>
</div>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Name</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Slug</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Domain</th>
<th class="px-4 py-3"></th>
</tr>
</thead>
<tbody>
{forEach hubs renderHub}
</tbody>
</table>
</div>
|]
renderHub :: Hub -> Html
renderHub hub = [hsx|
<tr class="border-b border-gray-100 hover:bg-gray-50">
<td class="px-4 py-3">
<a href={ShowHubAction { hubId = hub.id }}
class="font-medium text-indigo-600 hover:text-indigo-800">
{hub.name}
</a>
</td>
<td class="px-4 py-3 text-gray-500 font-mono text-xs">{hub.slug}</td>
<td class="px-4 py-3 text-gray-500">{hub.domain}</td>
<td class="px-4 py-3 text-right">
<a href={EditHubAction { hubId = hub.id }}
class="text-gray-500 hover:text-gray-700 text-xs mr-3">Edit</a>
<a href={DeleteHubAction { hubId = hub.id }}
class="text-red-500 hover:text-red-700 text-xs"
data-confirm="Delete this hub?">Delete</a>
</td>
</tr>
|]

24
Web/View/Hubs/New.hs Normal file
View File

@@ -0,0 +1,24 @@
module Web.View.Hubs.New where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data NewView = NewView { hub :: !Hub }
instance View NewView where
html NewView { .. } = [hsx|
<div class="max-w-lg">
<h1 class="text-2xl font-semibold mb-6">New Hub</h1>
{renderForm hub}
</div>
|]
renderForm :: Hub -> Html
renderForm hub = formFor hub [hsx|
{textField #name}
{(textField #slug) { helpText = "Lowercase, URL-safe identifier" }}
{textField #domain}
{submitButton}
|]

141
Web/View/Hubs/Show.hs Normal file
View File

@@ -0,0 +1,141 @@
module Web.View.Hubs.Show where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data ShowView = ShowView
{ hub :: !Hub
, widgets :: ![Widget]
, recentEvents :: ![InteractionEvent]
, recentAnnotations :: ![Annotation]
}
instance View ShowView where
html ShowView { .. } = [hsx|
<div class="mb-6">
<div class="flex items-center gap-2 text-sm text-gray-500 mb-2">
<a href={HubsAction} class="hover:text-gray-700">Hubs</a>
<span>/</span>
<span>{hub.name}</span>
</div>
<div class="flex items-center justify-between">
<div>
<h1 class="text-2xl font-semibold">{hub.name}</h1>
<p class="text-sm text-gray-500 mt-1">
<span class="font-mono bg-gray-100 px-1 rounded">{hub.slug}</span>
<span class="ml-2">{hub.domain}</span>
</p>
</div>
<div class="flex gap-2">
<a href={EditHubAction { hubId = hub.id }}
class="text-sm border border-gray-300 px-3 py-1.5 rounded hover:bg-gray-50">
Edit
</a>
<a href={NewWidgetAction}
class="text-sm bg-indigo-600 text-white px-3 py-1.5 rounded hover:bg-indigo-700">
New Widget
</a>
</div>
</div>
</div>
<div class="grid grid-cols-3 gap-4 mb-8">
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Widgets</p>
<p class="text-3xl font-semibold mt-1">{length widgets}</p>
</div>
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Recent Events</p>
<p class="text-3xl font-semibold mt-1">{length recentEvents}</p>
</div>
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Recent Annotations</p>
<p class="text-3xl font-semibold mt-1">{length recentAnnotations}</p>
</div>
</div>
<section class="mb-8">
<h2 class="text-lg font-medium mb-3">Widgets</h2>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Name</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Type</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Status</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Version</th>
</tr>
</thead>
<tbody>
{forEach widgets renderWidgetRow}
</tbody>
</table>
</div>
</section>
<section class="mb-8">
<h2 class="text-lg font-medium mb-3">Recent Interaction Events</h2>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Event</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Actor</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Occurred</th>
</tr>
</thead>
<tbody>
{forEach recentEvents renderEventRow}
</tbody>
</table>
</div>
</section>
<section>
<h2 class="text-lg font-medium mb-3">Recent Annotations</h2>
<div class="space-y-2">
{forEach recentAnnotations renderAnnotationCard}
</div>
</section>
|]
renderWidgetRow :: Widget -> Html
renderWidgetRow w = [hsx|
<tr class="border-b border-gray-100 hover:bg-gray-50">
<td class="px-4 py-3">
<a href={ShowWidgetAction { widgetId = w.id }}
class="font-medium text-indigo-600 hover:text-indigo-800">
{w.name}
</a>
</td>
<td class="px-4 py-3 text-gray-500">{w.widgetType}</td>
<td class="px-4 py-3">
<span class="inline-block px-2 py-0.5 rounded text-xs bg-green-100 text-green-800">
{w.status}
</span>
</td>
<td class="px-4 py-3 text-gray-500">v{show w.version}</td>
</tr>
|]
renderEventRow :: InteractionEvent -> Html
renderEventRow e = [hsx|
<tr class="border-b border-gray-100">
<td class="px-4 py-3 font-mono text-xs">{e.eventType}</td>
<td class="px-4 py-3 text-gray-500 text-xs">{e.actorType}</td>
<td class="px-4 py-3 text-gray-400 text-xs">{show e.occurredAt}</td>
</tr>
|]
renderAnnotationCard :: Annotation -> Html
renderAnnotationCard a = [hsx|
<div class="bg-white rounded border border-gray-200 px-4 py-3">
<div class="flex items-center gap-2 mb-1">
<span class="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded">{a.category}</span>
<span class="text-xs text-gray-400">{a.actorType}</span>
</div>
<p class="text-sm text-gray-700">{a.body}</p>
</div>
|]

40
Web/View/Sessions/New.hs Normal file
View File

@@ -0,0 +1,40 @@
module Web.View.Sessions.New where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data NewView = NewView { user :: !User }
instance View NewView where
html NewView { .. } = [hsx|
<div class="max-w-sm mx-auto mt-16">
<h1 class="text-2xl font-semibold mb-6">Sign in to inter-hub</h1>
<form method="POST" action={CreateSessionAction} class="space-y-4">
{forEach (getFlashMessages) renderFlash}
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
<input type="email" name="email" required
class="w-full border border-gray-300 rounded px-3 py-2 text-sm
focus:outline-none focus:ring-2 focus:ring-indigo-500" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Password</label>
<input type="password" name="password" required
class="w-full border border-gray-300 rounded px-3 py-2 text-sm
focus:outline-none focus:ring-2 focus:ring-indigo-500" />
</div>
<button type="submit"
class="w-full bg-indigo-600 text-white rounded px-4 py-2 text-sm font-medium
hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-indigo-500">
Sign in
</button>
</form>
</div>
|]
renderFlash :: Text -> Html
renderFlash msg = [hsx|
<div class="bg-red-50 border border-red-200 text-red-700 rounded px-3 py-2 text-sm">{msg}</div>
|]

27
Web/View/Widgets/Edit.hs Normal file
View File

@@ -0,0 +1,27 @@
module Web.View.Widgets.Edit where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
import Web.View.Widgets.New (renderForm)
data EditView = EditView
{ widget :: !Widget
, hubs :: ![Hub]
}
instance View EditView where
html EditView { .. } = [hsx|
<div class="max-w-lg">
<div class="flex items-center gap-2 text-sm text-gray-500 mb-4">
<a href={WidgetsAction} class="hover:text-gray-700">Widgets</a>
<span>/</span>
<a href={ShowWidgetAction { widgetId = widget.id }} class="hover:text-gray-700">{widget.name}</a>
<span>/</span>
<span>Edit</span>
</div>
<h1 class="text-2xl font-semibold mb-6">Edit Widget</h1>
{renderForm widget hubs}
</div>
|]

69
Web/View/Widgets/Index.hs Normal file
View File

@@ -0,0 +1,69 @@
module Web.View.Widgets.Index where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data IndexView = IndexView
{ widgets :: ![Widget]
, hubs :: ![Hub]
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="flex items-center justify-between mb-6">
<h1 class="text-2xl font-semibold">Widgets</h1>
<a href={NewWidgetAction}
class="bg-indigo-600 text-white text-sm font-medium px-4 py-2 rounded hover:bg-indigo-700">
Register Widget
</a>
</div>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Name</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Hub</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Type</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Status</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Version</th>
<th class="px-4 py-3"></th>
</tr>
</thead>
<tbody>
{forEach widgets (renderWidget hubs)}
</tbody>
</table>
</div>
|]
renderWidget :: [Hub] -> Widget -> Html
renderWidget hubs w = [hsx|
<tr class="border-b border-gray-100 hover:bg-gray-50">
<td class="px-4 py-3">
<a href={ShowWidgetAction { widgetId = w.id }}
class="font-medium text-indigo-600 hover:text-indigo-800">
{w.name}
</a>
</td>
<td class="px-4 py-3 text-gray-500">{hubName hubs w.hubId}</td>
<td class="px-4 py-3 text-gray-500">{w.widgetType}</td>
<td class="px-4 py-3">
<span class="inline-block px-2 py-0.5 rounded text-xs bg-green-100 text-green-800">
{w.status}
</span>
</td>
<td class="px-4 py-3 text-gray-500 text-xs">v{show w.version}</td>
<td class="px-4 py-3 text-right">
<a href={EditWidgetAction { widgetId = w.id }}
class="text-gray-500 hover:text-gray-700 text-xs">Edit</a>
</td>
</tr>
|]
hubName :: [Hub] -> Id Hub -> Text
hubName hubs hubId =
case find (\h -> h.id == hubId) hubs of
Just h -> h.name
Nothing -> ""

59
Web/View/Widgets/New.hs Normal file
View File

@@ -0,0 +1,59 @@
module Web.View.Widgets.New where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data NewView = NewView
{ widget :: !Widget
, hubs :: ![Hub]
}
instance View NewView where
html NewView { .. } = [hsx|
<div class="max-w-lg">
<h1 class="text-2xl font-semibold mb-6">Register Widget</h1>
{renderForm widget hubs}
</div>
|]
renderForm :: Widget -> [Hub] -> Html
renderForm widget hubs = formFor widget [hsx|
{textField #name}
{selectField #widgetType widgetTypeOptions}
{selectField #hubId (hubOptions hubs)}
{textField #capabilityRef}
{textField #viewContext}
{selectField #policyScope policyScopeOptions}
{selectField #status statusOptions}
{submitButton}
|]
hubOptions :: [Hub] -> [(Text, Id Hub)]
hubOptions hubs = map (\h -> (h.name, h.id)) hubs
widgetTypeOptions :: [(Text, Text)]
widgetTypeOptions =
[ ("Chart", "chart")
, ("Form", "form")
, ("Table", "table")
, ("Action", "action")
, ("Panel", "panel")
, ("Navigation", "nav")
, ("Other", "other")
]
policyScopeOptions :: [(Text, Text)]
policyScopeOptions =
[ ("Internal", "internal")
, ("Hub", "hub")
, ("Public", "public")
]
statusOptions :: [(Text, Text)]
statusOptions =
[ ("Active", "active")
, ("Deprecated", "deprecated")
, ("Draft", "draft")
]

162
Web/View/Widgets/Show.hs Normal file
View File

@@ -0,0 +1,162 @@
module Web.View.Widgets.Show where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
import Application.Helper.View (widgetEnvelope)
data ShowView = ShowView
{ widget :: !Widget
, hub :: !Hub
, versions :: ![WidgetVersion]
, events :: ![InteractionEvent]
, annotations :: ![Annotation]
}
instance View ShowView where
html ShowView { .. } = [hsx|
<div class="mb-2 flex items-center gap-2 text-sm text-gray-500">
<a href={HubsAction} class="hover:text-gray-700">Hubs</a>
<span>/</span>
<a href={ShowHubAction { hubId = hub.id }} class="hover:text-gray-700">{hub.name}</a>
<span>/</span>
<span>{widget.name}</span>
</div>
{widgetEnvelope widget [hsx|
<div class="flex items-center justify-between mb-4">
<div>
<h1 class="text-2xl font-semibold">{widget.name}</h1>
<p class="text-sm text-gray-500 mt-0.5">
{widget.widgetType}
<span class="ml-2 text-xs bg-gray-100 px-1.5 py-0.5 rounded">{widget.policyScope}</span>
<span class="ml-2 text-xs bg-green-100 text-green-700 px-1.5 py-0.5 rounded">{widget.status}</span>
<span class="ml-2 text-xs text-gray-400">v{show widget.version}</span>
</p>
</div>
<a href={EditWidgetAction { widgetId = widget.id }}
class="text-sm border border-gray-300 px-3 py-1.5 rounded hover:bg-gray-50">
Edit
</a>
</div>
|]}
<div class="grid grid-cols-3 gap-4 mb-8 mt-6">
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Total Events</p>
<p class="text-3xl font-semibold mt-1">{length events}</p>
</div>
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Annotations</p>
<p class="text-3xl font-semibold mt-1">{length annotations}</p>
</div>
<div class="bg-white rounded-lg border border-gray-200 p-4">
<p class="text-xs text-gray-500 uppercase tracking-wide">Versions</p>
<p class="text-3xl font-semibold mt-1">{length versions}</p>
</div>
</div>
<div class="grid grid-cols-2 gap-6 mb-8">
<section>
<div class="flex items-center justify-between mb-3">
<h2 class="text-lg font-medium">Annotations</h2>
<a href={NewAnnotationAction { widgetId = widget.id }}
class="text-sm text-indigo-600 hover:text-indigo-800">+ Add</a>
</div>
<div class="space-y-2">
{forEach rootAnnotations (renderAnnotation childrenOf)}
</div>
</section>
<section>
<h2 class="text-lg font-medium mb-3">Annotation Breakdown</h2>
<div class="bg-white rounded-lg border border-gray-200 p-4 space-y-2">
{forEach categoryBreakdown renderCategoryRow}
</div>
</section>
</div>
<section class="mb-8">
<h2 class="text-lg font-medium mb-3">Interaction Events</h2>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Event</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Actor</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Occurred</th>
</tr>
</thead>
<tbody>
{forEach events renderEventRow}
</tbody>
</table>
</div>
</section>
<section>
<h2 class="text-lg font-medium mb-3">Version History</h2>
<div class="bg-white rounded-lg border border-gray-200 overflow-hidden">
<table class="w-full text-sm">
<thead class="bg-gray-50 border-b border-gray-200">
<tr>
<th class="text-left px-4 py-3 font-medium text-gray-700">Version</th>
<th class="text-left px-4 py-3 font-medium text-gray-700">Recorded</th>
</tr>
</thead>
<tbody>
{forEach versions renderVersionRow}
</tbody>
</table>
</div>
</section>
|]
where
rootAnnotations = filter (\a -> isNothing a.parentId) annotations
childrenOf parent = filter (\a -> a.parentId == Just parent.id) annotations
categoryBreakdown =
[ (cat, length (filter (\a -> a.category == cat) annotations))
| cat <- ["friction","defect","wish","policy_concern","doc_gap","trust","other"]
, any (\a -> a.category == cat) annotations
]
renderAnnotation :: (Annotation -> [Annotation]) -> Annotation -> Html
renderAnnotation childrenOf a = [hsx|
<div class="bg-white rounded border border-gray-200 px-4 py-3">
<div class="flex items-center gap-2 mb-1">
<span class="text-xs bg-gray-100 text-gray-600 px-1.5 py-0.5 rounded">{a.category}</span>
<span class="text-xs text-gray-400">{a.actorType}</span>
<span class="text-xs text-gray-300 ml-auto">{show a.createdAt}</span>
</div>
<p class="text-sm text-gray-700">{a.body}</p>
<div class="ml-4 mt-2 space-y-2">
{forEach (childrenOf a) (renderAnnotation childrenOf)}
</div>
</div>
|]
renderEventRow :: InteractionEvent -> Html
renderEventRow e = [hsx|
<tr class="border-b border-gray-100">
<td class="px-4 py-3 font-mono text-xs text-gray-700">{e.eventType}</td>
<td class="px-4 py-3 text-gray-500 text-xs">{e.actorType}</td>
<td class="px-4 py-3 text-gray-400 text-xs">{show e.occurredAt}</td>
</tr>
|]
renderVersionRow :: WidgetVersion -> Html
renderVersionRow v = [hsx|
<tr class="border-b border-gray-100">
<td class="px-4 py-3 text-gray-700">v{show v.version}</td>
<td class="px-4 py-3 text-gray-400 text-xs">{show v.createdAt}</td>
</tr>
|]
renderCategoryRow :: (Text, Int) -> Html
renderCategoryRow (cat, count) = [hsx|
<div class="flex items-center justify-between text-sm">
<span class="text-gray-600">{cat}</span>
<span class="font-semibold">{show count}</span>
</div>
|]