Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

6.4 KB
Raw Blame
import React from 'react';
import { ResourceType } from 'librechat-data-provider';
import { Users, X, ExternalLink, Info } from 'lucide-react';
import { Button, Checkbox, TooltipAnchor, useMediaQuery } from '@librechat/client';
import type { TPrincipal, AccessRoleIds } from 'librechat-data-provider';
import AccessRolesPicker from '~/components/Sharing/AccessRolesPicker';
import PrincipalAvatar from '~/components/Sharing/PrincipalAvatar';
import { RESOURCE_CONFIGS } from '~/utils/resources';
import { principalKey } from '../shareChanges';
import { useLocalize } from '~/hooks';

interface SelectedPrincipalsListProps {
  principles: TPrincipal[];
  onRemoveHandler: (principalKey: string) => void;
  onRoleChange?: (principalKey: string, newRoleId: AccessRoleIds) => void;
  onInsightsAccessChange?: (principalKey: string, enabled: boolean) => void;
  showInsightsAccess?: boolean;
  resourceType?: ResourceType;
  className?: string;
}

export default function SelectedPrincipalsList({
  principles,
  onRemoveHandler,
  className = '',
  onRoleChange,
  onInsightsAccessChange,
  showInsightsAccess = false,
  resourceType = ResourceType.AGENT,
}: SelectedPrincipalsListProps) {
  const localize = useLocalize();
  const isMobile = useMediaQuery('(max-width: 768px)');

  const getPrincipalDisplayInfo = (principal: TPrincipal) => {
    const displayName = principal.name || localize('com_ui_unknown');
    const subtitle = isMobile
      ? `${principal.type} (${principal.source || 'local'})`
      : principal.email || `${principal.type} (${principal.source || 'local'})`;

    return { displayName, subtitle };
  };

  if (principles.length === 0) {
    return (
      <div className={`space-y-3 ${className}`}>
        <div className="border-border-medium text-text-secondary rounded-lg border border-dashed py-8 text-center">
          <Users className="mx-auto mb-2 h-8 w-8 opacity-50" aria-hidden="true" />
          <p className="mt-1 text-xs">{localize('com_ui_search_above_to_add_all')}</p>
        </div>
      </div>
    );
  }

  return (
    <div className={`space-y-3 ${className}`}>
      <div className="space-y-2">
        {principles.map((share) => {
          const { displayName, subtitle } = getPrincipalDisplayInfo(share);
          const ownerRoleId = RESOURCE_CONFIGS[resourceType]?.defaultOwnerRoleId;
          const isOwner = share.accessRoleId === ownerRoleId;
          const isSharedLink = resourceType === ResourceType.SHARED_LINK;
          const lockOwner = isSharedLink && isOwner;
          const shareKey = principalKey(share);
          const hasAutomaticInsightsAccess = share.isAdmin === true;
          const insightsDescription = localize(
            hasAutomaticInsightsAccess
              ? 'com_ui_view_agent_insights_admin_description'
              : 'com_ui_view_agent_insights_description',
          );
          return (
            <div
              key={`${shareKey}-principalList`}
              className="border-border-light flex flex-col gap-3 rounded-xl border bg-transparent p-3 sm:flex-row sm:items-center sm:justify-between"
            >
              <div className="flex min-w-0 flex-1 items-center gap-3">
                <PrincipalAvatar principal={share} size="md" />

                <div className="min-w-0 flex-1">
                  <div className="truncate text-sm font-medium">{displayName}</div>
                  <div className="text-text-secondary flex items-center gap-1 text-xs">
                    <span>{subtitle}</span>
                    {share.source === 'entra' && (
                      <>
                        <ExternalLink className="h-3 w-3" aria-hidden="true" />
                        <span>{localize('com_ui_azure_ad')}</span>
                      </>
                    )}
                  </div>
                </div>
              </div>

              <div className="flex w-full shrink-0 items-center justify-end gap-2 sm:w-auto">
                {showInsightsAccess && onInsightsAccessChange && (
                  <div className="me-auto flex items-center gap-2 sm:me-1">
                    <Checkbox
                      checked={hasAutomaticInsightsAccess || share.viewInsights === true}
                      disabled={hasAutomaticInsightsAccess}
                      onCheckedChange={(checked) =>
                        onInsightsAccessChange(shareKey, checked === true)
                      }
                      aria-label={localize('com_ui_view_agent_insights')}
                    />
                    <span className="text-text-secondary text-sm whitespace-nowrap">
                      {localize('com_ui_view_agent_insights')}
                    </span>
                    <TooltipAnchor
                      description={insightsDescription}
                      render={
                        <button
                          type="button"
                          aria-label={insightsDescription}
                          className="text-text-secondary hover:text-text-primary"
                        >
                          <Info className="size-4" aria-hidden="true" />
                        </button>
                      }
                    />
                  </div>
                )}
                {lockOwner ? (
                  <span className="text-text-secondary px-3 py-2 text-sm font-medium">
                    {localize('com_ui_role_owner')}
                  </span>
                ) : (
                  !!share.accessRoleId &&
                  !!onRoleChange && (
                    <AccessRolesPicker
                      resourceType={resourceType}
                      selectedRoleId={share.accessRoleId}
                      onRoleChange={(newRole) => {
                        onRoleChange?.(shareKey, newRole);
                      }}
                      className="min-w-0"
                    />
                  )
                )}
                {!lockOwner && (
                  <Button
                    variant="outline"
                    onClick={() => onRemoveHandler(shareKey)}
                    className="hover:border-status-error-border hover:bg-status-error-subtle hover:text-text-destructive h-9 w-9 p-0"
                    aria-label={localize('com_ui_remove_user', { 0: displayName })}
                  >
                    <X className="h-4 w-4" aria-hidden="true" />
                  </Button>
                )}
              </div>
            </div>
          );
        })}
      </div>
    </div>
  );
}