Path: blob/main/internal/window/quickswitcher/index.go
366 views
package quickswitcher12import (3"context"4"log/slog"5"slices"67"github.com/diamondburned/arikawa/v3/discord"8"github.com/diamondburned/gotkit/app"9"github.com/sahilm/fuzzy"10"libdb.so/dissent/internal/gtkcord"11)1213type index struct {14items indexItems15buffer indexItems16}1718const searchLimit = 251920var excludedChannelTypes = []discord.ChannelType{21discord.GuildCategory,22discord.GuildForum,23}2425var allowedChannelTypes = slices.DeleteFunc(26slices.Clone(gtkcord.AllowedChannelTypes),27func(t discord.ChannelType) bool {28return slices.Contains(excludedChannelTypes, t)29},30)3132func (idx *index) update(ctx context.Context) {33state := gtkcord.FromContext(ctx).Offline()34items := make([]indexItem, 0, 250)3536dms, err := state.PrivateChannels()37if err != nil {38app.Error(ctx, err)39return40}4142for i := range dms {43items = append(items, newChannelItem(state, nil, &dms[i]))44}4546guilds, err := state.Guilds()47if err != nil {48app.Error(ctx, err)49return50}5152for i, guild := range guilds {53chs, err := state.Channels(guild.ID, allowedChannelTypes)54if err != nil {55slog.Error(56"cannot populate channels for guild in quick switcher",57"guild", guild.Name,58"guild_id", guild.ID,59"err", err)60continue61}6263items = append(items, newGuildItem(&guilds[i]))64for j := range chs {65items = append(items, newChannelItem(state, &guilds[i], &chs[j]))66}67}6869idx.items = items70}7172func (idx *index) search(str string) []indexItem {73if idx.items == nil {74return nil75}7677idx.buffer = idx.buffer[:0]78if idx.buffer == nil {79idx.buffer = make([]indexItem, 0, searchLimit)80}8182matches := fuzzy.FindFrom(str, idx.items)83for i := 0; i < len(matches) && i < searchLimit; i++ {84idx.buffer = append(idx.buffer, idx.items[matches[i].Index])85}8687return idx.buffer88}899091