Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
import { sharingView, type SharedFolder } from '@aliasvault/client/sharing/SharingView';
import { SharingService, type InvitationNameDecryptor } from '@aliasvault/client/sharing/SharingService';
import { CapabilityKeys } from '@aliasvault/models/webapi';
import { useFocusEffect } from 'expo-router';
import { useCallback, useEffect, useState } from 'react';
import emitter from '@/utils/EventEmitter';
import { useCapabilities } from '@/context/CapabilityContext';
import { useWebApi } from '@/context/WebApiContext';
import NativeVaultManager from '@/specs/NativeVaultManager';
import type { GroupMemberInfo, ReceivedManifestInvitation } from '@aliasvault/models/webapi';
/** Decrypts an invitation's vault name natively, where the account private key lives. */
const decryptNameNatively: InvitationNameDecryptor = (encryptedName, manifestId) => NativeVaultManager.decryptManifestInvitationName(encryptedName, manifestId);
/**
* What the vault shows of sharing: the shared folders with their people, the invitations waiting for an answer, the
* people verified before and this account's own fingerprint.
*/
export type SharedFolders = {
isEnabled: boolean;
myUserId: string;
folders: SharedFolder[];
invitations: ReceivedManifestInvitation[];
invitationNames: Record<string, string>;
verifiedPeople: GroupMemberInfo[];
ownFingerprint: string | null;
reload: () => Promise<void>;
};
/**
* Load the sharing overview while vault sharing is enabled: when the screen gains focus, after every sync, and on request.
*/
export function useSharedFolders(): SharedFolders {
const webApi = useWebApi();
const isEnabled = useCapabilities()(CapabilityKeys.VaultSharing);
const [view, setView] = useState<ReturnType<typeof sharingView>>(sharingView(null));
const [invitationNames, setInvitationNames] = useState<Record<string, string>>({});
const [ownFingerprint, setOwnFingerprint] = useState<string | null>(null);
const reload = useCallback(async (): Promise<void> => {
if (!isEnabled) {
return;
}
try {
const overview = await SharingService.getOverview(webApi);
setView(sharingView(overview));
setInvitationNames(await SharingService.openInvitationNames(overview.receivedInvitations, decryptNameNatively));
// The own fingerprint is derived natively from the signing private key, which only the native layer can open.
setOwnFingerprint(await NativeVaultManager.getSigningKeyFingerprint());
} catch {
// Offline or not reachable: the vault keeps showing what it already knew.
}
}, [isEnabled, webApi]);
useFocusEffect(useCallback(() => {
void reload();
}, [reload]));
useEffect(() => {
const subscription = emitter.addListener('vaultSynced', () => void reload());
return (): void => subscription.remove();
}, [reload]);
return { isEnabled, ...view, invitationNames, ownFingerprint, reload };
}