shared

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ControllerHome             = "home"
	ControllerVisitorActivity  = "visitor-activity"
	ControllerVisitorPaths     = "visitor-paths"
	ControllerPageViewActivity = "page-view-activity"
)

Controller name constants

View Source
const (
	PathHome             = "/admin/home"
	PathVisitorActivity  = "/admin/visitor-activity"
	PathVisitorPaths     = "/admin/visitor-paths"
	PathPageViewActivity = "/admin/page-view-activity"
)

Path constants for admin routes

Variables

This section is empty.

Functions

func AdminHeaderUI

func AdminHeaderUI(r *http.Request, homeURL string) hb.TagInterface

AdminHeaderUI creates the admin header navigation

func Breadcrumbs(r *http.Request, pageBreadcrumbs []Breadcrumb) hb.TagInterface

Breadcrumbs creates a breadcrumb navigation component

func BreadcrumbsUI(breadcrumbs []Breadcrumb) hb.TagInterface

BreadcrumbsUI generates the UI for breadcrumbs

func BrowserBadge added in v1.5.0

func BrowserBadge(visitor statsstore.VisitorInterface) hb.TagInterface

BrowserBadge renders a badge showing the visitor's browser and version.

func CardUI

func CardUI(title string, body hb.TagInterface) hb.TagInterface

CardUI creates a standard card component

func ClampPerPage added in v1.5.0

func ClampPerPage(perPage int) int

ClampPerPage constrains a per-page value to the 1–100 range.

func CountryBadge added in v1.5.0

CountryBadge renders a badge with a CSS flag icon and country name tooltip.

func DeviceBadge added in v1.5.0

func DeviceBadge(visitor statsstore.VisitorInterface) hb.TagInterface

DeviceBadge renders a contextual badge for the visitor's device type.

func ExportCSV added in v1.5.0

func ExportCSV(w http.ResponseWriter, filename string, headers []string, rows [][]string) string

ExportCSV writes CSV content to the response writer with proper headers and a UTF-8 BOM for Excel/Google Sheets compatibility. It returns the CSV string so callers (which return a string from their Handler) can pass it through.

On error it sets a 500 status code and returns an error message string.

func ExportDropdown added in v1.5.0

func ExportDropdown(exportURL string) hb.TagInterface

ExportDropdown renders a dropdown with a link to the given export URL.

func ExportFilename added in v1.5.0

func ExportFilename(prefix string) string

ExportFilename generates a CSV filename with the current UTC date. Example: ExportFilename("visitor-paths") → "visitor-paths-2024-01-15.csv"

func FormatLocation added in v1.5.0

func FormatLocation(ui ControllerOptions, visitor statsstore.VisitorInterface) string

FormatLocation returns a human-readable location string for a visitor.

func FormatTimestamp added in v1.5.0

func FormatTimestamp(value string) string

FormatTimestamp parses a timestamp and returns a user-friendly formatted string.

func FullPathURL added in v1.5.0

func FullPathURL(ui ControllerOptions, path string) string

FullPathURL builds an absolute URL from a website base URL and a path.

func HTTPBuildQuery

func HTTPBuildQuery(queryData urlpkg.Values) string

HTTPBuildQuery converts URL values to a query string

func InfoLine added in v1.5.0

func InfoLine(label string, value hb.TagInterface) hb.TagInterface

InfoLine renders a labelled value line with a muted label and bold value.

func InfoMuted added in v1.5.0

func InfoMuted(text string) hb.TagInterface

InfoMuted renders a muted, italic span — used for placeholder values.

func InfoText added in v1.5.0

func InfoText(text string) hb.TagInterface

InfoText renders a plain body-text span.

func NavCardUI(title string, href string, icon string, description string) hb.TagInterface

NavCardUI creates a navigation card with icon and description

func OptionsButton added in v1.5.0

func OptionsButton() hb.TagInterface

OptionsButton renders a gear-icon button placeholder used in card headers.

func PaginationSummary added in v1.5.0

func PaginationSummary(page, pageSize int, totalCount int64, itemLabel string) hb.TagInterface

PaginationSummary renders a "Showing X-Y of Z" summary text.

func PaginationUI

func PaginationUI(currentPage int, totalPages int, urlFunc func(page int) string) hb.TagInterface

PaginationUI creates a pagination component

func ParseIntWithDefault added in v1.5.0

func ParseIntWithDefault(value string, defaultValue int) int

ParseIntWithDefault parses a string to int, returning the default on error or non-positive values.

func PathLink(ui ControllerOptions, path string) hb.TagInterface

PathLink renders an anchor tag with an external icon pointing to the full absolute URL for the given path.

func PerPageSelector added in v1.5.0

func PerPageSelector(r *http.Request, currentPageSize int, urlFunc func(params map[string]string) string) hb.TagInterface

PerPageSelector renders a button group for selecting rows per page. The urlFunc parameter receives the query-params map and should return the fully qualified URL for that per-page value.

func Query

func Query(queryData map[string]string) string

Query generates a query string from a map of parameters

func QueryParamsWith added in v1.5.0

func QueryParamsWith(r *http.Request, overrides map[string]string) map[string]string

QueryParamsWith takes the current request's query parameters, applies the given overrides (empty values delete the key), and returns a map suitable for passing to URL builder functions.

func QuickRangeButtons added in v1.5.0

func QuickRangeButtons(r *http.Request, urlFunc func(params map[string]string) string) hb.TagInterface

QuickRangeButtons renders a button group of common time-range filters. The urlFunc parameter receives the query-params map and should return the fully qualified URL for that range.

func RangeLabel added in v1.5.0

func RangeLabel(value string) string

RangeLabel converts a range code to a human-readable label.

func ResolvedCountryName added in v1.5.0

func ResolvedCountryName(ui ControllerOptions, code string) string

ResolvedCountryName resolves a country code to a human-readable name, falling back to the ISO code or "Unknown".

func ShortDate added in v1.5.0

func ShortDate(value string) string

ShortDate parses a timestamp and returns a YYYY-MM-DD string.

func StatCardUI

func StatCardUI(title string, value string, icon string, color string) hb.TagInterface

StatCardUI creates a card displaying a single statistic

func StrTruncate

func StrTruncate(s string, maxLength int) string

StrTruncate truncates a string to the specified length and adds ellipsis if needed

func TimeParse added in v1.5.0

func TimeParse(value string) (time.Time, error)

TimeParse attempts to parse a timestamp using several common layouts.

func URL

func URL(r *http.Request, path string, params map[string]string) string

URL generates a URL with the given path and parameters

func UrlHome

func UrlHome(r *http.Request, params ...map[string]string) string

func UrlPageViewActivity added in v0.7.0

func UrlPageViewActivity(r *http.Request, params ...map[string]string) string

func UrlVisitorActivity

func UrlVisitorActivity(r *http.Request, params ...map[string]string) string

func UrlVisitorPaths

func UrlVisitorPaths(r *http.Request, params ...map[string]string) string

Types

type Breadcrumb struct {
	Name string
	URL  string
}

Breadcrumb represents a navigation breadcrumb

type ContextKey

type ContextKey string

ContextKey is a custom type for context keys to avoid collisions

const (
	KeyAdminHomeURL ContextKey = "admin_home_url"
	KeyEndpoint     ContextKey = "endpoint"
)

Context keys

type ControllerOptions

type ControllerOptions struct {
	Logger            *slog.Logger
	Store             statsstore.StoreInterface
	Layout            LayoutInterface
	HomeURL           string
	WebsiteUrl        string
	CountryNameByIso2 func(iso2Code string) (string, error)
}

ControllerOptions contains the options for creating a new admin controller

type LayoutInterface

type LayoutInterface interface {
	SetTitle(title string)
	SetScriptURLs(scripts []string)
	SetScripts(scripts []string)
	SetStyleURLs(styles []string)
	SetStyles(styles []string)
	SetBody(string)
	// SetCountryNameByIso2 provides country lookup helpers used by the admin UI.
	SetCountryNameByIso2(func(iso2Code string) (string, error))
	Render(w http.ResponseWriter, r *http.Request) string
}

LayoutInterface defines the layout methods needed by controllers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL