Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

10.9 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { useState } from 'react'

import { IconDashboard, IconGraph, IconLock, IconNotebook, IconRewindPlay, IconBell, IconLetter } from '@posthog/icons'
import { LemonButton, LemonSwitch, LemonTable } from '@posthog/lemon-ui'

import { TZLabel } from 'lib/components/TZLabel'
import { IconSlack, IconMicrosoftTeams } from 'lib/lemon-ui/icons'
import { Link } from 'lib/lemon-ui/Link'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { userLogic } from 'scenes/userLogic'

import type { MemberOffboardingResourceApi } from 'products/platform_features/frontend/generated/api.schemas'
import { subscriptionDestination } from 'products/subscriptions/frontend/scenes/components/subscriptionDestination'

import { memberOffboardingLogic } from './memberOffboardingLogic'

const DESTINATION_ICONS: Record<string, JSX.Element> = {
    email: <IconLetter />,
    slack: <IconSlack />,
    teams: <IconMicrosoftTeams />,
}

const RESOURCE_ICONS = {
    dashboard: <IconDashboard />,
    insight: <IconGraph />,
    notebook: <IconNotebook />,
    recording: <IconRewindPlay />,
    subscription: <IconBell />,
    alert: <IconBell />,
}

export function MemberOffboardingResourceSection({
    resources,
    kind,
}: {
    resources: MemberOffboardingResourceApi[]
    kind: 'public' | 'subscriptions'
}): JSX.Element {
    const [bulkBarTarget, setBulkBarTarget] = useState<HTMLDivElement | null>(null)
    const { member, keep, removedLoading, savingResourcesLoading, savingResourceIds, getSelectionLabel } =
        useValues(memberOffboardingLogic)
    const { setKeep, disableResources } = useActions(memberOffboardingLogic)
    const { user } = useValues(userLogic)
    const isSelf = member?.user.uuid === user?.uuid
    const disabledReason = removedLoading ? 'Removing member…' : savingResourcesLoading ? 'Saving changes…' : undefined
    const isSubscription = kind === 'subscriptions'
    return (
        <section className="space-y-3">
            <h3>{isSubscription ? 'Subscriptions and alerts' : 'Publicly shared resources'}</h3>
            <p>
                {isSubscription
                    ? 'These subscriptions and alerts were created by this member. You can keep or disable them.'
                    : "These resources were shared by this member and are accessible to anyone with the link. You can disable public sharing. This won't delete them."}
            </p>
            {!isSelf && (
                <div className="relative h-9">
                    <div ref={setBulkBarTarget} className="absolute inset-0 [&:not(:empty)+span]:hidden" />
                    <span className="flex h-full items-center text-secondary text-sm">
                        {getSelectionLabel(resources.map((resource) => resource.id))}
                    </span>
                </div>
            )}
            <LemonTable
                dataSource={resources}
                rowKey="id"
                bulkSelection={
                    isSelf
                        ? undefined
                        : {
                              getKey: (resource) => resource.id,
                              noun: isSubscription
                                  ? ['subscription or alert', 'subscriptions and alerts']
                                  : ['resource', 'resources'],
                              rowAriaLabel: (resource) => `Select ${resource.name}`,
                              headerAriaLabel: isSubscription
                                  ? 'Select all subscriptions and alerts on this page'
                                  : 'Select all resources on this page',
                              clearSelectionKey: member?.user.uuid,
                              isRowSelectable: () => (disabledReason ? { disabledReason } : true),
                              barPortalTarget: bulkBarTarget,
                              actionsPlacement: 'start',
                              barClassName: 'justify-start! px-0! [&>span]:min-w-0 [&>span]:truncate',
                              getSelectionLabel: ({ selectedKeys }) => getSelectionLabel(selectedKeys),
                              renderActions: (ctx) => (
                                  <LemonButton
                                      type="secondary"
                                      status="danger"
                                      size="small"
                                      loading={resources.some((resource) => savingResourceIds.includes(resource.id))}
                                      disabledReason={disabledReason}
                                      onClick={() => {
                                          disableResources([...ctx.selectedKeys])
                                          ctx.clearSelection()
                                      }}
                                  >
                                      Disable selected
                                  </LemonButton>
                              ),
                          }
                }
                pagination={resources.length > 5 ? { pageSize: 5 } : undefined}
                columns={[
                    {
                        title: isSubscription ? 'Name' : 'Resource name',
                        key: 'name',
                        render: (_, resource) => (
                            <div className="flex items-center gap-2 min-w-0">
                                <Tooltip
                                    title={
                                        resource.destination_type ? (
                                            <div className="text-left">
                                                {subscriptionDestination(
                                                    resource.destination_type,
                                                    resource.destination ?? ''
                                                ).parts.map((recipient, index) => (
                                                    <div key={index}>{recipient}</div>
                                                ))}
                                            </div>
                                        ) : (
                                            resource.kind
                                        )
                                    }
                                >
                                    <span
                                        className="inline-flex text-lg text-secondary shrink-0"
                                        role="img"
                                        aria-label={resource.destination_type ?? resource.kind}
                                        tabIndex={resource.destination_type ? 0 : undefined}
                                    >
                                        {resource.destination_type
                                            ? (DESTINATION_ICONS[resource.destination_type] ?? <IconBell />)
                                            : RESOURCE_ICONS[resource.kind]}
                                    </span>
                                </Tooltip>
                                <Link
                                    to={resource.url ?? undefined}
                                    target="_blank"
                                    targetBlankIcon={false}
                                    className="break-words min-w-0"
                                    data-attr="offboarding-resource-link"
                                >
                                    {resource.name}
                                </Link>
                            </div>
                        ),
                    },
                    {
                        title: 'Project',
                        dataIndex: 'project_name',
                        key: 'project',
                        width: 124,
                        render: (name) => <span className="break-words">{name}</span>,
                    },
                    ...(isSubscription
                        ? [
                              {
                                  title: 'Created on',
                                  key: 'created_at',
                                  width: 96,
                                  render: (_: unknown, resource: MemberOffboardingResourceApi) =>
                                      resource.created_at ? (
                                          <TZLabel time={resource.created_at} />
                                      ) : (
                                          <span className="text-secondary">Unknown</span>
                                      ),
                              },
                          ]
                        : []),
                    ...(!isSelf
                        ? [
                              {
                                  title: isSubscription ? 'Enabled' : 'Publicly shared',
                                  key: 'disable',
                                  width: 112,
                                  render: (_: unknown, resource: MemberOffboardingResourceApi) => (
                                      <div className="flex items-center gap-2">
                                          <LemonSwitch
                                              loading={savingResourceIds.includes(resource.id)}
                                              checked={keep.includes(resource.id)}
                                              onChange={(enabled) => setKeep(resource.id, enabled)}
                                              disabledReason={disabledReason}
                                              aria-label={`${isSubscription ? 'Enable' : 'Public sharing for'} ${resource.name}`}
                                              data-attr={
                                                  isSubscription
                                                      ? 'offboarding-disable-subscription'
                                                      : 'offboarding-disable-dashboard'
                                              }
                                          />
                                          {resource.password_protected && (
                                              <Tooltip title="Password protected">
                                                  <span
                                                      className="flex text-secondary"
                                                      role="img"
                                                      aria-label="Password protected"
                                                  >
                                                      <IconLock />
                                                  </span>
                                              </Tooltip>
                                          )}
                                      </div>
                                  ),
                              },
                          ]
                        : []),
                ]}
            />
        </section>
    )
}