Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(teams): show and allow team slug to be edited (#3519)

Will Hutchinsonc477c48157b5
diff --git a/js/app/packages/app/component/settings/Team.tsx b/js/app/packages/app/component/settings/Team.tsx
index 22148d5db3..3e610d49a1 100644
--- a/js/app/packages/app/component/settings/Team.tsx
+++ b/js/app/packages/app/component/settings/Team.tsx
@@ -54,6 +54,8 @@ import { useHasPaidAccess } from '@core/auth/license';
import { usePaywallState } from '@core/constant/PaywallState';
import { z } from 'zod';
+import { getTeamSlugError, normalizeTeamSlugInput } from './teamSlug';
+
const roleOrder: Record<string, number> = {
[TeamRole.owner]: 0,
[TeamRole.admin]: 1,
@@ -733,6 +735,7 @@ function EmptyTeamState() {
function TeamManagement(props: {
teamId: string;
teamName: string;
+ teamSlug: string;
ownerId: string;
}) {
const userId = useUserId();
@@ -761,6 +764,12 @@ function TeamManagement(props: {
const [editingTeamName, setEditingTeamName] = createSignal<
string | undefined
>(undefined);
+ const [editingTeamSlug, setEditingTeamSlug] = createSignal<
+ string | undefined
+ >(undefined);
+ const [teamSlugError, setTeamSlugError] = createSignal<string | undefined>(
+ undefined
+ );
const [memberListWrapperRef, setMemberListWrapperRef] =
createSignal<HTMLDivElement>();
@@ -797,6 +806,36 @@ function TeamManagement(props: {
return editing !== undefined && editing.trim() !== props.teamName;
};
+ const teamSlugValue = () => editingTeamSlug() ?? props.teamSlug;
+ const hasTeamSlugInputChanged = () => {
+ const editing = editingTeamSlug();
+ return editing !== undefined && editing !== props.teamSlug;
+ };
+ const hasTeamSlugChanged = () => {
+ const editing = editingTeamSlug();
+ return (
+ editing !== undefined &&
+ normalizeTeamSlugInput(editing) !== props.teamSlug
+ );
+ };
+ const normalizedTeamSlugPreview = () => {
+ const editing = editingTeamSlug();
+ if (editing === undefined || !hasTeamSlugChanged()) return undefined;
+ if (getTeamSlugError(editing)) return undefined;
+
+ const normalized = normalizeTeamSlugInput(editing);
+ return normalized === editing ? undefined : normalized;
+ };
+ const canSaveTeamSlug = () => {
+ const editing = editingTeamSlug();
+ return (
+ editing !== undefined &&
+ hasTeamSlugChanged() &&
+ !patchTeamMutation.isPending &&
+ getTeamSlugError(editing) === undefined
+ );
+ };
+
const members = createMemo(() => {
const unsorted = teamQuery.data?.members ?? [];
return [...unsorted].sort((a, b) => {
@@ -826,6 +865,38 @@ function TeamManagement(props: {
setEditingTeamName(undefined);
};
+ const validateTeamSlug = (slug: string) => {
+ const error = getTeamSlugError(slug);
+ setTeamSlugError(error);
+ return error === undefined;
+ };
+
+ const handleTeamSlugChange = (slug: string) => {
+ setEditingTeamSlug(slug);
+ validateTeamSlug(slug);
+ };
+
+ const handleSaveTeamSlug = () => {
+ const editedSlug = editingTeamSlug();
+ if (!props.teamId || editedSlug === undefined) return;
+ if (!validateTeamSlug(editedSlug) || !hasTeamSlugChanged()) return;
+
+ patchTeamMutation.mutate(
+ { teamId: props.teamId, request: { slug: editedSlug } },
+ {
+ onSuccess: () => {
+ setEditingTeamSlug(undefined);
+ setTeamSlugError(undefined);
+ },
+ }
+ );
+ };
+
+ const handleCancelTeamSlugEdit = () => {
+ setEditingTeamSlug(undefined);
+ setTeamSlugError(undefined);
+ };
+
const handleDeleteTeam = () => {
if (!props.teamId) return;
@@ -929,7 +1000,7 @@ function TeamManagement(props: {
<Panel.Body>
<div class="flex h-full flex-col">
- <div class="flex items-center px-2 h-15.25 border-b border-edge-muted shrink-0">
+ <div class="flex flex-col gap-2 px-2 py-2 border-b border-edge-muted shrink-0">
<div class="flex items-center justify-between w-full border border-edge rounded-sm px-4 py-2">
<span class="text-sm text-ink-muted">Name</span>
<Show
@@ -981,6 +1052,76 @@ function TeamManagement(props: {
</div>
</Show>
</div>
+ <div class="flex items-center justify-between w-full border border-edge rounded-sm px-4 py-2 gap-3">
+ <span class="text-sm text-ink-muted">Slug</span>
+ <Show
+ when={isOwner()}
+ fallback={
+ <span class="text-sm text-ink">{props.teamSlug}</span>
+ }
+ >
+ <div class="flex items-center gap-2 min-w-0">
+ <div class="flex flex-col items-end gap-1 min-w-0">
+ <input
+ type="text"
+ value={teamSlugValue()}
+ onInput={(e) =>
+ handleTeamSlugChange(e.currentTarget.value)
+ }
+ onBlur={() => {
+ const editing = editingTeamSlug();
+ if (editing !== undefined) {
+ validateTeamSlug(editing);
+ }
+ }}
+ placeholder="Enter team slug"
+ class="text-sm bg-surface border-none outline-none text-ink text-right w-48"
+ />
+ <Show when={teamSlugError()}>
+ <p class="text-xs text-failure-ink text-right">
+ {teamSlugError()}
+ </p>
+ </Show>
+ <Show when={normalizedTeamSlugPreview()}>
+ <p class="text-xs text-ink-muted text-right">
+ Will save as {normalizedTeamSlugPreview()}
+ </p>
+ </Show>
+ </div>
+ <Show when={hasTeamSlugInputChanged()}>
+ <div class="flex items-center gap-1 shrink-0">
+ <Tooltip label="Save">
+ <Button
+ variant="active"
+ size="icon-sm"
+ class="rounded-xs"
+ disabled={!canSaveTeamSlug()}
+ onClick={handleSaveTeamSlug}
+ >
+ <Show
+ when={patchTeamMutation.isPending}
+ fallback={<CheckIcon class="size-4" />}
+ >
+ <SpinnerIcon class="size-4 animate-spin" />
+ </Show>
+ </Button>
+ </Tooltip>
+ <Tooltip label="Cancel">
+ <Button
+ variant="ghost"
+ size="icon-sm"
+ class="rounded-xs"
+ disabled={patchTeamMutation.isPending}
+ onClick={handleCancelTeamSlugEdit}
+ >
+ <XIcon class="size-4" />
+ </Button>
+ </Tooltip>
+ </div>
+ </Show>
+ </div>
+ </Show>
+ </div>
</div>
<div class="relative min-h-0 flex-1">
@@ -1294,6 +1435,7 @@ function TeamContent() {
<TeamManagement
teamId={t.id}
teamName={t.name}
+ teamSlug={t.slug}
ownerId={t.owner_id}
/>
)}
diff --git a/js/app/packages/app/component/settings/teamSlug.test.ts b/js/app/packages/app/component/settings/teamSlug.test.ts
new file mode 100644
index 0000000000..1e576bf32d
--- /dev/null
+++ b/js/app/packages/app/component/settings/teamSlug.test.ts
@@ -0,0 +1,79 @@
+import { describe, expect, it } from 'vitest';
+import {
+ TEAM_SLUG_ALLOWED_INPUT_REGEX,
+ TEAM_SLUG_MAX_LENGTH,
+ TEAM_SLUG_NORMALIZED_REGEX,
+ getTeamSlugError,
+ normalizeTeamSlugInput,
+} from './teamSlug.ts';
+
+describe('team slug validation', function () {
+ it('uses an explicit allowed-input regex for backend-accepted characters', function () {
+ expect(TEAM_SLUG_ALLOWED_INPUT_REGEX.test('abc XYZ_- \t\n\f\r')).toBe(
+ true
+ );
+ expect(TEAM_SLUG_ALLOWED_INPUT_REGEX.test('abc123')).toBe(false);
+ expect(TEAM_SLUG_ALLOWED_INPUT_REGEX.test('bad.slug')).toBe(false);
+ expect(TEAM_SLUG_ALLOWED_INPUT_REGEX.test('café')).toBe(false);
+ expect(TEAM_SLUG_ALLOWED_INPUT_REGEX.test('bad!slug')).toBe(false);
+ });
+
+ it('uses the normalized backend storage format regex', function () {
+ expect(TEAM_SLUG_NORMALIZED_REGEX.test('MY_TEAM_SLUG')).toBe(true);
+ expect(TEAM_SLUG_NORMALIZED_REGEX.test('MY__TEAM')).toBe(false);
+ expect(TEAM_SLUG_NORMALIZED_REGEX.test('_MY_TEAM')).toBe(false);
+ expect(TEAM_SLUG_NORMALIZED_REGEX.test('MY_TEAM_')).toBe(false);
+ expect(TEAM_SLUG_NORMALIZED_REGEX.test('my_team')).toBe(false);
+ });
+
+ it('normalizes valid input to uppercase underscore-separated words', function () {
+ expect(normalizeTeamSlugInput('my-team slug')).toBe('MY_TEAM_SLUG');
+ expect(getTeamSlugError('my-team slug')).toBeUndefined();
+ });
+
+ it('collapses leading, trailing, and repeated separators like the backend', function () {
+ expect(normalizeTeamSlugInput(' --my__team\t\nslug__ ')).toBe(
+ 'MY_TEAM_SLUG'
+ );
+ expect(getTeamSlugError(' --my__team\t\nslug__ ')).toBeUndefined();
+ });
+
+ it('rejects empty and separator-only input', function () {
+ expect(getTeamSlugError('')).toBe('team slug cannot be empty');
+ expect(getTeamSlugError(' -__\t\n\f\r')).toBe(
+ 'team slug cannot be empty'
+ );
+ });
+
+ it('rejects digits', function () {
+ expect(getTeamSlugError('TEAM1')).toContain('may only contain');
+ });
+
+ it('rejects periods', function () {
+ expect(getTeamSlugError('bad.slug')).toContain('may only contain');
+ });
+
+ it('rejects non-ASCII letters', function () {
+ expect(getTeamSlugError('café')).toContain('may only contain');
+ });
+
+ it('rejects punctuation', function () {
+ expect(getTeamSlugError('bad!slug')).toContain('may only contain');
+ });
+
+ it('enforces the max length after normalization', function () {
+ const rawLongSlug = 'AAAAA-----BBBBB-----CCCCC';
+ const normalizedRawLongSlug = normalizeTeamSlugInput(rawLongSlug);
+
+ expect(rawLongSlug.length).toBeGreaterThan(TEAM_SLUG_MAX_LENGTH);
+ expect(normalizedRawLongSlug).toBe('AAAAA_BBBBB_CCCCC');
+ expect(normalizedRawLongSlug.length).toBeLessThanOrEqual(
+ TEAM_SLUG_MAX_LENGTH
+ );
+ expect(getTeamSlugError(rawLongSlug)).toBeUndefined();
+
+ expect(getTeamSlugError('AAAAA_BBBBB_CCCCC_DDD')).toBe(
+ `team slug cannot be longer than ${TEAM_SLUG_MAX_LENGTH} characters`
+ );
+ });
+});
diff --git a/js/app/packages/app/component/settings/teamSlug.ts b/js/app/packages/app/component/settings/teamSlug.ts
new file mode 100644
index 0000000000..81af131ea8
--- /dev/null
+++ b/js/app/packages/app/component/settings/teamSlug.ts
@@ -0,0 +1,70 @@
+export const TEAM_SLUG_MAX_LENGTH = 20;
+
+export const TEAM_SLUG_ALLOWED_INPUT_REGEX: RegExp =
+ /^[A-Za-z_\- \t\n\f\r]*$/;
+
+export const TEAM_SLUG_NORMALIZED_REGEX: RegExp = /^[A-Z]+(_[A-Z]+)*$/;
+
+const ASCII_LETTER_REGEX: RegExp = /^[A-Za-z]$/;
+
+function isAsciiLetter(character: string): boolean {
+ return ASCII_LETTER_REGEX.test(character);
+}
+
+function isTeamSlugSeparator(character: string): boolean {
+ return (
+ character === '_' ||
+ character === '-' ||
+ character === ' ' ||
+ character === '\t' ||
+ character === '\n' ||
+ character === '\f' ||
+ character === '\r'
+ );
+}
+
+export function normalizeTeamSlugInput(input: string): string {
+ let normalized = '';
+ let lastWasSeparator = false;
+
+ for (const character of input) {
+ if (isTeamSlugSeparator(character)) {
+ if (normalized !== '' && !lastWasSeparator) {
+ normalized += '_';
+ }
+ lastWasSeparator = true;
+ continue;
+ }
+
+ if (isAsciiLetter(character)) {
+ normalized += character.toUpperCase();
+ } else {
+ normalized += character;
+ }
+ lastWasSeparator = false;
+ }
+
+ while (normalized.endsWith('_')) {
+ normalized = normalized.slice(0, -1);
+ }
+
+ return normalized;
+}
+
+export function getTeamSlugError(input: string): string | undefined {
+ if (!TEAM_SLUG_ALLOWED_INPUT_REGEX.test(input)) {
+ return 'team slug may only contain ASCII letters, spaces, hyphens, and underscores';
+ }
+
+ const normalized = normalizeTeamSlugInput(input);
+
+ if (normalized === '' || !TEAM_SLUG_NORMALIZED_REGEX.test(normalized)) {
+ return 'team slug cannot be empty';
+ }
+
+ if (normalized.length > TEAM_SLUG_MAX_LENGTH) {
+ return `team slug cannot be longer than ${TEAM_SLUG_MAX_LENGTH} characters`;
+ }
+
+ return undefined;
+}