mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-28 08:22:24 +00:00
feat: show provider metadata in saved profiles
This commit is contained in:
1 parent
3532c212f3
commit
6d84d6be4e
8 files changed
+362
-29
No files matched your search
@@ -1,14 +1,129 @@
|
||||
import nip07 from "./nip07.js";
|
||||
|
||||
const map = {
|
||||
[(nip07 && typeof nip07.id === "string" && nip07.id.trim()) || "nip07"]: nip07,
|
||||
};
|
||||
function normalizeProviderId(provider, fallbackId) {
|
||||
if (provider && typeof provider.id === "string") {
|
||||
const trimmed = provider.id.trim();
|
||||
if (trimmed) {
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(map, "nip07")) {
|
||||
map.nip07 = nip07;
|
||||
if (typeof fallbackId === "string") {
|
||||
const trimmed = fallbackId.trim();
|
||||
if (trimmed) {
|
||||
return trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
return "provider";
|
||||
}
|
||||
|
||||
export const providers = Object.freeze({ ...map });
|
||||
function normalizeProviderMetadata(provider, fallbackId) {
|
||||
const id = normalizeProviderId(provider, fallbackId);
|
||||
const label =
|
||||
provider && typeof provider.label === "string" && provider.label.trim()
|
||||
? provider.label.trim()
|
||||
: "Saved profile";
|
||||
const description =
|
||||
provider && typeof provider.description === "string" && provider.description.trim()
|
||||
? provider.description.trim()
|
||||
: "";
|
||||
const badgeVariant =
|
||||
provider && typeof provider.badgeVariant === "string" && provider.badgeVariant.trim()
|
||||
? provider.badgeVariant.trim()
|
||||
: "neutral";
|
||||
|
||||
return Object.freeze({ id, label, description, badgeVariant });
|
||||
}
|
||||
|
||||
const providerList = [nip07].filter(Boolean);
|
||||
const providerMap = {};
|
||||
const metadataMap = {};
|
||||
|
||||
providerList.forEach((provider, index) => {
|
||||
const fallbackId = `provider-${index}`;
|
||||
const id = normalizeProviderId(provider, fallbackId);
|
||||
providerMap[id] = provider;
|
||||
metadataMap[id] = normalizeProviderMetadata(provider, id);
|
||||
});
|
||||
|
||||
if (!Object.prototype.hasOwnProperty.call(providerMap, "nip07")) {
|
||||
providerMap.nip07 = nip07;
|
||||
metadataMap.nip07 = normalizeProviderMetadata(nip07, "nip07");
|
||||
} else if (!Object.prototype.hasOwnProperty.call(metadataMap, "nip07")) {
|
||||
metadataMap.nip07 = normalizeProviderMetadata(providerMap.nip07, "nip07");
|
||||
}
|
||||
|
||||
const builtInMetadata = [
|
||||
{
|
||||
id: "nsec",
|
||||
label: "Direct key",
|
||||
description: "Use a saved nsec key stored locally.",
|
||||
badgeVariant: "warning",
|
||||
},
|
||||
];
|
||||
|
||||
builtInMetadata.forEach((entry) => {
|
||||
if (!entry || typeof entry !== "object") {
|
||||
return;
|
||||
}
|
||||
|
||||
const id =
|
||||
typeof entry.id === "string" && entry.id.trim() ? entry.id.trim() : null;
|
||||
if (!id || Object.prototype.hasOwnProperty.call(metadataMap, id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
metadataMap[id] = Object.freeze({
|
||||
id,
|
||||
label:
|
||||
typeof entry.label === "string" && entry.label.trim()
|
||||
? entry.label.trim()
|
||||
: "Saved profile",
|
||||
description:
|
||||
typeof entry.description === "string" && entry.description.trim()
|
||||
? entry.description.trim()
|
||||
: "",
|
||||
badgeVariant:
|
||||
typeof entry.badgeVariant === "string" && entry.badgeVariant.trim()
|
||||
? entry.badgeVariant.trim()
|
||||
: "neutral",
|
||||
});
|
||||
});
|
||||
|
||||
export const providers = Object.freeze({ ...providerMap });
|
||||
export const providerMetadata = Object.freeze({ ...metadataMap });
|
||||
|
||||
const unknownMetadataCache = new Map();
|
||||
|
||||
function getFallbackMetadata(providerId) {
|
||||
const id =
|
||||
typeof providerId === "string" && providerId.trim() ? providerId.trim() : "unknown";
|
||||
if (!unknownMetadataCache.has(id)) {
|
||||
unknownMetadataCache.set(
|
||||
id,
|
||||
Object.freeze({
|
||||
id,
|
||||
label: "Saved profile",
|
||||
description: "",
|
||||
badgeVariant: "neutral",
|
||||
}),
|
||||
);
|
||||
}
|
||||
return unknownMetadataCache.get(id);
|
||||
}
|
||||
|
||||
export function getProviderMetadata(providerId) {
|
||||
if (typeof providerId === "string" && providerId.trim()) {
|
||||
const normalizedId = providerId.trim();
|
||||
if (Object.prototype.hasOwnProperty.call(providerMetadata, normalizedId)) {
|
||||
return providerMetadata[normalizedId];
|
||||
}
|
||||
return getFallbackMetadata(normalizedId);
|
||||
}
|
||||
|
||||
return getFallbackMetadata("saved-profile");
|
||||
}
|
||||
|
||||
export function getProvider(providerId = "nip07") {
|
||||
const id =
|
||||
|
||||
@@ -47,6 +47,7 @@ export default {
|
||||
id: PROVIDER_ID,
|
||||
label: PROVIDER_LABEL,
|
||||
description: PROVIDER_DESCRIPTION,
|
||||
badgeVariant: "info",
|
||||
capabilities: PROVIDER_CAPABILITIES,
|
||||
button: PROVIDER_BUTTON,
|
||||
messages: PROVIDER_MESSAGES,
|
||||
|
||||
@@ -521,6 +521,8 @@ export default class AuthService {
|
||||
providerResult,
|
||||
);
|
||||
|
||||
const entryProviderId = this.normalizeProviderId(providerId) || entryAuthType;
|
||||
|
||||
mutateSavedProfiles((profiles) => {
|
||||
const draft = Array.isArray(profiles) ? profiles.slice() : [];
|
||||
const existingIndex = draft.findIndex((entry) => {
|
||||
@@ -537,6 +539,7 @@ export default class AuthService {
|
||||
name: cachedProfile.name || draft[existingIndex]?.name || "",
|
||||
picture: cachedProfile.picture || draft[existingIndex]?.picture || "",
|
||||
authType: entryAuthType,
|
||||
providerId: entryProviderId,
|
||||
};
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
@@ -545,7 +548,8 @@ export default class AuthService {
|
||||
currentEntry.npub !== nextEntry.npub ||
|
||||
currentEntry.name !== nextEntry.name ||
|
||||
currentEntry.picture !== nextEntry.picture ||
|
||||
currentEntry.authType !== nextEntry.authType;
|
||||
currentEntry.authType !== nextEntry.authType ||
|
||||
currentEntry.providerId !== nextEntry.providerId;
|
||||
if (changed) {
|
||||
draft[existingIndex] = nextEntry;
|
||||
savedProfilesMutated = true;
|
||||
@@ -791,12 +795,16 @@ export default class AuthService {
|
||||
return { removed: changed };
|
||||
}
|
||||
|
||||
async switchProfile(pubkey) {
|
||||
async switchProfile(pubkey, options = {}) {
|
||||
const normalizedTarget = this.normalizeHexPubkey(pubkey);
|
||||
if (!normalizedTarget) {
|
||||
throw new Error("Unable to switch profiles: invalid account.");
|
||||
}
|
||||
|
||||
const normalizedOptions =
|
||||
options && typeof options === "object" ? { ...options } : {};
|
||||
const providerId = this.normalizeProviderId(normalizedOptions.providerId);
|
||||
|
||||
const normalizedActive = this.normalizeHexPubkey(getActiveProfilePubkey());
|
||||
if (normalizedActive && normalizedActive === normalizedTarget) {
|
||||
return { switched: false, reason: "already-active" };
|
||||
@@ -806,6 +814,7 @@ export default class AuthService {
|
||||
allowAccountSelection: true,
|
||||
expectPubkey: normalizedTarget,
|
||||
persistActive: true,
|
||||
...(providerId ? { providerId } : {}),
|
||||
};
|
||||
const autoApply = requestOptions.autoApply !== false;
|
||||
|
||||
@@ -827,7 +836,10 @@ export default class AuthService {
|
||||
if (!detail && !autoApply) {
|
||||
const fallbackTarget =
|
||||
this.normalizeHexPubkey(requestedPubkey) || requestedPubkey || normalizedTarget;
|
||||
detail = await this.login(fallbackTarget, { persistActive: true });
|
||||
detail = await this.login(fallbackTarget, {
|
||||
persistActive: true,
|
||||
providerId,
|
||||
});
|
||||
}
|
||||
|
||||
mutateSavedProfiles((profiles) => {
|
||||
|
||||
Reference in new issue
Block a user