Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export default function Landing() {
const [parsedProducts, setParsedProducts] = useState<ProductData[]>([]);
const [listingCount, setListingCount] = useState<number | null>(null);
const [sellerCount, setSellerCount] = useState<number | null>(null);
const productEventsLength = productEventContext.productEvents.length;

const signerContext = useContext(SignerContext);

useEffect(() => {
fetch("/api/db/marketplace-stats")
fetch("/api/db/marketplace-stats", { cache: "no-store" })
.then((r) => r.json())
.then((data) => {
if (typeof data.listingCount === "number")
Expand All @@ -46,7 +47,7 @@ export default function Landing() {
.catch((error) => {
console.error("Failed to fetch marketplace stats:", error);
});
}, []);
}, [productEventsLength]);
useEffect(() => {
if (router.pathname === "/" && signerContext.isLoggedIn) {
router.push("/marketplace");
Expand Down
75 changes: 45 additions & 30 deletions utils/nostr/fetch-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ function isHexString(value: string): boolean {
return /^[0-9a-fA-F]{64}$/.test(value);
}

function parseJsonSafely<T>(input: unknown): T | null {
if (typeof input !== "string") {
return null;
}

const trimmed = input.trim();
if (!trimmed) {
return null;
}

try {
return JSON.parse(trimmed) as T;
} catch {
return null;
}
}

export const fetchAllPosts = async (
nostr: NostrManager,
relays: string[],
Expand Down Expand Up @@ -443,22 +460,20 @@ export const fetchProfile = async (
}

for (const [pubkey, event] of latestDbEvents.entries()) {
try {
const content = JSON.parse(event.content);
const profile: NipProfile = {
pubkey: event.pubkey,
created_at: event.created_at,
content,
nip05Verified: false,
};
dbProfileMap.set(pubkey, profile);
updateProfileIfNewer(profile);
} catch (error) {
console.error(
`Failed to parse profile from DB: ${pubkey}`,
error
);
const content = parseJsonSafely<Record<string, any>>(event.content);
if (!content) {
console.warn(`Skipping invalid profile JSON from DB: ${pubkey}`);
continue;
}

const profile: NipProfile = {
pubkey: event.pubkey,
created_at: event.created_at,
content,
nip05Verified: false,
};
dbProfileMap.set(pubkey, profile);
updateProfileIfNewer(profile);
}

if (dbProfileMap.size > 0) {
Expand Down Expand Up @@ -494,23 +509,23 @@ export const fetchProfile = async (
!existing ||
event.created_at > existing.created_at
) {
try {
const content = JSON.parse(event.content);
const profile: NipProfile = {
pubkey: event.pubkey,
created_at: event.created_at,
content,
nip05Verified: false,
};
profileMap.set(event.pubkey, profile);
updatedProfiles.set(event.pubkey, profile);
updateProfileIfNewer(profile);
} catch (error) {
console.error(
`Failed parse profile for pubkey: ${event.pubkey}, ${event.content}`,
error
const content = parseJsonSafely<Record<string, any>>(event.content);
if (!content) {
console.warn(
`Skipping invalid profile JSON for pubkey: ${event.pubkey}`
);
continue;
}

const profile: NipProfile = {
pubkey: event.pubkey,
created_at: event.created_at,
content,
nip05Verified: false,
};
profileMap.set(event.pubkey, profile);
updatedProfiles.set(event.pubkey, profile);
updateProfileIfNewer(profile);
}
}

Expand Down