From 4488947eed445a4c648969b9a1befba5ec8b050a Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 6 May 2022 11:32:09 +0100 Subject: [PATCH 01/12] Initial round of typescripting --- .eslintrc.js | 5 + package.json | 5 + src/{Avatar.jsx => Avatar.tsx} | 18 +- ...ateRandomName.js => generateRandomName.ts} | 3 +- src/room/{PTTButton.jsx => PTTButton.tsx} | 18 +- src/room/{PTTCallView.jsx => PTTCallView.tsx} | 5 +- src/room/{usePTT.js => usePTT.ts} | 29 ++- src/types.d.ts | 11 + tsconfig.json | 24 ++ yarn.lock | 222 +++++++++++++++++- 10 files changed, 316 insertions(+), 24 deletions(-) rename src/{Avatar.jsx => Avatar.tsx} (76%) rename src/auth/{generateRandomName.js => generateRandomName.ts} (96%) rename src/room/{PTTButton.jsx => PTTButton.tsx} (79%) rename src/room/{PTTCallView.jsx => PTTCallView.tsx} (99%) rename src/room/{usePTT.js => usePTT.ts} (80%) create mode 100644 src/types.d.ts create mode 100644 tsconfig.json diff --git a/.eslintrc.js b/.eslintrc.js index 7788059e..89b58447 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -25,6 +25,11 @@ module.exports = { files: [ "src/**/*.{ts,tsx}", ], + extends: [ + "plugin:matrix-org/typescript", + "plugin:matrix-org/react", + "prettier", + ], }, ], settings: { diff --git a/package.json b/package.json index 1cd926f6..24712ea0 100644 --- a/package.json +++ b/package.json @@ -52,9 +52,13 @@ "devDependencies": { "@babel/core": "^7.16.5", "@storybook/react": "^6.5.0-alpha.5", + "@typescript-eslint/eslint-plugin": "^5.22.0", + "@typescript-eslint/parser": "^5.22.0", "babel-loader": "^8.2.3", "eslint": "^8.14.0", + "eslint-config-google": "^0.14.0", "eslint-config-prettier": "^8.5.0", + "eslint-plugin-import": "^2.26.0", "eslint-plugin-jsx-a11y": "^6.5.1", "eslint-plugin-matrix-org": "^0.4.0", "eslint-plugin-react": "^7.29.4", @@ -62,6 +66,7 @@ "prettier": "^2.6.2", "sass": "^1.42.1", "storybook-builder-vite": "^0.1.12", + "typescript": "^4.6.4", "vite": "^2.4.2", "vite-plugin-html-template": "^1.1.0", "vite-plugin-svgr": "^0.4.0" diff --git a/src/Avatar.jsx b/src/Avatar.tsx similarity index 76% rename from src/Avatar.jsx rename to src/Avatar.tsx index fcfc3339..c2fd11b0 100644 --- a/src/Avatar.jsx +++ b/src/Avatar.tsx @@ -1,5 +1,6 @@ import React, { useMemo } from "react"; import classNames from "classnames"; + import styles from "./Avatar.module.css"; const backgroundColors = [ @@ -13,7 +14,7 @@ const backgroundColors = [ "#74D12C", ]; -function hashStringToArrIndex(str, arrLength) { +function hashStringToArrIndex(str: string, arrLength: number) { let sum = 0; for (let i = 0; i < str.length; i++) { @@ -23,7 +24,16 @@ function hashStringToArrIndex(str, arrLength) { return sum % arrLength; } -export function Avatar({ +interface Props extends React.HTMLAttributes { + bgKey?: string; + src: string; + fallback: string; + size?: number; + className: string; + style: React.CSSProperties; +} + +export const Avatar: React.FC = ({ bgKey, src, fallback, @@ -31,7 +41,7 @@ export function Avatar({ className, style, ...rest -}) { +}) => { const backgroundColor = useMemo(() => { const index = hashStringToArrIndex( bgKey || fallback || src || "", @@ -55,4 +65,4 @@ export function Avatar({ )} ); -} +}; diff --git a/src/auth/generateRandomName.js b/src/auth/generateRandomName.ts similarity index 96% rename from src/auth/generateRandomName.js rename to src/auth/generateRandomName.ts index 7e6ff93e..fba6d9e8 100644 --- a/src/auth/generateRandomName.js +++ b/src/auth/generateRandomName.ts @@ -3,6 +3,7 @@ import { adjectives, colors, animals, + Config, } from "unique-names-generator"; const elements = [ @@ -126,7 +127,7 @@ const elements = [ "oganesson", ]; -export function generateRandomName(config) { +export function generateRandomName(config: Config): string { return uniqueNamesGenerator({ dictionaries: [colors, adjectives, animals, elements], style: "lowerCase", diff --git a/src/room/PTTButton.jsx b/src/room/PTTButton.tsx similarity index 79% rename from src/room/PTTButton.jsx rename to src/room/PTTButton.tsx index 6da9b863..6126b519 100644 --- a/src/room/PTTButton.jsx +++ b/src/room/PTTButton.tsx @@ -1,10 +1,22 @@ import React from "react"; import classNames from "classnames"; + import styles from "./PTTButton.module.css"; import { ReactComponent as MicIcon } from "../icons/Mic.svg"; import { Avatar } from "../Avatar"; -export function PTTButton({ +interface Props { + showTalkOverError: boolean; + activeSpeakerUserId: string; + activeSpeakerDisplayName: string; + activeSpeakerAvatarUrl: string; + activeSpeakerIsLocalUser: boolean; + size: number; + startTalking: () => void; + stopTalking: () => void; +} + +export const PTTButton: React.FC = ({ showTalkOverError, activeSpeakerUserId, activeSpeakerDisplayName, @@ -13,7 +25,7 @@ export function PTTButton({ size, startTalking, stopTalking, -}) { +}) => { return (