Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting previously uploaded image for picture upload #23072

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from

Conversation

karwosts
Copy link
Contributor

@karwosts karwosts commented Nov 30, 2024

Proposed change

In the image upload form, allow selecting a previously uploaded image via the browse media dialog.

Requires home-assistant/core#131468

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

@silamon silamon added wait for backend backend merged The backend PR for this frontend PR has been merged and removed backend merged The backend PR for this frontend PR has been merged labels Dec 1, 2024
@bramkragten bramkragten added the Needs UX Pull requests requiring a review from the Home Assistant design team label Dec 4, 2024
@bramkragten
Copy link
Member

Love this! ❤️

We need to think what to do with images sizes, currently we upload and cut an image to size, so we know what size (or ratio) image we will get. If we allow people to select previously uploaded images these may not have the requested ratio/size.

  • Do we cut these images again and upload them (under the same id, with a different size?)
  • Do we limit the selectable images to the specified ratio
  • Do we update the use of the images to always work no matter the size of the image?

1 use-case where this will happen is the user profile picture.

@karwosts karwosts added backend merged The backend PR for this frontend PR has been merged and removed wait for backend labels Dec 5, 2024
@karwosts
Copy link
Contributor Author

karwosts commented Dec 5, 2024

A sequence like the following might work, for image uploads with cropOptions:

  • Allow choosing image from media browser as currently
  • On pick, load that image_upload original into the cropper dialog and present the dialog.
  • If the aspect ratio of the image matches the aspect ratio of cropOptions, add a button in the dialog next to Crop that says Use Original.
  • If "use original" is picked, we don't upload anything new and instead just keep using the existing media id from image_upload.
  • If aspect ratio does not match desired, or if use original is not pressed, perform the required crop (as we do on new uploads), and upload the result into a new image_upload file.

Kind of hacked my way through this and I think all the pieces work.

@karwosts karwosts marked this pull request as draft December 7, 2024 18:08
@karwosts karwosts marked this pull request as ready for review December 8, 2024 05:11
@karwosts
Copy link
Contributor Author

karwosts commented Dec 8, 2024

Updated cropping as proposed.

@wendevlin
Copy link
Contributor

Updated cropping as proposed.

Thank you. We wait on a design for the select prev image button and come back to you

@wendevlin wendevlin added the Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label Dec 12, 2024
@wendevlin
Copy link
Contributor

Hi @karwosts,

We would like to integrate the "select media" button into the ha-file-upload

Here are designs:
image

Please make it optional, because we have file uploads that don't need the select option.

If you have questions just ask

@karwosts
Copy link
Contributor Author

I did hesitate in my original proposal to use the term "select from media" as we are not allowing (currently) selecting any media image, it can only be a file previously uploaded via image_upload, not anything from any other media integration.

Just want to be clear you're aware of that limitation.

@wendevlin
Copy link
Contributor

I did hesitate in my original proposal to use the term "select from media" as we are not allowing (currently) selecting any media image, it can only be a file previously uploaded via image_upload, not anything from any other media integration.

Just want to be clear you're aware of that limitation.

Ok good point, but I think we can risk it.

@karwosts
Copy link
Contributor Author

karwosts commented Dec 18, 2024

Moved select media button into secondary text:

image

@marcinbauer85
Copy link
Contributor

As for the design of the picker, I've done a very similar one for choosing local image via the Local path or web URL:
https://www.figma.com/design/AKeKccgRBaCy871REB5V9c/HA-04?node-id=2008-4840&t=oTfqhrYVnfiNFrWQ-11

If the first option now gets the ability to choose a local image, the second option right now is getting redundant. It only has the ability to paste in a web URL which is not aligned with HA principles of being local only. We'll discuss this internally, and get back as soon as possible.

@karwosts
Copy link
Contributor Author

karwosts commented Dec 18, 2024

Second option can also be used for local only images, for anything that anyone has manually loaded to their /www/ folder, which would have been how this has always been done up until the upload dialog was added last year.

Any files loaded manually to the /www/ folder are not available to the picker.

Or maybe for a URL which is still local but not hosted by HA.

@marcinbauer85
Copy link
Contributor

We could leave the Local Path via yaml, but simplify the UI to the screen below, and not have a breaking change. An added benefit is, that when uploading or selecting local media all files are accessible via the UI, while if an user uses a local URL from /www/ this later might become trash if the user doesn't clean internal files via a file browser plugin.
image

@wendevlin
Copy link
Contributor

We could leave the Local Path via yaml, but simplify the UI to the screen below, and not have a breaking change. An added benefit is, that when uploading or selecting local media all files are accessible via the UI, while if an user uses a local URL from /www/ this later might become trash if the user doesn't clean internal files via a file browser plugin.

No because local path is already in the UI, removing it would be a breaking change.
I would say we go the way @karwosts did it and if we find a better solution we still can improve

Copy link
Contributor

@wendevlin wendevlin left a comment

Choose a reason for hiding this comment

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

Awesome, just some details left.

navigateIds: [
{ media_content_id: undefined, media_content_type: undefined },
{
media_content_id: "media-source://image_upload",
Copy link
Contributor

Choose a reason for hiding this comment

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

please add a static constant for "media-source://image_upload"

const id = pickedMedia.item.media_content_id;
const stringToRemove = "media-source://image_upload/";
if (id.startsWith(stringToRemove)) {
const mediaId = id.substr(stringToRemove.length);
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines 197 to 198
const response = await fetch(url);
const data = await response.blob();
Copy link
Contributor

Choose a reason for hiding this comment

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

This can fail, please add error handling

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate a bit what you want to see here?

So HA gives us the list of images, we click on one, and then... it's not available? What should I do with that, a popup dialog that just says "unknown error"? I would have thought just whatever default logging exception to the console would be sufficient, and how this would usually be handled.

Or what is the specific mode of failure you were thinking of?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good.

If it fails to load use showAlertDialog with a text: "Load image preview in crop editor failed" (this is just an example).

I know this is an edge case but http requests can always fail and showing it only in console won't help the user in the situation.

@@ -72,6 +95,12 @@ export class HaImagecropperDialog extends LitElement {
<mwc-button slot="secondaryAction" @click=${this.closeDialog}>
${this.hass.localize("ui.common.cancel")}
</mwc-button>
${this._isTargetAspectRatio
? html` <mwc-button slot="primaryAction" @click=${this._useOriginal}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
? html` <mwc-button slot="primaryAction" @click=${this._useOriginal}>
? html`<mwc-button slot="primaryAction" @click=${this._useOriginal}>

Comment on lines 45 to 49
constructor() {
super();
this._chooseMedia = this._chooseMedia.bind(this);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
constructor() {
super();
this._chooseMedia = this._chooseMedia.bind(this);
}

use arrow function instead

@@ -141,16 +175,58 @@ export class HaPictureUpload extends LitElement {
}
}

private _chooseMedia(): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private _chooseMedia(): void {
private _chooseMedia = () => {

@home-assistant home-assistant bot marked this pull request as draft December 19, 2024 14:29
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@@ -39,13 +49,32 @@ export class HaPictureUpload extends LitElement {

public render(): TemplateResult {
if (!this.value) {
/* eslint-disable lit-a11y/anchor-is-valid */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not too sure about this check. Due I think to recent changes to dialogs/navigation, the href=# didn't work anymore, so I had to change to href="javascript:".

Eslint however doesn't seem to like the new "fake" anchor. I'm not sure how else to open a dialog from a link like was requested though.

Copy link
Contributor

Choose a reason for hiding this comment

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

Mhh yeah not the nicest solution.

What about using a button tag and style it like a link?

@karwosts karwosts marked this pull request as ready for review December 19, 2024 19:34
@home-assistant home-assistant bot requested a review from wendevlin December 19, 2024 19:34
@karwosts karwosts removed the Needs UX Pull requests requiring a review from the Home Assistant design team label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend merged The backend PR for this frontend PR has been merged cla-signed Noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants