forked from abner/for-legacy-web
remove most uses of as any in typescript
- replaced many uses of `as any` with another more specific cast `as T` - filled in missing typed for items that needed to be typed - new runtime code was added where necessary to satisfy the new types with comments - added missing theme variable "sidebar-active" to the Theme variables - forms using `react-hook-form` are now typechecked - changed some instances of `target` into `currentTarget` while removing `as any` assertions
This commit is contained in:
@@ -43,8 +43,8 @@ export function grabFiles(maxFileSize: number, cb: (files: File[]) => void, tooL
|
||||
input.type = "file";
|
||||
input.multiple = multiple ?? false;
|
||||
|
||||
input.onchange = async e => {
|
||||
const files = (e.target as any)?.files;
|
||||
input.onchange = async (e) => {
|
||||
const files = (e.currentTarget as HTMLInputElement)?.files;
|
||||
if (!files) return;
|
||||
for (let file of files) {
|
||||
if (file.size > maxFileSize) {
|
||||
|
||||
@@ -34,9 +34,10 @@ export interface ClientOperations {
|
||||
openDM: (user_id: string) => Promise<string>;
|
||||
}
|
||||
|
||||
export const AppContext = createContext<Client>(undefined as any);
|
||||
export const StatusContext = createContext<ClientStatus>(undefined as any);
|
||||
export const OperationsContext = createContext<ClientOperations>(undefined as any);
|
||||
// TODO: remove temporary non-null assertions and properly typecheck these as they aren't always immedietely initialized
|
||||
export const AppContext = createContext<Client>(null!);
|
||||
export const StatusContext = createContext<ClientStatus>(null!);
|
||||
export const OperationsContext = createContext<ClientOperations>(null!);
|
||||
|
||||
type Props = WithDispatcher & {
|
||||
auth: AuthState;
|
||||
@@ -93,16 +94,14 @@ function Context({ auth, children, dispatcher }: Props) {
|
||||
const login = () =>
|
||||
dispatcher({
|
||||
type: "LOGIN",
|
||||
session: client.session as any
|
||||
session: client.session! // TODO: verify that this null assertion is correct
|
||||
});
|
||||
|
||||
if (onboarding) {
|
||||
openScreen({
|
||||
id: "onboarding",
|
||||
callback: async (username: string) => {
|
||||
await (onboarding as any)(username, true);
|
||||
login();
|
||||
}
|
||||
callback: (username: string) =>
|
||||
onboarding(username, true).then(login)
|
||||
});
|
||||
} else {
|
||||
login();
|
||||
|
||||
@@ -23,11 +23,11 @@ type Props = WithDispatcher & {
|
||||
|
||||
var lastValues: { [key in SyncKeys]?: any } = { };
|
||||
|
||||
export function mapSync(packet: Sync.UserSettings, revision?: { [key: string]: number }) {
|
||||
export function mapSync(packet: Sync.UserSettings, revision?: Record<string, number>) {
|
||||
let update: { [key in SyncKeys]?: [ number, SyncData[key] ] } = {};
|
||||
for (let key of Object.keys(packet)) {
|
||||
let [ timestamp, obj ] = packet[key];
|
||||
if (timestamp < (revision ?? {} as any)[key] ?? 0) {
|
||||
if (timestamp < (revision ?? {})[key] ?? 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ export function registerEvents({
|
||||
}
|
||||
}
|
||||
|
||||
const listeners = {
|
||||
let listeners: Record<string, (...args: any[]) => void> = {
|
||||
connecting: () =>
|
||||
operations.ready() && setStatus(ClientStatus.CONNECTING),
|
||||
|
||||
@@ -87,21 +87,18 @@ export function registerEvents({
|
||||
ready: () => setStatus(ClientStatus.ONLINE)
|
||||
};
|
||||
|
||||
let listenerFunc: { [key: string]: Function };
|
||||
if (import.meta.env.DEV) {
|
||||
listenerFunc = {};
|
||||
for (const listener of Object.keys(listeners)) {
|
||||
listenerFunc[listener] = (...args: any[]) => {
|
||||
console.debug(`Calling ${listener} with`, args);
|
||||
(listeners as any)[listener](...args);
|
||||
};
|
||||
}
|
||||
} else {
|
||||
listenerFunc = listeners;
|
||||
listeners = new Proxy(listeners, {
|
||||
get: (target, listener, receiver) => (...args: unknown[]) => {
|
||||
console.debug(`Calling ${listener.toString()} with`, args);
|
||||
Reflect.get(target, listener)(...args)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for (const listener of Object.keys(listenerFunc)) {
|
||||
client.addListener(listener, (listenerFunc as any)[listener]);
|
||||
// TODO: clean this a bit and properly handle types
|
||||
for (const listener in listeners) {
|
||||
client.addListener(listener, listeners[listener]);
|
||||
}
|
||||
|
||||
function logMutation(target: string, key: string) {
|
||||
@@ -135,8 +132,8 @@ export function registerEvents({
|
||||
window.addEventListener("offline", offline);
|
||||
|
||||
return () => {
|
||||
for (const listener of Object.keys(listenerFunc)) {
|
||||
client.removeListener(listener, (listenerFunc as any)[listener]);
|
||||
for (const listener in listeners) {
|
||||
client.removeListener(listener, listeners[listener as keyof typeof listeners]);
|
||||
}
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useCallback, useContext, useEffect, useState } from "preact/hooks";
|
||||
import { Channels, Servers, Users } from "revolt.js/dist/api/objects";
|
||||
import { Client, PermissionCalculator } from 'revolt.js';
|
||||
import { AppContext } from "./RevoltClient";
|
||||
import Collection from "revolt.js/dist/maps/Collection";
|
||||
|
||||
export interface HookContext {
|
||||
client: Client,
|
||||
@@ -25,7 +26,16 @@ export function useForceUpdate(context?: HookContext): HookContext {
|
||||
return { client, forceUpdate: () => updateState(Math.random()) };
|
||||
}
|
||||
|
||||
function useObject(type: string, id?: string | string[], context?: HookContext) {
|
||||
// TODO: utils.d.ts maybe?
|
||||
type PickProperties<T, U> = Pick<T, {
|
||||
[K in keyof T]: T[K] extends U ? K : never
|
||||
}[keyof T]>
|
||||
|
||||
// The keys in Client that are an object
|
||||
// for some reason undefined keeps appearing despite there being no reason to so it's filtered out
|
||||
type ClientCollectionKey = Exclude<keyof PickProperties<Client, Collection<any>>, undefined>;
|
||||
|
||||
function useObject(type: ClientCollectionKey, id?: string | string[], context?: HookContext) {
|
||||
const ctx = useForceUpdate(context);
|
||||
|
||||
function update(target: any) {
|
||||
@@ -35,7 +45,7 @@ function useObject(type: string, id?: string | string[], context?: HookContext)
|
||||
}
|
||||
}
|
||||
|
||||
const map = (ctx.client as any)[type];
|
||||
const map = ctx.client[type];
|
||||
useEffect(() => {
|
||||
map.addListener("update", update);
|
||||
return () => map.removeListener("update", update);
|
||||
|
||||
Reference in New Issue
Block a user