Skip to content

Commit

Permalink
add type and support for dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 25, 2024
1 parent 5d0f429 commit d18bcb4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 21 deletions.
13 changes: 8 additions & 5 deletions internal/viewer/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package viewer

import (
"net/http"
"path/filepath"

"github.com/davecgh/go-spew/spew"
"github.com/rusq/slack"
Expand All @@ -13,10 +14,12 @@ func (v *Viewer) indexHandler(w http.ResponseWriter, r *http.Request) {
var page = struct {
Conversation slack.Channel
Name string
Type string
channels
}{
Conversation: slack.Channel{}, //blank.
Name: v.rtr.Name(),
Name: filepath.Base(v.src.Name()),
Type: v.src.Type(),
channels: v.ch,
}
if err := v.tmpl.ExecuteTemplate(w, "index.html", page); err != nil {
Expand All @@ -37,7 +40,7 @@ func (v *Viewer) newFileHandler(fn func(w http.ResponseWriter, r *http.Request,
}

func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id string) {
mm, err := v.rtr.AllMessages(id)
mm, err := v.src.AllMessages(id)
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -63,7 +66,7 @@ func (v *Viewer) channelHandler(w http.ResponseWriter, r *http.Request, id strin

v.lg.Debugf("conversation: %s, got %d messages", id, len(mm))

ci, err := v.rtr.ChannelInfo(id)
ci, err := v.src.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -89,7 +92,7 @@ func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string
http.NotFound(w, r)
return
}
mm, err := v.rtr.AllThreadMessages(id, ts)
mm, err := v.src.AllThreadMessages(id, ts)
if err != nil {
v.lg.Printf("%s: error: %v", id, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand All @@ -98,7 +101,7 @@ func (v *Viewer) threadHandler(w http.ResponseWriter, r *http.Request, id string

v.lg.Debugf("conversation: %s, thread: %s, got %d messages", id, ts, len(mm))

ci, err := v.rtr.ChannelInfo(id)
ci, err := v.src.ChannelInfo(id)
if err != nil {
v.lg.Printf("error: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
4 changes: 2 additions & 2 deletions internal/viewer/renderer/slack_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (*Slack) mbtImage(ib slack.Block) (string, error) {
return "", NewErrIncorrectType(&slack.ImageBlock{}, ib)
}
return fmt.Sprintf(
`<figure class="slack-image-block"><img class="slack-image" src="%[1]s" alt="%[2]s"><figcaption class="slack-image-caption">%[2]s</figcaption></figure>`,
slack.MBTImage, b.ImageURL, b.AltText,
`<figure class="slack-image-block"><img src="%[1]s" alt="%[2]s"><figcaption class="slack-image-caption">%[2]s</figcaption></figure>`,
b.ImageURL, b.AltText,
), nil
}
4 changes: 4 additions & 0 deletions internal/viewer/source/chunkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (c *ChunkDir) Name() string {
return c.d.Name()
}

func (c *ChunkDir) Type() string {
return "chunk"
}

func (c *ChunkDir) Users() ([]slack.User, error) {
return c.d.Users()
}
4 changes: 4 additions & 0 deletions internal/viewer/source/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (d Dump) Name() string {
return d.name
}

func (d Dump) Type() string {
return "dump"
}

func (d Dump) Channels() ([]slack.Channel, error) {
// if user was diligent enough to dump channels and save them in a file,
// we can use that.
Expand Down
4 changes: 4 additions & 0 deletions internal/viewer/source/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func (e *Export) Name() string {
return e.name
}

func (e *Export) Type() string {
return "export"
}

func (e *Export) AllMessages(channelID string) ([]slack.Message, error) {
// find the channel
name, ok := e.chanNames[channelID]
Expand Down
2 changes: 1 addition & 1 deletion internal/viewer/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestViewer_username(t *testing.T) {
v := &Viewer{
ch: tt.fields.ch,
um: tt.fields.um,
rtr: tt.fields.rtr,
src: tt.fields.rtr,
tmpl: tt.fields.tmpl,
srv: tt.fields.srv,
lg: tt.fields.lg,
Expand Down
41 changes: 32 additions & 9 deletions internal/viewer/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div class="container">
<section class="channel-list">
<h1>Slackdump</h1>
<small class="subtitle grey">{{.Name}}</small>
<small class="subtitle grey">{{.Type}}: {{.Name}}</small>
<!-- Channel list goes here -->
{{ if ( or .Public .Private) }}
<h2>Channels</h2>
Expand Down Expand Up @@ -64,8 +64,8 @@ <h2>{{ rendername .Conversation }}</h2>
<footer class="thread-info">
<a href="#" hx-get="/archives/{{$id}}/{{ $el.ThreadTimestamp }}" hx-target="#thread">
Replies: {{ $el.ReplyCount -}}
</a> <span class="latest-reply grey">Last reply: {{ time $el.LatestReply }}</span>
</footer>
</a> <span class="last-reply grey">Last reply: {{ time $el.LatestReply }}</span>
</footer>
{{ end }}
<hr>
</article>
Expand Down Expand Up @@ -123,12 +123,12 @@ <h2>Thread: {{ .ThreadID }}</h2>
<header class="message-header" id="#{{.Timestamp}}">
<span class="message-sender">
{{if is_app_msg . -}}
{{ username . }}
{{- else -}}
<a href="#" hx-get="/team/{{ .User }}" hx-target="#thread"> {{ username . }}</a>
{{ username . }}
{{- else -}}
<a href="#" hx-get="/team/{{ .User }}" hx-target="#thread"> {{ username . }}</a>
{{- end }}
</span>
<span class="message-timestamp">{{ time .Timestamp }}</span>
<span class="message-timestamp grey">{{ time .Timestamp }}</span>
</header>
<div class="message-content">
<p>{{/* .Text | rendertext */}}{{ render . }}</p>
Expand All @@ -137,10 +137,28 @@ <h2>Thread: {{ .ThreadID }}</h2>

{{ define "hx_css" }}
<style>
body{
body {
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

@media (prefers-color-scheme: dark) {
body {
/* Use dark background if system is in dark mode */
background-color: #202224;
color: #fff;
}
a {
color: #72b2ff;
}

a:hover {
color: #ff7b72;
}
a:visited {
color: #9669ff;
}
}

.container {
display: flex;
height: 97vh;
Expand All @@ -166,7 +184,12 @@ <h2>Thread: {{ .ThreadID }}</h2>
.thread {
display: none;
}


.message-timestamp,
.last-reply {
font-size: 12px;
}

.grey {
color: #777;
}
Expand Down
26 changes: 22 additions & 4 deletions internal/viewer/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Viewer struct {
// data
ch channels
um st.UserIndex
rtr Sourcer
src Sourcer
tmpl *template.Template

// handles
Expand All @@ -34,6 +34,8 @@ type Sourcer interface {
// Name should return the name of the retriever underlying media, i.e.
// directory or archive.
Name() string
// Type should return the type of the retriever, i.e. "chunk" or "export".
Type() string
// Channels should return all channels.
Channels() ([]slack.Channel, error)
// Users should return all users.
Expand Down Expand Up @@ -91,10 +93,10 @@ func New(ctx context.Context, addr string, r Sourcer) (*Viewer, error) {
}
um := st.NewUserIndex(uu)

// sr := renderer.NewSlack(renderer.WithUsers(indexusers(uu)), renderer.WithChannels(indexchannels(all)))
sr := &renderer.Debug{}
sr := renderer.NewSlack(renderer.WithUsers(indexusers(uu)), renderer.WithChannels(indexchannels(all)))
// sr := &renderer.Debug{}
v := &Viewer{
rtr: r,
src: r,
ch: cc,
um: um,
lg: logger.FromContext(ctx),
Expand Down Expand Up @@ -139,3 +141,19 @@ func (v *Viewer) Close() error {
}
return ee
}

func indexusers(uu []slack.User) map[string]slack.User {
var um = make(map[string]slack.User, len(uu))
for _, u := range uu {
um[u.ID] = u
}
return um
}

func indexchannels(cc []slack.Channel) map[string]slack.Channel {
var cm = make(map[string]slack.Channel, len(cc))
for _, c := range cc {
cm[c.ID] = c
}
return cm
}

0 comments on commit d18bcb4

Please sign in to comment.