Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

7.1 KB
Raw Blame
import { useId, useState } from 'react'
import { Share2, Eye, EyeOff, Loader2, X } from 'lucide-react'
import { useVacayStore } from '../../store/vacayStore'
import { useTranslation } from '../../i18n'
import CustomSelect from '../shared/CustomSelect'
import { DialogButton, DialogFooter, DialogHeader, DialogShell, DialogTile, FooterSpacer, NEUTRAL_TINT, fs } from '../shared/DialogShell'
import VacayBadge from './VacayBadge'
import { useVacayShareActions } from './useVacayShareActions'
import { useVacayUserPicker } from './useVacayUserPicker'

/**
 * Sidebar card for read-only calendar sharing (#444/#667). Deliberately separate
 * from the Persons card: Persons is the fusion (merge) feature, this card only
 * grants view access. Incoming rows toggle that person's calendar overlay.
 */
export default function VacaySharedCalendars() {
  const { t } = useTranslation()
  const { outgoingShares, incomingShares, shareWith } = useVacayStore()

  const [showShare, setShowShare] = useState(false)
  const {
    available: availableUsers, selected: selectedUser, setSelected: setSelectedUser,
    sending: sharing, load: loadAvailable, send,
  } = useVacayUserPicker({
    endpoint: '/addons/vacay/shares/available-users',
    submit: shareWith,
    successKey: 'vacay.shareSent',
    errorKey: 'vacay.shareFailed',
  })
  const shareLabelId = useId()

  const handleShare = () => send(() => {
    setShowShare(false)
    setSelectedUser(null)
  })

  const { toggleHidden: handleToggleHidden, remove: handleRemove } = useVacayShareActions()

  const closeShare = () => setShowShare(false)

  const empty = incomingShares.length === 0 && outgoingShares.length === 0

  return (
    <div className="vg-card rounded-[22px]" style={{ padding: '14px 18px' }}>
      <div className="flex items-center justify-between mb-2">
        <span style={{ fontSize: 11, fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.14em', color: 'var(--vg-ink3)' }}>{t('vacay.sharedCalendars')}</span>
        <button type="button" onClick={() => { setShowShare(true); void loadAvailable() }}
          className="w-7 h-7 rounded-lg flex items-center justify-center transition-colors"
          style={{ color: 'var(--vg-ink3)' }}
          title={t('vacay.shareCalendar')}>
          <Share2 size={14} />
        </button>
      </div>

      {empty && (
        <p style={{ fontSize: 12, color: 'var(--vg-ink3)', lineHeight: 1.5 }}>{t('vacay.sharedEmpty')}</p>
      )}

      {incomingShares.length > 0 && (
        <div className="flex flex-col gap-1">
          {incomingShares.map(s => (
            <div key={s.id}
              role="button"
              // No press-scale on the row: shrinking it mid-click slides the remove X
              // out from under the pointer, so the click retargets onto the row and
              // toggles visibility instead of removing the share (#2158).
              data-no-press
              tabIndex={0}
              onClick={() => handleToggleHidden(s.id, !s.hidden)}
              onKeyDown={e => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleToggleHidden(s.id, !s.hidden) } }}
              className="flex items-center gap-2.5 group transition-colors cursor-pointer"
              style={{ padding: '7px 10px', borderRadius: 12, opacity: s.hidden ? 0.55 : 1 }}
              title={s.hidden ? t('vacay.showInCalendar') : t('vacay.hideFromCalendar')}>
              {/* Ring dot — mirrors how shared days render in the grid (outline, not fill). */}
              <span className="w-3 h-3 rounded-full shrink-0" style={{ border: `2.5px solid ${s.color}` }} />
              <span className="truncate min-w-0" style={{ fontSize: 13, fontWeight: 600, color: 'var(--vg-ink)' }}>
                {s.username}
              </span>
              <VacayBadge label={t('vacay.viewOnly')} />
              <span className="ms-auto flex items-center gap-1">
                <button type="button" onClick={e => { e.stopPropagation(); handleRemove(s.id) }}
                  className="opacity-0 group-hover:opacity-100 w-5 h-5 rounded flex items-center justify-center transition-all"
                  style={{ color: 'var(--vg-ink3)' }}
                  title={t('vacay.remove')}>
                  <X size={12} />
                </button>
                {s.hidden
                  ? <EyeOff size={14} style={{ color: 'var(--vg-ink3)' }} />
                  : <Eye size={14} style={{ color: 'var(--vg-ink2)' }} />}
              </span>
            </div>
          ))}
        </div>
      )}

      {outgoingShares.length > 0 && (
        <div className="flex flex-col gap-1" style={{ marginTop: incomingShares.length > 0 ? 10 : 0 }}>
          <span style={{ fontSize: 10, fontWeight: 600, textTransform: 'uppercase', letterSpacing: '0.1em', color: 'var(--vg-ink3)', padding: '0 10px' }}>
            {t('vacay.youShareWith')}
          </span>
          {outgoingShares.map(s => (
            <div key={s.id} className="flex items-center gap-2.5 group"
              style={{ padding: '5px 10px', borderRadius: 12 }}>
              <Share2 size={12} style={{ color: 'var(--vg-ink3)' }} />
              <span className="truncate min-w-0" style={{ fontSize: 13, color: 'var(--vg-ink2)' }}>
                {s.username}
              </span>
              <button type="button" onClick={() => handleRemove(s.id)}
                className="ms-auto opacity-0 group-hover:opacity-100 text-[10px] px-1.5 py-0.5 rounded transition-all"
                style={{ color: 'var(--vg-ink3)' }}>
                {t('vacay.stopSharing')}
              </button>
            </div>
          ))}
        </div>
      )}

      <DialogShell
        open={showShare}
        onClose={closeShare}
        labelledBy={shareLabelId}
        width="narrow"
        header={(
          <DialogHeader
            tile={<DialogTile><Share2 size={20} strokeWidth={1.9} className="text-content-muted" /></DialogTile>}
            tint={NEUTRAL_TINT}
            labelId={shareLabelId}
            onClose={closeShare}
            title={t('vacay.shareCalendar')}
            sub={t('vacay.shareCalendarHint')}
            subWraps
          />
        )}
        footer={(
          <DialogFooter>
            <FooterSpacer />
            <DialogButton onClick={closeShare}>{t('common.cancel')}</DialogButton>
            <DialogButton variant="primary" onClick={handleShare} disabled={!selectedUser || sharing}
              icon={sharing ? <Loader2 size={14} className="animate-spin" /> : undefined}>
              {t('vacay.share')}
            </DialogButton>
          </DialogFooter>
        )}
      >
        {availableUsers.length === 0 ? (
          <p className="m-0 rounded-[12px] bg-surface-secondary px-4 py-5 text-center text-content-faint" style={fs(12.5, 'body')}>
            {t('vacay.noUsersAvailable')}
          </p>
        ) : (
          <CustomSelect
            value={selectedUser}
            onChange={v => setSelectedUser(Number(v))}
            options={availableUsers.map(u => ({ value: u.id, label: u.username }))}
            placeholder={t('vacay.selectUser')}
            searchable
          />
        )}
      </DialogShell>
    </div>
  )
}