datastar/site/routes_errors.templ

139 lines
2.9 KiB
Plaintext

package site
import (
"github.com/delaneyj/toolbelt"
"strings"
)
templ ErrorView(typ, name string, args templ.Component) {
<div class="flex flex-col gap-8">
<div class="prose prose-lg flex-1">
<h1>{ name }</h1>
<div class="alert block p-8 pt-4">
<h3>{ typ } Error</h3>
@args
</div>
<h2>Explanation</h2>
{ children... }
</div>
</div>
}
templ runtimeArgs(info *RuntimeErrorInfo) {
{{
pluginName := toolbelt.Kebab(info.Plugin.Name)
pluginType := strings.ToLower(info.Plugin.Type)
elementTag := strings.ToLower(info.Element.Tag)
rawKey := toolbelt.Kebab(info.Expression.RawKey)
dataAttribute := "data-" + rawKey + "=\"" + info.Expression.Value + "\""
}}
if pluginName != "" {
<div>
A runtime error was encountered in the <code>data-{ pluginName }</code> { pluginType } plugin.
</div>
if info.Error != "" {
<div>
<pre>
{ info.Error }
</pre>
</div>
}
<div>
<h3>Element</h3>
<pre>
&lt;{ elementTag } id="{ info.Element.ID }" { dataAttribute }&gt;&lt;/{ elementTag }&gt;
</pre>
</div>
if info.Expression.FnContent != "" {
<div>
<h3>Expression</h3>
<pre>
{ info.Expression.FnContent }
</pre>
</div>
}
}
}
templ RuntimeErrorView(name string, info *RuntimeErrorInfo) {
@ErrorView("Runtime", name, runtimeArgs(info)) {
{ children... }
}
}
templ initArgs(info *InitErrorInfo) {
{{
pluginName := toolbelt.Kebab(info.Plugin.Name)
pluginType := strings.ToLower(info.Plugin.Type)
}}
if pluginName != "" {
<div>
An initializion error was encountered in the <code>{ pluginName }</code> { pluginType } plugin.
</div>
}
}
templ InitErrorView(name string, info *InitErrorInfo) {
@ErrorView("Init", name, initArgs(info)) {
{ children... }
}
}
templ sampleHtmlCode(heading string) {
<p>{ heading }:</p>
@htmlSource() {
{ children... }
}
}
templ sampleCode(heading, lang, source string) {
<p>{ heading }:</p>
@code(lang, source)
}
templ attributeDocs(pluginName string) {
<p>
See the docs for the
<a href={ templ.SafeURL("/reference/attribute_plugins#data-" + pluginName) }>
<code>data-{ pluginName }</code>
</a> attribute.
</p>
}
templ actionDocs(pluginName string) {
<p>
See the docs for the
<a href={ templ.SafeURL("/reference/action_plugins#" + pluginName) }>
<code>{ "@" + pluginName }()</code>
</a> action.
</p>
}
templ eventDocs(eventName string) {
<p>
See the docs for the
<a href={ templ.SafeURL("/reference/sse_events#datastar-" + eventName) }>
<code>datastar-{ eventName }</code>
</a> event.
</p>
}
templ expressionDocs() {
<p>
See the docs on
<a href="/guide/datastar_expressions">
Datastar expressions
</a>.
</p>
}
templ signalNames(signals ...string) {
{{
prefixedNames := make([]string, len(signals))
for i, s := range signals {
prefixedNames[i] = "$" + s
}
}}
<pre><code>{ strings.Join( prefixedNames, ", ") }</code></pre>
}