package main import ( "context" "strings" "git.dev.alexdunmow.com/block/pluginsdk/blocks" "git.dev.alexdunmow.com/block/pluginsdk/plugin" "git.dev.alexdunmow.com/block/pluginsdk/plugin/wasmguest" "git.dev.alexdunmow.com/block/pluginsdk/templates" ) const pluginName = "weather-forecast-6aa47a" const settingsSchema = `{"schema":"weather-forecast.settings.v1","fields":[{"key":"town_name","label":"Town name","type":"string","default":""},{"key":"latitude","label":"Latitude","type":"string","default":""},{"key":"longitude","label":"Longitude","type":"string","default":""}]}` const unavailable = "The forecast is not available right now." // Registration is the plugin entry point. var Registration = plugin.PluginRegistration{ Name: pluginName, Version: "1.0.0", Register: register, SettingsSchema: func() []byte { return []byte(settingsSchema) }, } func register(_ templates.TemplateRegistry, br blocks.BlockRegistry) error { br.Register(blocks.BlockMeta{ Key: "weather_forecast", Title: "Local weather forecast", Category: blocks.CategoryContent, }, renderBlock) return nil } func renderBlock(ctx context.Context, content map[string]any) string { heading, _ := content["heading"].(string) days := parseDays(content["days_shown"]) svc := wasmguest.HostServices() if svc.Settings == nil || svc.OutboundHTTP == nil { return renderForecast(heading, "", nil, unavailable) } set, err := svc.Settings.GetPluginSettings(ctx, pluginName) if err != nil { return renderForecast(heading, "", nil, unavailable) } lat, ok1 := settingFloat(set, "latitude") lon, ok2 := settingFloat(set, "longitude") if !ok1 || !ok2 || !validCoords(lat, lon) { return renderForecast(heading, "", nil, "The town location has not been set up yet.") } forecast, err := fetchForecast(ctx, svc.OutboundHTTP, lat, lon, days) if err != nil || len(forecast) == 0 { return renderForecast(heading, "", nil, unavailable) } town, _ := set["town_name"].(string) return renderForecast(heading, strings.TrimSpace(town), forecast, "") }