mirror of
https://github.com/temetro/temetro.git
synced 2026-08-11 11:09:04 +00:00
d237504af9
Add three UI locales (so/ar/de) with full ~1,660-key translations alongside en/fr, selectable in Settings → Profile. Arabic gets full right-to-left support: - config.ts registers the locales and exports a `dirFor` helper; an inline <head> script in layout.tsx sets <html dir/lang> before first paint (no RTL flash), and i18n-provider keeps them in sync on language change. - ~160 physical direction utilities converted to logical (ms/me/ps/pe/ start/end/text-start/text-end); directional chevrons/arrows get rtl:rotate-180; chat-bubble align variants fixed to logical. - IBM Plex Sans Arabic appended to the sans/heading font stacks for per-character Arabic fallback. - Language persists to the backend user_settings and re-applies on sign-in so it roams across devices (localStorage stays the offline source of truth). - New scripts/check-locales.mjs (npm run check-locales) enforces key/placeholder parity and Arabic CLDR plural completeness. Bump to 0.3.0. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
274 lines
8.6 KiB
TypeScript
274 lines
8.6 KiB
TypeScript
"use client";
|
|
|
|
import { Info, UserPlus } from "lucide-react";
|
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { AddStaffDialog } from "@/components/settings/add-staff-dialog";
|
|
import {
|
|
EmployeeDetailDialog,
|
|
type StaffMember,
|
|
} from "@/components/settings/employee-detail-dialog";
|
|
import { specialtyLabel } from "@/lib/staff";
|
|
import {
|
|
SettingsCard,
|
|
SettingsSection,
|
|
} from "@/components/settings/settings-parts";
|
|
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogClose,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogPopup,
|
|
DialogTitle,
|
|
} from "@/components/ui/dialog";
|
|
import { ROLE_LABELS } from "@/lib/access";
|
|
import { apiFetch } from "@/lib/api-client";
|
|
import { authClient } from "@/lib/auth-client";
|
|
import { notify } from "@/lib/toast";
|
|
|
|
function roleLabel(role?: string | null): string {
|
|
if (!role) return ROLE_LABELS.member;
|
|
return (ROLE_LABELS as Record<string, string>)[role] ?? role;
|
|
}
|
|
|
|
function initials(name?: string | null, email?: string | null): string {
|
|
const source = name?.trim() || email?.trim() || "?";
|
|
return (
|
|
source
|
|
.split(/\s+/)
|
|
.map((w) => w[0])
|
|
.filter(Boolean)
|
|
.join("")
|
|
.slice(0, 2)
|
|
.toUpperCase() || "?"
|
|
);
|
|
}
|
|
|
|
export function CareTeamPanel({
|
|
initialMemberId,
|
|
}: {
|
|
initialMemberId?: string;
|
|
}) {
|
|
const { t } = useTranslation();
|
|
const { data: session } = authClient.useSession();
|
|
const [members, setMembers] = useState<StaffMember[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [adding, setAdding] = useState(false);
|
|
const [selected, setSelected] = useState<StaffMember | null>(null);
|
|
const [pendingRemove, setPendingRemove] = useState<StaffMember | null>(null);
|
|
const [removing, setRemoving] = useState(false);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
const data = await apiFetch<StaffMember[]>("/api/staff");
|
|
setMembers(data);
|
|
setError(null);
|
|
} catch (err) {
|
|
setError(
|
|
err instanceof Error ? err.message : t("settings.careTeam.loadError"),
|
|
);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void load();
|
|
}, [load]);
|
|
|
|
// Deep-link: open a specific member (e.g. from a password-reset system card).
|
|
const appliedDeepLink = useRef(false);
|
|
useEffect(() => {
|
|
if (appliedDeepLink.current || !initialMemberId || members.length === 0)
|
|
return;
|
|
const target = members.find((m) => m.userId === initialMemberId);
|
|
if (target) {
|
|
setSelected(target);
|
|
appliedDeepLink.current = true;
|
|
}
|
|
}, [initialMemberId, members]);
|
|
|
|
const myRole = members.find((m) => m.userId === session?.user?.id)?.role;
|
|
const canManage = myRole === "owner" || myRole === "admin";
|
|
|
|
const confirmRemove = async () => {
|
|
if (!pendingRemove || removing) return;
|
|
setRemoving(true);
|
|
const { error: err } = await authClient.organization.removeMember({
|
|
memberIdOrEmail: pendingRemove.id,
|
|
});
|
|
setRemoving(false);
|
|
if (err) {
|
|
notify.error(
|
|
t("settings.careTeam.remove.failedTitle"),
|
|
err.message ?? t("settings.careTeam.remove.failedBody"),
|
|
);
|
|
return;
|
|
}
|
|
notify.success(
|
|
t("settings.careTeam.remove.removedTitle"),
|
|
t("settings.careTeam.remove.removedBody", {
|
|
name: pendingRemove.name ?? pendingRemove.email ?? "",
|
|
}),
|
|
);
|
|
setPendingRemove(null);
|
|
void load();
|
|
};
|
|
|
|
return (
|
|
<SettingsSection
|
|
description={t("settings.careTeam.description")}
|
|
title={t("settings.careTeam.title")}
|
|
>
|
|
{error && (
|
|
<p className="rounded-2xl bg-destructive/10 px-3 py-2 text-sm text-destructive">
|
|
{error}
|
|
</p>
|
|
)}
|
|
|
|
{canManage && (
|
|
<div className="flex items-center justify-between gap-4">
|
|
<p className="flex items-center gap-1.5 text-xs text-muted-foreground">
|
|
<Info className="size-3.5 shrink-0" />
|
|
{t("settings.careTeam.clickHint")}
|
|
</p>
|
|
<Button onClick={() => setAdding(true)} type="button">
|
|
<UserPlus className="size-4" />
|
|
{t("settings.careTeam.addMember")}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
|
|
<SettingsCard className="divide-y divide-border">
|
|
{loading ? (
|
|
<p className="p-6 text-center text-sm text-muted-foreground">
|
|
{t("settings.careTeam.loading")}
|
|
</p>
|
|
) : (
|
|
members.map((m) => {
|
|
const isSelf = m.userId === session?.user?.id;
|
|
// Prefer the login username; fall back to email for owners who
|
|
// signed up by email.
|
|
const secondary = m.username ? `@${m.username}` : m.email;
|
|
const body = (
|
|
<>
|
|
<Avatar className="size-8">
|
|
<AvatarFallback>
|
|
{initials(m.name, m.email)}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="min-w-0 flex-1 text-start">
|
|
<p className="truncate text-sm font-medium">
|
|
{m.name || m.email || m.userId}
|
|
{isSelf && (
|
|
<span className="ms-1 text-xs text-muted-foreground">
|
|
{t("settings.careTeam.you")}
|
|
</span>
|
|
)}
|
|
</p>
|
|
{secondary && (
|
|
<p className="truncate text-xs text-muted-foreground">
|
|
{secondary}
|
|
</p>
|
|
)}
|
|
</div>
|
|
{specialtyLabel(t, m.specialty) && (
|
|
<Badge variant="outline">
|
|
{specialtyLabel(t, m.specialty)}
|
|
</Badge>
|
|
)}
|
|
<Badge className="capitalize" variant="secondary">
|
|
{roleLabel(m.role)}
|
|
</Badge>
|
|
</>
|
|
);
|
|
// Admins click a row to open the employee detail dialog (view
|
|
// permissions, change role, remove). Non-managers see a static row.
|
|
return canManage ? (
|
|
<button
|
|
className="flex w-full items-center gap-3 px-4 py-3 transition-colors hover:bg-accent/50"
|
|
key={m.id}
|
|
onClick={() => setSelected(m)}
|
|
type="button"
|
|
>
|
|
{body}
|
|
</button>
|
|
) : (
|
|
<div className="flex items-center gap-3 px-4 py-3" key={m.id}>
|
|
{body}
|
|
</div>
|
|
);
|
|
})
|
|
)}
|
|
</SettingsCard>
|
|
|
|
{canManage && (
|
|
<AddStaffDialog
|
|
onCreated={() => void load()}
|
|
onOpenChange={setAdding}
|
|
open={adding}
|
|
/>
|
|
)}
|
|
|
|
{/* Click a member to view details, change role, or remove. */}
|
|
<EmployeeDetailDialog
|
|
editable={
|
|
canManage &&
|
|
selected?.userId !== session?.user?.id &&
|
|
selected?.role !== "owner"
|
|
}
|
|
member={selected}
|
|
onChanged={() => void load()}
|
|
onOpenChange={(o) => !o && setSelected(null)}
|
|
onRemove={(m) => {
|
|
setSelected(null);
|
|
setPendingRemove(m);
|
|
}}
|
|
open={selected !== null}
|
|
/>
|
|
|
|
{/* Confirm before removing a member — destructive and not reversible. */}
|
|
<Dialog
|
|
onOpenChange={(o) => !o && setPendingRemove(null)}
|
|
open={pendingRemove !== null}
|
|
>
|
|
<DialogPopup className="sm:max-w-sm">
|
|
<DialogHeader>
|
|
<DialogTitle>{t("settings.careTeam.remove.title")}</DialogTitle>
|
|
<DialogDescription>
|
|
{t("settings.careTeam.remove.description", {
|
|
name:
|
|
pendingRemove?.name ??
|
|
pendingRemove?.email ??
|
|
t("settings.careTeam.remove.thisMember"),
|
|
})}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogFooter>
|
|
<DialogClose render={<Button type="button" variant="outline" />}>
|
|
{t("settings.careTeam.remove.cancel")}
|
|
</DialogClose>
|
|
<Button
|
|
disabled={removing}
|
|
onClick={confirmRemove}
|
|
type="button"
|
|
variant="destructive"
|
|
>
|
|
{removing
|
|
? t("settings.careTeam.remove.removing")
|
|
: t("settings.careTeam.remove.confirm")}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogPopup>
|
|
</Dialog>
|
|
</SettingsSection>
|
|
);
|
|
}
|