Select the types of activity you want to include in your feed.
Make the project ready for a frontend that will be coupled to the go
application via a custom inertia backend implementation. Frontend will
consist of svelte components with inertia, tailwind and postcss.
···11+package inertia
22+33+import (
44+ "context"
55+ "net/http"
66+ "slices"
77+)
88+99+type Props map[string]Prop
1010+1111+type Prop struct {
1212+ Value func() (any, error)
1313+ Lazy bool
1414+ Optional bool
1515+ Always bool
1616+ Deferred bool
1717+ DeferredKey string
1818+ Default bool
1919+ isError bool
2020+}
2121+2222+func (p *Prop) ShouldDefer(r *http.Request) bool {
2323+ return true
2424+}
2525+2626+func (p *Prop) IsReturned(r *http.Request, key string, only []string, except []string) bool {
2727+ isFirstLoad := !isInertiaPartialRequest(r)
2828+2929+ if isFirstLoad && p.Deferred {
3030+ return false
3131+ }
3232+3333+ if slices.Contains(except, key) {
3434+ return false
3535+ }
3636+3737+ if slices.Contains(only, key) {
3838+ return true
3939+ }
4040+4141+ if p.Always || p.Default {
4242+ return true
4343+ }
4444+4545+ if isFirstLoad && p.Lazy {
4646+ return true
4747+ }
4848+4949+ return false
5050+}
5151+5252+const PropsCtxKey = "INERTIA_KEY"
5353+5454+type PropOption func(*Prop)
5555+5656+// The value is always evaluated during a request, but can be left out of the response when
5757+// the request is a partial request. In other Inertia adapters this would be the same as just
5858+// passing the value.
5959+func Default(value any, opts ...PropOption) Prop {
6060+ return Prop{Value: func() (any, error) { return value, nil }, Default: true}
6161+}
6262+6363+// The value is evaluated and returned for the first request to a page. For all partial request
6464+// to that same page, the value is not evaluated and returned unless the property is specified
6565+// in the `X-Inertia-Partial-Data` header. In other Inertia adapters this would be the same as
6666+// passing a closure.
6767+func Lazy(valuefn func() (any, error), opts ...PropOption) Prop {
6868+ return Prop{Value: valuefn, Lazy: true}
6969+}
7070+7171+// The value is only evaluated during a request when the request is a partial request. The value
7272+// is never returned during the first request from a page.
7373+func Optional(valuefn func() (any, error), opts ...PropOption) Prop {
7474+ return Prop{Value: valuefn, Optional: true}
7575+}
7676+7777+// The value is always returned during a partial request, and the first request from a page.
7878+func Always(value any) Prop {
7979+ return Prop{Value: func() (any, error) { return value, nil }, Always: true}
8080+}
8181+8282+func WithDeferredKey(key string) PropOption {
8383+ return func(p *Prop) {
8484+ p.DeferredKey = key
8585+ }
8686+}
8787+8888+// Besides setting the props in the handler when rendering the page, you can also set props in the
8989+// middleware. This is useful when you want to set props that are available to all pages. One can
9090+// use the SetProp function to set props. The value will be stored in the request context and will
9191+// be retrieved when rendering the page.
9292+func SetProp(r *http.Request, key string, value Prop) {
9393+ props, ok := r.Context().Value(PropsCtxKey).(Props)
9494+9595+ if !ok {
9696+ props = make(Props)
9797+ }
9898+9999+ props[key] = value
100100+101101+ ctx := context.WithValue(r.Context(), PropsCtxKey, props)
102102+103103+ *r = *r.WithContext(ctx)
104104+}