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 React, { useCallback, useMemo, useState } from 'react';
|
||||||
import { Box, Button, IconButton, Scroll, Spinner, Text, color } from 'folds';
|
import { Box, Button, IconButton, Scroll, Spinner, Text, color } from 'folds';
|
||||||
import { useAtomValue } from 'jotai';
|
import { useAtomValue } from 'jotai';
|
||||||
|
import { EventType } from 'matrix-js-sdk';
|
||||||
import { Icon, Icons } from '../../../components/icons';
|
import { Icon, Icons } from '../../../components/icons';
|
||||||
import { Page, PageContent, PageHeader } from '../../../components/page';
|
import { Page, PageContent, PageHeader } from '../../../components/page';
|
||||||
import { useRoom } from '../../../hooks/useRoom';
|
import { useRoom } from '../../../hooks/useRoom';
|
||||||
import { usePowerLevels } from '../../../hooks/usePowerLevels';
|
import { IPowerLevels, usePowerLevels } from '../../../hooks/usePowerLevels';
|
||||||
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
||||||
import { StateEvent } from '../../../../types/matrix/room';
|
import { StateEvent } from '../../../../types/matrix/room';
|
||||||
import { usePermissionGroups } from './usePermissionItems';
|
import { usePermissionGroups } from './usePermissionItems';
|
||||||
@@ -148,7 +149,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cap all power level values at my power level
|
// Cap all power level values at my power level
|
||||||
const capPowerLevel = (value: any) => {
|
const capPowerLevel = (value: number | undefined): number | undefined => {
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
return Math.min(value, myPowerInChild);
|
return Math.min(value, myPowerInChild);
|
||||||
}
|
}
|
||||||
@@ -156,7 +157,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Create the merged power levels with capped values
|
// Create the merged power levels with capped values
|
||||||
const mergedPowerLevels: any = {
|
const mergedPowerLevels: IPowerLevels = {
|
||||||
...powerLevels,
|
...powerLevels,
|
||||||
users: mergedUsers,
|
users: mergedUsers,
|
||||||
ban: capPowerLevel(powerLevels.ban),
|
ban: capPowerLevel(powerLevels.ban),
|
||||||
@@ -172,7 +173,8 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
if (powerLevels.events) {
|
if (powerLevels.events) {
|
||||||
mergedPowerLevels.events = {};
|
mergedPowerLevels.events = {};
|
||||||
Object.entries(powerLevels.events).forEach(([eventType, power]) => {
|
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) {
|
if (powerLevels.notifications) {
|
||||||
mergedPowerLevels.notifications = {};
|
mergedPowerLevels.notifications = {};
|
||||||
Object.entries(powerLevels.notifications).forEach(([key, power]) => {
|
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
|
// Also copy power level tags if they exist
|
||||||
if (powerLevelTags) {
|
if (powerLevelTags) {
|
||||||
@@ -257,7 +260,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cap all power level values at my power level
|
// Cap all power level values at my power level
|
||||||
const capPowerLevel = (value: any) => {
|
const capPowerLevel = (value: number | undefined): number | undefined => {
|
||||||
if (typeof value === 'number') {
|
if (typeof value === 'number') {
|
||||||
return Math.min(value, myPowerInChild);
|
return Math.min(value, myPowerInChild);
|
||||||
}
|
}
|
||||||
@@ -265,7 +268,7 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Create the merged power levels with capped values
|
// Create the merged power levels with capped values
|
||||||
const mergedPowerLevels: any = {
|
const mergedPowerLevels: IPowerLevels = {
|
||||||
...powerLevels,
|
...powerLevels,
|
||||||
users: mergedUsers,
|
users: mergedUsers,
|
||||||
ban: capPowerLevel(powerLevels.ban),
|
ban: capPowerLevel(powerLevels.ban),
|
||||||
@@ -281,7 +284,8 @@ export function Permissions({ requestClose }: PermissionsProps) {
|
|||||||
if (powerLevels.events) {
|
if (powerLevels.events) {
|
||||||
mergedPowerLevels.events = {};
|
mergedPowerLevels.events = {};
|
||||||
Object.entries(powerLevels.events).forEach(([eventType, power]) => {
|
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) {
|
if (powerLevels.notifications) {
|
||||||
mergedPowerLevels.notifications = {};
|
mergedPowerLevels.notifications = {};
|
||||||
Object.entries(powerLevels.notifications).forEach(([key, power]) => {
|
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;
|
completed += 1;
|
||||||
|
|
||||||
|
|||||||
@@ -43,11 +43,22 @@ const DEFAULT_POWER_LEVELS: Required<IPowerLevels> = {
|
|||||||
|
|
||||||
const fillMissingPowers = (powerLevels: IPowerLevels): IPowerLevels =>
|
const fillMissingPowers = (powerLevels: IPowerLevels): IPowerLevels =>
|
||||||
produce(powerLevels, (draftPl: IPowerLevels) => {
|
produce(powerLevels, (draftPl: IPowerLevels) => {
|
||||||
const keys = Object.keys(DEFAULT_POWER_LEVELS) as unknown as (keyof IPowerLevels)[];
|
const numberKeys = [
|
||||||
keys.forEach((key) => {
|
'users_default',
|
||||||
if (draftPl[key] === undefined) {
|
'state_default',
|
||||||
draftPl[key] = DEFAULT_POWER_LEVELS[key] as any;
|
'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') {
|
if (draftPl.notifications && typeof draftPl.notifications.room !== 'number') {
|
||||||
draftPl.notifications.room = DEFAULT_POWER_LEVELS.notifications.room;
|
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 => {
|
export const isCapacitorNative = (): boolean => {
|
||||||
if (typeof window === 'undefined') return false;
|
if (typeof window === 'undefined') return false;
|
||||||
const cap = (window as any).Capacitor;
|
const cap = window.Capacitor;
|
||||||
return Boolean(
|
return Boolean(
|
||||||
cap?.isNativePlatform?.() ||
|
cap?.isNativePlatform?.() ||
|
||||||
cap?.getPlatform?.() === 'android' ||
|
cap?.getPlatform?.() === 'android' ||
|
||||||
@@ -26,7 +26,7 @@ export const openExternalUrl = async (url: string): Promise<void> => {
|
|||||||
// Use Electron's shell.openExternal if in Electron
|
// Use Electron's shell.openExternal if in Electron
|
||||||
if (isElectron()) {
|
if (isElectron()) {
|
||||||
try {
|
try {
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.shell?.openExternal) {
|
if (electron?.shell?.openExternal) {
|
||||||
await electron.shell.openExternal(url);
|
await electron.shell.openExternal(url);
|
||||||
console.log('[openExternalUrl] Electron shell.openExternal succeeded');
|
console.log('[openExternalUrl] Electron shell.openExternal succeeded');
|
||||||
@@ -64,7 +64,7 @@ export const getYouTubeStream = async (url: string): Promise<YouTubeStreamInfo>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for Electron
|
// Check for Electron
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.youtube?.getStream) {
|
if (electron?.youtube?.getStream) {
|
||||||
const result = await electron.youtube.getStream(url);
|
const result = await electron.youtube.getStream(url);
|
||||||
// Electron returns { success: true, data: { video_url, title } } or { success: false, error }
|
// 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
|
// Check for Tauri
|
||||||
if ((window as any).__TAURI__) {
|
if (window.__TAURI__) {
|
||||||
const { invoke } = await import('@tauri-apps/api/core');
|
const { invoke } = await import('@tauri-apps/api/core');
|
||||||
return invoke<YouTubeStreamInfo>('get_youtube_stream', { url });
|
return invoke<YouTubeStreamInfo>('get_youtube_stream', { url });
|
||||||
}
|
}
|
||||||
@@ -93,13 +93,13 @@ export const isYouTubeStreamingAvailable = (): boolean => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for Electron
|
// Check for Electron
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.youtube?.getStream) {
|
if (electron?.youtube?.getStream) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for Tauri
|
// 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
|
// Use Electron's window focus if in Electron
|
||||||
if (isElectron()) {
|
if (isElectron()) {
|
||||||
try {
|
try {
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.window?.focus) {
|
if (electron?.window?.focus) {
|
||||||
await electron.window.focus();
|
await electron.window.focus();
|
||||||
}
|
}
|
||||||
@@ -138,18 +138,16 @@ export const setupNotificationTapListener = async (
|
|||||||
// Use Electron's native notification handler if in Electron
|
// Use Electron's native notification handler if in Electron
|
||||||
if (isElectron()) {
|
if (isElectron()) {
|
||||||
try {
|
try {
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.notification?.onNavigate) {
|
if (electron?.notification?.onNavigate) {
|
||||||
electron.notification.onNavigate((data: { path?: string }) => {
|
electron.notification.onNavigate((data: { path?: string }) => {
|
||||||
if (data.path && typeof data.path === 'string' && notificationTapCallback) {
|
if (data.path && typeof data.path === 'string' && notificationTapCallback) {
|
||||||
notificationTapCallback(data.path);
|
notificationTapCallback(data.path);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn('Failed to set up Electron notification listener:', 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
|
// Use Electron's native notification API if in Electron
|
||||||
if (isElectron()) {
|
if (isElectron()) {
|
||||||
try {
|
try {
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.notification?.show) {
|
if (electron?.notification?.show) {
|
||||||
await electron.notification.show({
|
await electron.notification.show({
|
||||||
title,
|
title,
|
||||||
@@ -308,7 +306,7 @@ export const readClipboardImage = async (): Promise<File | null> => {
|
|||||||
// Use Electron's clipboard API if in Electron
|
// Use Electron's clipboard API if in Electron
|
||||||
if (isElectron()) {
|
if (isElectron()) {
|
||||||
try {
|
try {
|
||||||
const { electron } = window as any;
|
const { electron } = window;
|
||||||
if (electron?.clipboard?.readImage) {
|
if (electron?.clipboard?.readImage) {
|
||||||
const result = await electron.clipboard.readImage();
|
const result = await electron.clipboard.readImage();
|
||||||
// IPC may return a data URL string or { success, data } depending on path.
|
// IPC may return a data URL string or { success, data } depending on path.
|
||||||
|
|||||||
Vendored
+23
@@ -1,6 +1,7 @@
|
|||||||
/// <reference types="vite/client" />
|
/// <reference types="vite/client" />
|
||||||
|
|
||||||
import type { DebugPacket } from './app/state/debugPackets';
|
import type { DebugPacket } from './app/state/debugPackets';
|
||||||
|
import type { YouTubeStreamInfo } from './app/utils/tauri';
|
||||||
|
|
||||||
declare module '*.svg' {
|
declare module '*.svg' {
|
||||||
const content: string;
|
const content: string;
|
||||||
@@ -51,6 +52,24 @@ interface ElectronAPI {
|
|||||||
};
|
};
|
||||||
clipboard?: {
|
clipboard?: {
|
||||||
writeText: (text: string) => void;
|
writeText: (text: string) => void;
|
||||||
|
readImage?: () => Promise<string | { data?: string }>;
|
||||||
|
};
|
||||||
|
shell?: {
|
||||||
|
openExternal?: (url: string) => Promise<unknown>;
|
||||||
|
};
|
||||||
|
youtube?: {
|
||||||
|
getStream?: (
|
||||||
|
url: string,
|
||||||
|
) => Promise<{ success: boolean; data: YouTubeStreamInfo; error?: string }>;
|
||||||
|
};
|
||||||
|
notification?: {
|
||||||
|
show?: (options: {
|
||||||
|
title: string;
|
||||||
|
body: string;
|
||||||
|
icon?: string;
|
||||||
|
path?: string;
|
||||||
|
}) => Promise<unknown>;
|
||||||
|
onNavigate?: (callback: (data: { path?: string }) => void) => void;
|
||||||
};
|
};
|
||||||
media?: {
|
media?: {
|
||||||
saveFile: (payload: { filename: string; mimeType?: string; data: Uint8Array }) => Promise<{
|
saveFile: (payload: { filename: string; mimeType?: string; data: Uint8Array }) => Promise<{
|
||||||
@@ -148,6 +167,10 @@ declare global {
|
|||||||
invoke: (command: string, args?: unknown) => Promise<unknown>;
|
invoke: (command: string, args?: unknown) => Promise<unknown>;
|
||||||
listen: (event: string, handler: (...args: unknown[]) => void) => () => void;
|
listen: (event: string, handler: (...args: unknown[]) => void) => () => void;
|
||||||
};
|
};
|
||||||
|
Capacitor?: {
|
||||||
|
isNativePlatform?: () => boolean;
|
||||||
|
getPlatform?: () => string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user