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.
{titled && area && (
{t(area.labelKey)}
{t(area.hintKey)}
)}
{children}
)
}
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')}
)}
)
}
/** 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 (
{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')}>
>
) : (
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()} />