Type the window and power-level casts. [skip build]
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import React, { useCallback, useMemo, useState } from 'react';
|
||||
import { Box, Button, IconButton, Scroll, Spinner, Text, color } from 'folds';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import { EventType } from 'matrix-js-sdk';
|
||||
import { Icon, Icons } from '../../../components/icons';
|
||||
import { Page, PageContent, PageHeader } from '../../../components/page';
|
||||
import { useRoom } from '../../../hooks/useRoom';
|
||||
import { usePowerLevels } from '../../../hooks/usePowerLevels';
|
||||
import { IPowerLevels, usePowerLevels } from '../../../hooks/usePowerLevels';
|
||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||
import { StateEvent } from '../../../../types/matrix/room';
|
||||
import { usePermissionGroups } from './usePermissionItems';
|
||||
@@ -148,7 +149,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
|
||||
// Cap all power level values at my power level
|
||||
const capPowerLevel = (value: any) => {
|
||||
const capPowerLevel = (value: number | undefined): number | undefined => {
|
||||
if (typeof value === 'number') {
|
||||
return Math.min(value, myPowerInChild);
|
||||
}
|
||||
@@ -156,7 +157,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
};
|
||||
|
||||
// Create the merged power levels with capped values
|
||||
const mergedPowerLevels: any = {
|
||||
const mergedPowerLevels: IPowerLevels = {
|
||||
...powerLevels,
|
||||
users: mergedUsers,
|
||||
ban: capPowerLevel(powerLevels.ban),
|
||||
@@ -172,7 +173,8 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (powerLevels.events) {
|
||||
mergedPowerLevels.events = {};
|
||||
Object.entries(powerLevels.events).forEach(([eventType, power]) => {
|
||||
mergedPowerLevels.events[eventType] = capPowerLevel(power);
|
||||
const capped = capPowerLevel(power);
|
||||
if (capped !== undefined) mergedPowerLevels.events![eventType] = capped;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -180,11 +182,12 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (powerLevels.notifications) {
|
||||
mergedPowerLevels.notifications = {};
|
||||
Object.entries(powerLevels.notifications).forEach(([key, power]) => {
|
||||
mergedPowerLevels.notifications[key] = capPowerLevel(power);
|
||||
const capped = capPowerLevel(power);
|
||||
if (capped !== undefined) mergedPowerLevels.notifications![key] = capped;
|
||||
});
|
||||
}
|
||||
|
||||
await mx.sendStateEvent(childId, StateEvent.RoomPowerLevels as any, mergedPowerLevels);
|
||||
await mx.sendStateEvent(childId, EventType.RoomPowerLevels, mergedPowerLevels);
|
||||
|
||||
// Also copy power level tags if they exist
|
||||
if (powerLevelTags) {
|
||||
@@ -257,7 +260,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
}
|
||||
|
||||
// Cap all power level values at my power level
|
||||
const capPowerLevel = (value: any) => {
|
||||
const capPowerLevel = (value: number | undefined): number | undefined => {
|
||||
if (typeof value === 'number') {
|
||||
return Math.min(value, myPowerInChild);
|
||||
}
|
||||
@@ -265,7 +268,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
};
|
||||
|
||||
// Create the merged power levels with capped values
|
||||
const mergedPowerLevels: any = {
|
||||
const mergedPowerLevels: IPowerLevels = {
|
||||
...powerLevels,
|
||||
users: mergedUsers,
|
||||
ban: capPowerLevel(powerLevels.ban),
|
||||
@@ -281,7 +284,8 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (powerLevels.events) {
|
||||
mergedPowerLevels.events = {};
|
||||
Object.entries(powerLevels.events).forEach(([eventType, power]) => {
|
||||
mergedPowerLevels.events[eventType] = capPowerLevel(power);
|
||||
const capped = capPowerLevel(power);
|
||||
if (capped !== undefined) mergedPowerLevels.events![eventType] = capped;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -289,11 +293,12 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
||||
if (powerLevels.notifications) {
|
||||
mergedPowerLevels.notifications = {};
|
||||
Object.entries(powerLevels.notifications).forEach(([key, power]) => {
|
||||
mergedPowerLevels.notifications[key] = capPowerLevel(power);
|
||||
const capped = capPowerLevel(power);
|
||||
if (capped !== undefined) mergedPowerLevels.notifications![key] = capped;
|
||||
});
|
||||
}
|
||||
|
||||
await mx.sendStateEvent(childId, StateEvent.RoomPowerLevels as any, mergedPowerLevels);
|
||||
await mx.sendStateEvent(childId, EventType.RoomPowerLevels, mergedPowerLevels);
|
||||
|
||||
completed += 1;
|
||||
|
||||
|
||||
@@ -43,11 +43,22 @@ const DEFAULT_POWER_LEVELS: Required<IPowerLevels> = {
|
||||
|
||||
const fillMissingPowers = (powerLevels: IPowerLevels): IPowerLevels =>
|
||||
produce(powerLevels, (draftPl: IPowerLevels) => {
|
||||
const keys = Object.keys(DEFAULT_POWER_LEVELS) as unknown as (keyof IPowerLevels)[];
|
||||
keys.forEach((key) => {
|
||||
if (draftPl[key] === undefined) {
|
||||
draftPl[key] = DEFAULT_POWER_LEVELS[key] as any;
|
||||
}
|
||||
const numberKeys = [
|
||||
'users_default',
|
||||
'state_default',
|
||||
'events_default',
|
||||
'historical',
|
||||
'invite',
|
||||
'redact',
|
||||
'kick',
|
||||
'ban',
|
||||
] as const;
|
||||
const recordKeys = ['events', 'users', 'notifications'] as const;
|
||||
numberKeys.forEach((key) => {
|
||||
if (draftPl[key] === undefined) draftPl[key] = DEFAULT_POWER_LEVELS[key];
|
||||
});
|
||||
recordKeys.forEach((key) => {
|
||||
if (draftPl[key] === undefined) draftPl[key] = DEFAULT_POWER_LEVELS[key];
|
||||
});
|
||||
if (draftPl.notifications && typeof draftPl.notifications.room !== 'number') {
|
||||
draftPl.notifications.room = DEFAULT_POWER_LEVELS.notifications.room;
|
||||
|
||||
+10
-12
@@ -8,7 +8,7 @@ export const isElectron = (): boolean => typeof window !== 'undefined' && 'elect
|
||||
*/
|
||||
export const isCapacitorNative = (): boolean => {
|
||||
if (typeof window === 'undefined') return false;
|
||||
const cap = (window as any).Capacitor;
|
||||
const cap = window.Capacitor;
|
||||
return Boolean(
|
||||
cap?.isNativePlatform?.() ||
|
||||
cap?.getPlatform?.() === 'android' ||
|
||||
@@ -26,7 +26,7 @@ export const openExternalUrl = async (url: string): Promise<void> => {
|
||||
// Use Electron's shell.openExternal if in Electron
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.shell?.openExternal) {
|
||||
await electron.shell.openExternal(url);
|
||||
console.log('[openExternalUrl] Electron shell.openExternal succeeded');
|
||||
@@ -64,7 +64,7 @@ export const getYouTubeStream = async (url: string): Promise<YouTubeStreamInfo>
|
||||
}
|
||||
|
||||
// Check for Electron
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.youtube?.getStream) {
|
||||
const result = await electron.youtube.getStream(url);
|
||||
// Electron returns { success: true, data: { video_url, title } } or { success: false, error }
|
||||
@@ -75,7 +75,7 @@ export const getYouTubeStream = async (url: string): Promise<YouTubeStreamInfo>
|
||||
}
|
||||
|
||||
// Check for Tauri
|
||||
if ((window as any).__TAURI__) {
|
||||
if (window.__TAURI__) {
|
||||
const { invoke } = await import('@tauri-apps/api/core');
|
||||
return invoke<YouTubeStreamInfo>('get_youtube_stream', { url });
|
||||
}
|
||||
@@ -93,13 +93,13 @@ export const isYouTubeStreamingAvailable = (): boolean => {
|
||||
}
|
||||
|
||||
// Check for Electron
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.youtube?.getStream) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check for Tauri
|
||||
return !!(window as any).__TAURI__;
|
||||
return !!window.__TAURI__;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -116,7 +116,7 @@ export const focusWindow = async (): Promise<void> => {
|
||||
// Use Electron's window focus if in Electron
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.window?.focus) {
|
||||
await electron.window.focus();
|
||||
}
|
||||
@@ -138,18 +138,16 @@ export const setupNotificationTapListener = async (
|
||||
// Use Electron's native notification handler if in Electron
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.notification?.onNavigate) {
|
||||
electron.notification.onNavigate((data: { path?: string }) => {
|
||||
if (data.path && typeof data.path === 'string' && notificationTapCallback) {
|
||||
notificationTapCallback(data.path);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('Failed to set up Electron notification listener:', err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -273,7 +271,7 @@ export const sendNotification = async (options: {
|
||||
// Use Electron's native notification API if in Electron
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.notification?.show) {
|
||||
await electron.notification.show({
|
||||
title,
|
||||
@@ -308,7 +306,7 @@ export const readClipboardImage = async (): Promise<File | null> => {
|
||||
// Use Electron's clipboard API if in Electron
|
||||
if (isElectron()) {
|
||||
try {
|
||||
const { electron } = window as any;
|
||||
const { electron } = window;
|
||||
if (electron?.clipboard?.readImage) {
|
||||
const result = await electron.clipboard.readImage();
|
||||
// IPC may return a data URL string or { success, data } depending on path.
|
||||
|
||||
Reference in New Issue
Block a user