Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import React, { useCallback, useMemo, useState } from 'react';
|
||||
import { Box, Button, IconButton, Scroll, Spinner, Text, color } from 'folds';
|
||||
import { Icon, Icons } from '../../../components/icons';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { Icon, Icons } from '../../../components/icons';
|
||||
import { Page, PageContent, PageHeader } from '../../../components/page';
|
||||
import { useRoom } from '../../../hooks/useRoom';
|
||||
import { usePowerLevels } from '../../../hooks/usePowerLevels';
|
||||
@@ -14,7 +14,11 @@ import { useRoomPermissions } from '../../../hooks/useRoomPermissions';
|
||||
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
|
||||
import { allRoomsAtom } from '../../../state/room-list/roomList';
|
||||
import { roomToParentsAtom } from '../../../state/room/roomToParents';
|
||||
import { useRecursiveChildScopeFactory, useRecursiveChildSpaceScopeFactory, useSpaceChildren } from '../../../state/hooks/roomList';
|
||||
import {
|
||||
useRecursiveChildScopeFactory,
|
||||
useRecursiveChildSpaceScopeFactory,
|
||||
useSpaceChildren,
|
||||
} from '../../../state/hooks/roomList';
|
||||
|
||||
type PermissionsProps = {
|
||||
requestClose: () => void;
|
||||
@@ -38,14 +42,14 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
const childSpaces = useSpaceChildren(
|
||||
allRoomsAtom,
|
||||
room.roomId,
|
||||
useRecursiveChildSpaceScopeFactory(mx, roomToParents)
|
||||
useRecursiveChildSpaceScopeFactory(mx, roomToParents),
|
||||
);
|
||||
|
||||
// Get all child rooms recursively
|
||||
const childRooms = useSpaceChildren(
|
||||
allRoomsAtom,
|
||||
room.roomId,
|
||||
useRecursiveChildScopeFactory(mx, roomToParents)
|
||||
useRecursiveChildScopeFactory(mx, roomToParents),
|
||||
);
|
||||
|
||||
// Combine both spaces and rooms for syncing
|
||||
@@ -64,18 +68,26 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
|
||||
// Check if we have permission to edit power levels in the child
|
||||
const childPowerLevels = childRoom.currentState.getStateEvents(StateEvent.RoomPowerLevels, '');
|
||||
const childPowerLevels = childRoom.currentState.getStateEvents(
|
||||
StateEvent.RoomPowerLevels,
|
||||
'',
|
||||
);
|
||||
const childPowerLevelsContent = childPowerLevels?.getContent() || {};
|
||||
const childCreators = new Set<string>();
|
||||
const creatorId = childRoom.currentState.getStateEvents('m.room.create', '')?.getContent()?.creator;
|
||||
const creatorId = childRoom.currentState
|
||||
.getStateEvents('m.room.create', '')
|
||||
?.getContent()?.creator;
|
||||
if (creatorId) childCreators.add(creatorId);
|
||||
|
||||
const myUserId = mx.getSafeUserId();
|
||||
const myPower = childPowerLevelsContent.users?.[myUserId] ?? childPowerLevelsContent.users_default ?? 0;
|
||||
|
||||
const myPower =
|
||||
childPowerLevelsContent.users?.[myUserId] ?? childPowerLevelsContent.users_default ?? 0;
|
||||
|
||||
// Check the specific power level required for m.room.power_levels events
|
||||
const requiredPower = childPowerLevelsContent.events?.[StateEvent.RoomPowerLevels] ??
|
||||
childPowerLevelsContent.state_default ?? 50;
|
||||
const requiredPower =
|
||||
childPowerLevelsContent.events?.[StateEvent.RoomPowerLevels] ??
|
||||
childPowerLevelsContent.state_default ??
|
||||
50;
|
||||
|
||||
if (myPower >= requiredPower || childCreators.has(myUserId)) {
|
||||
syncable.push(childId);
|
||||
@@ -90,7 +102,10 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
const [syncState, syncPermissions] = useAsyncCallback(
|
||||
useCallback(async () => {
|
||||
// Get the power level tags from this space
|
||||
const powerLevelTagsEvent = room.currentState.getStateEvents('in.cinny.room.power_level_tags', '');
|
||||
const powerLevelTagsEvent = room.currentState.getStateEvents(
|
||||
'in.cinny.room.power_level_tags',
|
||||
'',
|
||||
);
|
||||
const powerLevelTags = powerLevelTagsEvent?.getContent();
|
||||
|
||||
let completed = 0;
|
||||
@@ -103,15 +118,19 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (!childRoom) continue;
|
||||
|
||||
// Get current power levels in the child room
|
||||
const childPowerLevelsEvent = childRoom.currentState.getStateEvents(StateEvent.RoomPowerLevels, '');
|
||||
const childPowerLevelsEvent = childRoom.currentState.getStateEvents(
|
||||
StateEvent.RoomPowerLevels,
|
||||
'',
|
||||
);
|
||||
const childPowerLevels = childPowerLevelsEvent?.getContent() || {};
|
||||
|
||||
|
||||
// Get my power level in the child room
|
||||
const myPowerInChild = childPowerLevels.users?.[myUserId] ?? childPowerLevels.users_default ?? 0;
|
||||
|
||||
const myPowerInChild =
|
||||
childPowerLevels.users?.[myUserId] ?? childPowerLevels.users_default ?? 0;
|
||||
|
||||
// Build merged users: only sync users with power <= mine, preserve higher-powered users
|
||||
const mergedUsers: Record<string, number> = {};
|
||||
|
||||
|
||||
// Copy users from parent where power <= my power in child
|
||||
if (powerLevels.users) {
|
||||
Object.entries(powerLevels.users).forEach(([userId, power]) => {
|
||||
@@ -120,7 +139,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Preserve all users in child room who have power >= mine
|
||||
if (childPowerLevels.users) {
|
||||
Object.entries(childPowerLevels.users).forEach(([userId, power]) => {
|
||||
@@ -129,7 +148,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Cap all power level values at my power level
|
||||
const capPowerLevel = (value: any) => {
|
||||
if (typeof value === 'number') {
|
||||
@@ -137,7 +156,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
// Create the merged power levels with capped values
|
||||
const mergedPowerLevels: any = {
|
||||
...powerLevels,
|
||||
@@ -150,7 +169,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
state_default: capPowerLevel(powerLevels.state_default),
|
||||
users_default: capPowerLevel(powerLevels.users_default),
|
||||
};
|
||||
|
||||
|
||||
// Cap event-specific power levels
|
||||
if (powerLevels.events) {
|
||||
mergedPowerLevels.events = {};
|
||||
@@ -158,7 +177,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
mergedPowerLevels.events[eventType] = capPowerLevel(power);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Cap notification power levels
|
||||
if (powerLevels.notifications) {
|
||||
mergedPowerLevels.notifications = {};
|
||||
@@ -166,19 +185,23 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
mergedPowerLevels.notifications[key] = capPowerLevel(power);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
await mx.sendStateEvent(childId, StateEvent.RoomPowerLevels as any, mergedPowerLevels);
|
||||
|
||||
|
||||
// Also copy power level tags if they exist
|
||||
if (powerLevelTags) {
|
||||
await mx.sendStateEvent(childId, 'in.cinny.room.power_level_tags' as any, powerLevelTags);
|
||||
await mx.sendStateEvent(
|
||||
childId,
|
||||
'in.cinny.room.power_level_tags' as any,
|
||||
powerLevelTags,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
completed += 1;
|
||||
|
||||
|
||||
// Add a small delay to avoid rate limiting
|
||||
if (completed < total) {
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to sync permissions to ${childId}:`, error);
|
||||
@@ -186,7 +209,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
|
||||
return { completed, total };
|
||||
}, [mx, syncableChildren, powerLevels, room])
|
||||
}, [mx, syncableChildren, powerLevels, room]),
|
||||
);
|
||||
|
||||
const [syncPowerLevelsState, syncPowerLevelsOnly] = useAsyncCallback(
|
||||
@@ -201,15 +224,19 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (!childRoom) continue;
|
||||
|
||||
// Get current power levels in the child room
|
||||
const childPowerLevelsEvent = childRoom.currentState.getStateEvents(StateEvent.RoomPowerLevels, '');
|
||||
const childPowerLevelsEvent = childRoom.currentState.getStateEvents(
|
||||
StateEvent.RoomPowerLevels,
|
||||
'',
|
||||
);
|
||||
const childPowerLevels = childPowerLevelsEvent?.getContent() || {};
|
||||
|
||||
|
||||
// Get my power level in the child room
|
||||
const myPowerInChild = childPowerLevels.users?.[myUserId] ?? childPowerLevels.users_default ?? 0;
|
||||
|
||||
const myPowerInChild =
|
||||
childPowerLevels.users?.[myUserId] ?? childPowerLevels.users_default ?? 0;
|
||||
|
||||
// Build merged users: only sync users with power <= mine, preserve higher-powered users
|
||||
const mergedUsers: Record<string, number> = {};
|
||||
|
||||
|
||||
// Copy users from parent where power <= my power in child
|
||||
if (powerLevels.users) {
|
||||
Object.entries(powerLevels.users).forEach(([userId, power]) => {
|
||||
@@ -218,7 +245,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Preserve all users in child room who have power >= mine
|
||||
if (childPowerLevels.users) {
|
||||
Object.entries(childPowerLevels.users).forEach(([userId, power]) => {
|
||||
@@ -227,7 +254,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Cap all power level values at my power level
|
||||
const capPowerLevel = (value: any) => {
|
||||
if (typeof value === 'number') {
|
||||
@@ -235,7 +262,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
|
||||
// Create the merged power levels with capped values
|
||||
const mergedPowerLevels: any = {
|
||||
...powerLevels,
|
||||
@@ -248,7 +275,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
state_default: capPowerLevel(powerLevels.state_default),
|
||||
users_default: capPowerLevel(powerLevels.users_default),
|
||||
};
|
||||
|
||||
|
||||
// Cap event-specific power levels
|
||||
if (powerLevels.events) {
|
||||
mergedPowerLevels.events = {};
|
||||
@@ -256,7 +283,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
mergedPowerLevels.events[eventType] = capPowerLevel(power);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Cap notification power levels
|
||||
if (powerLevels.notifications) {
|
||||
mergedPowerLevels.notifications = {};
|
||||
@@ -264,14 +291,14 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
mergedPowerLevels.notifications[key] = capPowerLevel(power);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
await mx.sendStateEvent(childId, StateEvent.RoomPowerLevels as any, mergedPowerLevels);
|
||||
|
||||
|
||||
completed += 1;
|
||||
|
||||
|
||||
// Add a small delay to avoid rate limiting
|
||||
if (completed < total) {
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Failed to sync power levels to ${childId}:`, error);
|
||||
@@ -279,7 +306,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
|
||||
return { completed, total };
|
||||
}, [mx, syncableChildren, powerLevels])
|
||||
}, [mx, syncableChildren, powerLevels]),
|
||||
);
|
||||
|
||||
const handleEditPowers = () => {
|
||||
@@ -333,18 +360,28 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
<Text size="T300">
|
||||
Sync these permissions and power level tags to all child subspaces and rooms.
|
||||
{nonSyncableChildren.length > 0 ? (
|
||||
<> This will sync to {syncableChildren.length} child{' '}
|
||||
{syncableChildren.length === 1 ? 'space/room' : 'spaces/rooms'} where you have permission.
|
||||
<>
|
||||
{' '}
|
||||
This will sync to {syncableChildren.length} child{' '}
|
||||
{syncableChildren.length === 1 ? 'space/room' : 'spaces/rooms'} where you
|
||||
have permission.
|
||||
<Text as="span" style={{ color: color.Warning.Main }}>
|
||||
{' '}You don't have permission to edit {nonSyncableChildren.length} other{' '}
|
||||
{' '}
|
||||
You don't have permission to edit {
|
||||
nonSyncableChildren.length
|
||||
} other{' '}
|
||||
{nonSyncableChildren.length === 1 ? 'space/room' : 'spaces/rooms'}
|
||||
{nonSyncableChildren.length <= 5 && (
|
||||
<>: {nonSyncableChildren.map(c => c.name).join(', ')}</>
|
||||
)}.
|
||||
<>: {nonSyncableChildren.map((c) => c.name).join(', ')}</>
|
||||
)}
|
||||
.
|
||||
</Text>
|
||||
</>
|
||||
) : (
|
||||
<> This will overwrite the power levels and custom role tags in {syncableChildren.length} child{' '}
|
||||
<>
|
||||
{' '}
|
||||
This will overwrite the power levels and custom role tags in{' '}
|
||||
{syncableChildren.length} child{' '}
|
||||
{syncableChildren.length === 1 ? 'space/room' : 'spaces/rooms'}.
|
||||
</>
|
||||
)}
|
||||
@@ -406,7 +443,8 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
</Button>
|
||||
{powerLevelsSyncSuccess && (
|
||||
<Text size="T300" style={{ color: color.Success.Main }}>
|
||||
Successfully synced power levels to {syncPowerLevelsState.data.completed} of{' '}
|
||||
Successfully synced power levels to{' '}
|
||||
{syncPowerLevelsState.data.completed} of{' '}
|
||||
{syncPowerLevelsState.data.total} children
|
||||
</Text>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user