diff --git a/app/routes/home.tsx b/app/routes/home.tsx index fc6a627..31163c0 100644 --- a/app/routes/home.tsx +++ b/app/routes/home.tsx @@ -2,7 +2,8 @@ import { useEffect, useState } from 'react'; import QRCode from 'qrcode'; -import { supabase } from '~/lib/supabase'; +import { pb } from '~/lib/pocketbase'; +import { ImageOff } from 'lucide-react'; export default function Dashboard() { const [name, setName] = useState(''); @@ -12,12 +13,15 @@ export default function Dashboard() { const [loading, setLoading] = useState(false); const fetchAssets = async () => { - const { data } = await supabase - .from('ar_assets') - .select('*') - .order('created_at', { ascending: false }); - - setAssets(data || []); + try { + const records = await pb.collection('ar_assets').getFullList({ + sort: '-created', + }); + setAssets(records); + } catch (err) { + console.error('Failed to fetch assets:', err); + setAssets([]); + } }; useEffect(() => { @@ -25,62 +29,45 @@ export default function Dashboard() { }, []); const createAsset = async () => { - if (!name || !glbUrl || !usdzUrl) { - alert('Fill all fields'); + if (!name || !glbUrl) { + alert('Please provide a name and a GLB URL'); return; } setLoading(true); - const { data, error } = await supabase - .from('ar_assets') - .insert({ + try { + const record = await pb.collection('ar_assets').create({ name, glb_url: glbUrl, - usdz_url: usdzUrl, - }) - .select() - .single(); + usdz_url: usdzUrl || '', + }); - if (error || !data) { - alert(error?.message); + const qrUrl = `${import.meta.env.VITE_FRONTEND_URL}/ar/${record.id}`; + + const qrImage = await QRCode.toDataURL(qrUrl, { + width: 512, + margin: 2, + }); + + const qrBlob = await (await fetch(qrImage)).blob(); + + const qrFormData = new FormData(); + qrFormData.append('qr_url', qrUrl); + qrFormData.append('qr_image', new File([qrBlob], `${record.id}.png`, { type: 'image/png' })); + + await pb.collection('ar_assets').update(record.id, qrFormData); + + setName(''); + setGlbUrl(''); + setUsdzUrl(''); + + fetchAssets(); + } catch (err: any) { + alert(err?.message || 'Failed to create asset'); + } finally { setLoading(false); - return; } - - // const qrUrl = `http://10.20.2.107:5173/ar/${data.id}`; - const qrUrl = `${window.location.origin}/ar/${data.id}`; - - const qrImage = await QRCode.toDataURL(qrUrl, { - width: 512, - margin: 2, - }); - - const qrBlob = await (await fetch(qrImage)).blob(); - const filePath = `${data.id}.png`; - - await supabase.storage.from('qr-codes').upload(filePath, qrBlob, { - upsert: true, - contentType: 'image/png', - }); - - const { data: publicUrl } = supabase.storage - .from('qr-codes') - .getPublicUrl(filePath); - - await supabase - .from('ar_assets') - .update({ - qr_url: qrUrl, - qr_image_url: publicUrl.publicUrl, - }) - .eq('id', data.id); - - setName(''); - setGlbUrl(''); - setUsdzUrl(''); - setLoading(false); - fetchAssets(); }; return ( @@ -101,24 +88,35 @@ export default function Dashboard() {
- setName(e.target.value)} - /> - setGlbUrl(e.target.value)} - /> - setUsdzUrl(e.target.value)} - /> +
+ + setName(e.target.value)} + /> +
+
+ + setGlbUrl(e.target.value)} + /> +
+
+ + setUsdzUrl(e.target.value)} + /> +