2022-03-28 21:49:01 +00:00
|
|
|
package components
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/maxence-charriere/go-app/v9/pkg/app"
|
|
|
|
"github.com/mlctrez/goapp-mdc/pkg/drawer"
|
|
|
|
"github.com/mlctrez/goapp-mdc/pkg/icon"
|
|
|
|
"github.com/mlctrez/goapp-mdc/pkg/list"
|
|
|
|
"go.mills.io/saltyim/internal/pwa/storage"
|
|
|
|
)
|
|
|
|
|
2022-03-30 00:35:43 +00:00
|
|
|
const (
|
|
|
|
navigationUpdateAction = "navigation.update.action"
|
|
|
|
)
|
|
|
|
|
2022-04-07 03:43:09 +00:00
|
|
|
var DrawerOpen bool
|
|
|
|
|
2022-03-28 21:49:01 +00:00
|
|
|
type Navigation struct {
|
|
|
|
app.Compo
|
2022-04-07 03:43:09 +00:00
|
|
|
Type drawer.Type
|
2022-03-28 21:49:01 +00:00
|
|
|
drawer *drawer.Drawer
|
|
|
|
items list.Items
|
|
|
|
Contacts []string
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:35:43 +00:00
|
|
|
func (n *Navigation) HasContact(addr string) bool {
|
|
|
|
for _, contact := range n.Contacts {
|
|
|
|
if contact == addr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:49:01 +00:00
|
|
|
func (n *Navigation) Render() app.UI {
|
|
|
|
if n.drawer == nil {
|
|
|
|
n.drawer = &drawer.Drawer{
|
2022-04-07 03:43:09 +00:00
|
|
|
Type: n.Type,
|
2022-03-28 21:49:01 +00:00
|
|
|
Id: "navigationDrawer",
|
|
|
|
List: &list.List{Type: list.Navigation, Id: "navigationList"},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
items := list.Items{
|
2022-03-29 22:06:06 +00:00
|
|
|
{Type: list.ItemTypeAnchor, Graphic: icon.MISettings, Href: "/config", Text: "Settings"},
|
|
|
|
{Type: list.ItemTypeAnchor, Graphic: icon.MIPersonAdd, Href: "/newchat", Text: "New Chat"},
|
2022-03-28 21:49:01 +00:00
|
|
|
{Type: list.ItemTypeDivider},
|
|
|
|
}
|
|
|
|
for _, contact := range n.Contacts {
|
|
|
|
i := &list.Item{Type: list.ItemTypeAnchor, Graphic: icon.MIPerson, Href: "/#" + contact, Text: contact}
|
|
|
|
items = append(items, i)
|
|
|
|
}
|
|
|
|
n.drawer.List.Items = items.UIList()
|
|
|
|
|
|
|
|
return n.drawer
|
|
|
|
}
|
|
|
|
|
2022-03-29 22:06:06 +00:00
|
|
|
func (n *Navigation) LoadFromStorage(ctx app.Context) {
|
|
|
|
n.Contacts = storage.ContactsLocalStorage(ctx).List()
|
|
|
|
}
|
|
|
|
|
2022-03-28 21:49:01 +00:00
|
|
|
func (n *Navigation) OnMount(ctx app.Context) {
|
|
|
|
n.items.SelectHref(ctx.Page().URL().Path)
|
|
|
|
ctx.Handle(string(list.Select), func(context app.Context, action app.Action) {
|
|
|
|
if action.Value == n.drawer.List {
|
2022-04-07 03:43:09 +00:00
|
|
|
if !DesktopMode {
|
|
|
|
n.drawer.ActionClose(context)
|
|
|
|
DrawerOpen = false
|
|
|
|
}
|
2022-03-28 21:49:01 +00:00
|
|
|
}
|
|
|
|
})
|
2022-03-30 00:35:43 +00:00
|
|
|
ctx.Handle(navigationUpdateAction, n.navigationUpdate)
|
2022-04-07 03:43:09 +00:00
|
|
|
ctx.Defer(func(context app.Context) { NavigationUpdate(context) })
|
2022-03-29 22:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Navigation) navigationUpdate(ctx app.Context, action app.Action) {
|
2022-03-30 00:35:43 +00:00
|
|
|
n.LoadFromStorage(ctx)
|
2022-03-29 22:06:06 +00:00
|
|
|
n.drawer.List.Update()
|
2022-03-28 21:49:01 +00:00
|
|
|
}
|
2022-03-30 00:35:43 +00:00
|
|
|
|
2022-04-07 03:43:09 +00:00
|
|
|
func NavigationUpdate(ctx app.Context) {
|
2022-03-30 00:35:43 +00:00
|
|
|
ctx.NewAction(navigationUpdateAction)
|
|
|
|
}
|