Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

7.6 KB
Raw Blame
'use client';

import { Alert, Breadcrumb, createStaticStyles, Flexbox, Icon, Text, toast } from '@lobehub/ui';
import { Form, type FormGroupItem, useForm } from '@lobehub/ui/form';
import { ChevronRight, InfoIcon, UsersIcon } from 'lucide-react';
import { memo, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router';

import { useActiveWorkspaceSlug } from '@/business/client/hooks/useActiveWorkspaceSlug';
import AsyncError from '@/components/AsyncError';
import Loading from '@/components/Loading/BrandTextLoading';
import { FORM_STYLE } from '@/const/layoutTokens';
import NavHeader from '@/features/NavHeader';
import WideScreenContainer from '@/features/WideScreenContainer';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { buildWorkspaceAwarePath } from '@/features/Workspace/workspaceAwarePath';
import type { PermissionResourceType } from '@/services/resourcePermission';
import { isForbiddenError } from '@/utils/forbiddenError';

import { AddCollaboratorButton, CollaboratorList } from './Collaborators';
import PolicySelect from './PolicySelect';
import { useAccessLevelOptions } from './useAccessLevelOptions';
import { useResourcePermission } from './useResourcePermission';

const styles = createStaticStyles(({ css }) => ({
  body: css`
    position: relative;
    overflow-y: auto;
    display: flex;
  `,
  breadcrumb: css`
    a {
      display: flex;
      align-items: center;
    }
  `,
  rowIcon: css`
    display: flex;
    align-items: center;
    align-self: flex-start;
    height: 1em;
  `,
}));

interface ResourceAccessPageProps {
  /**
   * Per-type copy, already translated by the caller (static keys keep the
   * typed-i18n check): what the level grants day to day, and the private-mode
   * framing.
   */
  copy: {
    /** Shown under the Collaborators group title when `showCollaborators` is on. */
    collaboratorsDesc?: string;
    generalAccessDesc: string;
    privateHint: string;
    privateNotice: string;
  };
  /** Where a non-manager (or a missing/private-foreign resource) is sent. */
  redirectPath: string;
  /** Workspace-relative path of the resource itself β€” the breadcrumb's way back. */
  resourceHomePath: string;
  resourceId: string;
  /** Display name for the breadcrumb; omitted while it loads. */
  resourceName?: string | null;
  resourceType: PermissionResourceType;
  /**
   * Render the per-user Collaborators group below General access. Off by
   * default β€” only resource types whose flows support per-user grants
   * (knowledge bases today) turn it on.
   */
  showCollaborators?: boolean;
}

/**
 * A standalone Member-Permissions page for single-dimension resources
 * (Knowledge Base, Document) β€” the same page shape as Agent's, minus the
 * agent-specific editable-settings groups. Private resources are configurable
 * by their creator: the level is what members get the moment the resource is
 * published (the server stores it ahead of publishing).
 */
const ResourceAccessPage = memo<ResourceAccessPageProps>(
  ({
    copy,
    redirectPath,
    resourceHomePath,
    resourceId,
    resourceName,
    resourceType,
    showCollaborators,
  }) => {
    const { t } = useTranslation('setting');
    const navigate = useWorkspaceAwareNavigate();
    const activeWorkspaceSlug = useActiveWorkspaceSlug();
    const { data, error, isConfirmed, isLoading, mutate, setAccessLevel, updating } =
      useResourcePermission(resourceType, resourceId);

    const isPrivate = data?.visibility === 'private';
    const accessOptions = useAccessLevelOptions({
      accessLevel: data?.accessLevel,
      isPrivate,
      resourceType,
    });
    const form = useForm();

    // Managing member access is a manager-only surface, like Agent's page: a
    // non-manager (or a private resource that is not the caller's) gets a
    // reason toast and lands back where they came from. A persisted record may
    // predate a grant, so only a record re-fetched this session redirects.
    const isDenied = (!!error && isForbiddenError(error)) || (isConfirmed && !data?.canManage);
    useEffect(() => {
      if (!isDenied) return;
      toast.error(t('permission.noManagePermission'));
      navigate(redirectPath, { replace: true });
    }, [isDenied, navigate, redirectPath, t]);

    const accessGroup: FormGroupItem = {
      children: [
        {
          avatar: (
            <span className={styles.rowIcon}>
              <Icon icon={UsersIcon} size={16} />
            </span>
          ),
          children: (
            <PolicySelect
              loading={updating}
              options={accessOptions}
              value={data?.accessLevel}
              onChange={(level) => void setAccessLevel(level)}
            />
          ),
          desc: isPrivate ? copy.privateHint : copy.generalAccessDesc,
          label: t('permission.page.accessLevelLabel'),
        },
      ],
      title: t('permission.page.memberGroup'),
    };

    const formGroups: FormGroupItem[] = [accessGroup];
    if (showCollaborators) {
      formGroups.push({
        children: <CollaboratorList resourceId={resourceId} resourceType={resourceType} />,
        desc: copy.collaboratorsDesc,
        extra: <AddCollaboratorButton resourceId={resourceId} resourceType={resourceType} />,
        title: t('permission.collaborators.title'),
      });
    }

    return (
      <Flexbox height={'100%'} width={'100%'}>
        <NavHeader
          styles={{ left: { paddingInlineStart: 24 } }}
          left={
            <Breadcrumb
              className={styles.breadcrumb}
              separator={<Icon icon={ChevronRight} size={14} />}
              items={[
                ...(resourceName
                  ? [
                      {
                        title: (
                          <Link to={buildWorkspaceAwarePath(resourceHomePath, activeWorkspaceSlug)}>
                            <Text
                              ellipsis
                              as={'span'}
                              color={'inherit'}
                              style={{ maxWidth: 200 }}
                              weight={500}
                            >
                              {resourceName}
                            </Text>
                          </Link>
                        ),
                      },
                    ]
                  : []),
                {
                  title: (
                    <Text as={'span'} color={'inherit'} weight={500}>
                      {t('permission.page.title')}
                    </Text>
                  ),
                },
              ]}
            />
          }
        />
        <Flexbox className={styles.body} flex={1} width={'100%'}>
          <WideScreenContainer>
            <Flexbox gap={16} paddingBlock={16}>
              {error && !isDenied ? (
                <AsyncError error={error} variant={'inline'} onRetry={() => mutate()} />
              ) : isLoading || isDenied ? (
                <Loading debugId="ResourceAccessPage" />
              ) : (
                <>
                  {isPrivate ? (
                    <Alert
                      icon={<Icon icon={InfoIcon} />}
                      style={{ width: '100%' }}
                      title={copy.privateNotice}
                      type={'info'}
                    />
                  ) : null}
                  <Form form={form} items={formGroups} itemsType={'group'} {...FORM_STYLE} />
                </>
              )}
            </Flexbox>
          </WideScreenContainer>
        </Flexbox>
      </Flexbox>
    );
  },
);

ResourceAccessPage.displayName = 'ResourceAccessPage';

export default ResourceAccessPage;