mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
refactor(collections): share the list sharing logic between desktop and phone
useCollectionSharing holds the invitable users lookup, the roster order, invite, role, cancel, remove and leave for both the share dialog and the share sheet. The dialog keeps one busy member per kind of row action, the sheet one for all.
This commit is contained in:
@@ -1,14 +1,10 @@
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Check, ChevronDown, Clock, Crown, Loader2, LogOut, UserPlus, X } from 'lucide-react'
|
||||
import type { CollectionMember, CollectionRole } from '@trek/shared'
|
||||
import type { CollectionMember } from '@trek/shared'
|
||||
import { COLLECTION_ROLES } from '@trek/shared'
|
||||
import type { TranslationFn } from '../../../types'
|
||||
import { collectionsApi } from '../../../api/collections'
|
||||
import { useCollectionStore } from '../../../store/collectionStore'
|
||||
import { useAuthStore } from '../../../store/authStore'
|
||||
import { useToast } from '../../../components/shared/Toast'
|
||||
import { getApiErrorMessage } from '../../../utils/apiError'
|
||||
import { avatarSrc } from '../../../utils/avatarSrc'
|
||||
import { useCollectionSharing } from '../../../components/Collections/useCollectionSharing'
|
||||
import MSheet from '../../components/MSheet'
|
||||
import { CancelPill, Eyebrow, PrimaryPill, SheetHeader } from './MCollSheetKit'
|
||||
|
||||
@@ -44,91 +40,16 @@ function MemberAvatar({ member }: { member: CollectionMember }) {
|
||||
export default function MCollShareSheet({
|
||||
open, collectionId, collectionName, isOwner, members, onClose, onAfterLeave, t,
|
||||
}: MCollShareSheetProps) {
|
||||
const toast = useToast()
|
||||
const currentUserId = useAuthStore(s => s.user?.id)
|
||||
const invite = useCollectionStore(s => s.invite)
|
||||
const cancelInvite = useCollectionStore(s => s.cancelInvite)
|
||||
const removeMember = useCollectionStore(s => s.removeMember)
|
||||
const setMemberRole = useCollectionStore(s => s.setMemberRole)
|
||||
const leave = useCollectionStore(s => s.leave)
|
||||
|
||||
const [availableUsers, setAvailableUsers] = useState<{ id: number; username: string }[]>([])
|
||||
const {
|
||||
currentUserId, availableUsers, sortedMembers, selectedUserId, setSelectedUserId, inviteRole, setInviteRole,
|
||||
inviting, confirmLeave, setConfirmLeave, leaving, busyUserId, cycleRole, cancel, remove, handleInvite, handleLeave,
|
||||
} = useCollectionSharing({ open, collectionId, isOwner, members, onAfterLeave, t, singleBusySlot: true })
|
||||
const [userPickerOpen, setUserPickerOpen] = useState(false)
|
||||
const [selectedUserId, setSelectedUserId] = useState<number | null>(null)
|
||||
const [inviteRole, setInviteRole] = useState<CollectionRole>('editor')
|
||||
const [busyUserId, setBusyUserId] = useState<number | null>(null)
|
||||
const [inviting, setInviting] = useState(false)
|
||||
const [confirmLeave, setConfirmLeave] = useState(false)
|
||||
const [leaving, setLeaving] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (!open || !isOwner || collectionId == null) return
|
||||
let cancelled = false
|
||||
collectionsApi.availableUsers(collectionId)
|
||||
.then(data => { if (!cancelled) setAvailableUsers(data.users) })
|
||||
.catch(() => { if (!cancelled) setAvailableUsers([]) })
|
||||
return () => { cancelled = true }
|
||||
}, [open, isOwner, collectionId, members.length])
|
||||
|
||||
useEffect(() => {
|
||||
if (open) return
|
||||
setSelectedUserId(null)
|
||||
setUserPickerOpen(false)
|
||||
setConfirmLeave(false)
|
||||
if (!open) setUserPickerOpen(false)
|
||||
}, [open])
|
||||
|
||||
const sortedMembers = useMemo(() => {
|
||||
const rank = (m: CollectionMember) => (m.is_owner ? 0 : m.status === 'accepted' ? 1 : 2)
|
||||
return [...members].sort((a, b) => rank(a) - rank(b) || a.username.localeCompare(b.username))
|
||||
}, [members])
|
||||
|
||||
const run = async (userId: number, action: () => Promise<void>) => {
|
||||
if (busyUserId != null) return
|
||||
setBusyUserId(userId)
|
||||
try {
|
||||
await action()
|
||||
} catch (err) {
|
||||
toast.error(getApiErrorMessage(err, t('common.error')))
|
||||
} finally {
|
||||
setBusyUserId(null)
|
||||
}
|
||||
}
|
||||
|
||||
const cycleRole = (member: CollectionMember) => {
|
||||
if (collectionId == null) return
|
||||
const current = member.role ?? 'editor'
|
||||
const next = COLLECTION_ROLES[(COLLECTION_ROLES.indexOf(current) + 1) % COLLECTION_ROLES.length]
|
||||
void run(member.user_id, () => setMemberRole(collectionId, member.user_id, next))
|
||||
}
|
||||
|
||||
const handleInvite = async () => {
|
||||
if (collectionId == null || selectedUserId == null || inviting) return
|
||||
setInviting(true)
|
||||
try {
|
||||
await invite(collectionId, selectedUserId, inviteRole)
|
||||
toast.success(t('collections.invite.sent'))
|
||||
setSelectedUserId(null)
|
||||
} catch (err) {
|
||||
toast.error(getApiErrorMessage(err, t('collections.invite.error')))
|
||||
} finally {
|
||||
setInviting(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleLeave = async () => {
|
||||
if (collectionId == null || leaving) return
|
||||
setLeaving(true)
|
||||
try {
|
||||
await leave(collectionId)
|
||||
toast.success(t('collections.share.left'))
|
||||
onAfterLeave()
|
||||
} catch (err) {
|
||||
toast.error(getApiErrorMessage(err, t('common.error')))
|
||||
} finally {
|
||||
setLeaving(false)
|
||||
}
|
||||
}
|
||||
|
||||
const selectedUser = availableUsers.find(u => u.id === selectedUserId) ?? null
|
||||
const badge = 'inline-flex flex-none items-center gap-[3px] font-geist text-[0.625rem] font-extrabold'
|
||||
|
||||
@@ -145,7 +66,7 @@ export default function MCollShareSheet({
|
||||
{sortedMembers.map(member => {
|
||||
const isSelf = member.user_id === currentUserId
|
||||
const pending = member.status === 'pending'
|
||||
const busy = busyUserId === member.user_id
|
||||
const busy = busyUserId('role') === member.user_id
|
||||
return (
|
||||
<div
|
||||
key={member.user_id}
|
||||
@@ -184,9 +105,7 @@ export default function MCollShareSheet({
|
||||
{isOwner && !member.is_owner && collectionId != null && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => run(member.user_id, () =>
|
||||
pending ? cancelInvite(collectionId, member.user_id) : removeMember(collectionId, member.user_id),
|
||||
)}
|
||||
onClick={() => (pending ? cancel(member.user_id) : remove(member.user_id))}
|
||||
disabled={busy}
|
||||
aria-label={pending ? t('collections.share.cancel') : t('collections.share.remove')}
|
||||
className="flex h-7 w-7 flex-none items-center justify-center rounded-full text-m-faint active:bg-[color:var(--m-ic)] disabled:opacity-50"
|
||||
|
||||
Reference in New Issue
Block a user