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

Replace RelatedResource FlightIcons with Favicons #313

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{{@url}}
</div>
<div class="external-resource-favicon">
<FlightIcon @name={{get-link-icon @url}} />
<Favicon @url={{@url}} />
</div>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
<img src="/images/document.png" class="h-auto w-[18px]" />
</div>
{{else}}
<FlightIcon
@name={{get-link-icon this.url}}
class={{if (eq (get-link-icon this.url) "globe") "opacity-40"}}
/>
<Favicon @url={{this.url}} />
jeffdaley marked this conversation as resolved.
Show resolved Hide resolved
{{/if}}
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export default class DocumentSidebarRelatedResourcesListItemResourceComponent ex
/**
* The full URL of an ExternalResource
*/
private get url() {
protected get url() {
assert("url must exist in the resource", "url" in this.args.resource);
return this.args.resource.url;
}

/**
* The url to display in the ResourceList.
* Strips the protocol, "www" and path.
Expand Down
17 changes: 17 additions & 0 deletions web/app/components/favicon.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{#if this.fallbackIconIsShown}}
<FlightIcon
...attributes
data-test-fallback-favicon
@name="globe"
jfreda marked this conversation as resolved.
Show resolved Hide resolved
class="opacity-60"
/>
{{else}}
<img
...attributes
data-test-favicon
{{on "error" this.showFallbackIcon}}
src={{this.faviconURL}}
height="16"
width="16"
/>
{{/if}}
26 changes: 26 additions & 0 deletions web/app/components/favicon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { action } from "@ember/object";
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";

interface FaviconComponentSignature {
Element: SVGElement | HTMLImageElement;
Args: {
url: string;
};
}

export default class FaviconComponent extends Component<FaviconComponentSignature> {
@tracked protected fallbackIconIsShown = false;

protected readonly faviconURL = `https://www.google.com/s2/favicons?sz=64&domain=${this.args.url}`;

@action protected showFallbackIcon() {
this.fallbackIconIsShown = true;
}
}

declare module "@glint/environment-ember-loose/registry" {
export default interface Registry {
Favicon: typeof FaviconComponent;
}
}
54 changes: 0 additions & 54 deletions web/app/helpers/get-link-icon.ts

This file was deleted.

39 changes: 39 additions & 0 deletions web/tests/integration/components/favicon-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { module, test } from "qunit";
import { setupRenderingTest } from "ember-qunit";
import { find, render, waitFor } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";

const FALLBACK_ICON_SELECTOR = "[data-test-fallback-favicon]";
const FAVICON_SELECTOR = "[data-test-favicon]";

module("Integration | Component | favicon", function (hooks) {
setupRenderingTest(hooks);

test("it appends the passed-in URL to the google favicon path", async function (assert) {
await render(hbs`
<Favicon @url="https://hashicorp.com"/>
`);

assert
.dom(FAVICON_SELECTOR)
.hasAttribute(
"src",
"https://www.google.com/s2/favicons?sz=64&domain=https://hashicorp.com"
);
});

test("it shows a fallback icon when the favicon URL is invalid", async function (assert) {
await render(hbs`
<Favicon @url="-"/>
`);

assert.dom(FALLBACK_ICON_SELECTOR).doesNotExist();

find(FAVICON_SELECTOR)?.dispatchEvent(new Event("error"));

await waitFor(FALLBACK_ICON_SELECTOR);

assert.dom(FALLBACK_ICON_SELECTOR).exists();
assert.dom(FAVICON_SELECTOR).doesNotExist();
});
});