- Introduced user status management with the ability to update online, busy, offline, and away statuses. - Added metadata fields for user preferences, including phrase preferences and friends list. - Updated API and database schema to accommodate new user fields. - Enhanced the authentication component to handle additional user data effectively. - Implemented hooks for socket management and OLM setup to improve user experience.
59 lines
1.4 KiB
TypeScript
59 lines
1.4 KiB
TypeScript
"use client"
|
|
|
|
import { checkOlmStatus, handleOlmAccountCreation, SendKeysToServerFn } from "@/lib/olm";
|
|
import { useEffect, useState } from "react";
|
|
|
|
interface UseOlmSetupOptions {
|
|
userId: string | undefined;
|
|
hasServerOlm: boolean | undefined;
|
|
sendKeysToServer: SendKeysToServerFn;
|
|
}
|
|
|
|
export function useOlmSetup({ userId, hasServerOlm, sendKeysToServer }: UseOlmSetupOptions) {
|
|
const [olmStatus, setOlmStatus] = useState<SiPher.OlmStatus>("checking");
|
|
const [showOlmModal, setShowOlmModal] = useState(false);
|
|
|
|
// Check OLM status when user data and server status are available
|
|
useEffect(() => {
|
|
if (!userId || hasServerOlm === undefined) return;
|
|
|
|
const checkStatus = async () => {
|
|
const status = await checkOlmStatus(userId, hasServerOlm);
|
|
setOlmStatus(status);
|
|
|
|
if (status === "not_setup" || status === "mismatched") {
|
|
setShowOlmModal(true);
|
|
}
|
|
};
|
|
|
|
checkStatus();
|
|
}, [userId, hasServerOlm]);
|
|
|
|
// Handle OLM account creation
|
|
const handleCreateAccount = async (password: string): Promise<void> => {
|
|
if (!userId || !password.trim()) return;
|
|
|
|
setOlmStatus("creating");
|
|
const success = await handleOlmAccountCreation(
|
|
userId,
|
|
password,
|
|
sendKeysToServer,
|
|
olmStatus === "mismatched"
|
|
);
|
|
|
|
if (success) {
|
|
setOlmStatus("synced");
|
|
setShowOlmModal(false);
|
|
} else {
|
|
setOlmStatus("not_setup");
|
|
}
|
|
};
|
|
|
|
return {
|
|
olmStatus,
|
|
showOlmModal,
|
|
setShowOlmModal,
|
|
handleCreateAccount
|
|
};
|
|
}
|
|
|