feat: add profile fields for local users
DB→domain→API→AP→frontend end-to-end. Fields stored as JSONB, exposed via PATCH /users/me, serialized as AP PropertyValue attachment. Editor in federation settings, display on profile card.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import { getMe } from "@/lib/api";
|
||||
import { FederationPanel } from "@/components/federation/federation-panel";
|
||||
import { MigrationSettings } from "@/components/federation/migration-settings";
|
||||
import { ProfileFieldsEditor } from "@/components/profile-fields-editor";
|
||||
|
||||
export default async function FederationSettingsPage() {
|
||||
const token = (await cookies()).get("auth_token")?.value;
|
||||
@@ -9,6 +11,8 @@ export default async function FederationSettingsPage() {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const me = await getMe(token);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="glass-effect glossy-effect bottom rounded-md shadow-fa-lg p-4">
|
||||
@@ -18,6 +22,7 @@ export default async function FederationSettingsPage() {
|
||||
other instances.
|
||||
</p>
|
||||
</div>
|
||||
<ProfileFieldsEditor initial={me.profileFields} />
|
||||
<FederationPanel />
|
||||
<MigrationSettings />
|
||||
</div>
|
||||
|
||||
@@ -224,6 +224,27 @@ export default async function ProfilePage({ params }: ProfilePageProps) {
|
||||
{user.bio}
|
||||
</p>
|
||||
|
||||
{user.profileFields.length > 0 && (
|
||||
<div className="mt-4 space-y-0 text-sm">
|
||||
{user.profileFields.map((field) => (
|
||||
<div
|
||||
key={field.name}
|
||||
className="grid grid-cols-[minmax(0,5rem)_1fr] gap-2 border-t py-1"
|
||||
>
|
||||
<span
|
||||
className="font-medium text-muted-foreground truncate"
|
||||
title={field.name}
|
||||
>
|
||||
{field.name}
|
||||
</span>
|
||||
<span className="break-all min-w-0">
|
||||
{field.value}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{isOwnProfile && (
|
||||
<div
|
||||
id="profile-card__stats"
|
||||
|
||||
99
thoughts-frontend/components/profile-fields-editor.tsx
Normal file
99
thoughts-frontend/components/profile-fields-editor.tsx
Normal file
@@ -0,0 +1,99 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useAuth } from "@/hooks/use-auth";
|
||||
import { updateProfile, type ProfileField } from "@/lib/api";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { toast } from "sonner";
|
||||
import { Plus, Trash2 } from "lucide-react";
|
||||
|
||||
const MAX_FIELDS = 4;
|
||||
|
||||
export function ProfileFieldsEditor({
|
||||
initial,
|
||||
}: {
|
||||
initial: ProfileField[];
|
||||
}) {
|
||||
const { token } = useAuth();
|
||||
const [fields, setFields] = useState<ProfileField[]>(initial);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const update = (i: number, key: "name" | "value", val: string) => {
|
||||
setFields((prev) => prev.map((f, j) => (j === i ? { ...f, [key]: val } : f)));
|
||||
};
|
||||
|
||||
const add = () => {
|
||||
if (fields.length >= MAX_FIELDS) return;
|
||||
setFields((prev) => [...prev, { name: "", value: "" }]);
|
||||
};
|
||||
|
||||
const remove = (i: number) => {
|
||||
setFields((prev) => prev.filter((_, j) => j !== i));
|
||||
};
|
||||
|
||||
const save = async () => {
|
||||
if (!token) return;
|
||||
const clean = fields.filter((f) => f.name.trim() || f.value.trim());
|
||||
setSaving(true);
|
||||
try {
|
||||
await updateProfile({ profileFields: clean }, token);
|
||||
setFields(clean);
|
||||
toast.success("Profile fields saved.");
|
||||
} catch {
|
||||
toast.error("Failed to save profile fields.");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="glass-effect glossy-effect bottom rounded-md shadow-fa-lg p-4 space-y-3">
|
||||
<div>
|
||||
<h3 className="text-lg font-medium">Profile fields</h3>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Add up to {MAX_FIELDS} custom fields visible on your profile and
|
||||
across the fediverse (e.g. Website, Pronouns).
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
{fields.map((f, i) => (
|
||||
<div key={i} className="flex gap-2 items-center">
|
||||
<Input
|
||||
value={f.name}
|
||||
onChange={(e) => update(i, "name", e.target.value)}
|
||||
placeholder="Label"
|
||||
className="max-w-[8rem] text-sm"
|
||||
/>
|
||||
<Input
|
||||
value={f.value}
|
||||
onChange={(e) => update(i, "value", e.target.value)}
|
||||
placeholder="Value"
|
||||
className="text-sm"
|
||||
/>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={() => remove(i)}
|
||||
className="shrink-0"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className="flex gap-2">
|
||||
{fields.length < MAX_FIELDS && (
|
||||
<Button variant="outline" size="sm" onClick={add}>
|
||||
<Plus className="h-4 w-4 mr-1" /> Add field
|
||||
</Button>
|
||||
)}
|
||||
<Button size="sm" onClick={save} disabled={saving}>
|
||||
{saving ? "Saving…" : "Save"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
import { cache } from "react";
|
||||
import { z } from "zod";
|
||||
|
||||
export const ProfileFieldSchema = z.object({
|
||||
name: z.string(),
|
||||
value: z.string(),
|
||||
});
|
||||
export type ProfileField = z.infer<typeof ProfileFieldSchema>;
|
||||
|
||||
export const UserSchema = z.object({
|
||||
id: z.string().uuid(),
|
||||
username: z.string(),
|
||||
@@ -9,6 +15,7 @@ export const UserSchema = z.object({
|
||||
avatarUrl: z.string().nullable(),
|
||||
headerUrl: z.string().nullable(),
|
||||
customCss: z.string().nullable(),
|
||||
profileFields: z.array(ProfileFieldSchema).default([]),
|
||||
local: z.boolean(),
|
||||
isFollowedByViewer: z.boolean(),
|
||||
joinedAt: z.coerce.date().nullable(),
|
||||
@@ -16,12 +23,6 @@ export const UserSchema = z.object({
|
||||
|
||||
export const MeSchema = UserSchema;
|
||||
|
||||
export const ProfileFieldSchema = z.object({
|
||||
name: z.string(),
|
||||
value: z.string(),
|
||||
});
|
||||
export type ProfileField = z.infer<typeof ProfileFieldSchema>;
|
||||
|
||||
export const RemoteActorSchema = z.object({
|
||||
handle: z.string(),
|
||||
displayName: z.string().nullable(),
|
||||
@@ -77,6 +78,7 @@ export const UpdateProfileSchema = z.object({
|
||||
displayName: z.string().max(50).optional(),
|
||||
bio: z.string().max(4000).optional(),
|
||||
customCss: z.string().optional(),
|
||||
profileFields: z.array(ProfileFieldSchema).max(4).optional(),
|
||||
});
|
||||
|
||||
export const SearchResultsSchema = z.object({
|
||||
|
||||
Reference in New Issue
Block a user