forked from quic-issues/427e7578-d7bf-49c8-aee9-2dd999e25316
Implementation, thanks amp
This commit is contained in:
62
app/src/lib/stores/profile.svelte.ts
Normal file
62
app/src/lib/stores/profile.svelte.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { loadProfile, saveProfile } from '$lib/db.js';
|
||||
import { getUserId } from '$lib/crypto.js';
|
||||
import type { Tag, UserProfile } from '$lib/types.js';
|
||||
|
||||
let profile = $state<UserProfile | null>(null);
|
||||
let loading = $state(true);
|
||||
|
||||
export function getProfile() {
|
||||
return {
|
||||
get current() { return profile; },
|
||||
get loading() { return loading; }
|
||||
};
|
||||
}
|
||||
|
||||
export async function initProfile(): Promise<UserProfile> {
|
||||
loading = true;
|
||||
const existing = await loadProfile();
|
||||
if (existing) {
|
||||
profile = existing;
|
||||
loading = false;
|
||||
return existing;
|
||||
}
|
||||
|
||||
const userId = await getUserId();
|
||||
const newProfile: UserProfile = {
|
||||
id: userId,
|
||||
name: '',
|
||||
bio: '',
|
||||
tags: [],
|
||||
updatedAt: Date.now(),
|
||||
signature: ''
|
||||
};
|
||||
|
||||
await saveProfile(newProfile);
|
||||
profile = newProfile;
|
||||
loading = false;
|
||||
return newProfile;
|
||||
}
|
||||
|
||||
export async function updateProfile(updates: Partial<Pick<UserProfile, 'name' | 'bio' | 'tags'>>): Promise<void> {
|
||||
if (!profile) return;
|
||||
|
||||
profile = {
|
||||
...profile,
|
||||
...updates,
|
||||
updatedAt: Date.now()
|
||||
};
|
||||
|
||||
await saveProfile(profile);
|
||||
}
|
||||
|
||||
export async function addTag(tag: Tag): Promise<void> {
|
||||
if (!profile) return;
|
||||
const tags = [...profile.tags, tag];
|
||||
await updateProfile({ tags });
|
||||
}
|
||||
|
||||
export async function removeTag(index: number): Promise<void> {
|
||||
if (!profile) return;
|
||||
const tags = profile.tags.filter((_, i) => i !== index);
|
||||
await updateProfile({ tags });
|
||||
}
|
||||
Reference in New Issue
Block a user