-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add my link support * Update src/main.ts
- Loading branch information
Showing
6 changed files
with
194 additions
and
13 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,100 @@ | ||
import { html, LitElement, TemplateResult } from "lit"; | ||
import { customElement, property, state } from "lit/decorators"; | ||
import { navigate } from "../homeassistant-frontend/src/common/navigate"; | ||
import { | ||
createSearchParam, | ||
extractSearchParamsObject, | ||
} from "../homeassistant-frontend/src/common/url/search-params"; | ||
import "../homeassistant-frontend/src/layouts/hass-error-screen"; | ||
import { | ||
ParamType, | ||
Redirect, | ||
Redirects, | ||
} from "../homeassistant-frontend/src/panels/my/ha-panel-my"; | ||
import { HomeAssistant, Route } from "../homeassistant-frontend/src/types"; | ||
import { Hacs } from "./data/hacs"; | ||
|
||
export const REDIRECTS: Redirects = { | ||
hacs_repository: { | ||
redirect: "/hacs/repository", | ||
params: { | ||
owner: "string", | ||
repository: "string", | ||
category: "string?", | ||
}, | ||
}, | ||
}; | ||
|
||
@customElement("hacs-my-redirect") | ||
class HacsMyRedirect extends LitElement { | ||
@property({ attribute: false }) public hass!: HomeAssistant; | ||
|
||
@property({ attribute: false }) public hacs!: Hacs; | ||
|
||
@property({ attribute: false }) public route!: Route; | ||
|
||
@state() private _error?: TemplateResult | string; | ||
|
||
protected firstUpdated(_changedProperties): void { | ||
const dividerPos = this.route.path.indexOf("/", 1); | ||
const path = this.route.path.substr(dividerPos + 1); | ||
const redirect = REDIRECTS[path]; | ||
|
||
if (!redirect) { | ||
this._error = this.hacs.localize("my.not_supported", { | ||
link: html`<a | ||
target="_blank" | ||
rel="noreferrer noopener" | ||
href="https://my.home-assistant.io/faq.html#supported-pages" | ||
> | ||
${this.hacs.localize("my.faq_link")} | ||
</a>`, | ||
}); | ||
return; | ||
} | ||
|
||
let url: string; | ||
try { | ||
url = this._createRedirectUrl(redirect); | ||
} catch (err: any) { | ||
this._error = this.hacs.localize("my.error"); | ||
return; | ||
} | ||
|
||
navigate(url, { replace: true }); | ||
} | ||
|
||
protected render(): TemplateResult { | ||
if (this._error) { | ||
return html`<hass-error-screen .error=${this._error}></hass-error-screen>`; | ||
} | ||
return html``; | ||
} | ||
|
||
private _createRedirectUrl(redirect: Redirect): string { | ||
const params = this._createRedirectParams(redirect); | ||
return `${redirect.redirect}${params}`; | ||
} | ||
|
||
private _createRedirectParams(redirect: Redirect): string { | ||
const params = extractSearchParamsObject(); | ||
if (!redirect.params && !Object.keys(params).length) { | ||
return ""; | ||
} | ||
const resultParams = {}; | ||
for (const [key, type] of Object.entries(redirect.params || {})) { | ||
if (!params[key] && type.endsWith("?")) { | ||
continue; | ||
} | ||
if (!params[key] || !this._checkParamType(type, params[key])) { | ||
throw Error(); | ||
} | ||
resultParams[key] = params[key]; | ||
} | ||
return `?${createSearchParam(resultParams)}`; | ||
} | ||
|
||
private _checkParamType(type: ParamType, _value: string) { | ||
return type === "string" || type === "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