fix(contacts): create self-contact in guest mode too

ensureSelfContact now works without profile data — creates a minimal
"Ich" contact in guest/local mode, syncs with profile when authenticated.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Till JS 2026-04-02 21:42:46 +02:00
parent 31d168c02b
commit baca70155a
2 changed files with 17 additions and 12 deletions

View file

@ -115,9 +115,10 @@ export const contactsStore = {
/**
* Ensure the self-contact exists and is synced with the user's profile.
* Creates the contact if missing, updates it if profile data changed.
* Works without profile data (guest mode) creates a minimal self-contact.
*/
async ensureSelfContact(profile: UserProfile): Promise<void> {
const nameParts = (profile.name || '').split(' ');
async ensureSelfContact(profile?: UserProfile | null): Promise<void> {
const nameParts = (profile?.name || '').split(' ');
const firstName = nameParts[0] || undefined;
const lastName = nameParts.slice(1).join(' ') || undefined;
@ -126,10 +127,10 @@ export const contactsStore = {
if (!existing) {
const self: LocalContact = {
id: SELF_CONTACT_ID,
firstName,
firstName: firstName || 'Ich',
lastName,
email: profile.email || undefined,
photoUrl: profile.image || undefined,
email: profile?.email || undefined,
photoUrl: profile?.image || undefined,
isFavorite: true,
isArchived: false,
createdAt: new Date().toISOString(),
@ -139,7 +140,9 @@ export const contactsStore = {
return;
}
// Sync profile fields if they changed
// Only sync if we have profile data
if (!profile) return;
const needsUpdate =
existing.firstName !== firstName ||
existing.lastName !== lastName ||

View file

@ -20,13 +20,15 @@
// Ensure self-contact exists and stays synced with profile
onMount(async () => {
if (!authStore.isAuthenticated) return;
try {
const profile = await profileService.getProfile();
await contactsStore.ensureSelfContact(profile);
} catch {
// Profile fetch may fail for guest users — that's OK
let profile = null;
if (authStore.isAuthenticated) {
try {
profile = await profileService.getProfile();
} catch {
// Profile fetch may fail — continue with guest self-contact
}
}
await contactsStore.ensureSelfContact(profile);
});
</script>