Skip to content

Commit

Permalink
add proper types to changed blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
emma-sg committed Mar 8, 2024
1 parent 68240c3 commit 97df2d5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/appmain.ts
Expand Up @@ -25,6 +25,7 @@ import "./item";
import "./item-index";
import "./chooser";
import { type LoadInfo } from "./item";
import type { FavIconEventDetail } from "./types";

// ===========================================================================
@customElement("replay-app-main")
Expand Down Expand Up @@ -481,8 +482,7 @@ export class ReplayWebApp extends LitElement {
}
}

// @ts-expect-error [// TODO: Fix this the next time the file is edited.] - TS7006 - Parameter 'event' implicitly has an 'any' type.
onFavIcons(event) {
onFavIcons(event: CustomEvent<FavIconEventDetail>) {
updateFaviconLinks(event.detail);
}

Expand Down
21 changes: 17 additions & 4 deletions src/embed.ts
Expand Up @@ -10,6 +10,18 @@ import { ifDefined } from "lit/directives/if-defined.js";
import { wrapCss, rwpLogo, updateFaviconLinks } from "./misc";
import { SWManager } from "./swmanager";
import { property } from "lit/decorators.js";
import type { FavIconEventDetail } from "./types";

type IframeMessage = MessageEvent<
| {
type: "urlchange";
view?: string;
title?: string;
}
| ({
type: "favicons";
} & FavIconEventDetail)
>;

const scriptSrc =
document.currentScript && (document.currentScript as HTMLScriptElement).src;
Expand Down Expand Up @@ -114,8 +126,7 @@ class Embed extends LitElement {
}
}

// @ts-expect-error [// TODO: Fix this the next time the file is edited.] - TS7006 - Parameter 'event' implicitly has an 'any' type.
handleMessage(event) {
handleMessage(event: IframeMessage) {
const iframe = this.renderRoot.querySelector("iframe");

if (iframe && event.source === iframe.contentWindow) {
Expand All @@ -135,7 +146,7 @@ class Embed extends LitElement {
}
}

handleUrlChangeMessage(data) {
handleUrlChangeMessage(data: { view?: string; title?: string }) {
if (!data.view) {
return;
}
Expand All @@ -160,7 +171,9 @@ class Embed extends LitElement {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.doRegister();

window.addEventListener("message", (event) => this.handleMessage(event));
window.addEventListener("message", (event: IframeMessage) =>
this.handleMessage(event),
);

if (this.deepLink) {
this.updateFromHash();
Expand Down
7 changes: 2 additions & 5 deletions src/item.ts
Expand Up @@ -57,7 +57,7 @@ import fasCaretDown from "@fortawesome/fontawesome-free/svgs/solid/caret-down.sv
import { RWPEmbedReceipt } from "./embed-receipt";
import Split from "split.js";

import type { ItemType, URLResource } from "./types";
import type { FavIconEventDetail, ItemType, URLResource } from "./types";
import type { Replay } from "./replay";
import { ifDefined } from "lit/directives/if-defined.js";

Expand Down Expand Up @@ -1701,15 +1701,12 @@ class Item extends LitElement {
this.showSidebar = false;
}

// @ts-expect-error [// TODO: Fix this the next time the file is edited.] - TS7006 - Parameter 'event' implicitly has an 'any' type.
async onFavIcons(event) {
async onFavIcons(event: CustomEvent<FavIconEventDetail>) {
if (this.embed && window.parent !== window) {
window.parent.postMessage({ type: "favicons", ...event.detail }, "*");
}

for (const icon of event.detail.icons) {
// TODO: Fix this the next time the file is edited.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const resp = await fetch(icon.href);
if (resp.status === 200) {
const ct = resp.headers.get("Content-Type");
Expand Down
3 changes: 2 additions & 1 deletion src/misc.ts
Expand Up @@ -7,6 +7,7 @@ import { styleMap } from "lit/directives/style-map.js";
import allCssRaw from "../assets/main.scss";

import rwpLogo from "../assets/logo.svg";
import type { FavIconEventDetail } from "./types";

const apiPrefix = "./w/api";
const replayPrefix = "./w";
Expand Down Expand Up @@ -40,7 +41,7 @@ function clickOnSpacebarPress(event) {
// Update favicon links from an array of {rel, href} objects
// remove all existing icon links
// used by both embed and main app
export function updateFaviconLinks(data) {
export function updateFaviconLinks(data: FavIconEventDetail) {
const head = document.querySelector("head")!;
const oldLinks = document.querySelectorAll("link[rel*='icon']");

Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Expand Up @@ -50,3 +50,10 @@ export type ItemType = {
totalSize?: unknown;
size?: number | string;
};

export type FavIconEventDetail = {
icons: {
rel: string;
href: string;
}[];
};

0 comments on commit 97df2d5

Please sign in to comment.