diff --git a/.changeset/device-display-name.md b/.changeset/device-display-name.md new file mode 100644 index 000000000..57c6b7230 --- /dev/null +++ b/.changeset/device-display-name.md @@ -0,0 +1,5 @@ +--- +default: minor +--- + +# Update device display name post-login to reflect the current platform and flavor (e.g. "Sable Desktop (Nightly) on Windows"). diff --git a/src-tauri/build.rs b/src-tauri/build.rs index 36ed7391f..d4624f92b 100644 --- a/src-tauri/build.rs +++ b/src-tauri/build.rs @@ -32,10 +32,10 @@ fn main() { ) { format!("{} Nightly", base) } else { - base + base.clone() }; - println!("cargo:rustc-env=SABLE_PRODUCT_NAME={}", full_name); + println!("cargo:rustc-env=SABLE_PRODUCT_NAME={}", base); println!( "cargo:rustc-env=TAURI_CONFIG={{\"productName\":\"{}\"}}", full_name diff --git a/src/app/hooks/useDeviceDisplayName.ts b/src/app/hooks/useDeviceDisplayName.ts new file mode 100644 index 000000000..7c9793cc6 --- /dev/null +++ b/src/app/hooks/useDeviceDisplayName.ts @@ -0,0 +1,47 @@ +import { useEffect, useRef } from 'react'; +import { ClientEvent, SyncState } from '$types/matrix-sdk'; +import type { MatrixClient } from '$types/matrix-sdk'; +import { deviceDisplayName } from '$utils/platform'; +import { createLogger } from '$utils/debug'; + +const SYNC_READY = new Set([SyncState.Prepared, SyncState.Syncing, SyncState.Catchup]); +const log = createLogger('useDeviceDisplayName'); + +async function updateDeviceName(mx: MatrixClient) { + const desired = deviceDisplayName(); + const device = await mx.getDevice(mx.getDeviceId()!); + if (device?.display_name === desired) { + log.log('already set:', desired); + return; + } + await mx.setDeviceDetails(mx.getDeviceId()!, { display_name: desired }); + log.log(desired); +} + +export function useDeviceDisplayName(mx: MatrixClient | undefined) { + const updatedRef = useRef(false); + + useEffect(() => { + if (!mx || updatedRef.current) return; + + const state = mx.getSyncState(); + if (state && SYNC_READY.has(state)) { + updatedRef.current = true; + updateDeviceName(mx).catch(() => {}); + return; + } + + const onSync = (syncState: SyncState) => { + if (SYNC_READY.has(syncState)) { + mx.removeListener(ClientEvent.Sync, onSync); + updatedRef.current = true; + updateDeviceName(mx).catch(() => {}); + } + }; + mx.on(ClientEvent.Sync, onSync); + + return () => { + mx.removeListener(ClientEvent.Sync, onSync); + }; + }, [mx]); +} diff --git a/src/app/pages/client/ClientRoot.tsx b/src/app/pages/client/ClientRoot.tsx index 3cd4f0ba9..4078f6cea 100644 --- a/src/app/pages/client/ClientRoot.tsx +++ b/src/app/pages/client/ClientRoot.tsx @@ -37,6 +37,7 @@ import { MatrixClientProvider } from '$hooks/useMatrixClient'; import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; import { useSyncState } from '$hooks/useSyncState'; import { useCrossSigningResetDetect } from '$hooks/useCrossSigningResetDetect'; +import { useDeviceDisplayName } from '$hooks/useDeviceDisplayName'; import { useMatrixEvent } from '$hooks/useMatrixEvent'; import { stopPropagation } from '$utils/keyboard'; import { AuthMetadataProvider, getSessionAuthMetadata } from '$hooks/useAuthMetadata'; @@ -351,6 +352,7 @@ export function ClientRoot({ children }: ClientRootProps) { useAppVisibility(mx); useNetworkRecovery(mx); useCrossSigningResetDetect(mx); + useDeviceDisplayName(mx); useEffect( () => () => { diff --git a/vite.config.ts b/vite.config.ts index 18e33d244..f980a90c8 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -138,7 +138,6 @@ function serverMatrixSdkCryptoWasm() { export default defineConfig(({ command }) => { const buildFlavor = process.env.SABLE_BUILD_FLAVOR || (command === 'serve' ? 'dev' : 'stable'); - const productName = buildFlavor !== 'stable' ? `${baseProductName} Nightly` : baseProductName; return { clearScreen: false, @@ -150,7 +149,7 @@ export default defineConfig(({ command }) => { APP_VERSION: JSON.stringify(appVersion), BUILD_HASH: JSON.stringify(buildHash ?? ''), IS_RELEASE_TAG: JSON.stringify(isReleaseTag), - SABLE_PRODUCT_NAME: JSON.stringify(productName), + SABLE_PRODUCT_NAME: JSON.stringify(baseProductName), SABLE_BUILD_FLAVOR: JSON.stringify(buildFlavor), }, resolve: {