From 0e4addd3fc78d4c7297c77609d07aee82a292a15 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 3 Jan 2025 13:45:18 +0200 Subject: [PATCH] Clean up code --- web/src/api/rpc.ts | 3 +- web/src/api/types/mxtypes.ts | 30 +++---- web/src/ui/rightpanel/UserExtendedProfile.tsx | 86 +++++++++---------- 3 files changed, 59 insertions(+), 60 deletions(-) diff --git a/web/src/api/rpc.ts b/web/src/api/rpc.ts index 9530a8a..083ca75 100644 --- a/web/src/api/rpc.ts +++ b/web/src/api/rpc.ts @@ -19,7 +19,8 @@ import type { ClientWellKnown, EventID, EventRowID, - EventType, JSONValue, + EventType, + JSONValue, LoginFlowsResponse, LoginRequest, Mentions, diff --git a/web/src/api/types/mxtypes.ts b/web/src/api/types/mxtypes.ts index 31d9d95..9b84fde 100644 --- a/web/src/api/types/mxtypes.ts +++ b/web/src/api/types/mxtypes.ts @@ -76,6 +76,21 @@ export interface UserProfile { [custom: string]: unknown } +export interface PronounSet { + subject?: string + object?: string + possessive_determiner?: string + possessive_pronoun?: string + reflexive?: string + summary: string + language: string +} + +export interface ExtendedUserProfile extends UserProfile { + "us.cloke.msc4175.tz"?: string + "io.fsky.nyx.pronouns"?: PronounSet[] +} + export type Membership = "join" | "leave" | "ban" | "invite" | "knock" export interface MemberEventContent extends UserProfile { @@ -294,18 +309,3 @@ export interface RespOpenIDToken { matrix_server_name: string token_type: "Bearer" } - -export interface PronounSet { - subject?: string - object?: string - possessive_determiner?: string - possessive_pronoun?: string - reflexive?: string - summary: string - language: string -} - -export interface ExtendedProfileAttributes { - "us.cloke.msc4175.tz"?: string - "io.fsky.nyx.pronouns"?: PronounSet[] -} diff --git a/web/src/ui/rightpanel/UserExtendedProfile.tsx b/web/src/ui/rightpanel/UserExtendedProfile.tsx index 54bbd9d..e0f351c 100644 --- a/web/src/ui/rightpanel/UserExtendedProfile.tsx +++ b/web/src/ui/rightpanel/UserExtendedProfile.tsx @@ -1,10 +1,10 @@ import { useEffect, useState } from "react" import Client from "@/api/client.ts" -import { ExtendedProfileAttributes, PronounSet, UserProfile } from "@/api/types" -import { ensureArray } from "@/util/validation.ts" +import { ExtendedUserProfile, PronounSet } from "@/api/types" +import { ensureArray, ensureString } from "@/util/validation.ts" interface ExtendedProfileProps { - profile: UserProfile & ExtendedProfileAttributes + profile: ExtendedUserProfile client: Client userID: string } @@ -51,54 +51,52 @@ function SetTimezoneElement({ tz, client }: SetTimezoneProps) { // The defaulting to the current timezone causes `newTz !== tz` to never be true when the user has // no timezone set. - return ( - <> - setTz(e.currentTarget.value)} - /> - - { - zones.map((zone) => - - ) + return <> + setTz(e.currentTarget.value)} + /> + + {zones.map((zone) => + } export default function UserExtendedProfile({ profile, client, userID }: ExtendedProfileProps) { - if (!profile) return null + if (!profile) { + return null + } const extendedProfileKeys = ["us.cloke.msc4175.tz", "io.fsky.nyx.pronouns"] - const hasExtendedProfile = extendedProfileKeys.some((key) => key in profile) - if (!hasExtendedProfile && client.userID !== userID) return null + const hasExtendedProfile = extendedProfileKeys.some((key) => profile[key]) + if (!hasExtendedProfile && client.userID !== userID) { + return null + } // Explicitly only return something if the profile has the keys we're looking for. // otherwise there's an ugly and pointless
for no real reason. - const pronouns: PronounSet[] = ensureArray(profile["io.fsky.nyx.pronouns"]) as PronounSet[] - const userTimezone: string | undefined = profile["us.cloke.msc4175.tz"] - return ( - <> -
-
- {userTimezone && <> -
Time:
- - } - {userID === client.userID && <> -
Set Timezone:
- - } - {pronouns.length >= 1 && <> -
Pronouns:
-
- {pronouns.map((pronounSet: PronounSet) => (pronounSet.summary)).join("/")} -
- } -
- - ) + const pronouns = ensureArray(profile["io.fsky.nyx.pronouns"]) as PronounSet[] + const userTimezone = ensureString(profile["us.cloke.msc4175.tz"]) + return <> +
+
+ {userTimezone && <> +
Time:
+ + } + {userID === client.userID && <> +
Set Timezone:
+ + } + {pronouns.length >= 1 && <> +
Pronouns:
+
+ {pronouns.map((pronounSet: PronounSet) => (pronounSet.summary)).join("/")} +
+ } +
+ }