Helper functions for integrating the media selector component.
Provides convenience functions to generate media selector URLs, parse returned selections, and handle media selector integration from any LiveView or controller.
Examples
# In a LiveView template
<.link navigate={MediaSelectorHelper.media_selector_url(@current_path, mode: :single)}>
Select Featured Image
</.link>
# In handle_params to receive selection
def handle_params(params, _uri, socket) do
case MediaSelectorHelper.parse_selected_media(params) do
{:ok, [file_uuid]} ->
socket
|> assign(:featured_image_uuid, file_uuid)
|> put_flash(:info, "Image selected!")
{:ok, file_uuids} ->
socket
|> assign(:gallery_uuids, file_uuids)
|> put_flash(:info, "#{length(file_uuids)} images selected!")
:none ->
socket
end
end
Summary
Functions
Extracts the first file UUID from selected media.
Generates a URL to the media selector page.
Parses the selected media from LiveView params.
Functions
Extracts the first file UUID from selected media.
Useful for single-selection scenarios where you only need one file UUID.
Examples
get_first_selected(%{"selected_media" => "id1,id2,id3"})
#=> {:ok, "id1"}
get_first_selected(%{"selected_media" => "single-id"})
#=> {:ok, "single-id"}
get_first_selected(%{})
#=> :none
Generates a URL to the media selector page.
Parameters
return_to- The URL to return to after selection (required)opts- Keyword list of options::mode- Selection mode::singleor:multiple(default::single):filter- File type filter::image,:video, or:all(default::all):selected- List of pre-selected file UUIDs (optional)
Examples
# Single image selection
media_selector_url("/admin/blog/edit", mode: :single, filter: :image)
#=> "/admin/media/selector?return_to=%2Fadmin%2Fblog%2Fedit&mode=single&filter=image"
# Multiple selection with pre-selected files
media_selector_url("/admin/gallery", mode: :multiple, selected: ["id1", "id2"])
#=> "/admin/media/selector?return_to=%2Fadmin%2Fgallery&mode=multiple&selected=id1%2Cid2"
Parses the selected media from LiveView params.
Returns {:ok, [file_uuids]} if media was selected, or :none if no selection.
Examples
parse_selected_media(%{"selected_media" => "id1,id2,id3"})
#=> {:ok, ["id1", "id2", "id3"]}
parse_selected_media(%{"selected_media" => "single-id"})
#=> {:ok, ["single-id"]}
parse_selected_media(%{})
#=> :none