forked from oliverschwendener/ueli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracted favicon url image generation
- Loading branch information
1 parent
189cc7b
commit 1b1241d
Showing
16 changed files
with
185 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { Image } from "@common/Core/Image"; | ||
|
||
export interface UrlImageGenerator { | ||
getImage(url: string): Image; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./UrlImageGenerator"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { Dependencies } from "@Core/Dependencies"; | ||
import type { DependencyRegistry } from "@Core/DependencyRegistry"; | ||
import { UrlImageGenerator } from "./UrlImageGenerator"; | ||
|
||
export class ImageGeneratorModule { | ||
public static bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) { | ||
dependencyRegistry.register( | ||
"UrlImageGenerator", | ||
new UrlImageGenerator(dependencyRegistry.get("SettingsManager")), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { SettingsManager } from "@Core/SettingsManager"; | ||
import type { Image } from "@common/Core/Image"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { UrlImageGenerator } from "./UrlImageGenerator"; | ||
|
||
describe(UrlImageGenerator, () => { | ||
it("should generate correct favicon urls using the Google favicon provider", () => { | ||
const getValueMock = vi.fn().mockReturnValue("Google"); | ||
const settingsManager = <SettingsManager>{ getValue: (k, d) => getValueMock(k, d) }; | ||
|
||
const urlImageGenerator = new UrlImageGenerator(settingsManager); | ||
|
||
expect(urlImageGenerator.getImage("https://github.com")).toEqual(<Image>{ | ||
url: "https://www.google.com/s2/favicons?domain=github.com&sz=48", | ||
}); | ||
|
||
expect(getValueMock).toHaveBeenCalledWith("imageGenerator.faviconApiProvider", "Google"); | ||
}); | ||
|
||
it("should generate correct favicon urls using the Favicone favicon provider", () => { | ||
const getValueMock = vi.fn().mockReturnValue("Favicone"); | ||
const settingsManager = <SettingsManager>{ getValue: (k, d) => getValueMock(k, d) }; | ||
|
||
const urlImageGenerator = new UrlImageGenerator(settingsManager); | ||
|
||
expect(urlImageGenerator.getImage("https://github.com")).toEqual(<Image>{ | ||
url: "https://favicone.com/github.com?s=48", | ||
}); | ||
|
||
expect(getValueMock).toHaveBeenCalledWith("imageGenerator.faviconApiProvider", "Google"); | ||
}); | ||
|
||
it("should use the Google API when the configured provider is invalid", () => { | ||
const getValueMock = vi.fn().mockReturnValue("Invalid"); | ||
const settingsManager = <SettingsManager>{ getValue: (k, d) => getValueMock(k, d) }; | ||
|
||
const urlImageGenerator = new UrlImageGenerator(settingsManager); | ||
|
||
expect(urlImageGenerator.getImage("https://github.com")).toEqual(<Image>{ | ||
url: "https://www.google.com/s2/favicons?domain=github.com&sz=48", | ||
}); | ||
|
||
expect(getValueMock).toHaveBeenCalledWith("imageGenerator.faviconApiProvider", "Google"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { SettingsManager } from "@Core/SettingsManager"; | ||
import type { Image } from "@common/Core/Image"; | ||
import type { UrlImageGenerator as UrlImageGeneratorInterface } from "./Contract"; | ||
|
||
export class UrlImageGenerator implements UrlImageGeneratorInterface { | ||
public constructor(private readonly settingsManager: SettingsManager) {} | ||
|
||
public getImage(url: string): Image { | ||
const { host } = new URL(url); | ||
const size = 48; | ||
|
||
const imageUrls: Record<string, () => string> = { | ||
Google: () => `https://www.google.com/s2/favicons?domain=${host}&sz=${size}`, | ||
Favicone: () => `https://favicone.com/${host}?s=${size}`, | ||
}; | ||
|
||
return { | ||
url: Object.keys(imageUrls).includes(this.getFaviconApiProvider()) | ||
? imageUrls[this.getFaviconApiProvider()]() | ||
: imageUrls["Google"](), | ||
}; | ||
} | ||
|
||
private getFaviconApiProvider(): string { | ||
return this.settingsManager.getValue("imageGenerator.faviconApiProvider", "Google"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./Contract"; | ||
export * from "./ImageGeneratorModule"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import type { SearchResultItem } from "@common/Core"; | ||
|
||
export interface BrowserBookmark { | ||
toSearchResultItem(...args: unknown[]): SearchResultItem; | ||
getName(): string; | ||
getUrl(): string; | ||
getId(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/renderer/Core/Settings/Pages/General/UrlImageGenerator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useSetting } from "@Core/Hooks"; | ||
import { Dropdown, Field, Option } from "@fluentui/react-components"; | ||
|
||
export const UrlImageGenerator = () => { | ||
const faviconApiProviders = ["Google", "Favicone"]; | ||
|
||
const { value: faviconApiProvider, updateValue: setFaviconApiProvider } = useSetting<string>({ | ||
key: "imageGenerator.faviconApiProvider", | ||
defaultValue: "Google", | ||
}); | ||
|
||
return ( | ||
<Field label="Favicon API" hint="This API is used to generate icons for URLs"> | ||
<Dropdown | ||
value={faviconApiProvider} | ||
selectedOptions={[faviconApiProvider]} | ||
onOptionSelect={(_, { optionValue }) => optionValue && setFaviconApiProvider(optionValue)} | ||
> | ||
{faviconApiProviders.map((f) => ( | ||
<Option key={`faviconApi-${f}`} value={f} text={f}> | ||
{f} | ||
</Option> | ||
))} | ||
</Dropdown> | ||
</Field> | ||
); | ||
}; |
Oops, something went wrong.