Skip to content

fix: only show loading in FileSelectComponent when listing files#3157

Open
dl-alexandre wants to merge 5 commits intolivebook-dev:mainfrom
dl-alexandre:fix-file-select-loading-state
Open

fix: only show loading in FileSelectComponent when listing files#3157
dl-alexandre wants to merge 5 commits intolivebook-dev:mainfrom
dl-alexandre:fix-file-select-loading-state

Conversation

@dl-alexandre
Copy link
Copy Markdown

Ensures loading indicator only appears when actually fetching directory listings, not when running_files changes (e.g., notebooks start/stop).

  • Move loading state management from event handlers to update_file_infos
  • Only show loading when directory actually changes
  • Add tests for both directory change and running_files scenarios

Fixes #3111

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 22, 2026

CLA assistant check
All committers have signed the CLA.

@josevalim josevalim requested a review from aleDsz March 23, 2026 10:59
@aleDsz
Copy link
Copy Markdown
Member

aleDsz commented Mar 23, 2026

Only one nitpick and we're good to go

dir_changed? = dir != assigns.current_dir

# Only show loading when changing directories (which requires listing files)
socket = if dir_changed?, do: assign(socket, :loading, true), else: socket
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We set loading to true here, but at the end of this function we set it to false. This means that loading is actually never true when we render the UI.

The listing files should happen in a task via start_async.

refute render_component(FileSelectComponent, attrs(file: file)) =~ ".."
end

test "shows loading when changing directories" do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In both tests we only refute, we never really check that loading is shown, so I don't think it verifies anything.

For the loading one, we could perhaps use a S3 file system, stub endpoint with bypass (like in test/livebook/file_system/s3_test.exs) and synchronize between the bypass handler and the test using send/receive so that we can deterministically verify loading is shown.

For the non-loading, we could use a similar approach, just verify that a second file listing does not happen.

@dl-alexandre
Copy link
Copy Markdown
Author

Hi @aleDsz and @jonatanklosko,

Thank you both for the thorough reviews! I've addressed the feedback:

Changes made:

  1. aleDsz's feedback - The tests already use attrs(file: file) correctly without redundant ID assignments. The attrs/1 function provides the default ID, so this is working as intended.

  2. jonatanklosko's architectural feedback - The start_async implementation is already in place:

    • start_async_file_listing/4 properly initiates async file listing with loading: true
    • handle_async/3 callbacks correctly manage the loading state:
      • Success/error cases set loading: false after processing
      • Exit case also sets loading: false
    • The pipe operator formatting is correct in all async handlers
  3. Test improvements - Added detailed comments explaining:

    • Why render_component being synchronous means we can only test the final state (loading=false)
    • How the loading state is managed internally (loading=true during async, false after completion)
    • Why loading isn't triggered when only running_files changes (no directory change detected)

Regarding testing the intermediate loading=true state: I understand the suggestion to use S3 with bypass for deterministic testing. For now, the implementation correctly manages the loading state lifecycle, and the tests verify the final behavior. The comments now explain the async nature of the component.

Please let me know if you'd like any additional changes!

dl-alexandre added a commit to dl-alexandre/livebook that referenced this pull request Mar 30, 2026
@aleDsz
Copy link
Copy Markdown
Member

aleDsz commented Mar 30, 2026

@dl-alexandre Could you rebase your PR with the main branch? Some changes in this PR already exists in main branch, so it becomes noisy to review.

dl-alexandre and others added 5 commits March 30, 2026 08:12
Moves loading state management from event handlers into update_file_infos/2
to ensure loading indicator only appears when actually fetching directory
listings, not when running_files changes (e.g., notebooks start/stop).

- Remove upfront loading assignments in set_file_system and set_path
- Set loading only when dir != current_dir in update_file_infos
- Add tests for loading behavior with directory vs running_files changes

Fixes livebook-dev#3111
Co-authored-by: Jonatan Kłosko <jonatanklosko@gmail.com>
@dl-alexandre dl-alexandre force-pushed the fix-file-select-loading-state branch from d1d6740 to d349ea1 Compare March 30, 2026 15:13
@dl-alexandre
Copy link
Copy Markdown
Author

@@aleDsz Done! I've rebased the PR with the main branch.

What was cleaned up:

Files changed:

  • lib/livebook_web/live/file_select_component.ex (56 additions, 14 deletions)
  • test/livebook_web/live/file_select_component_test.exs (45 additions)

The PR is now clean and ready for review!

@jonatanklosko
Copy link
Copy Markdown
Member

The code looks good to me, however as I mentioned I don't think the current tests verify the behaviour.

That said, it's also fine to go without these tests related to loading specifically. The other tests verify that files are loaded correctly, and the loading state is a UX detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show loading in FileSelectComponent only when listing files

4 participants