Compare commits
16 Commits
a7f02c8dd7
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e0a323ff91 | |||
| 3f418669f3 | |||
| 5409167a96 | |||
| 618c034b05 | |||
| 8e226e9ea1 | |||
| 2c84d3d818 | |||
| c7dc944264 | |||
| 286a01c4d6 | |||
| 3d92096bb8 | |||
| da9f4bb0ec | |||
| 44c7126ac5 | |||
| e4c4cdce41 | |||
| 20c5df67cd | |||
| 36fab5e2c0 | |||
| d193e9d7a2 | |||
| 3494ad6515 |
11
Justfile
11
Justfile
@@ -1,5 +1,12 @@
|
|||||||
build:
|
dev:
|
||||||
docker build -t podcaster .
|
cd web && pnpm build
|
||||||
|
PODCASTS_DIRPATH=./podcasts go run .
|
||||||
|
|
||||||
|
build tag="latest":
|
||||||
|
docker build -t git.zatch.ru/tsivinsky/podcaster:{{tag}} .
|
||||||
|
|
||||||
|
push tag="latest":
|
||||||
|
docker push git.zatch.ru/tsivinsky/podcaster:{{tag}}
|
||||||
|
|
||||||
run:
|
run:
|
||||||
docker run -p 5000:5000 --name podcaster -e PODCASTS_DIRPATH=/podcasts -e DB_PATH=/db/sqlite.db -v ./db:/db -v ./podcasts:/podcasts podcaster:latest
|
docker run -p 5000:5000 --name podcaster -e PODCASTS_DIRPATH=/podcasts -e DB_PATH=/db/sqlite.db -v ./db:/db -v ./podcasts:/podcasts podcaster:latest
|
||||||
|
|||||||
161
main.go
161
main.go
@@ -5,19 +5,39 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
_ "time/tzdata"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type OrderType = string
|
||||||
|
|
||||||
|
const (
|
||||||
|
OrderTypeAsc OrderType = "ASC"
|
||||||
|
OrderTypeDesc OrderType = "DESC"
|
||||||
|
)
|
||||||
|
|
||||||
|
func validateOrderType(orderType string) OrderType {
|
||||||
|
switch strings.ToLower(orderType) {
|
||||||
|
case "asc":
|
||||||
|
return OrderTypeAsc
|
||||||
|
case "desc":
|
||||||
|
return OrderTypeDesc
|
||||||
|
default:
|
||||||
|
return OrderTypeAsc
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Podcast struct {
|
type Podcast struct {
|
||||||
ID int64 `json:"id" db:"id"`
|
ID int64 `json:"id" db:"id"`
|
||||||
Name string `json:"name" db:"name"`
|
Name string `json:"name" db:"name"`
|
||||||
@@ -37,6 +57,7 @@ type Episode struct {
|
|||||||
Url string `json:"url" db:"url"`
|
Url string `json:"url" db:"url"`
|
||||||
PodcastId int64 `json:"podcastId" db:"podcast_id"`
|
PodcastId int64 `json:"podcastId" db:"podcast_id"`
|
||||||
Number int `json:"number" db:"number"`
|
Number int `json:"number" db:"number"`
|
||||||
|
Listened bool `json:"listened" db:"listened"`
|
||||||
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
CreatedAt time.Time `json:"createdAt" db:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,8 +218,23 @@ func downloadEpisodeAudioFile(url string) ([]byte, error) {
|
|||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPodcastEpisodes(db *sqlx.DB, podcastId int64) ([]*Episode, error) {
|
func getPodcastEpisodes(db *sqlx.DB, podcastId int64, orderBy string, orderType OrderType) ([]*Episode, error) {
|
||||||
rows, err := db.Queryx("SELECT * FROM episodes WHERE podcast_id = ?", podcastId)
|
allowedColumns := map[string]bool{
|
||||||
|
"title": true,
|
||||||
|
"pubdate": true,
|
||||||
|
"number": true,
|
||||||
|
"created_at": true,
|
||||||
|
}
|
||||||
|
if !allowedColumns[orderBy] {
|
||||||
|
orderBy = "pubdate"
|
||||||
|
}
|
||||||
|
|
||||||
|
orderType = validateOrderType(orderType)
|
||||||
|
|
||||||
|
query := fmt.Sprintf("SELECT * FROM episodes WHERE podcast_id = :podcast_id ORDER BY %s %s", orderBy, orderType)
|
||||||
|
rows, err := db.NamedQuery(query, map[string]any{
|
||||||
|
"podcast_id": podcastId,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to query db: %v", err)
|
return nil, fmt.Errorf("failed to query db: %v", err)
|
||||||
}
|
}
|
||||||
@@ -226,13 +262,11 @@ func deletePodcastById(db *sqlx.DB, podcastId int64) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
//go:embed views
|
//go:embed web/dist/*
|
||||||
viewsFS embed.FS
|
webFS embed.FS
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tmpl := template.Must(template.ParseFS(viewsFS, "**/*.html"))
|
|
||||||
|
|
||||||
podcastsDirPath := os.Getenv("PODCASTS_DIRPATH")
|
podcastsDirPath := os.Getenv("PODCASTS_DIRPATH")
|
||||||
|
|
||||||
dbPath := os.Getenv("DB_PATH")
|
dbPath := os.Getenv("DB_PATH")
|
||||||
@@ -275,6 +309,8 @@ func main() {
|
|||||||
);
|
);
|
||||||
`)
|
`)
|
||||||
|
|
||||||
|
db.Exec(`ALTER TABLE episodes ADD COLUMN listened boolean default false;`)
|
||||||
|
|
||||||
go func(db *sqlx.DB) {
|
go func(db *sqlx.DB) {
|
||||||
for {
|
for {
|
||||||
podcasts, err := getPodcasts(db)
|
podcasts, err := getPodcasts(db)
|
||||||
@@ -334,20 +370,21 @@ func main() {
|
|||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
|
||||||
podcasts, err := getPodcasts(db)
|
path := r.URL.Path
|
||||||
|
if path == "/" {
|
||||||
|
path = "/index.html"
|
||||||
|
}
|
||||||
|
path = "web/dist" + path
|
||||||
|
|
||||||
|
_, err := webFS.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
path = "web/dist/index.html"
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpl.ExecuteTemplate(w, "index.html", struct {
|
http.ServeFileFS(w, r, webFS, path)
|
||||||
Podcasts []*Podcast
|
|
||||||
}{
|
|
||||||
Podcasts: podcasts,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("GET /podcasts", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /api/podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||||
podcasts, err := getPodcasts(db)
|
podcasts, err := getPodcasts(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
@@ -357,35 +394,7 @@ func main() {
|
|||||||
sendJSON(w, podcasts, 200)
|
sendJSON(w, podcasts, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("GET /podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("POST /api/podcasts", func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.Atoi(r.PathValue("id"))
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
podcast, err := getPodcastById(db, int64(id))
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
episodes, err := getPodcastEpisodes(db, podcast.ID)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, err.Error(), 404)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl.ExecuteTemplate(w, "podcast.html", struct {
|
|
||||||
Podcast *Podcast
|
|
||||||
Episodes []*Episode
|
|
||||||
}{
|
|
||||||
Podcast: podcast,
|
|
||||||
Episodes: episodes,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
mux.HandleFunc("POST /podcasts", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
var body struct {
|
var body struct {
|
||||||
Feed string `json:"feed"`
|
Feed string `json:"feed"`
|
||||||
}
|
}
|
||||||
@@ -430,14 +439,30 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("GET /podcasts/{id}/episodes", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("GET /api/podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id, err := strconv.Atoi(r.PathValue("id"))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
podcast, err := getPodcastById(db, int64(id))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "podcast not found", 404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sendJSON(w, podcast, 200)
|
||||||
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("GET /api/podcasts/{id}/episodes", func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.Atoi(r.PathValue("id"))
|
id, err := strconv.Atoi(r.PathValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 404)
|
http.Error(w, err.Error(), 404)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
episodes, err := getPodcastEpisodes(db, int64(id))
|
episodes, err := getPodcastEpisodes(db, int64(id), "pubdate", OrderTypeDesc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
@@ -446,7 +471,7 @@ func main() {
|
|||||||
sendJSON(w, episodes, 200)
|
sendJSON(w, episodes, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
mux.HandleFunc("DELETE /podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("DELETE /api/podcasts/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.Atoi(r.PathValue("id"))
|
id, err := strconv.Atoi(r.PathValue("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), 404)
|
http.Error(w, err.Error(), 404)
|
||||||
@@ -474,6 +499,46 @@ func main() {
|
|||||||
}{true}, 200)
|
}{true}, 200)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("PATCH /api/episodes/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body struct {
|
||||||
|
Listened *bool `json:"listened"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fields := map[string]any{}
|
||||||
|
|
||||||
|
if body.Listened != nil {
|
||||||
|
fields["listened"] = &body.Listened
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fields) == 0 {
|
||||||
|
sendJSON(w, struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
}{true}, 200)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fields["id"] = r.PathValue("id")
|
||||||
|
|
||||||
|
f := []string{}
|
||||||
|
for field := range fields {
|
||||||
|
f = append(f, fmt.Sprintf("%s = :%s", field, field))
|
||||||
|
}
|
||||||
|
q := fmt.Sprintf("UPDATE episodes SET %s WHERE id = :id", strings.Join(f, ", "))
|
||||||
|
_, err := db.NamedExec(q, fields)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
sendJSON(w, struct {
|
||||||
|
Ok bool `json:"ok"`
|
||||||
|
}{true}, 200)
|
||||||
|
})
|
||||||
|
|
||||||
fmt.Println("starting http server on http://localhost:5000")
|
fmt.Println("starting http server on http://localhost:5000")
|
||||||
if err := http.ListenAndServe(":5000", mux); err != nil {
|
if err := http.ListenAndServe(":5000", mux); err != nil {
|
||||||
log.Fatalf("failed to start http server: %v\n", err)
|
log.Fatalf("failed to start http server: %v\n", err)
|
||||||
|
|||||||
@@ -1,94 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<title>podcaster</title>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
*,
|
|
||||||
*::before,
|
|
||||||
*::after {
|
|
||||||
box-sizing: border-box;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
max-width: 1440px;
|
|
||||||
width: 100%;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast-form {
|
|
||||||
margin-top: 12px;
|
|
||||||
display: flex;
|
|
||||||
gap: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast-form__input {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast-form__btn {
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcasts {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(auto-fit, 250px);
|
|
||||||
gap: 12px;
|
|
||||||
margin-top: 20px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast__cover {
|
|
||||||
width: 100%;
|
|
||||||
aspect-ratio: 1/1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media screen and (max-width: 1024px) {
|
|
||||||
.podcast-form {
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
.podcast-form__btn {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@media screen and (max-width: 528px) {
|
|
||||||
.podcasts {
|
|
||||||
grid-template-columns: 1fr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>podcaster</h1>
|
|
||||||
|
|
||||||
<form method="POST" action="/podcasts" class="podcast-form">
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
inputmode="url"
|
|
||||||
placeholder="rss feed"
|
|
||||||
name="feed"
|
|
||||||
class="podcast-form__input"
|
|
||||||
/>
|
|
||||||
<button type="submit" class="podcast-form__btn">Add podcast</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<div class="podcasts">
|
|
||||||
{{range .Podcasts}}
|
|
||||||
<a href="/podcasts/{{.ID}}" class="podcast">
|
|
||||||
<img src="{{.Image}}" alt="" class="podcast__cover" />
|
|
||||||
<span>{{.Name}}</span>
|
|
||||||
</a>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
||||||
<title>{{.Podcast.Name}}</title>
|
|
||||||
|
|
||||||
<style>
|
|
||||||
*,
|
|
||||||
*::before,
|
|
||||||
*::after {
|
|
||||||
box-sizing: border-box;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
max-width: 1440px;
|
|
||||||
width: 100%;
|
|
||||||
margin: 0 auto;
|
|
||||||
padding: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header {
|
|
||||||
margin-top: 12px;
|
|
||||||
display: flex;
|
|
||||||
gap: 8px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header img {
|
|
||||||
width: 300px;
|
|
||||||
aspect-ration: 1/1;
|
|
||||||
}
|
|
||||||
|
|
||||||
.header .info {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.episodes {
|
|
||||||
margin-top: 24px;
|
|
||||||
padding: 10px;
|
|
||||||
border-top: 1px solid;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1><a href="/">podcaster</a></h1>
|
|
||||||
|
|
||||||
<div class="header">
|
|
||||||
<img src="{{.Podcast.Image}}" alt="" />
|
|
||||||
<div class="info">
|
|
||||||
<h2>{{.Podcast.Name}}</h2>
|
|
||||||
<p>{{.Podcast.Description}}</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="episodes">
|
|
||||||
{{range .Episodes}}
|
|
||||||
<div>
|
|
||||||
<span>{{.Title}}</span>
|
|
||||||
</div>
|
|
||||||
{{end}}
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
24
web/.gitignore
vendored
Normal file
24
web/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
pnpm-debug.log*
|
||||||
|
lerna-debug.log*
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
dist
|
||||||
|
dist-ssr
|
||||||
|
*.local
|
||||||
|
|
||||||
|
# Editor directories and files
|
||||||
|
.vscode/*
|
||||||
|
!.vscode/extensions.json
|
||||||
|
.idea
|
||||||
|
.DS_Store
|
||||||
|
*.suo
|
||||||
|
*.ntvs*
|
||||||
|
*.njsproj
|
||||||
|
*.sln
|
||||||
|
*.sw?
|
||||||
23
web/eslint.config.js
Normal file
23
web/eslint.config.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import js from "@eslint/js";
|
||||||
|
import globals from "globals";
|
||||||
|
import reactHooks from "eslint-plugin-react-hooks";
|
||||||
|
import reactRefresh from "eslint-plugin-react-refresh";
|
||||||
|
import tseslint from "typescript-eslint";
|
||||||
|
import { defineConfig, globalIgnores } from "eslint/config";
|
||||||
|
|
||||||
|
export default defineConfig([
|
||||||
|
globalIgnores(["dist"]),
|
||||||
|
{
|
||||||
|
files: ["**/*.{ts,tsx}"],
|
||||||
|
extends: [
|
||||||
|
js.configs.recommended,
|
||||||
|
tseslint.configs.recommended,
|
||||||
|
reactHooks.configs.flat.recommended,
|
||||||
|
reactRefresh.configs.vite,
|
||||||
|
],
|
||||||
|
languageOptions: {
|
||||||
|
ecmaVersion: 2020,
|
||||||
|
globals: globals.browser,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
13
web/index.html
Normal file
13
web/index.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>web</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script type="module" src="/src/main.tsx"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
37
web/package.json
Normal file
37
web/package.json
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"name": "web",
|
||||||
|
"private": true,
|
||||||
|
"version": "0.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc -b && vite build",
|
||||||
|
"lint": "eslint .",
|
||||||
|
"preview": "vite preview"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@tanstack/react-query": "^5.90.21",
|
||||||
|
"dayjs": "^1.11.19",
|
||||||
|
"react": "^19.2.0",
|
||||||
|
"react-dom": "^19.2.0",
|
||||||
|
"react-hook-form": "^7.71.2",
|
||||||
|
"react-router": "^7.13.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@eslint/js": "^9.39.1",
|
||||||
|
"@tailwindcss/vite": "^4.2.1",
|
||||||
|
"@types/node": "^24.10.1",
|
||||||
|
"@types/react": "^19.2.7",
|
||||||
|
"@types/react-dom": "^19.2.3",
|
||||||
|
"@vitejs/plugin-react": "^5.1.1",
|
||||||
|
"babel-plugin-react-compiler": "^1.0.0",
|
||||||
|
"eslint": "^9.39.1",
|
||||||
|
"eslint-plugin-react-hooks": "^7.0.1",
|
||||||
|
"eslint-plugin-react-refresh": "^0.4.24",
|
||||||
|
"globals": "^16.5.0",
|
||||||
|
"tailwindcss": "^4.2.1",
|
||||||
|
"typescript": "~5.9.3",
|
||||||
|
"typescript-eslint": "^8.48.0",
|
||||||
|
"vite": "^7.3.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
2475
web/pnpm-lock.yaml
generated
Normal file
2475
web/pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
41
web/src/api/episodes.ts
Normal file
41
web/src/api/episodes.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
export type EpisodeDetail = {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
pubDate: string;
|
||||||
|
guid: string;
|
||||||
|
url: string;
|
||||||
|
podcastId: number;
|
||||||
|
number: number;
|
||||||
|
listened: boolean;
|
||||||
|
createdAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const usePodcastEpisodesQuery = (
|
||||||
|
id: number | string | null | undefined,
|
||||||
|
) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["podcasts", id, "episodes"],
|
||||||
|
enabled: typeof id !== "undefined" && id !== null,
|
||||||
|
queryFn: async () => {
|
||||||
|
const resp = await fetch(`/api/podcasts/${id}/episodes`);
|
||||||
|
return (await resp.json()) as EpisodeDetail[];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useUpdateEpisodeMutation = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async ({
|
||||||
|
id,
|
||||||
|
...data
|
||||||
|
}: Partial<EpisodeDetail> & { id: number }) => {
|
||||||
|
const resp = await fetch(`/api/episodes/${id}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await resp.json();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
60
web/src/api/podcasts.ts
Normal file
60
web/src/api/podcasts.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
export type PodcastDetail = {
|
||||||
|
id: number;
|
||||||
|
name: string;
|
||||||
|
description: string;
|
||||||
|
feed: string;
|
||||||
|
language: string;
|
||||||
|
link: string;
|
||||||
|
image: string;
|
||||||
|
createdAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const usePodcastsQuery = () => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["podcasts"],
|
||||||
|
queryFn: async () => {
|
||||||
|
const resp = await fetch("/api/podcasts");
|
||||||
|
return (await resp.json()) as PodcastDetail[];
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const usePodcastQuery = (id: number | string | null | undefined) => {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["podcasts", id],
|
||||||
|
enabled: typeof id !== "undefined" && id !== null,
|
||||||
|
queryFn: async () => {
|
||||||
|
const resp = await fetch(`/api/podcasts/${id}`);
|
||||||
|
return (await resp.json()) as PodcastDetail;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CreatePodcastData = {
|
||||||
|
feed: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useCreatePodcastMutation = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async (data: CreatePodcastData) => {
|
||||||
|
const resp = await fetch("/api/podcasts", {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await resp.json();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useDeletePodcastMutation = () => {
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async ({ id }: { id: number }) => {
|
||||||
|
const resp = await fetch(`/api/podcasts/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
});
|
||||||
|
return await resp.json();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
44
web/src/components/NewPodcastForm.tsx
Normal file
44
web/src/components/NewPodcastForm.tsx
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import {
|
||||||
|
useCreatePodcastMutation,
|
||||||
|
type CreatePodcastData,
|
||||||
|
} from "../api/podcasts";
|
||||||
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
export const NewPodcastForm = () => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
const form = useForm<CreatePodcastData>({
|
||||||
|
defaultValues: {
|
||||||
|
feed: "",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mutation = useCreatePodcastMutation();
|
||||||
|
|
||||||
|
const onSubmit = form.handleSubmit((data) => {
|
||||||
|
mutation.mutate(data, {
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["podcasts"] });
|
||||||
|
form.reset();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<form className="mt-3 flex gap-1" onSubmit={onSubmit}>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
inputMode="url"
|
||||||
|
placeholder="rss feed"
|
||||||
|
className="w-full"
|
||||||
|
{...form.register("feed", {
|
||||||
|
required: true,
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
<button type="submit" className="whitespace-nowrap">
|
||||||
|
Add podcast
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
);
|
||||||
|
};
|
||||||
17
web/src/icons/fastforward.tsx
Normal file
17
web/src/icons/fastforward.tsx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export const FastForwardIcon = ({
|
||||||
|
size = 32,
|
||||||
|
color = "#000000",
|
||||||
|
}: {
|
||||||
|
size?: number;
|
||||||
|
color?: string;
|
||||||
|
}) => (
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width={size}
|
||||||
|
height={size}
|
||||||
|
fill={color}
|
||||||
|
viewBox="0 0 256 256"
|
||||||
|
>
|
||||||
|
<path d="M248.67,114.66,160.48,58.5A15.91,15.91,0,0,0,136,71.84v37.3L56.48,58.5A15.91,15.91,0,0,0,32,71.84V184.16A15.92,15.92,0,0,0,56.48,197.5L136,146.86v37.3a15.92,15.92,0,0,0,24.48,13.34l88.19-56.16a15.8,15.8,0,0,0,0-26.68ZM48,183.94V72.07L135.82,128Zm104,0V72.07L239.82,128Z"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
17
web/src/icons/pause.tsx
Normal file
17
web/src/icons/pause.tsx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export const PauseIcon = ({
|
||||||
|
size = 32,
|
||||||
|
color = "#000000",
|
||||||
|
}: {
|
||||||
|
size?: number;
|
||||||
|
color?: string;
|
||||||
|
}) => (
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width={size}
|
||||||
|
height={size}
|
||||||
|
fill={color}
|
||||||
|
viewBox="0 0 256 256"
|
||||||
|
>
|
||||||
|
<path d="M200,32H160a16,16,0,0,0-16,16V208a16,16,0,0,0,16,16h40a16,16,0,0,0,16-16V48A16,16,0,0,0,200,32Zm0,176H160V48h40ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Zm0,176H56V48H96Z"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
17
web/src/icons/play.tsx
Normal file
17
web/src/icons/play.tsx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export const PlayIcon = ({
|
||||||
|
size = 32,
|
||||||
|
color = "#000000",
|
||||||
|
}: {
|
||||||
|
size?: number;
|
||||||
|
color?: string;
|
||||||
|
}) => (
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width={size}
|
||||||
|
height={size}
|
||||||
|
fill={color}
|
||||||
|
viewBox="0 0 256 256"
|
||||||
|
>
|
||||||
|
<path d="M232.4,114.49,88.32,26.35a16,16,0,0,0-16.2-.3A15.86,15.86,0,0,0,64,39.87V216.13A15.94,15.94,0,0,0,80,232a16.07,16.07,0,0,0,8.36-2.35L232.4,141.51a15.81,15.81,0,0,0,0-27ZM80,215.94V40l143.83,88Z"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
17
web/src/icons/rewind.tsx
Normal file
17
web/src/icons/rewind.tsx
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
export const RewindIcon = ({
|
||||||
|
size = 32,
|
||||||
|
color = "#000000",
|
||||||
|
}: {
|
||||||
|
size?: number;
|
||||||
|
color?: string;
|
||||||
|
}) => (
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
width={size}
|
||||||
|
height={size}
|
||||||
|
fill={color}
|
||||||
|
viewBox="0 0 256 256"
|
||||||
|
>
|
||||||
|
<path d="M223.77,58a16,16,0,0,0-16.25.53L128,109.14V71.84A15.91,15.91,0,0,0,103.52,58.5L15.33,114.66a15.8,15.8,0,0,0,0,26.68l88.19,56.16A15.91,15.91,0,0,0,128,184.16v-37.3l79.52,50.64A15.91,15.91,0,0,0,232,184.16V71.84A15.83,15.83,0,0,0,223.77,58ZM112,183.93,24.18,128,112,72.06Zm104,0L128.18,128,216,72.06Z"></path>
|
||||||
|
</svg>
|
||||||
|
);
|
||||||
1
web/src/index.css
Normal file
1
web/src/index.css
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import "tailwindcss";
|
||||||
40
web/src/main.tsx
Normal file
40
web/src/main.tsx
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { StrictMode } from "react";
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import "./index.css";
|
||||||
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
|
import { BrowserRouter, Routes } from "react-router";
|
||||||
|
import { Route } from "react-router";
|
||||||
|
import { HomePage } from "./pages/home";
|
||||||
|
import { PodcastPage } from "./pages/podcast";
|
||||||
|
import { PlayerProvider } from "./player/provider";
|
||||||
|
import { Player } from "./player/player";
|
||||||
|
import { Link } from "react-router";
|
||||||
|
|
||||||
|
const queryClient = new QueryClient({
|
||||||
|
defaultOptions: {
|
||||||
|
queries: {
|
||||||
|
retry: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
createRoot(document.getElementById("root")!).render(
|
||||||
|
<StrictMode>
|
||||||
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<PlayerProvider>
|
||||||
|
<div className="max-w-[1440px] w-full mx-auto p-2 relative">
|
||||||
|
<BrowserRouter>
|
||||||
|
<Link to="/" className="w-fit">
|
||||||
|
<h1 className="text-3xl font-semibold">podcaster</h1>
|
||||||
|
</Link>
|
||||||
|
<Routes>
|
||||||
|
<Route path="/" element={<HomePage />} />
|
||||||
|
<Route path="/podcasts/:id" element={<PodcastPage />} />
|
||||||
|
</Routes>
|
||||||
|
</BrowserRouter>
|
||||||
|
</div>
|
||||||
|
<Player />
|
||||||
|
</PlayerProvider>
|
||||||
|
</QueryClientProvider>
|
||||||
|
</StrictMode>,
|
||||||
|
);
|
||||||
25
web/src/pages/home.tsx
Normal file
25
web/src/pages/home.tsx
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { usePodcastsQuery } from "../api/podcasts";
|
||||||
|
import { NewPodcastForm } from "../components/NewPodcastForm";
|
||||||
|
|
||||||
|
export const HomePage = () => {
|
||||||
|
const { data: podcasts } = usePodcastsQuery();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<NewPodcastForm />
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 sm:grid-cols-[repeat(auto-fit,250px)] gap-3 mt-5">
|
||||||
|
{podcasts?.map((podcast) => (
|
||||||
|
<a
|
||||||
|
key={podcast.id}
|
||||||
|
href={`/podcasts/${podcast.id}`}
|
||||||
|
className="block"
|
||||||
|
>
|
||||||
|
<img src={podcast.image} alt="" className="w-full aspect-square" />
|
||||||
|
<span>{podcast.name}</span>
|
||||||
|
</a>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
162
web/src/pages/podcast.tsx
Normal file
162
web/src/pages/podcast.tsx
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
import { useParams } from "react-router";
|
||||||
|
import { useDeletePodcastMutation, usePodcastQuery } from "../api/podcasts";
|
||||||
|
import {
|
||||||
|
usePodcastEpisodesQuery,
|
||||||
|
useUpdateEpisodeMutation,
|
||||||
|
} from "../api/episodes";
|
||||||
|
import { usePlayerContext } from "../player/context";
|
||||||
|
import { PlayIcon } from "../icons/play";
|
||||||
|
import { PauseIcon } from "../icons/pause";
|
||||||
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { useNavigate } from "react-router";
|
||||||
|
|
||||||
|
export const PodcastPage = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const { id } = useParams<{ id: string }>();
|
||||||
|
|
||||||
|
const { data: podcast } = usePodcastQuery(id);
|
||||||
|
const { data: episodes } = usePodcastEpisodesQuery(id);
|
||||||
|
|
||||||
|
const player = usePlayerContext();
|
||||||
|
|
||||||
|
const updateEpisodeMutation = useUpdateEpisodeMutation();
|
||||||
|
const deletePodcastMutation = useDeletePodcastMutation();
|
||||||
|
|
||||||
|
const handleDeletePodcast = () => {
|
||||||
|
if (
|
||||||
|
!confirm(
|
||||||
|
"Are you sure you want to delete this podcast with all episodes?",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
if (typeof id === "undefined") return;
|
||||||
|
|
||||||
|
deletePodcastMutation.mutate(
|
||||||
|
{
|
||||||
|
id: parseInt(id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["podcasts"] });
|
||||||
|
navigate("/");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleMarkCompleted = (episodeId: number) => {
|
||||||
|
updateEpisodeMutation.mutate(
|
||||||
|
{
|
||||||
|
id: episodeId,
|
||||||
|
listened: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["podcasts", id, "episodes"],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleUnmarkCompleted = (episodeId: number) => {
|
||||||
|
updateEpisodeMutation.mutate(
|
||||||
|
{
|
||||||
|
id: episodeId,
|
||||||
|
listened: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onSuccess() {
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["podcasts", id, "episodes"],
|
||||||
|
});
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="mt-3 flex gap-2">
|
||||||
|
<img src={podcast?.image} alt="" className="w-[300px] aspect-square" />
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<h2 className="text-2xl font-semibold">{podcast?.name}</h2>
|
||||||
|
<p>{podcast?.description}</p>
|
||||||
|
<div className="mt-auto flex items-center gap-2">
|
||||||
|
<a
|
||||||
|
href={podcast?.link}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
className="block bg-blue-500 text-white py-1 px-4 rounded"
|
||||||
|
>
|
||||||
|
Website
|
||||||
|
</a>
|
||||||
|
<button
|
||||||
|
className="bg-red-500 text-white py-1 px-4 rounded w-fit cursor-pointer"
|
||||||
|
disabled={deletePodcastMutation.isPending}
|
||||||
|
onClick={handleDeletePodcast}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mt-6 p-2.5 border-t flex flex-col gap-1">
|
||||||
|
{episodes?.map((episode) => (
|
||||||
|
<div key={episode.id} className="flex justify-between items-center">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{player.status === "playing" &&
|
||||||
|
player.episode?.id === episode.id ? (
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
player.setStatus("paused");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<PauseIcon size={24} />
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
player.setStatus("playing");
|
||||||
|
player.setEpisode(episode);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<PlayIcon size={24} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<span>{episode.title}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{episode.listened ? (
|
||||||
|
<button
|
||||||
|
className="text-green-700"
|
||||||
|
disabled={
|
||||||
|
updateEpisodeMutation.isPending &&
|
||||||
|
updateEpisodeMutation.variables.id === episode.id
|
||||||
|
}
|
||||||
|
onClick={() => handleUnmarkCompleted(episode.id)}
|
||||||
|
>
|
||||||
|
Unmark completed
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
disabled={
|
||||||
|
updateEpisodeMutation.isPending &&
|
||||||
|
updateEpisodeMutation.variables.id === episode.id
|
||||||
|
}
|
||||||
|
onClick={() => handleMarkCompleted(episode.id)}
|
||||||
|
>
|
||||||
|
Mark completed
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<span>{new Date(episode.createdAt).toLocaleString()}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
21
web/src/player/context.ts
Normal file
21
web/src/player/context.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { createContext, useContext } from "react";
|
||||||
|
import type { EpisodeDetail } from "../api/episodes";
|
||||||
|
|
||||||
|
export type PlayerStatus = "stopped" | "playing" | "paused";
|
||||||
|
|
||||||
|
export type PlayerContext = {
|
||||||
|
status: PlayerStatus;
|
||||||
|
setStatus: (status: PlayerStatus) => void;
|
||||||
|
episode: EpisodeDetail | null;
|
||||||
|
setEpisode: (episode: EpisodeDetail | null) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const playerContext = createContext<PlayerContext | null>(null);
|
||||||
|
|
||||||
|
export const usePlayerContext = () => {
|
||||||
|
const ctx = useContext(playerContext);
|
||||||
|
if (!ctx) {
|
||||||
|
throw new Error("No PlayerProvider in component tree");
|
||||||
|
}
|
||||||
|
return ctx;
|
||||||
|
};
|
||||||
149
web/src/player/player.tsx
Normal file
149
web/src/player/player.tsx
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
import { usePlayerContext } from "./context";
|
||||||
|
import { RewindIcon } from "../icons/rewind";
|
||||||
|
import { FastForwardIcon } from "../icons/fastforward";
|
||||||
|
import { PauseIcon } from "../icons/pause";
|
||||||
|
import { PlayIcon } from "../icons/play";
|
||||||
|
|
||||||
|
export const Player = () => {
|
||||||
|
const { status, episode, setStatus } = usePlayerContext();
|
||||||
|
|
||||||
|
const audioRef = useRef<HTMLAudioElement>(null);
|
||||||
|
|
||||||
|
const [currentTime, setCurrentTime] = useState(0);
|
||||||
|
const [duration, setDuration] = useState(0);
|
||||||
|
const [showTimeLeft, setShowTimeLeft] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!episode) return;
|
||||||
|
audioRef.current = new Audio(episode.url);
|
||||||
|
|
||||||
|
audioRef.current.addEventListener("timeupdate", (e) => {
|
||||||
|
const t = e.target as HTMLAudioElement;
|
||||||
|
setCurrentTime(t.currentTime);
|
||||||
|
});
|
||||||
|
|
||||||
|
audioRef.current.addEventListener("loadeddata", (e) => {
|
||||||
|
const t = e.target as HTMLAudioElement;
|
||||||
|
setDuration(t.duration);
|
||||||
|
});
|
||||||
|
}, [episode]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!audioRef.current) return;
|
||||||
|
|
||||||
|
if (status === "playing") {
|
||||||
|
audioRef.current.play();
|
||||||
|
} else {
|
||||||
|
audioRef.current.pause();
|
||||||
|
}
|
||||||
|
}, [status, audioRef]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyPress = (e: KeyboardEvent) => {
|
||||||
|
switch (e.key) {
|
||||||
|
case "ArrowLeft":
|
||||||
|
if (audioRef.current) {
|
||||||
|
audioRef.current.currentTime -= 10;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "ArrowRight":
|
||||||
|
if (audioRef.current) {
|
||||||
|
audioRef.current.currentTime += 10;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", handleKeyPress);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.addEventListener("keydown", handleKeyPress);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const progress = currentTime / duration;
|
||||||
|
const timeLeft = duration - currentTime;
|
||||||
|
|
||||||
|
if (status === "stopped") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="fixed bottom-0 left-0 right-0 z-50">
|
||||||
|
<div className="max-w-[1440px] w-full mx-auto">
|
||||||
|
<div className="bg-white py-2 px-4 flex flex-col">
|
||||||
|
<div className="w-full flex items-center gap-4">
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
{status === "playing" ? (
|
||||||
|
<button onClick={() => setStatus("paused")}>
|
||||||
|
<PauseIcon size={24} />
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button onClick={() => setStatus("playing")}>
|
||||||
|
<PlayIcon size={24} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!audioRef.current) return;
|
||||||
|
audioRef.current.currentTime -= 10;
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<RewindIcon size={24} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
if (!audioRef.current) return;
|
||||||
|
audioRef.current.currentTime += 10;
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<FastForwardIcon size={24} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className="w-full h-2 flex items-center rounded-lg bg-neutral-200 overflow-hidden hover:h-5 transition-[height] ease-linear"
|
||||||
|
onClick={(e) => {
|
||||||
|
if (!audioRef.current) return;
|
||||||
|
|
||||||
|
const rect = e.currentTarget.getBoundingClientRect();
|
||||||
|
const left = e.clientX - rect.x;
|
||||||
|
const percent = Math.floor((left / rect.width) * 100);
|
||||||
|
const newTime = (duration * percent) / 100;
|
||||||
|
audioRef.current.currentTime = newTime;
|
||||||
|
setCurrentTime(newTime);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
style={{ width: `${progress * 100}%` }}
|
||||||
|
className="bg-red-500 h-full"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
className="min-w-[70px] text-right cursor-pointer"
|
||||||
|
onClick={() => setShowTimeLeft((prev) => !prev)}
|
||||||
|
>
|
||||||
|
{showTimeLeft ? "-" : ""}
|
||||||
|
{formatTime(showTimeLeft ? timeLeft : currentTime)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3 className="text-base font-semibold mt-2">{episode?.title}</h3>
|
||||||
|
<p className="opacity-50 mt-1">{formatTime(duration)}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const formatTime = (time: number): string => {
|
||||||
|
let hours = Math.floor(time / 3600);
|
||||||
|
let minutes = Math.floor((time % 3600) / 60);
|
||||||
|
let seconds = Math.floor(time % 60);
|
||||||
|
|
||||||
|
const h = String(hours).padStart(2, "0");
|
||||||
|
const m = String(minutes).padStart(2, "0");
|
||||||
|
const s = String(seconds).padStart(2, "0");
|
||||||
|
|
||||||
|
return [h, m, s].join(":");
|
||||||
|
};
|
||||||
23
web/src/player/provider.tsx
Normal file
23
web/src/player/provider.tsx
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { useState, type ReactNode } from "react";
|
||||||
|
import {
|
||||||
|
type PlayerContext,
|
||||||
|
type PlayerStatus,
|
||||||
|
playerContext,
|
||||||
|
} from "./context";
|
||||||
|
import type { EpisodeDetail } from "../api/episodes";
|
||||||
|
|
||||||
|
export const PlayerProvider = ({ children }: { children: ReactNode }) => {
|
||||||
|
const [status, setStatus] = useState<PlayerStatus>("stopped");
|
||||||
|
const [episode, setEpisode] = useState<EpisodeDetail | null>(null);
|
||||||
|
|
||||||
|
const value = {
|
||||||
|
status,
|
||||||
|
setStatus,
|
||||||
|
episode,
|
||||||
|
setEpisode,
|
||||||
|
} satisfies PlayerContext;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<playerContext.Provider value={value}>{children}</playerContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
28
web/tsconfig.app.json
Normal file
28
web/tsconfig.app.json
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||||
|
"target": "ES2022",
|
||||||
|
"useDefineForClassFields": true,
|
||||||
|
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"types": ["vite/client"],
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"noEmit": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
7
web/tsconfig.json
Normal file
7
web/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files": [],
|
||||||
|
"references": [
|
||||||
|
{ "path": "./tsconfig.app.json" },
|
||||||
|
{ "path": "./tsconfig.node.json" }
|
||||||
|
]
|
||||||
|
}
|
||||||
26
web/tsconfig.node.json
Normal file
26
web/tsconfig.node.json
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||||
|
"target": "ES2023",
|
||||||
|
"lib": ["ES2023"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"types": ["node"],
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"verbatimModuleSyntax": true,
|
||||||
|
"moduleDetection": "force",
|
||||||
|
"noEmit": true,
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"erasableSyntaxOnly": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedSideEffectImports": true
|
||||||
|
},
|
||||||
|
"include": ["vite.config.ts"]
|
||||||
|
}
|
||||||
15
web/vite.config.ts
Normal file
15
web/vite.config.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { defineConfig } from "vite";
|
||||||
|
import react from "@vitejs/plugin-react";
|
||||||
|
import tailwindcss from "@tailwindcss/vite";
|
||||||
|
|
||||||
|
// https://vite.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [
|
||||||
|
tailwindcss(),
|
||||||
|
react({
|
||||||
|
babel: {
|
||||||
|
plugins: [["babel-plugin-react-compiler"]],
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user