Files
redux-scraper/app/views/domain/posts/default/_section_file.html.erb
Dylan Knutson f2f8a9c34a Refactor PostFiles component to use URL parameters and simplify implementation
- Change from hash fragments (#file=2) to URL parameters (?idx=2) for server-side prerendering support
- Simplify React component by removing complex client-side hydration logic
- Remove unnecessary props: totalFiles, hasMultipleFiles (derive from files.length)
- Remove redundant useCallback and popstate handlers
- Update Rails helper to read URL parameter and pass correct initialSelectedIndex
- Maintain all functionality: carousel, keyboard navigation, URL state management
2025-08-09 00:59:26 +00:00

68 lines
2.3 KiB
Plaintext

<% if policy(post).view_file? %>
<% ok_files = post.files.state_ok.order(:created_at).to_a %>
<% current_file = ok_files.first || post.primary_file_for_view %>
<% if ok_files.any? && current_file&.log_entry&.status_code == 200 %>
<!-- React PostFiles Component handles everything -->
<%
# Extract file index from URL parameter
idx_param = params[:idx]
initial_file_index = idx_param.present? ? idx_param.to_i : nil
%>
<%= react_component(
"PostFiles",
{
prerender: true,
props: props_for_post_files(ok_files, initial_file_index: initial_file_index),
html_options: {
id: "post-files-component"
}
}
) %>
<% elsif current_file.present? && (log_entry = current_file.log_entry) %>
<!-- Fallback for error states -->
<section id="file-display-section">
<section class="flex grow justify-center text-slate-500">
<div>
<i class="fa-solid fa-exclamation-triangle"></i>
File error
<% if log_entry.status_code == 404 %>
(404 not found)
<% else %>
(<%= log_entry.status_code %>)
<% end %>
</div>
</section>
</section>
<% elsif current_file.present? && current_file.state_pending? %>
<!-- Fallback for pending state -->
<section id="file-display-section">
<section class="flex grow justify-center text-slate-500">
<div>
<i class="fa-solid fa-file-arrow-down"></i>
File pending download
</div>
</section>
</section>
<% else %>
<!-- Fallback for no file -->
<section id="file-display-section">
<section class="flex grow justify-center overflow-clip">
<div class="text-slate-500">
<i class="fa-solid fa-file-arrow-down"></i>
No file
</div>
</section>
</section>
<% end %>
<% else %>
<section class="sky-section">
<%= link_to post.external_url_for_view.to_s,
target: "_blank",
rel: "noopener noreferrer",
class: "section-header flex items-center gap-2 hover:text-slate-600" do %>
<span>View Post on <%= domain_name_for_model(post) %></span>
<i class="fa-solid fa-arrow-up-right-from-square"></i>
<% end %>
</section>
<% end %>