Parse URLs in data value display, make the layout denser

This commit is contained in:
Kuba Orlik 2021-11-07 10:09:41 +01:00
parent fa42e848ea
commit 3f61445831
5 changed files with 98 additions and 9 deletions

View File

@ -31,6 +31,10 @@
#app { #app {
user-select: text; user-select: text;
} }
tr:hover {
background-color: hsla(0, 0%, 0%, 0.1);
}
</style> </style>
</head> </head>
<body> <body>

View File

@ -24,11 +24,6 @@ const Sidebar = () => {
console.log("tab change!"); console.log("tab change!");
const tab = await getCurrentTab(); const tab = await getCurrentTab();
const url = new URL(tab.url); const url = new URL(tab.url);
console.log(
"NEW ORIGIN",
url.origin,
url.origin.startsWith("moz-extension")
);
if (url.origin.startsWith("moz-extension")) { if (url.origin.startsWith("moz-extension")) {
return; return;
} }

View File

@ -1,6 +1,66 @@
import React from "react"; import React from "react";
import memory from "./memory"; import memory from "./memory";
import { Sources } from "./request-cluster"; import { Sources } from "./request-cluster";
import { hyphenate, isJSONObject, isURL, parseToObject } from "./util";
function StolenDataValueTable({
object,
prefixKey = "",
}: {
object: Record<string, unknown>;
prefixKey: string;
}) {
return (
<table>
<tbody>
{Object.entries(object).map(([key, value]) => (
<tr key={`${prefixKey}.${key}`}>
<th>{hyphenate(key)}</th>
<td>
<StolenDataValue
value={value}
prefixKey={`${prefixKey}.${key}`}
/>
</td>
</tr>
))}
</tbody>
</table>
);
}
function StolenDataValue({
value,
prefixKey = "",
}: {
value: unknown;
prefixKey?: string;
}) {
if (!value) {
return <></>;
}
console.log("parsing value!", value);
if (isJSONObject(value)) {
const object = parseToObject(value);
return <StolenDataValueTable object={object} prefixKey={prefixKey} />;
} else if (isURL(value)) {
const url = new URL(value);
const object = {
host: url.host,
path: url.pathname,
...Object.fromEntries(
(
url.searchParams as unknown as {
entries: () => Iterable<[string, string]>;
}
).entries()
),
};
return <StolenDataValueTable object={object} prefixKey={prefixKey} />;
} else {
return <>{value.toString()}</>;
}
}
export default function StolenDataRow({ export default function StolenDataRow({
origin, origin,
@ -43,12 +103,12 @@ export default function StolenDataRow({
key={origin + cluster.id + entry.getUniqueKey()} key={origin + cluster.id + entry.getUniqueKey()}
data-key={origin + cluster.id + entry.getUniqueKey()} data-key={origin + cluster.id + entry.getUniqueKey()}
> >
<th style={{ maxWidth: "200px", wordWrap: "break-word" }}> <th style={{ width: "100px", overflowWrap: "anywhere" }}>
{entry.getNames().join(",")} {entry.getNames().map(hyphenate).join(", ")}
</th> </th>
<td>{entry.getSources().map((source) => icons[source])}</td> <td>{entry.getSources().map((source) => icons[source])}</td>
<td style={{ wordWrap: "anywhere" as any }}> <td style={{ wordWrap: "anywhere" as any }}>
{entry.getValues()[0]} <StolenDataValue value={entry.getValues()[0]} />
</td> </td>
</tr> </tr>
))} ))}

View File

@ -2,7 +2,7 @@
"compilerOptions": { "compilerOptions": {
"jsx": "react", "jsx": "react",
"esModuleInterop": true, "esModuleInterop": true,
"lib": ["es2017", "dom"], "lib": ["es2017", "dom", "es2019"],
"typeRoots": ["node_modules/@types", "node_modules/web-ext-types"], "typeRoots": ["node_modules/@types", "node_modules/web-ext-types"],
"outDir": "lib" "outDir": "lib"
} }

30
util.ts
View File

@ -46,3 +46,33 @@ export async function getTabByID(id: number) {
const tabs = await browser.tabs.query({ currentWindow: true }); const tabs = await browser.tabs.query({ currentWindow: true });
return tabs.find((tab) => tab.id == id); return tabs.find((tab) => tab.id == id);
} }
export function parseToObject(str: unknown): Record<string, unknown> {
if (typeof str === "string") {
return JSON.parse(str);
} else if (typeof str == "object") {
return str as Record<string, unknown>;
}
}
export function isJSONObject(
str: unknown
): str is Record<string, unknown> | string | number {
try {
return JSON.stringify(parseToObject(str))[0] == "{";
} catch (e) {
return false;
}
}
export function isURL(str: unknown): str is string {
try {
return !!(typeof str === "string" && new URL(str));
} catch (e) {
return false;
}
}
export function hyphenate(str: string): string {
return str.replace(/[_\[A-Z]/g, `${String.fromCharCode(173)}$&`);
}