Redmage/views/components/progress/image_download.templ

135 lines
3.7 KiB
Plaintext
Raw Normal View History

package progress
2024-06-05 16:18:22 +07:00
import "time"
import "fmt"
type ImageDownloadStartNotificationData struct {
ID string
Subreddit string
PostURL string
PostName string
PostTitle string
AutoRemoveDuration time.Duration
}
templ ImageDownloadStartNotification(data ImageDownloadStartNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
2024-06-10 14:52:53 +07:00
class="alert alert-info hover:bg-info-content transition-all"
2024-06-05 16:18:22 +07:00
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
Start Downloading
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
}
2024-06-05 16:18:22 +07:00
func truncateTitle(s string) string {
if len(s) > 20 {
return s[:20] + "..."
}
return s
}
2024-06-05 16:18:22 +07:00
type ImageDownloadEndNotificationData struct {
ID string
Subreddit string
PostURL string
PostName string
PostTitle string
AutoRemoveDuration time.Duration
}
2024-06-05 16:18:22 +07:00
templ ImageDownloadEndNotification(data ImageDownloadEndNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
2024-06-10 14:52:53 +07:00
class="alert alert-success hover:bg-success-content transition-all"
2024-06-05 16:18:22 +07:00
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
Finished Downloading
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
}
2024-06-10 14:52:53 +07:00
type ImageDownloadErrorNotificationData struct {
ID string
Subreddit string
PostURL string
PostName string
PostTitle string
Error error
AutoRemoveDuration time.Duration
}
templ ImageDownloadErrorNotification(data ImageDownloadErrorNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
class="alert alert-error hover:bg-error-content transition-all"
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
{ data.Error.Error() }
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
}
type ImageDownloadProgressNotificationData struct {
ID string
Subreddit string
PostURL string
PostName string
PostTitle string
ContentLength int64
Downloaded int64
AutoRemoveDuration time.Duration
}
func (i ImageDownloadProgressNotificationData) GetProgress() float64 {
return float64(i.Downloaded) / float64(i.ContentLength)
}
templ ImageDownloadProgressNotification(data ImageDownloadProgressNotificationData) {
<div
id={ data.ID }
if data.AutoRemoveDuration > 0 {
x-data={ fmt.Sprintf("{ init() { setTimeout(() => $el.remove(), %d) }}", data.AutoRemoveDuration.Milliseconds()) }
}
onclick="this.remove()"
class="alert alert-info hover:bg-info-content transition-all"
>
<span>
<a
target="_blank"
href={ templ.SafeURL(fmt.Sprintf("https://www.reddit.com/r/%s", data.Subreddit)) }
>{ data.Subreddit }</a>:
Progress: { fmt.Sprintf("%.2f%%", data.GetProgress()*100) }
<a href={ templ.SafeURL(data.PostURL) }>{ truncateTitle(data.PostTitle) }</a>
</span>
</div>
}