'use client'; import { useEffect, useState, useRef } from 'react'; import { useLoaderData, Link, redirect, useNavigate } from 'react-router'; import type { Route } from './+types/asset-preview'; import { pb } from '~/lib/pocketbase'; import { Button } from '~/components/ui/button'; import { Badge } from '~/components/ui/badge'; import { Card } from '~/components/ui/card'; import { Download, ImageOff, FileCode, ArrowLeft, Box } from 'lucide-react'; import { toast } from 'sonner'; export async function clientLoader({ params }: Route.ClientLoaderArgs) { if (!pb.authStore.isValid) { return redirect('/login'); } try { const asset = await pb.collection('ar_assets').getOne(params.id); return { asset, error: null }; } catch (err: any) { console.error('Failed to load asset in clientLoader:', err); return { asset: null, error: err?.message || 'Failed to fetch asset details' }; } } export default function Preview() { const { asset, error } = useLoaderData(); const navigate = useNavigate(); useEffect(() => { if (error || !asset) { toast.error(error || 'Asset not found'); navigate('/', { replace: true }); } }, [error, asset, navigate]); if (!asset) return null; const glbUrl = asset.glb_file ? pb.files.getURL(asset, asset.glb_file) : asset.glb_url; const usdzUrl = asset.usdz_file ? pb.files.getURL(asset, asset.usdz_file) : asset.usdz_url; const downloadQR = async () => { const qrUrl = asset.qr_image ? pb.files.getURL(asset, asset.qr_image) : asset.qr_file ? pb.files.getURL(asset, asset.qr_file) : null; if (!qrUrl) { toast.error('No QR code image found for this asset'); return; } try { const response = await fetch(qrUrl); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${asset.name.replace(/\s+/g, '_')}_qr.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); window.URL.revokeObjectURL(url); toast.success('QR Code downloaded successfully'); } catch (err) { console.error('Failed to download QR code:', err); toast.error('Failed to download QR code'); } }; return (
Back to Dashboard
t.
{glbUrl ? ( ) : (
Initializing 3D viewport…
)}
Asset Details

{asset.name}

{asset.qr_image ? ( {asset.name} ) : asset.qr_file ? ( {asset.name} ) : (
No QR code
)}

Scan this QR code with a smartphone camera to instantly project the 3D model in your space using AR.

GLB File Target
{glbUrl}
{usdzUrl && (
USDZ File Target
{usdzUrl}
)}
Launch AR View
); }