Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(organization): confirm downgrades and bind group loads

Maxc55841d574e0
diff --git a/ui/src/features/organization/pages/OrganizationGroupsPages.tsx b/ui/src/features/organization/pages/OrganizationGroupsPages.tsx
index 2d5cb3678..242c14807 100644
--- a/ui/src/features/organization/pages/OrganizationGroupsPages.tsx
+++ b/ui/src/features/organization/pages/OrganizationGroupsPages.tsx
@@ -1,4 +1,11 @@
-import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
+import React, {
+ useCallback,
+ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+} from 'react';
import {
Archive,
Boxes,
@@ -47,6 +54,7 @@ import {
TableFrame,
} from '../components';
import { useOrganization } from '../context';
+import { isCurrentOrganizationLoad } from '../policy';
type GroupFilter = 'active' | 'archived' | 'all';
@@ -331,36 +339,69 @@ function MemberPickerDialog({
export function OrganizationGroupsPage() {
const { t } = useTranslation();
const { detail, selectedOrganizationId, request, dataVersion } = useOrganization();
- const [groups, setGroups] = useState<OrganizationGroup[] | null>(null);
- const [members, setMembers] = useState<OrganizationMember[]>([]);
+ const [directory, setDirectory] = useState<{
+ organizationId: string;
+ groups: OrganizationGroup[];
+ members: OrganizationMember[];
+ } | null>(null);
const [filter, setFilter] = useState<GroupFilter>('active');
const [search, setSearch] = useState('');
- const [error, setError] = useState<string>();
+ const [errorState, setErrorState] = useState<{ organizationId: string; code: string } | null>(null);
const [dialogOpen, setDialogOpen] = useState(false);
+ const loadGeneration = useRef(0);
+ const selectedOrganizationIdRef = useRef(selectedOrganizationId);
+ const currentDirectory = directory?.organizationId === selectedOrganizationId ? directory : null;
+ const groups = currentDirectory?.groups ?? null;
+ const members = currentDirectory?.members ?? [];
+ const error = errorState?.organizationId === selectedOrganizationId ? errorState.code : undefined;
const canManage = detail?.capabilities.can_manage_organization === true;
+ useLayoutEffect(() => {
+ selectedOrganizationIdRef.current = selectedOrganizationId;
+ loadGeneration.current += 1;
+ }, [selectedOrganizationId]);
+
const load = useCallback(async () => {
if (!selectedOrganizationId) return;
- setError(undefined);
+ const organizationId = selectedOrganizationId;
+ const generation = ++loadGeneration.current;
+ const isCurrent = () => isCurrentOrganizationLoad(
+ organizationId,
+ selectedOrganizationIdRef.current,
+ generation,
+ loadGeneration.current,
+ );
+ setErrorState(null);
try {
const [groupResult, memberResult] = await Promise.all([
request<{ groups: OrganizationGroup[] }>(
- `/api/cloud-management/organizations/${encodeURIComponent(selectedOrganizationId)}/groups`,
+ `/api/cloud-management/organizations/${encodeURIComponent(organizationId)}/groups`,
),
canManage
? request<{ members: OrganizationMember[] }>(
- `/api/cloud-management/organizations/${encodeURIComponent(selectedOrganizationId)}/members`,
+ `/api/cloud-management/organizations/${encodeURIComponent(organizationId)}/members`,
)
: Promise.resolve({ members: [] }),
]);
- setGroups(groupResult.groups);
- setMembers(memberResult.members);
+ if (!isCurrent()) return;
+ setDirectory({
+ organizationId,
+ groups: groupResult.groups,
+ members: memberResult.members,
+ });
} catch (caught) {
- setError(caught instanceof OrganizationApiError ? caught.code : 'generic');
+ if (!isCurrent()) return;
+ setErrorState({
+ organizationId,
+ code: caught instanceof OrganizationApiError ? caught.code : 'generic',
+ });
}
}, [canManage, request, selectedOrganizationId]);
- useEffect(() => { void load(); }, [dataVersion, load]);
+ useEffect(() => {
+ const timer = window.setTimeout(() => void load(), 0);
+ return () => window.clearTimeout(timer);
+ }, [dataVersion, load]);
const filtered = useMemo(() => (groups ?? []).filter((group) => (
(filter === 'all' || (filter === 'active' ? !group.archived_at : Boolean(group.archived_at)))
@@ -431,35 +472,88 @@ export function OrganizationGroupDetailPage() {
const { t } = useTranslation();
const { groupId = '' } = useParams();
const { selectedOrganizationId, request, dataVersion, invalidate } = useOrganization();
- const [group, setGroup] = useState<OrganizationGroup | null>(null);
- const [allMembers, setAllMembers] = useState<OrganizationMember[]>([]);
- const [error, setError] = useState<string>();
+ const [directory, setDirectory] = useState<{
+ organizationId: string;
+ groupId: string;
+ group: OrganizationGroup;
+ members: OrganizationMember[];
+ } | null>(null);
+ const [errorState, setErrorState] = useState<{
+ organizationId: string;
+ groupId: string;
+ code: string;
+ } | null>(null);
const [editOpen, setEditOpen] = useState(false);
const [membersOpen, setMembersOpen] = useState(false);
const [archiveOpen, setArchiveOpen] = useState(false);
+ const loadGeneration = useRef(0);
+ const selectionRef = useRef({ organizationId: selectedOrganizationId, groupId });
+ const currentDirectory = (
+ directory?.organizationId === selectedOrganizationId
+ && directory.groupId === groupId
+ ) ? directory : null;
+ const group = currentDirectory?.group ?? null;
+ const allMembers = currentDirectory?.members ?? [];
+ const error = (
+ errorState?.organizationId === selectedOrganizationId
+ && errorState.groupId === groupId
+ ) ? errorState.code : undefined;
+
+ useLayoutEffect(() => {
+ selectionRef.current = { organizationId: selectedOrganizationId, groupId };
+ loadGeneration.current += 1;
+ }, [groupId, selectedOrganizationId]);
const load = useCallback(async (): Promise<OrganizationGroup | null> => {
if (!selectedOrganizationId || !groupId) return null;
- setError(undefined);
+ const organizationId = selectedOrganizationId;
+ const requestedGroupId = groupId;
+ const generation = ++loadGeneration.current;
+ const isCurrent = () => (
+ isCurrentOrganizationLoad(
+ organizationId,
+ selectionRef.current.organizationId,
+ generation,
+ loadGeneration.current,
+ )
+ && requestedGroupId === selectionRef.current.groupId
+ );
+ setErrorState(null);
try {
const result = await request<{ group: OrganizationGroup }>(
- `/api/cloud-management/organizations/${encodeURIComponent(selectedOrganizationId)}/groups/${encodeURIComponent(groupId)}`,
+ `/api/cloud-management/organizations/${encodeURIComponent(organizationId)}/groups/${encodeURIComponent(requestedGroupId)}`,
);
- setGroup(result.group);
+ if (!isCurrent()) return null;
+ let members: OrganizationMember[] = [];
if (result.group.can_manage) {
- const members = await request<{ members: OrganizationMember[] }>(
- `/api/cloud-management/organizations/${encodeURIComponent(selectedOrganizationId)}/members`,
+ const memberResult = await request<{ members: OrganizationMember[] }>(
+ `/api/cloud-management/organizations/${encodeURIComponent(organizationId)}/members`,
);
- setAllMembers(members.members);
+ if (!isCurrent()) return null;
+ members = memberResult.members;
}
+ setDirectory({
+ organizationId,
+ groupId: requestedGroupId,
+ group: result.group,
+ members,
+ });
return result.group;
} catch (caught) {
- setError(caught instanceof OrganizationApiError ? caught.code : 'generic');
+ if (!isCurrent()) return null;
+ setErrorState({
+ organizationId,
+ groupId: requestedGroupId,
+ code: caught instanceof OrganizationApiError ? caught.code : 'generic',
+ });
return null;
}
}, [groupId, request, selectedOrganizationId]);
- useEffect(() => { void load(); }, [dataVersion, load]);
+ useEffect(() => {
+ const timer = window.setTimeout(() => void load(), 0);
+ return () => window.clearTimeout(timer);
+ }, [dataVersion, load]);
const setArchived = async (archived: boolean) => {
if (!selectedOrganizationId || !group) return;
@@ -477,7 +571,11 @@ export function OrganizationGroupDetailPage() {
setArchiveOpen(false);
} catch (caught) {
if (isRevisionConflict(caught)) await load();
- setError(caught instanceof OrganizationApiError ? caught.code : 'generic');
+ setErrorState({
+ organizationId: selectedOrganizationId,
+ groupId,
+ code: caught instanceof OrganizationApiError ? caught.code : 'generic',
+ });
}
};
diff --git a/ui/src/features/organization/pages/OrganizationMembersPage.tsx b/ui/src/features/organization/pages/OrganizationMembersPage.tsx
index 6f8dfec00..e3c3aa82a 100644
--- a/ui/src/features/organization/pages/OrganizationMembersPage.tsx
+++ b/ui/src/features/organization/pages/OrganizationMembersPage.tsx
@@ -56,7 +56,10 @@ import {
TableFrame,
} from '../components';
import { useOrganization } from '../context';
-import { isCurrentOrganizationLoad } from '../policy';
+import {
+ isCurrentOrganizationLoad,
+ requiresMemberRoleDowngradeConfirmation,
+} from '../policy';
type MemberFilter = 'all' | MemberStatus;
@@ -89,6 +92,7 @@ function MemberDialog({
const [conflict, setConflict] = useState(false);
const [authoritativeMember, setAuthoritativeMember] = useState<OrganizationMember | null>(null);
const [revision, setRevision] = useState(0);
+ const [downgradeConfirmOpen, setDowngradeConfirmOpen] = useState(false);
useEffect(() => {
if (!open) return;
@@ -99,6 +103,7 @@ function MemberDialog({
setError(undefined);
setConflict(false);
setAuthoritativeMember(null);
+ setDowngradeConfirmOpen(false);
}, [member, open]);
const save = async () => {
@@ -154,66 +159,94 @@ function MemberDialog({
setAuthoritativeMember(null);
};
+ const requestSave = () => {
+ if (member && requiresMemberRoleDowngradeConfirmation(member.role, role)) {
+ setDowngradeConfirmOpen(true);
+ return;
+ }
+ void save();
+ };
+
return (
- <Dialog open={open} onOpenChange={onOpenChange}>
- <DialogContent>
- <DialogHeader>
- <DialogTitle>{t(member ? 'organization.members.editTitle' : 'organization.members.inviteTitle')}</DialogTitle>
- <DialogDescription>{t(member ? 'organization.members.editBody' : 'organization.members.inviteBody')}</DialogDescription>
- </DialogHeader>
- {conflict ? <ConflictBanner onReload={reloadAuthoritativeMember} /> : null}
- {error ? <ErrorBanner code={error} /> : null}
- <div className="space-y-4">
- <div className="space-y-1.5">
- <Label htmlFor="organization-member-email">{t('organization.fields.email')}</Label>
- <Input
- id="organization-member-email"
- type="email"
- value={email}
- disabled={Boolean(member)}
- onChange={(event) => setEmail(event.target.value)}
- placeholder={t('organization.members.emailPlaceholder')}
- />
- </div>
- <div className="space-y-1.5">
- <Label htmlFor="organization-member-role">{t('organization.fields.role')}</Label>
- <Select id="organization-member-role" value={role} onChange={(event) => setRole(event.target.value as typeof role)}>
- <option value="member">{t('organization.roles.member')}</option>
- <option value="admin">{t('organization.roles.admin')}</option>
- </Select>
- </div>
- <fieldset>
- <legend className="mb-2 text-[12px] font-medium">{t('organization.fields.groups')}</legend>
- <div className="max-h-48 space-y-1 overflow-y-auto rounded-lg border border-border p-2">
- {groups.filter((group) => !group.archived_at).map((group) => (
- <button
- key={group.id}
- type="button"
- role="checkbox"
- aria-checked={groupIds.includes(group.id)}
- className="flex w-full items-center gap-3 rounded-md px-2 py-2 text-left hover:bg-foreground/[0.04]"
- onClick={() => setGroupIds((current) => current.includes(group.id)
- ? current.filter((id) => id !== group.id)
- : [...current, group.id])}
- >
- <Checkbox checked={groupIds.includes(group.id)} presentational />
- <GroupPill name={group.name} color={group.color} />
- </button>
- ))}
- {groups.filter((group) => !group.archived_at).length === 0 ? (
- <div className="px-2 py-4 text-center text-[12px] text-muted">{t('organization.groups.emptyTitle')}</div>
- ) : null}
+ <>
+ <Dialog
+ open={open}
+ onOpenChange={(nextOpen) => {
+ if (!nextOpen) setDowngradeConfirmOpen(false);
+ onOpenChange(nextOpen);
+ }}
+ >
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>{t(member ? 'organization.members.editTitle' : 'organization.members.inviteTitle')}</DialogTitle>
+ <DialogDescription>{t(member ? 'organization.members.editBody' : 'organization.members.inviteBody')}</DialogDescription>
+ </DialogHeader>
+ {conflict ? <ConflictBanner onReload={reloadAuthoritativeMember} /> : null}
+ {error ? <ErrorBanner code={error} /> : null}
+ <div className="space-y-4">
+ <div className="space-y-1.5">
+ <Label htmlFor="organization-member-email">{t('organization.fields.email')}</Label>
+ <Input
+ id="organization-member-email"
+ type="email"
+ value={email}
+ disabled={Boolean(member)}
+ onChange={(event) => setEmail(event.target.value)}
+ placeholder={t('organization.members.emailPlaceholder')}
+ />
</div>
- </fieldset>
- </div>
- <DialogFooter>
- <Button variant="ghost" onClick={() => onOpenChange(false)}>{t('common.cancel')}</Button>
- <Button variant="brand" disabled={saving || conflict || !email.trim()} onClick={() => void save()}>
- {saving ? t('organization.actions.saving') : t(member ? 'organization.actions.saveChanges' : 'organization.actions.sendInvite')}
- </Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
+ <div className="space-y-1.5">
+ <Label htmlFor="organization-member-role">{t('organization.fields.role')}</Label>
+ <Select id="organization-member-role" value={role} onChange={(event) => setRole(event.target.value as typeof role)}>
+ <option value="member">{t('organization.roles.member')}</option>
+ <option value="admin">{t('organization.roles.admin')}</option>
+ </Select>
+ </div>
+ <fieldset>
+ <legend className="mb-2 text-[12px] font-medium">{t('organization.fields.groups')}</legend>
+ <div className="max-h-48 space-y-1 overflow-y-auto rounded-lg border border-border p-2">
+ {groups.filter((group) => !group.archived_at).map((group) => (
+ <button
+ key={group.id}
+ type="button"
+ role="checkbox"
+ aria-checked={groupIds.includes(group.id)}
+ className="flex w-full items-center gap-3 rounded-md px-2 py-2 text-left hover:bg-foreground/[0.04]"
+ onClick={() => setGroupIds((current) => current.includes(group.id)
+ ? current.filter((id) => id !== group.id)
+ : [...current, group.id])}
+ >
+ <Checkbox checked={groupIds.includes(group.id)} presentational />
+ <GroupPill name={group.name} color={group.color} />
+ </button>
+ ))}
+ {groups.filter((group) => !group.archived_at).length === 0 ? (
+ <div className="px-2 py-4 text-center text-[12px] text-muted">{t('organization.groups.emptyTitle')}</div>
+ ) : null}
+ </div>
+ </fieldset>
+ </div>
+ <DialogFooter>
+ <Button variant="ghost" onClick={() => onOpenChange(false)}>{t('common.cancel')}</Button>
+ <Button variant="brand" disabled={saving || conflict || !email.trim()} onClick={requestSave}>
+ {saving ? t('organization.actions.saving') : t(member ? 'organization.actions.saveChanges' : 'organization.actions.sendInvite')}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ <ConfirmDialog
+ open={downgradeConfirmOpen}
+ onOpenChange={setDowngradeConfirmOpen}
+ title={t('organization.members.downgradeTitle')}
+ description={t('organization.members.downgradeBody', { email: member?.email })}
+ confirmLabel={t('organization.members.downgradeConfirm')}
+ destructive
+ onConfirm={async () => {
+ await save();
+ setDowngradeConfirmOpen(false);
+ }}
+ />
+ </>
);
}
diff --git a/ui/src/features/organization/policy.test.ts b/ui/src/features/organization/policy.test.ts
index 54bb2a689..0f2ff2319 100644
--- a/ui/src/features/organization/policy.test.ts
+++ b/ui/src/features/organization/policy.test.ts
@@ -5,6 +5,7 @@ import {
hasDuplicateProjectPrincipals,
isCurrentOrganizationLoad,
normalizeOrganizationPrincipal,
+ requiresMemberRoleDowngradeConfirmation,
} from './policy';
describe('Organization policy helpers', () => {
@@ -38,4 +39,10 @@ describe('Organization policy helpers', () => {
expect(isCurrentOrganizationLoad('org-a', 'org-b', 4, 4)).toBe(false);
expect(isCurrentOrganizationLoad('org-b', 'org-b', 3, 4)).toBe(false);
});
+
+ it('requires confirmation before the member editor removes admin access', () => {
+ expect(requiresMemberRoleDowngradeConfirmation('admin', 'member')).toBe(true);
+ expect(requiresMemberRoleDowngradeConfirmation('member', 'admin')).toBe(false);
+ expect(requiresMemberRoleDowngradeConfirmation('member', 'member')).toBe(false);
+ });
});
diff --git a/ui/src/features/organization/policy.ts b/ui/src/features/organization/policy.ts
index b24970eef..6a4e8ffe0 100644
--- a/ui/src/features/organization/policy.ts
+++ b/ui/src/features/organization/policy.ts
@@ -1,4 +1,4 @@
-import type { ProjectBinding, SyncCounts } from './api/types';
+import type { OrganizationRole, ProjectBinding, SyncCounts } from './api/types';
export type OrganizationPrincipalKind = ProjectBinding['principal_kind'];
export type AggregateSyncStatus = 'none' | 'in_sync' | 'applying' | 'offline' | 'error';
@@ -37,3 +37,10 @@ export function isCurrentOrganizationLoad(
&& requestGeneration === currentGeneration
);
}
+
+export function requiresMemberRoleDowngradeConfirmation(
+ currentRole: OrganizationRole,
+ nextRole: OrganizationRole,
+): boolean {
+ return currentRole === 'admin' && nextRole === 'member';
+}
diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json
index 9b29296ad..e2075c6d7 100644
--- a/ui/src/i18n/en.json
+++ b/ui/src/i18n/en.json
@@ -3517,6 +3517,9 @@
"editTitle": "Edit member",
"editBody": "Changes to role and complete group membership share one revision.",
"emailPlaceholder": "[email protected]",
+ "downgradeTitle": "Downgrade this admin?",
+ "downgradeBody": "{{email}} will lose Organization management access. Their membership and group assignments will remain unchanged.",
+ "downgradeConfirm": "Downgrade to member",
"removeTitle": "Remove this member?",
"removeBody": "{{email}} will lose Organization membership and any access derived from it."
},
diff --git a/ui/src/i18n/zh.json b/ui/src/i18n/zh.json
index 8940a1648..4369252b4 100644
--- a/ui/src/i18n/zh.json
+++ b/ui/src/i18n/zh.json
@@ -3517,6 +3517,9 @@
"editTitle": "编辑成员",
"editBody": "角色和完整群组成员关系共用同一 revision。",
"emailPlaceholder": "[email protected]",
+ "downgradeTitle": "将此管理员降级?",
+ "downgradeBody": "{{email}} 将失去组织管理权限,其成员身份和群组分配保持不变。",
+ "downgradeConfirm": "降级为成员",
"removeTitle": "移除此成员?",
"removeBody": "{{email}} 将失去组织成员身份以及由此获得的全部权限。"
},