routing fixes
This commit is contained in:
parent
a490471e4f
commit
0ee9686fb1
2 changed files with 34 additions and 16 deletions
|
|
@ -8,8 +8,8 @@ function readCookie(name: string, cookieHeader: string) {
|
||||||
|
|
||||||
export async function GET(req: Request) {
|
export async function GET(req: Request) {
|
||||||
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
|
const base = process.env.NEXT_PUBLIC_API_BASE_URL!;
|
||||||
// <- add username to the requested fields
|
// include username here
|
||||||
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
const url = `${base}/users/me?fields=id,username,display_name,first_name,last_name,email`;
|
||||||
|
|
||||||
const cookieHeader = req.headers.get("cookie") ?? "";
|
const cookieHeader = req.headers.get("cookie") ?? "";
|
||||||
const ma_at = readCookie("ma_at", cookieHeader);
|
const ma_at = readCookie("ma_at", cookieHeader);
|
||||||
|
|
@ -18,12 +18,14 @@ export async function GET(req: Request) {
|
||||||
if (cookieHeader) headers.cookie = cookieHeader;
|
if (cookieHeader) headers.cookie = cookieHeader;
|
||||||
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
|
if (ma_at) headers.authorization = `Bearer ${ma_at}`;
|
||||||
|
|
||||||
const res = await fetch(url, { headers, cache: "no-store" });
|
const res = await fetch(url, { headers, cache: "no-store" });
|
||||||
const text = await res.text().catch(() => "");
|
const body = await res.json().catch(() => ({}));
|
||||||
let body: any; try { body = text ? JSON.parse(text) : {}; } catch { body = {}; }
|
|
||||||
|
|
||||||
return new NextResponse(JSON.stringify(body), {
|
return new NextResponse(JSON.stringify(body), {
|
||||||
status: res.status,
|
status: res.status,
|
||||||
headers: { "content-type": "application/json", "cache-control": "no-store" },
|
headers: {
|
||||||
|
"content-type": "application/json",
|
||||||
|
"cache-control": "no-store",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,16 +33,33 @@ function useOptions(path: string) {
|
||||||
.then((r) => r.json())
|
.then((r) => r.json())
|
||||||
.then((j) => {
|
.then((j) => {
|
||||||
if (!alive) return;
|
if (!alive) return;
|
||||||
|
|
||||||
const raw = (j?.data ?? j) as any[];
|
const raw = (j?.data ?? j) as any[];
|
||||||
|
|
||||||
const normalized: Opt[] = Array.isArray(raw)
|
const normalized: Opt[] = Array.isArray(raw)
|
||||||
? raw
|
? raw
|
||||||
.map((x) => ({
|
.map((x) => {
|
||||||
id: String(x?.id ?? x?.value ?? x?.key ?? ""),
|
const id = String(x?.id ?? x?.value ?? x?.key ?? "").trim();
|
||||||
// recognize common label fields + 'opacity'
|
|
||||||
label: String(x?.label ?? x?.name ?? x?.title ?? x?.text ?? x?.opacity ?? ""),
|
// Accept common label fields + "opacity" (string field)
|
||||||
}))
|
let label =
|
||||||
|
(x?.label ??
|
||||||
|
x?.name ??
|
||||||
|
x?.title ??
|
||||||
|
x?.text ??
|
||||||
|
x?.opacity) as string | undefined;
|
||||||
|
|
||||||
|
// Nice fallback for sources where you have make/model
|
||||||
|
if (!label && (x?.make || x?.model)) {
|
||||||
|
label = [x.make, x.model].filter(Boolean).join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
label = String(label ?? "").trim();
|
||||||
|
return { id, label };
|
||||||
|
})
|
||||||
.filter((o) => o.id && o.label)
|
.filter((o) => o.id && o.label)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
setOpts(normalized);
|
setOpts(normalized);
|
||||||
})
|
})
|
||||||
.finally(() => alive && setLoading(false));
|
.finally(() => alive && setLoading(false));
|
||||||
|
|
@ -168,13 +185,12 @@ export default function SettingsSubmit({ initialTarget }: { initialTarget?: Targ
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Prefer username; then display_name; then full name; then email.
|
|
||||||
const meLabel =
|
const meLabel =
|
||||||
(me?.username && me.username.trim()) ||
|
(me?.username?.trim()) ||
|
||||||
(me?.display_name && me.display_name.trim()) ||
|
(me?.email?.trim()) ||
|
||||||
([me?.first_name, me?.last_name].filter(Boolean).join(" ").trim()) ||
|
([me?.first_name, me?.last_name].filter(Boolean).join(" ").trim()) ||
|
||||||
(me?.email && me.email.trim()) ||
|
(me?.display_name?.trim()) ||
|
||||||
"";
|
(me?.id ? `User ${me.id.slice(0, 8)}…${me.id.slice(-4)}` : "Unknown user");
|
||||||
|
|
||||||
|
|
||||||
// Options
|
// Options
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue