44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useAuth } from '@/features/auth/hooks/useAuth';
|
|
import { useCollaborators } from './usePlaylist';
|
|
import {
|
|
canEdit as canEditPermission,
|
|
canDelete as canDeletePermission,
|
|
canAddTracks as canAddTracksPermission,
|
|
canRemoveTracks as canRemoveTracksPermission,
|
|
canManageCollaborators as canManageCollaboratorsPermission,
|
|
canRead as canReadPermission,
|
|
} from '../utils/permissions';
|
|
import type { Playlist } from '../types';
|
|
|
|
export function usePlaylistPermissions(playlist?: Playlist) {
|
|
const { user } = useAuth();
|
|
const { data: collaborators = [] } = useCollaborators(
|
|
playlist ? String(playlist.id) : '',
|
|
);
|
|
|
|
return useMemo(() => {
|
|
if (!playlist || !user) {
|
|
return {
|
|
canEdit: false,
|
|
canDelete: false,
|
|
canAddTracks: false,
|
|
canRemoveTracks: false,
|
|
canManageCollaborators: false,
|
|
canRead: false,
|
|
isOwner: false,
|
|
};
|
|
}
|
|
|
|
const userId = user.id;
|
|
return {
|
|
canEdit: canEditPermission(playlist, userId, collaborators),
|
|
canDelete: canDeletePermission(playlist, userId),
|
|
canAddTracks: canAddTracksPermission(playlist, userId, collaborators),
|
|
canRemoveTracks: canRemoveTracksPermission(playlist, userId, collaborators),
|
|
canManageCollaborators: canManageCollaboratorsPermission(playlist, userId),
|
|
canRead: canReadPermission(playlist, userId, collaborators),
|
|
isOwner: String(playlist.user_id) === String(userId),
|
|
};
|
|
}, [playlist, user, collaborators]);
|
|
}
|