import { useId, useState, type ReactNode } from 'react' import { Check, Copy, Crown, Link2, LogOut, Pencil, Plus, Settings, Trash2, UserMinus, UserPlus, UserRound, Users } from 'lucide-react' import { useTranslation } from '../../i18n' import CustomSelect from '../shared/CustomSelect' import ConfirmDialog from '../shared/ConfirmDialog' import { Tooltip } from '../shared/Tooltip' import { DialogButton, DialogHeader, DialogSection, DialogShell, DialogTile, NEUTRAL_TINT, PILL, fs } from '../shared/DialogShell' import { AddRowButton, INPUT, LABEL, PANEL } from '../shared/dialogParts' import { LinkedJournalIcon } from './LinkedJournalIcon' import { TripJournalLinkSection } from './TripJournalLinkSection' import { TripMemberAvatar } from './TripMemberAvatar' import { useTripJournalSectionVisible } from './useTripJournalLink' import { SHARE_OPTIONS, SHARE_SECTIONS, useShareLink, useTripInviteLink, useTripMembers, type TripMembersState } from './useTripShare' interface TripShareDialogProps { isOpen: boolean onClose: () => void tripId: number tripTitle: string onMembersChanged?: () => void } /** A question the dialog asks before it acts, answered in a ConfirmDialog over it. */ interface Ask { title: string message: string confirmLabel: string run: () => void } const COUNT = 'rounded-full bg-surface-tertiary px-2 py-[2px] font-geist font-bold text-content-muted' const ROWS = 'flex flex-col gap-0.5 rounded-[14px] border border-edge-faint bg-surface-secondary p-1.5' const ROW = 'flex min-h-[48px] items-center gap-3 rounded-[10px] px-2.5 py-1.5 hover:bg-surface-card' const BADGE = 'inline-flex flex-none items-center gap-1 rounded-full px-2 py-[2px] font-semibold' /** A fixed height on a wide screen, so the dialog does not jump when another area is picked. */ const BODY = 'flex min-h-0 flex-1 max-md:flex-col md:h-[min(640px,calc(100dvh-240px))]' const NAV = 'flex flex-none gap-1 border-edge-faint bg-surface-secondary p-3 md:w-[236px] md:flex-col md:border-e max-md:overflow-x-auto max-md:border-b' const DANGER_BUTTON = 'inline-flex items-center gap-1.5 rounded-[10px] bg-surface-card px-3.5 py-2 font-medium text-danger shadow-sm ring-1 ring-edge-faint hover:bg-danger-soft disabled:cursor-default disabled:opacity-50' /** * The trip's Settings on the desktop: who is on the trip, its guests, the two * links that let others in, and the journal the trip shows. */ export default function TripShareDialog(props: TripShareDialogProps) { return props.isOpen ? : null } type Area = 'people' | 'sharing' | 'journal' /** The areas of the dialog, each with its icon, its name and the line under the name. */ const AREAS: { id: Area; icon: (on: boolean) => ReactNode; labelKey: string; hintKey: string }[] = [ { id: 'people', icon: () => , labelKey: 'trip.settings.people', hintKey: 'trip.settings.peopleHint' }, { id: 'sharing', icon: () => , labelKey: 'trip.settings.sharing', hintKey: 'trip.settings.sharingHint' }, { id: 'journal', icon: () => , labelKey: 'trip.journal.title', hintKey: 'trip.journal.hint' }, ] function OpenDialog({ onClose, tripId, tripTitle, onMembersChanged }: TripShareDialogProps) { const { t } = useTranslation() const labelId = useId() const m = useTripMembers({ isOpen: true, tripId, onClose, onMembersChanged }) const [ask, setAsk] = useState(null) const [area, setArea] = useState('people') const journalSection = useTripJournalSectionVisible(tripId) const areas = AREAS.filter(a => a.id === 'people' || (a.id === 'sharing' ? m.canManageShare : journalSection)) const current = areas.find(a => a.id === area) ?? areas[0] const panelOf = (id: Area) => areas.find(a => a.id === id) const persons = `${m.realMembers.length} ${t('members.persons', { count: m.realMembers.length })}` return ( <> 1 ? 'wide' : 'detail'} blocked={!!ask} bodyClassName={BODY} header={( } tint={NEUTRAL_TINT} labelId={labelId} onClose={onClose} eyebrow={t('trip.settings.title')} title={tripTitle} pills={m.loading ? undefined : ( <> {persons} {m.guests.length > 0 && {t('members.guests')} {m.guests.length}} )} /> )} > {areas.length > 1 && }
{/* Every area stays mounted, so its links and lists are loaded by the time it is picked. */} 1}> {m.canManageMembers && } {(m.isCurrentOwner || m.guests.length > 0) && } {m.canManageShare && ( )} {journalSection && ( )}
setAsk(null)} onConfirm={() => { ask?.run(); setAsk(null) }} title={ask?.title} message={ask?.message} confirmLabel={ask?.confirmLabel} /> ) } /** The areas down the side on a wide screen, a row of pills above the content on a narrow one. */ function AreaNav({ areas, current, onPick }: { areas: typeof AREAS; current: Area; onPick: (area: Area) => void }) { const { t } = useTranslation() return ( ) } /** One area: its name and what it is for, then its parts. Hidden rather than unmounted while another one shows. */ function AreaPanel({ area, shown, titled, children }: { area: (typeof AREAS)[number] | undefined; shown: boolean; titled: boolean; children: ReactNode }) { const { t } = useTranslation() return ( // The class, not the hidden attribute alone: flex would win over the attribute's display:none. ) } function InviteUser({ m }: { m: TripMembersState }) { const { t } = useTranslation() return (
m.setSelectedUserId(String(value))} placeholder={t('members.selectUser')} options={[ { value: '', label: t('members.selectUser') }, // As a string: the select compares values strictly and the pick is kept as one. ...m.availableUsers.map(u => ({ value: String(u.id), label: u.username })), ]} searchable style={{ flex: 1 }} size="sm" /> void m.add()} disabled={m.adding || !m.selectedUserId} icon={}> {m.adding ? '...' : t('members.invite')}
{m.availableUsers.length === 0 && m.allUsers.length > 0 && (

{t('members.allHaveAccess')}

)}
) } /** A round icon button of a row, named by its tooltip; faint until the pointer is on it. */ function RowAction({ label, onClick, disabled, danger, children }: { label: string; onClick: () => void; disabled?: boolean; danger?: boolean; children: ReactNode }) { return ( ) } function SectionLabel({ text, count }: { text: string; count?: number }) { return ( {text} {count != null && {count}} ) } function MembersList({ m, onAsk }: { m: TripMembersState; onAsk: (ask: Ask) => void }) { const { t } = useTranslation() return ( }> {m.loading ? (
{[1, 2].map(i =>
)}
) : (
{m.realMembers.map(member => { const isSelf = member.id === m.user?.id const isOwner = member.role === 'owner' const canRemove = isSelf || (m.canManageMembers && !isOwner) return (
{member.username} {isSelf && {t('members.you')}} {isOwner && ( {t('members.owner')} )}
{m.isCurrentOwner && !isOwner && ( onAsk({ title: t('members.makeOwner'), message: t('members.confirmTransfer', { name: member.username }), confirmLabel: t('common.confirm'), run: () => void m.transfer(member.id), })}> )} {canRemove && ( onAsk({ title: isSelf ? t('members.leaveTrip') : t('members.removeAccess'), message: isSelf ? t('members.confirmLeave') : t('members.confirmRemove'), confirmLabel: t('common.confirm'), run: () => void m.remove(member.id, isSelf), })}> {isSelf ? : } )}
) })}
)} ) } /** Accountless participants (#1362): the owner names, renames and removes them. */ function GuestsList({ m, onAsk }: { m: TripMembersState; onAsk: (ask: Ask) => void }) { const { t } = useTranslation() return ( }>

{t('members.guestsHint')}

{m.guests.length > 0 && (
{m.guests.map(g => (
{m.renamingGuestId === g.id ? ( m.setRenameValue(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void m.commitRename(g.id) // Escape ends the rename, not the dialog. if (e.key === 'Escape') { e.preventDefault(); m.cancelRename() } }} onBlur={() => void m.commitRename(g.id)} maxLength={50} aria-label={t('common.rename')} className={`${INPUT} flex-1`} /> ) : (
{g.username} {t('members.guest')}
)} {m.isCurrentOwner && m.renamingGuestId !== g.id && ( <> m.startRename(g)}> onAsk({ title: t('members.removeAccess'), message: t('members.confirmRemoveGuest'), confirmLabel: t('common.delete'), run: () => void m.removeGuest(g.id), })}> )}
))}
)} {m.isCurrentOwner && (
m.setNewGuestName(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') void m.addGuest() }} placeholder={t('members.guestNamePlaceholder')} aria-label={t('members.guestNamePlaceholder')} maxLength={50} className={`${INPUT} flex-1`} /> void m.addGuest()} disabled={m.addingGuest || !m.newGuestName.trim()} icon={}> {m.addingGuest ? '...' : t('members.addGuest')}
)}
) } /** A link as a read-only line with its copy button, the way the calendar feed shows its addresses. */ function LinkLine({ url, copied, onCopy }: { url: string; copied: boolean; onCopy: () => void }) { const { t } = useTranslation() return (
e.currentTarget.select()} className="min-w-0 flex-1 truncate border-0 bg-transparent font-mono text-content outline-none" style={fs(11.5)} />
) } export function LinkCard({ icon, title, hint, children }: { icon: ReactNode; title: string; hint: string; children: ReactNode }) { return (
{icon}

{title}

{hint}

{children}
) } /** One toggle of the link, a filled pill with a tick while it is on. */ function PermChip({ on, disabled, label, hint, onToggle }: { on: boolean; disabled?: boolean; label: string; hint?: string; onToggle: () => void }) { const chip = ( ) return hint ? {chip} : chip } /** The read-only public link, and which parts of the trip it shows. */ function PublicLink({ tripId }: { tripId: number }) { const { t } = useTranslation() const link = useShareLink(tripId) if (link.loading) return null return ( } title={t('share.linkTitle')} hint={t('share.linkHint')}>
{SHARE_SECTIONS.map(opt => ( void link.setPerm(opt.key, !link.perms[opt.key])} /> ))}
{t('share.options')}
{SHARE_OPTIONS.map(opt => ( void link.setPerm(opt.key, !link.perms[opt.key])} /> ))}
{link.url ? ( <> void link.copy()} />
) : ( void link.create()}>{t('share.createLink')} )}
) } /** The link an existing user opens to join as a member (#1143). */ function InviteLink({ tripId }: { tripId: number }) { const { t } = useTranslation() const link = useTripInviteLink(tripId) if (link.loading) return null return ( } title={t('trip.invite.linkTitle')} hint={t('trip.invite.linkHint')}> {link.url ? ( <> void link.copy()} />
void link.create()} disabled={link.busy} icon={}> {t('trip.invite.regenerate')}
) : ( void link.create()} disabled={link.busy}>{t('trip.invite.create')} )}
) }