2026-06-03 23:13:33 -04:00
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import { Method } from 'matrix-js-sdk';
|
|
|
|
|
import { useMatrixClient } from './useMatrixClient';
|
|
|
|
|
|
|
|
|
|
export type ExtendedProfile = {
|
|
|
|
|
pronouns?: string;
|
|
|
|
|
timezone?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useExtendedProfile = (userId: string): ExtendedProfile => {
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const [extProfile, setExtProfile] = useState<ExtendedProfile>({});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
let cancelled = false;
|
2026-06-10 21:31:58 -04:00
|
|
|
const myUserId = mx.getUserId();
|
2026-06-03 23:13:33 -04:00
|
|
|
|
|
|
|
|
const fetchField = async <T extends Record<string, string>>(
|
|
|
|
|
field: string,
|
|
|
|
|
): Promise<string | undefined> => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await mx.http.authedRequest<T>(
|
|
|
|
|
Method.Get,
|
|
|
|
|
`/profile/${encodeURIComponent(userId)}/${field}`,
|
|
|
|
|
);
|
|
|
|
|
return res[field as keyof T] as string | undefined;
|
|
|
|
|
} catch {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-10 21:31:58 -04:00
|
|
|
const run = async () => {
|
|
|
|
|
const [pronouns, tzFromProfile] = await Promise.all([
|
|
|
|
|
fetchField<{ 'm.pronouns': string }>('m.pronouns'),
|
|
|
|
|
fetchField<{ 'm.tz': string }>('m.tz'),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
let timezone = tzFromProfile;
|
|
|
|
|
// Standard Synapse doesn't support m.tz — fall back to account data for own profile
|
|
|
|
|
if (!timezone && userId === myUserId) {
|
|
|
|
|
try {
|
|
|
|
|
const res = await mx.http.authedRequest<{ timezone: string }>(
|
|
|
|
|
Method.Get,
|
|
|
|
|
`/user/${encodeURIComponent(userId)}/account_data/im.lotus.timezone`,
|
|
|
|
|
);
|
|
|
|
|
timezone = res.timezone || undefined;
|
|
|
|
|
} catch {
|
|
|
|
|
// not set yet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 23:13:33 -04:00
|
|
|
if (!cancelled) {
|
|
|
|
|
setExtProfile({ pronouns: pronouns || undefined, timezone: timezone || undefined });
|
|
|
|
|
}
|
2026-06-10 21:31:58 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
run();
|
2026-06-03 23:13:33 -04:00
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
};
|
|
|
|
|
}, [mx, userId]);
|
|
|
|
|
|
|
|
|
|
return extProfile;
|
|
|
|
|
};
|