All files / libs/tenants/teams/views/details/src/lib/components/header/team-name-quick-edit TeamNameQuickEdit.tsx

100% Statements 76/76
100% Branches 12/12
100% Functions 0/0
100% Lines 76/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 4x 1x 1x 3x 3x 3x 3x 3x 3x 4x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 4x 4x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 5x 5x 5x 5x 5x 1x  
import { IconArchive } from '@tabler/icons-react';
import { HttpStatusCode } from 'axios';
import { memo, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import { z } from 'zod';
 
import { Group, QuickEdit, QuickEditAsyncValidationError } from '@allshares/studio-design-system';
import { isHttpError } from '@amalia/core/http/client';
import { toError } from '@amalia/ext/typescript';
import { canModifyTeams } from '@amalia/kernel/auth/shared';
import { useAbilityContext } from '@amalia/kernel/auth/state';
import { useSetTeamName } from '@amalia/tenants/teams/state';
import { type TeamContract } from '@amalia/tenants/teams/types';
 
const nameValidationSchema = z.string().min(2).max(100);
 
export type TeamNameQuickEditProps = {
  readonly team: Pick<TeamContract, 'archived' | 'id' | 'name'>;
};
 
export const TeamNameQuickEdit = memo(function TeamNameQuickEdit({ team }: TeamNameQuickEditProps) {
  const ability = useAbilityContext();
  const { mutateAsync: setTeamName, error } = useSetTeamName();
 
  const handleSetTeamName = useCallback(
    async (newName: string) => {
      if (newName === team.name) {
        return;
      }
 
      try {
        await setTeamName({
          teamId: team.id,
          name: newName,
        });
      } catch (err) {
        throw new QuickEditAsyncValidationError(toError(err));
      }
    },
    [setTeamName, team],
  );
 
  return (
    <QuickEdit
      disabled={!canModifyTeams(ability)}
      schema={nameValidationSchema}
      value={team.name}
      variant="heading1Bold"
      error={
        isHttpError(error) && error.statusCode === HttpStatusCode.Conflict ? (
          <FormattedMessage defaultMessage="A team with the same name already exists." />
        ) : (
          error?.message
        )
      }
      formattedValue={
        team.archived ? (
          <Group
            align="center"
            gap={6}
          >
            <IconArchive
              data-testid="archive-icon"
              size={24}
            />
 
            <span>{team.name}</span>
          </Group>
        ) : (
          team.name
        )
      }
      onChange={handleSetTeamName}
    />
  );
});