feat: add breath colors
This commit is contained in:
@@ -26,6 +26,29 @@ interface BreathingTemplate {
|
||||
duration?: number;
|
||||
description?: string;
|
||||
image_id?: number;
|
||||
breathing_color_id?: number | null;
|
||||
breathing_color?: BreathingColor | null;
|
||||
}
|
||||
|
||||
interface BreathingColor {
|
||||
id: number;
|
||||
name?: string | null;
|
||||
colors: string[];
|
||||
}
|
||||
|
||||
// "#AARRGGBB" (ARGB) or "#RRGGBB" → a CSS color string.
|
||||
function cssColor(c?: string): string {
|
||||
if (!c) return "transparent";
|
||||
const hex = c.replace("#", "");
|
||||
if (hex.length === 8) return `#${hex.slice(2)}${hex.slice(0, 2)}`; // ARGB→RGB(drop alpha)
|
||||
return `#${hex}`;
|
||||
}
|
||||
|
||||
// Linear gradient (or solid) preview for a list of colors.
|
||||
function gradientStyle(colors: string[]): React.CSSProperties {
|
||||
const cs = (colors.length ? colors : ["#00000000"]).map(cssColor);
|
||||
if (cs.length === 1) return { background: cs[0] };
|
||||
return { background: `linear-gradient(135deg, ${cs.join(", ")})` };
|
||||
}
|
||||
|
||||
interface BreathingForm {
|
||||
@@ -57,11 +80,13 @@ function numOrUndefined(v: string): number | undefined {
|
||||
export default function BreathingPage() {
|
||||
const { data, loading, error, reload } =
|
||||
useList<BreathingTemplate>("/breathing-templates");
|
||||
const { data: palette } = useList<BreathingColor>("/breathing-colors");
|
||||
const toast = useToast();
|
||||
|
||||
const [editing, setEditing] = useState<BreathingTemplate | null>(null);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [form, setForm] = useState<BreathingForm>(emptyForm);
|
||||
const [breathingColorId, setBreathingColorId] = useState<number | null>(null);
|
||||
const [saving, setSaving] = useState(false);
|
||||
|
||||
const [deleting, setDeleting] = useState<BreathingTemplate | null>(null);
|
||||
@@ -74,6 +99,7 @@ export default function BreathingPage() {
|
||||
function openCreate() {
|
||||
setEditing(null);
|
||||
setForm(emptyForm);
|
||||
setBreathingColorId(null);
|
||||
setOpen(true);
|
||||
}
|
||||
function openEdit(row: BreathingTemplate) {
|
||||
@@ -87,6 +113,7 @@ export default function BreathingPage() {
|
||||
description: row.description ?? "",
|
||||
image_id: row.image_id != null ? String(row.image_id) : "",
|
||||
});
|
||||
setBreathingColorId(row.breathing_color_id ?? null);
|
||||
setOpen(true);
|
||||
}
|
||||
|
||||
@@ -102,6 +129,7 @@ export default function BreathingPage() {
|
||||
duration: numOrUndefined(form.duration),
|
||||
description: form.description,
|
||||
image_id: numOrUndefined(form.image_id),
|
||||
breathing_color_id: breathingColorId ?? undefined,
|
||||
};
|
||||
if (editing) {
|
||||
// Update is JSON on /breathing-templates/:id
|
||||
@@ -167,6 +195,21 @@ export default function BreathingPage() {
|
||||
header: "مدت زمان",
|
||||
render: (r) => (r.duration != null ? formatDuration(r.duration) : "—"),
|
||||
},
|
||||
{
|
||||
key: "color",
|
||||
header: "رنگ",
|
||||
className: "w-20",
|
||||
render: (r) =>
|
||||
r.breathing_color?.colors?.length ? (
|
||||
<span
|
||||
className="inline-block h-6 w-10 rounded-md border border-border"
|
||||
style={gradientStyle(r.breathing_color.colors)}
|
||||
title={r.breathing_color.name ?? ""}
|
||||
/>
|
||||
) : (
|
||||
"—"
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
@@ -288,6 +331,44 @@ export default function BreathingPage() {
|
||||
placeholder="توضیح کوتاه درباره قالب"
|
||||
/>
|
||||
</Field>
|
||||
|
||||
<Field
|
||||
label="رنگ تمرین"
|
||||
hint={
|
||||
palette.length
|
||||
? "یک رنگ از پالت انتخاب کنید (در «رنگهای تنفس» مدیریت میشود)."
|
||||
: "هنوز رنگی در «رنگهای تنفس» تعریف نشده است."
|
||||
}
|
||||
>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
{/* "no color" option */}
|
||||
<button
|
||||
type="button"
|
||||
title="بدون رنگ"
|
||||
onClick={() => setBreathingColorId(null)}
|
||||
className={`flex h-9 w-9 items-center justify-center rounded-full border-2 text-muted ${
|
||||
breathingColorId === null ? "border-primary ring-2 ring-primary/40" : "border-border"
|
||||
}`}
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
{palette.map((p) => {
|
||||
const selected = breathingColorId === p.id;
|
||||
return (
|
||||
<button
|
||||
key={p.id}
|
||||
type="button"
|
||||
title={p.name ?? ""}
|
||||
onClick={() => setBreathingColorId(p.id)}
|
||||
style={gradientStyle(p.colors)}
|
||||
className={`h-9 w-9 rounded-full border-2 transition ${
|
||||
selected ? "border-primary ring-2 ring-primary/40" : "border-border"
|
||||
}`}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</Field>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user