|
|
|
@ -2,53 +2,38 @@ import { error } from "@sveltejs/kit"; |
|
|
|
import type { PageLoad } from "./$types"; |
|
|
|
import type { PageLoad } from "./$types"; |
|
|
|
import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; |
|
|
|
import { fetchEventByDTag, fetchEventById, fetchEventByNaddr, fetchEventByNevent } from "../../../../lib/utils/websocket_utils.ts"; |
|
|
|
import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; |
|
|
|
import type { NostrEvent } from "../../../../lib/utils/websocket_utils.ts"; |
|
|
|
import { browser } from "$app/environment"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const load: PageLoad = async ({ params }) => { |
|
|
|
export const load: PageLoad = async ({ params }) => { |
|
|
|
const { type, identifier } = params; |
|
|
|
const { type, identifier } = params; |
|
|
|
|
|
|
|
|
|
|
|
// Only fetch on the client side where WebSocket is available
|
|
|
|
let indexEvent: NostrEvent | null; |
|
|
|
if (!browser) { |
|
|
|
|
|
|
|
// Return basic data for SSR
|
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
publicationType: "", |
|
|
|
|
|
|
|
indexEvent: null, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let indexEvent: NostrEvent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
// Handle different identifier types
|
|
|
|
// Handle different identifier types
|
|
|
|
switch (type) { |
|
|
|
switch (type) { |
|
|
|
case 'id': |
|
|
|
case 'id': |
|
|
|
indexEvent = await fetchEventById(identifier); |
|
|
|
indexEvent = await fetchEventById(identifier); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'd': |
|
|
|
case 'd': |
|
|
|
indexEvent = await fetchEventByDTag(identifier); |
|
|
|
indexEvent = await fetchEventByDTag(identifier); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'naddr': |
|
|
|
case 'naddr': |
|
|
|
indexEvent = await fetchEventByNaddr(identifier); |
|
|
|
indexEvent = await fetchEventByNaddr(identifier); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'nevent': |
|
|
|
case 'nevent': |
|
|
|
indexEvent = await fetchEventByNevent(identifier); |
|
|
|
indexEvent = await fetchEventByNevent(identifier); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
default: |
|
|
|
error(400, `Unsupported identifier type: ${type}`); |
|
|
|
error(400, `Unsupported identifier type: ${type}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!indexEvent) { |
|
|
|
if (!indexEvent) { |
|
|
|
error(404, `Event not found for ${type}: ${identifier}`); |
|
|
|
error(404, `Event not found for ${type}: ${identifier}`); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; |
|
|
|
const publicationType = indexEvent.tags.find((tag) => tag[0] === "type")?.[1] ?? ""; |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
publicationType, |
|
|
|
publicationType, |
|
|
|
indexEvent, |
|
|
|
indexEvent, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
console.error('Failed to fetch publication:', err); |
|
|
|
|
|
|
|
error(404, `Failed to load publication: ${err}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
}; |