Skip to content

Commit

Permalink
Fix sitemap (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcraigk authored Oct 7, 2024
1 parent c900ec5 commit 17b029c
Show file tree
Hide file tree
Showing 16 changed files with 81 additions and 66 deletions.
5 changes: 2 additions & 3 deletions app/javascript/components/PlaylistIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,19 @@ const PlaylistIndex = () => {
totalEntries,
page,
sortOption,
filter: initialFilter,
filter,
perPage
} = useLoaderData();
const navigate = useNavigate();
const [searchTerm, setSearchTerm] = useState("");
const [filter, setFilter] = useState(initialFilter);
const { user } = useOutletContext();
const {
tempPerPage,
handlePageClick,
handleSortChange,
handlePerPageInputChange,
handlePerPageBlurOrEnter
} = paginationHelper(page, sortOption, perPage);
} = paginationHelper(page, sortOption, perPage, "", filter);

const handleFilterChange = (event) => {
setFilter(event.target.value);
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/components/Tracks.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { useOutletContext, Link } from "react-router-dom";
import { formatDurationTrack, formatDurationShow, formatDate } from "./helpers/utils";
import { formatDurationTrack, formatDurationShow, formatDate, truncate } from "./helpers/utils";
import TagBadges from "./controls/TagBadges";
import HighlightedText from "./controls/HighlightedText";
import LikeButton from "./controls/LikeButton";
Expand Down Expand Up @@ -106,7 +106,7 @@ const Tracks = ({ tracks, viewStyle, numbering = false, omitSecondary = false, h
)
}
<HighlightedText
text={track.title}
text={truncate(track.title, 50)}
highlight={highlight}
/>
</span>
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/VenueIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const VenueIndex = () => {
handleSortChange,
handlePerPageInputChange,
handlePerPageBlurOrEnter
} = paginationHelper(page, sortOption, perPage);
} = paginationHelper(page, sortOption, perPage, firstChar);

const handleFirstCharChange = (event) => {
navigate(`?page=1&sort=${sortOption}&first_char=${event.target.value}&per_page=${perPage}`);
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/controls/HighlightedText.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";

const HighlightedText = ({ text, highlight }) => {
if (!highlight) return <span>{text}</span>;
if (!highlight) return <>{text}</>;

const escapedHighlight = highlight.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
const parts = text.split(new RegExp(`(${escapedHighlight})`, 'gi'));
Expand Down
27 changes: 13 additions & 14 deletions app/javascript/components/helpers/pagination.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
import { useState } from "react";
import { useNavigate } from "react-router-dom";

export const paginationHelper = (initialPage, initialSortOption, initialPerPage) => {
export const paginationHelper = (initialPage, initialSortOption, initialPerPage, firstChar = "", filter = "all") => {
const navigate = useNavigate();

const [currentPage, setCurrentPage] = useState(initialPage); // Use initialPage
const [currentSortOption, setCurrentSortOption] = useState(initialSortOption); // Use initialSortOption
const [tempPerPage, setTempPerPage] = useState(initialPerPage); // Use initialPerPage
const [currentPage, setCurrentPage] = useState(initialPage);
const [currentSortOption, setCurrentSortOption] = useState(initialSortOption);
const [tempPerPage, setTempPerPage] = useState(initialPerPage);

const handlePageClick = (data, firstChar = '') => {
const handlePageClick = (data) => {
const selectedPage = data.selected + 1;
setCurrentPage(selectedPage); // Update current page
navigate(`?page=${selectedPage}&sort=${currentSortOption}&first_char=${firstChar}&per_page=${tempPerPage}`);
setCurrentPage(selectedPage);
navigate(`?page=${selectedPage}&sort=${currentSortOption}&first_char=${firstChar}&per_page=${tempPerPage}&filter=${filter}`);
};

const handleSortChange = (event, firstChar = '') => {
const handleSortChange = (event) => {
const newSortOption = event.target.value;
setCurrentSortOption(newSortOption); // Update current sort option
navigate(`?page=1&sort=${newSortOption}&first_char=${firstChar}&per_page=${tempPerPage}`);
setCurrentSortOption(newSortOption);
navigate(`?page=1&sort=${newSortOption}&first_char=${firstChar}&per_page=${tempPerPage}&filter=${filter}`);
};

const handlePerPageInputChange = (e) => {
setTempPerPage(e.target.value);
};

const submitPerPage = (firstChar = '') => {
const submitPerPage = () => {
if (tempPerPage && !isNaN(tempPerPage) && tempPerPage > 0) {
navigate(`?page=1&sort=${currentSortOption}&first_char=${firstChar}&per_page=${tempPerPage}`);
navigate(`?page=1&sort=${currentSortOption}&first_char=${firstChar}&per_page=${tempPerPage}&filter=${filter}`);
}
};

const handlePerPageBlurOrEnter = (e, firstChar = '') => {
const handlePerPageBlurOrEnter = (e) => {
if (e.type === "blur" || (e.type === "keydown" && e.key === "Enter")) {
e.preventDefault();
submitPerPage(firstChar);
e.target.blur();
}
};
Expand Down
5 changes: 5 additions & 0 deletions app/javascript/components/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,8 @@ export const parseTimeParam = (t) => {
}
return Number(t);
};

export const truncate = (str, n) => {
if (!str) return "";
return str.length > n ? str.slice(0, n) + "..." : str;
};
5 changes: 5 additions & 0 deletions app/javascript/stylesheets/bulma-overrides.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
background-color: white;
}

.input, .button {
border-color: $header-gray;
color: $text-gray;
}

.dropdown-item {
cursor: pointer;
text-decoration: none !important;
Expand Down
7 changes: 4 additions & 3 deletions app/jobs/album_zip_cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ def perform
total_size = get_total_size
return if total_size <= App.album_zip_disk_limit

# Continue deleting the oldest blobs until we are under the disk limit
# Continue deleting the oldest attachments until we are under the disk limit
while total_size > App.album_zip_disk_limit
oldest_blob =
ActiveStorage::Blob.joins(:attachments)
.where(active_storage_attachments: { name: "album_zip", record_type: "Show" })
.order(:created_at)
.first
break unless oldest_blob
attachment = oldest_blob.attachments.find_by(name: "album_zip", record_type: "Show")
break unless attachment
byte_size = oldest_blob.byte_size
oldest_blob.purge
binding.irb
attachment.destroy
total_size -= byte_size
end
end
Expand Down
5 changes: 3 additions & 2 deletions app/jobs/album_zip_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ class AlbumZipJob
attr_reader :show_id

def perform(show_id)
return if total_size > App.album_zip_disk_limit

@show_id = show_id

return show.update(album_zip_requested_at: nil) if total_size > App.album_zip_disk_limit

create_and_attach_album_zip
show.update!(album_zip_requested_at: nil)
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/sitemap_refresh_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class SitemapRefreshJob
include Sidekiq::Job

def perform
system("bundle exec rails -s sitemap:refresh:no_ping")
system("bundle exec rails sitemap:refresh:no_ping")
end
end
7 changes: 3 additions & 4 deletions app/models/concerns/has_cover_art.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ def generate_album!
CoverArtImageService.call(self)
AlbumCoverService.call(self)
tracks.each(&:apply_id3_tags)
# AlbumZipJob.perform_async(id)
end

def album_zip_url
return unless album_zip.attached?
key = album_zip.blob.key
"#{App.base_url}/blob/#{album_zip.blob.key}"
"#{App.content_base_url}/blob/#{album_zip.blob.key}"
end

def cover_art_path
Expand All @@ -48,7 +47,7 @@ def album_zip_path
def cover_art_variant_url(variant)
if cover_art.attached?
key = cover_art.variant(variant).processed.key
"#{App.base_url}/blob/#{key}"
"#{App.content_base_url}/blob/#{key}"
else
placeholder(variant)
end
Expand All @@ -63,7 +62,7 @@ def placeholder(variant)

def attachment_url(attachment, placeholder)
if attachment.attached?
"#{App.base_url}/blob/#{attachment.blob.key}"
"#{App.content_base_url}/blob/#{attachment.blob.key}"
else
path = ActionController::Base.helpers.asset_pack_path \
"static/images/placeholders/#{placeholder}.jpg"
Expand Down
49 changes: 28 additions & 21 deletions app/services/show_importer/orchestrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,31 +99,38 @@ def delete(position)
private

def generate_album_interactively
# Step 1: Generate and confirm cover art prompt
loop do
puts "Generating cover art prompt..."
CoverArtPromptService.call(show)
puts "💬 #{show.cover_art_prompt}"
print "(C)onfirm or (R)edo? "
input = $stdin.gets.chomp.downcase
break if input == "c"
end

# Step 2: Generate and confirm cover art image
loop do
puts "Generating cover art..."
loop do # rubocop:disable Metrics/BlockLength
puts "Generating cover art image..."
CoverArtImageService.call(show)
puts "🏞 #{App.base_url}/blob/#{show.cover_art.blob.key}"
print "(C)onfirm or (R)edo? "
print "(C)onfirm, (R)egenerate, (N)ew prompt, or C(u)stomize prompt? "
input = $stdin.gets.chomp.downcase
break if input == "c"
end
case input
when "r"
next
when "n"
puts "Generating cover art prompt..."
CoverArtPromptService.call(show)
puts "💬 #{show.cover_art_prompt}"
next
when "u"
print "Custom prompt (or blank to use existing): "
prompt = $stdin.gets.chomp
if prompt.present?
puts "New prompt: 💬 #{prompt}"
show.update!(cover_art_prompt: prompt)
else
puts "Using existing prompt"
end
next
else
break
end

puts "Making album..."
AlbumCoverService.call(show)
puts "🌌 #{show.album_cover_url}"
show.tracks.each(&:apply_id3_tags)
# AlbumZipJob.perform_async(show.id)
AlbumCoverService.call(show)
puts "🌌 #{show.album_cover_url}"
show.tracks.each(&:apply_id3_tags)
end
end

def save_song_gaps(show)
Expand Down
6 changes: 4 additions & 2 deletions config/sitemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@
# Tags
add "/tags"
Tag.find_each do |tag|
add "/show_tags/#{tag.slug}", lastmod: tag.show_tags.order(updated_at: :desc).first.updated_at
add "/track_tags/#{tag.slug}", lastmod: tag.track_tags.order(updated_at: :desc).first.updated_at
add "/show_tags/#{tag.slug}",
lastmod: tag.show_tags.order(created_at: :desc).first&.created_at
add "/track_tags/#{tag.slug}",
lastmod: tag.track_tags.order(created_at: :desc).first&.created_at
end

# Shows
Expand Down
9 changes: 3 additions & 6 deletions lib/tasks/shows.rake
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ namespace :shows do
puts "💬 #{show.cover_art_prompt}"
end

# Prompt and cover art
loop do
puts "Generating cover art image..."
CoverArtImageService.call(show)
Expand Down Expand Up @@ -70,14 +71,10 @@ namespace :shows do
end
end

# Album cover
AlbumCoverService.call(show)
puts "🌌 #{show.album_cover_url}"
# Apply cover art to mp3 files
show.tracks.each do |track|
track.apply_id3_tags
end

# AlbumZipJob.perform_async(show.id)
show.tracks.each(&:apply_id3_tags)

puts show.url
pbar.increment
Expand Down
Binary file added public/sitemap.xml.gz
Binary file not shown.
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2683,9 +2683,9 @@ [email protected]:
integrity sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==

electron-to-chromium@^1.5.28:
version "1.5.32"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.32.tgz#4a05ee78e29e240aabaf73a67ce9fe73f52e1bc7"
integrity sha512-M+7ph0VGBQqqpTT2YrabjNKSQ2fEl9PVx6AK3N558gDH9NO8O6XN9SXXFWRo9u9PbEg/bWq+tjXQr+eXmxubCw==
version "1.5.33"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.5.33.tgz#8f64698661240e70fdbc4b032e6085e391f05e09"
integrity sha512-+cYTcFB1QqD4j4LegwLfpCNxifb6dDFUAwk6RsLusCwIaZI6or2f+q8rs5tTB2YC53HhOlIbEaqHMAAC8IOIwA==

emojis-list@^3.0.0:
version "3.0.0"
Expand Down Expand Up @@ -3239,9 +3239,9 @@ http-parser-js@>=0.5.1:
integrity sha512-SGeBX54F94Wgu5RH3X5jsDtf4eHyRogWX1XGT3b4HuW3tQPM4AaBzoUji/4AAJNXCEOWZ5O0DgZmJw1947gD5Q==

http-proxy-middleware@^2.0.3:
version "2.0.6"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.6.tgz#e1a4dd6979572c7ab5a4e4b55095d1f32a74963f"
integrity sha512-ya/UeJ6HVBYxrgYotAZo1KvPWlgB48kUJLDePFeneHsVujFaW5WNj2NgWCAE//B1Dl02BIfYlpNgBy8Kf8Rjmw==
version "2.0.7"
resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.7.tgz#915f236d92ae98ef48278a95dedf17e991936ec6"
integrity sha512-fgVY8AV7qU7z/MmXJ/rxwbrtQH4jBQ9m7kp3llF0liB7glmFeVZFBepQb32T3y8n8k2+AEYuMPCpinYW+/CuRA==
dependencies:
"@types/http-proxy" "^1.17.8"
http-proxy "^1.18.1"
Expand Down

0 comments on commit 17b029c

Please sign in to comment.