feat: extension supports adding
parent
bd0f74b658
commit
a7508c9a83
|
@ -17,9 +17,9 @@
|
|||
|
||||
let instanceVersion: string;
|
||||
let instanceVersionError: string;
|
||||
$: Boolean($instanceUrlStore) && updateVersion();
|
||||
$: Boolean($instanceUrlStore) && fetchVersion();
|
||||
|
||||
async function updateVersion() {
|
||||
async function fetchVersion() {
|
||||
instanceVersion = undefined;
|
||||
instanceVersionError = undefined;
|
||||
|
||||
|
@ -57,44 +57,134 @@
|
|||
})) as unknown as string | undefined;
|
||||
});
|
||||
|
||||
function visit(address: string) {
|
||||
browser.tabs.create({ url: `${$cleanInstanceUrl}/#/browse/${address}` });
|
||||
window.close();
|
||||
enum PRIMARY_TYPE {
|
||||
URL = "URL",
|
||||
CONTENT = "Content",
|
||||
UNKNOWN = "???",
|
||||
}
|
||||
|
||||
async function openAsUrl() {
|
||||
open({ url: currentUrl });
|
||||
let primaryType = PRIMARY_TYPE.UNKNOWN;
|
||||
$: primaryType = !contentType
|
||||
? PRIMARY_TYPE.UNKNOWN
|
||||
: contentType == "text/html"
|
||||
? PRIMARY_TYPE.URL
|
||||
: PRIMARY_TYPE.CONTENT;
|
||||
|
||||
enum PRIMARY_ACTION {
|
||||
OPEN = "Open",
|
||||
SAVE = "Save",
|
||||
UNKNOWN = "???",
|
||||
}
|
||||
|
||||
async function openContent() {
|
||||
open({ urlContent: currentUrl });
|
||||
}
|
||||
|
||||
async function open(input: { url: string } | { urlContent: string }) {
|
||||
opening = true;
|
||||
try {
|
||||
const address = await api.getAddress(input);
|
||||
|
||||
// const obj = (await (
|
||||
// await fetch(`${$cleanInstanceUrl}/api/obj/${address}`)
|
||||
// ).json()) as EntityListing;
|
||||
|
||||
visit(address);
|
||||
} catch (err) {
|
||||
openError = processError(err);
|
||||
let primaryAction = PRIMARY_ACTION.UNKNOWN;
|
||||
$: {
|
||||
if (primaryType !== PRIMARY_TYPE.UNKNOWN) {
|
||||
let count =
|
||||
primaryType == PRIMARY_TYPE.URL ? urlEntriesCount : contentEntriesCount;
|
||||
if (count !== undefined) {
|
||||
primaryAction = count > 0 ? PRIMARY_ACTION.OPEN : PRIMARY_ACTION.SAVE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let primaryAction: [string, () => void] | undefined;
|
||||
$: primaryAction =
|
||||
contentType &&
|
||||
(contentType == "text/html"
|
||||
? ["Open as URL", openAsUrl]
|
||||
: ["Open Content", openContent]);
|
||||
let urlAddress: string | undefined = undefined;
|
||||
let contentAddress: string | undefined = undefined;
|
||||
|
||||
$: primaryActionLabel = primaryAction ? primaryAction[0] : "...";
|
||||
function performPrimaryAction() {
|
||||
primaryAction[1]();
|
||||
$: currentUrl &&
|
||||
api
|
||||
.getAddress({ url: currentUrl })
|
||||
.then((address) => {
|
||||
urlAddress = address;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
urlAddress = undefined;
|
||||
});
|
||||
|
||||
$: currentUrl &&
|
||||
api
|
||||
.getAddress({ urlContent: currentUrl })
|
||||
.then((address) => (contentAddress = address))
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
contentAddress = undefined;
|
||||
});
|
||||
|
||||
let urlEntriesCount: number | undefined = undefined;
|
||||
let contentEntriesCount: number | undefined = undefined;
|
||||
|
||||
$: urlAddress &&
|
||||
api
|
||||
.fetchEntity(urlAddress)
|
||||
.then(
|
||||
(obj) => (urlEntriesCount = Object.keys(obj.listing.entries).length)
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
urlEntriesCount = undefined;
|
||||
});
|
||||
|
||||
$: contentAddress &&
|
||||
api
|
||||
.fetchEntity(contentAddress)
|
||||
.then(
|
||||
(obj) => (contentEntriesCount = Object.keys(obj.listing.entries).length)
|
||||
)
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
contentEntriesCount = undefined;
|
||||
});
|
||||
|
||||
async function performPrimary() {
|
||||
if (
|
||||
primaryType == PRIMARY_TYPE.UNKNOWN ||
|
||||
primaryAction == PRIMARY_ACTION.UNKNOWN
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (primaryType) {
|
||||
case PRIMARY_TYPE.URL:
|
||||
await performUrl();
|
||||
break;
|
||||
case PRIMARY_TYPE.CONTENT:
|
||||
await performContent();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
async function performUrl() {
|
||||
if (urlEntriesCount === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (urlEntriesCount === 0) {
|
||||
await api.putEntry({
|
||||
entity: {
|
||||
t: "Url",
|
||||
c: currentUrl,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
visitAddress(urlAddress);
|
||||
}
|
||||
|
||||
async function performContent() {
|
||||
if (contentEntriesCount === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (contentEntriesCount === 0) {
|
||||
await api.putBlob(new URL(currentUrl));
|
||||
}
|
||||
|
||||
visitAddress(contentAddress);
|
||||
}
|
||||
|
||||
function visitAddress(address: string) {
|
||||
browser.tabs.create({ url: `${$cleanInstanceUrl}/#/browse/${address}` });
|
||||
window.close();
|
||||
}
|
||||
|
||||
function processError(err: unknown): string {
|
||||
|
@ -114,19 +204,39 @@
|
|||
<div class="primary-controls">
|
||||
<button
|
||||
class="button"
|
||||
disabled={!primaryAction}
|
||||
on:click={performPrimaryAction}
|
||||
disabled={primaryAction == PRIMARY_ACTION.UNKNOWN}
|
||||
on:click={performPrimary}
|
||||
>
|
||||
{primaryActionLabel}
|
||||
{primaryAction} as {primaryType}
|
||||
</button>
|
||||
<div class="label">Content type: {contentType || "???"}</div>
|
||||
</div>
|
||||
<div class="controls row">
|
||||
<button class="button" on:click={openAsUrl}>Open as URL</button>
|
||||
<button class="button" on:click={openContent}>Open Content</button>
|
||||
<button
|
||||
class="button"
|
||||
disabled={urlEntriesCount === undefined}
|
||||
on:click={performUrl}
|
||||
>
|
||||
{urlEntriesCount === undefined
|
||||
? "???"
|
||||
: urlEntriesCount > 0
|
||||
? "Open"
|
||||
: "Save"} URL
|
||||
</button>
|
||||
<button
|
||||
class="button"
|
||||
disabled={contentEntriesCount === undefined}
|
||||
on:click={performContent}
|
||||
>
|
||||
{contentEntriesCount === undefined
|
||||
? "???"
|
||||
: contentEntriesCount > 0
|
||||
? "Open"
|
||||
: "Save"} Content
|
||||
</button>
|
||||
</div>
|
||||
{#if opening && !openError}
|
||||
<div class="status-label">Opening, please wait...</div>
|
||||
<div class="status-label">Working, please wait...</div>
|
||||
{/if}
|
||||
{#if openError}
|
||||
<div class="status-label error">{openError}</div>
|
||||
|
|
Loading…
Reference in New Issue