Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

7.5 KB
Raw Blame
'use client';

import { useState } from 'react';
import { toast } from 'sonner';
import { useTranslations } from 'next-intl';
import { WorkspaceSectionExtras } from '@/cloud';
import type { TeamCreation, WorkspacePatch } from '@/lib/api/endpoints/workspaces';
import { useUpdateWorkspace, useWorkspaceQuery } from '@/services/workspaces.service';
import {
  workspaceInitial,
  workspaceTileClass,
  workspaceTileStyle,
} from '@/components/layout/utils/workspaceTile';
import SectionPageView from '@/components/common/page/SectionPageView';
import SettingsCard from '@/components/common/page/SettingsCard';
import SettingsSection from '@/components/common/page/SettingsSection';
import SectionPageSkeleton from '@/components/common/skeleton/SectionPageSkeleton';
import SettingsColorField from '@/features/settings/components/crud/SettingsColorField';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select';
import WorkspaceDeleteSection from './WorkspaceDeleteSection';
import WorkspaceSharingSection from './WorkspaceSharingSection';

const TEAM_CREATION: TeamCreation[] = ['owner', 'managers', 'members'];
const HEX = /^#[0-9a-f]{6}$/i;
// The highest run limit the api takes where the instance sets no ceiling.
const MAX_RUN_LIMIT = 1000;

export default function WorkspaceInfoSection({ workspaceId }: { workspaceId: number }) {
  const t = useTranslations('teams.workspace');
  const tCommon = useTranslations('common');
  const workspace = useWorkspaceQuery(workspaceId).data;
  const update = useUpdateWorkspace(workspaceId);
  const [name, setName] = useState<string | null>(null);
  // An empty field clears the colour back to the neutral tile.
  const [color, setColor] = useState<string | null>(null);
  const [teamCreation, setTeamCreation] = useState<TeamCreation | null>(null);
  const [runLimit, setRunLimit] = useState<string | null>(null);

  if (!workspace) return <SectionPageSkeleton rows={2} />;

  const isOwner = workspace.role === 'owner';
  const nextName = (name ?? workspace.name).trim();
  const nextColor = color === null ? workspace.color : color.trim() || null;
  const nextTeamCreation = teamCreation ?? workspace.teamCreation;
  const cap = workspace.maxConcurrentRunsCap;
  const maxRunLimit = cap > 0 ? cap : MAX_RUN_LIMIT;
  // Checked only once edited, so a stored limit above a ceiling lowered since does not
  // block saving the other fields.
  const runLimitValid =
    runLimit === null || (/^\d+$/.test(runLimit.trim()) && Number(runLimit) <= maxRunLimit);
  const nextRunLimit = runLimit === null ? workspace.maxConcurrentRuns : Number(runLimit);
  let runLimitHint = t('runLimit.description');
  if (!isOwner) runLimitHint = t('runLimit.ownerOnly');
  else if (cap > 0) runLimitHint = t('runLimit.descriptionCapped', { max: cap });
  const patch: WorkspacePatch = {
    ...(nextName !== workspace.name && { name: nextName }),
    ...(nextColor !== workspace.color && { color: nextColor }),
    ...(nextTeamCreation !== workspace.teamCreation && { teamCreation: nextTeamCreation }),
    ...(runLimitValid &&
      nextRunLimit !== workspace.maxConcurrentRuns && { maxConcurrentRuns: nextRunLimit }),
  };
  const valid = nextName !== '' && (nextColor === null || HEX.test(nextColor)) && runLimitValid;
  const canSave = valid && Object.keys(patch).length > 0 && !update.isPending;

  async function save() {
    await update.mutateAsync(patch);
    setName(null);
    setColor(null);
    setTeamCreation(null);
    setRunLimit(null);
    toast.success(t('info.saved'));
  }

  return (
    <SectionPageView
      title={t('info.title')}
      description={t('info.description')}
      actions={
        <Button size="sm" className="h-8" disabled={!canSave} onClick={() => void save()}>
          {tCommon('save')}
        </Button>
      }
    >
      <div className="space-y-10">
        <SettingsSection title={t('info.workspace')} description={t('info.workspaceHint')}>
          <SettingsCard className="space-y-4 p-4">
            <div className="space-y-1.5">
              <Label htmlFor="workspace-name">{tCommon('name')}</Label>
              <Input
                id="workspace-name"
                value={name ?? workspace.name}
                maxLength={60}
                onChange={(e) => setName(e.target.value)}
              />
            </div>
            <div className="flex items-center justify-between gap-4">
              <Label>{t('info.color')}</Label>
              <div className="flex items-center gap-3">
                {nextColor && (
                  <Button
                    variant="ghost"
                    size="sm"
                    className="h-7 text-xs text-muted-foreground"
                    onClick={() => setColor('')}
                  >
                    {t('info.colorReset')}
                  </Button>
                )}
                <SettingsColorField value={color ?? workspace.color ?? ''} onChange={setColor} />
                <span
                  aria-hidden
                  className={workspaceTileClass(false)}
                  style={workspaceTileStyle(nextColor && HEX.test(nextColor) ? nextColor : null)}
                >
                  {workspaceInitial(nextName || workspace.name)}
                </span>
              </div>
            </div>
            <div className="flex items-center justify-between gap-4">
              <Label>{t('info.role')}</Label>
              <Badge variant="secondary" className="font-normal">
                {t(`roles.${workspace.role}`)}
              </Badge>
            </div>
          </SettingsCard>
        </SettingsSection>

        <SettingsSection
          title={t('teamCreation.title')}
          description={isOwner ? t('teamCreation.description') : t('teamCreation.ownerOnly')}
          action={
            <Select
              value={nextTeamCreation}
              disabled={!isOwner}
              onValueChange={(value) => setTeamCreation(value as TeamCreation)}
            >
              <SelectTrigger className="h-8 w-56">
                <SelectValue />
              </SelectTrigger>
              <SelectContent>
                {TEAM_CREATION.map((value) => (
                  <SelectItem key={value} value={value}>
                    {t(`teamCreation.${value}`)}
                  </SelectItem>
                ))}
              </SelectContent>
            </Select>
          }
        />

        <SettingsSection
          title={t('runLimit.title')}
          description={runLimitHint}
          action={
            isOwner ? (
              <Input
                type="number"
                min={0}
                max={maxRunLimit}
                step={1}
                inputMode="numeric"
                className="h-8 w-24"
                aria-label={t('runLimit.title')}
                aria-invalid={!runLimitValid}
                value={runLimit ?? String(workspace.maxConcurrentRuns)}
                onChange={(e) => setRunLimit(e.target.value)}
              />
            ) : (
              <span className="text-sm">
                {workspace.maxConcurrentRuns > 0 ? workspace.maxConcurrentRuns : t('runLimit.none')}
              </span>
            )
          }
        />

        <WorkspaceSharingSection workspace={workspace} />

        <WorkspaceSectionExtras slug="general" workspace={workspace} />

        {isOwner && <WorkspaceDeleteSection workspace={workspace} />}
      </div>
    </SectionPageView>
  );
}