Spaces:
Running
Running
File size: 2,766 Bytes
72f0edb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
import { supabase } from "@/integrations/supabase/client";
import { Database } from "@/integrations/supabase/types";
import { ContentItem, Category } from "@/lib/types";
// Type for Urdf table row
type UrdfRow = Database["public"]["Tables"]["urdf"]["Row"];
// Convert a Supabase URDF row to our ContentItem format
export const mapUrdfToContentItem = (urdf: UrdfRow): ContentItem => {
// Convert tags to categories
const categories = urdf.tags || [];
return {
id: urdf.id,
title: urdf.name,
imageUrl: urdf.image_uri || "/placeholder.svg", // This now contains the storage path or full URL
description: urdf.summary || undefined,
categories,
urdfPath: urdf.urdf_uri || "",
};
};
// Get all URDFs
export const getUrdfs = async (): Promise<ContentItem[]> => {
console.log("Fetching URDFs");
const { data, error } = await supabase.from("urdf").select("*").order("name");
console.log("URDFs fetched", data);
if (error) {
console.error("Error fetching URDFs:", error);
throw error;
}
return (data || []).map(mapUrdfToContentItem);
};
// Get unique categories from all URDFs
export const getCategories = async (): Promise<Category[]> => {
const { data, error } = await supabase.from("urdf").select("tags");
if (error) {
console.error("Error fetching categories:", error);
throw error;
}
// Extract all unique tags across all URDFs
const allTags = new Set<string>();
data.forEach((urdf) => {
if (urdf.tags && Array.isArray(urdf.tags)) {
urdf.tags.forEach((tag) => allTags.add(tag));
}
});
// Convert to Category objects
return Array.from(allTags).map((tag) => ({
id: tag,
name: tag.charAt(0).toUpperCase() + tag.slice(1), // Capitalize first letter
}));
};
// Get URDF by ID
export const getUrdfById = async (id: string): Promise<ContentItem | null> => {
const { data, error } = await supabase
.from("urdf")
.select("*")
.eq("id", id)
.single();
if (error) {
if (error.code === "PGRST116") {
// Row not found
return null;
}
console.error("Error fetching URDF by ID:", error);
throw error;
}
return mapUrdfToContentItem(data);
};
// Get public URL for image
export const getImageUrl = async (path: string): Promise<string> => {
if (!path || path.startsWith('http') || path.startsWith('data:')) {
return path || '/placeholder.svg';
}
try {
// Get public URL for the image
const { data, error } = await supabase.storage
.from('urdf-images')
.createSignedUrl(path, 3600); // 1 hour expiry
if (data && !error) {
return data.signedUrl;
}
} catch (error) {
console.error(`Error fetching image at ${path}:`, error);
}
return '/placeholder.svg';
};
|