Skip to main content

useRelationshipTypes

Location: src/hooks/useRelationshipTypes.js

Overview

Manages relationship type definitions -- both system-provided types (where user_id is NULL) and user-created custom types. Relationship types allow users to categorize their friendships (e.g., "close friend", "colleague", "family"). Types are organized by category and sorted alphabetically.

Signature

function useRelationshipTypes(): {
types: RelationshipTypeRow[],
typesByCategory: Record<string, RelationshipTypeRow[]>,
loading: boolean,
error: string | null,
saving: boolean,
createType: (params: { name: string, description?: string, category?: string, icon?: string, color?: string }) => Promise<{ data?: RelationshipTypeRow, error?: string }>,
updateType: (id: string, updates: { name?: string, description?: string, category?: string, icon?: string, color?: string }) => Promise<{ data?: RelationshipTypeRow, error?: string }>,
deleteType: (id: string) => Promise<{ success?: boolean, error?: string }>,
refetch: () => Promise<void>
}

Parameters

None. Automatically resolves the current authenticated user.

Return Value

PropertyTypeDescription
typesRelationshipTypeRow[]All available relationship types (system + user's custom), sorted by category then name.
typesByCategoryRecord<string, RelationshipTypeRow[]>Types grouped by their category field. Categories with no types are omitted.
loadingbooleantrue while the initial fetch is in progress.
errorstring | nullError message if the fetch failed.
savingbooleantrue while a create, update, or delete operation is in progress.
createType(params) => PromiseCreate a new custom relationship type. The name field is required; category defaults to "other". Returns { data } with the created row or { error }.
updateType(id, updates) => PromiseUpdate a custom relationship type. Only the user's own custom types can be updated (enforced by RLS via user_id match). Returns { data } or { error }.
deleteType(id) => PromiseDelete a custom relationship type. Only the user's own custom types can be deleted. Returns { success } or { error }.
refetch() => Promise<void>Re-fetch all relationship types.

Usage

function RelationshipTypeManager() {
const { typesByCategory, loading, saving, createType, deleteType } = useRelationshipTypes();

const handleCreate = async () => {
const { error } = await createType({
name: 'Hiking Buddy',
category: 'activity',
icon: 'mountain'
});
if (error) console.error(error);
};

if (loading) return <Skeleton />;

return (
<div>
{Object.entries(typesByCategory).map(([category, types]) => (
<div key={category}>
<h3>{category}</h3>
{types.map(type => (
<div key={type.id}>
<span>{type.name}</span>
{type.user_id && (
<button onClick={() => deleteType(type.id)} disabled={saving}>
Delete
</button>
)}
</div>
))}
</div>
))}
<button onClick={handleCreate} disabled={saving}>Add Custom Type</button>
</div>
);
}

Last updated: 2026-02-07