Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 2x 2x | import {
Appearance,
AppearanceFields,
appearanceOf,
} from "@/components/appearance/AppearancePicker";
import { BOARD_ICON_REGISTRY } from "@/components/appearance/icons";
import ResourceHistorySection from "@/components/diff/ResourceHistorySection/ResourceHistorySection";
import { Dashboard } from "@/objects/Dashboard";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { ChevronDown, History, X } from "lucide-react";
import React from "react";
export interface BoardSettingsValues {
name: string;
description: string | null;
icon: string | null;
accent: string | null;
}
export interface BoardSettingsProps {
board: Dashboard;
saving: boolean;
error: string | null;
onCancel: () => void;
onSave: (values: BoardSettingsValues) => void;
onDelete?: () => void;
}
const field =
"w-full rounded-lg border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-900 px-2.5 py-1.5 text-xs text-gray-900 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500/40";
/** Rename a board and set how it presents itself. */
const BoardSettings: React.FC<BoardSettingsProps> = ({
board,
saving,
error,
onCancel,
onSave,
onDelete,
}) => {
const [name, setName] = React.useState(board.name);
const [description, setDescription] = React.useState(board.description ?? "");
const [appearance, setAppearance] = React.useState<Appearance>(() =>
appearanceOf(BOARD_ICON_REGISTRY, board.icon, board.accent),
);
const [confirmingDelete, setConfirmingDelete] = React.useState(false);
// Collapsed by default: this modal is primarily for renaming a board, and
// the history is a two-column panel that would dominate it.
const [showHistory, setShowHistory] = React.useState(false);
const { getApiHandler } = useApiHandler();
const trimmed = name.trim();
const values = (): BoardSettingsValues => ({
name: trimmed,
description,
icon: appearance.icon,
accent: appearance.accent,
});
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/40 p-4"
onClick={onCancel}
>
<div
className={`w-full max-h-[85vh] flex flex-col rounded-xl bg-white dark:bg-gray-800 shadow-xl transition-[max-width] ${
showHistory ? "max-w-2xl" : "max-w-md"
}`}
onClick={(e) => e.stopPropagation()}
>
<div className="flex items-center justify-between px-4 py-3 border-b border-gray-100 dark:border-gray-700">
<h3 className="text-sm font-semibold text-gray-900 dark:text-white">
Dashboard settings
</h3>
<button
type="button"
onClick={onCancel}
className="rounded-lg p-1 text-gray-400 hover:bg-gray-100 dark:hover:bg-gray-700"
>
<X className="w-4 h-4" />
</button>
</div>
<div className="flex-1 overflow-auto px-4 py-3 space-y-3">
<div>
<span className="mb-1 block text-xs font-medium text-gray-700 dark:text-gray-200">
Name
</span>
<input
autoFocus
className={field}
value={name}
onChange={(e) => setName(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && trimmed) {
onSave(values());
}
}}
/>
</div>
<div>
<span className="mb-1 block text-xs font-medium text-gray-700 dark:text-gray-200">
Description
</span>
<input
className={field}
placeholder="What this board is for"
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
</div>
<AppearanceFields
registry={BOARD_ICON_REGISTRY}
value={appearance}
onChange={setAppearance}
/>
{error && (
<p className="rounded-lg bg-red-50 dark:bg-red-900/20 px-3 py-2 text-[11px] text-red-600 dark:text-red-400">
{error}
</p>
)}
<div className="pt-2 border-t border-gray-100 dark:border-gray-700">
<button
type="button"
onClick={() => setShowHistory((open) => !open)}
aria-expanded={showHistory}
className="flex w-full items-center gap-1.5 text-xs font-medium text-gray-700 dark:text-gray-200"
>
<ChevronDown
className={`size-3.5 transition-transform ${
showHistory ? "" : "-rotate-90"
}`}
/>
<History className="size-3.5 text-gray-400 dark:text-gray-500" />
Edit history
</button>
{showHistory && (
<ResourceHistorySection
bare
className="mt-3"
source={getApiHandler().dashboard_router}
resourceId={board.id}
queryKey={QUERY_KEYS.GET.DASHBOARD_HISTORY}
fieldLabels={{
name: "Name",
description: "Description",
icon: "Icon",
accent: "Accent",
is_home: "Home board",
position: "Position",
}}
/>
)}
</div>
{onDelete && (
<div className="pt-2 border-t border-gray-100 dark:border-gray-700">
{confirmingDelete ? (
<div className="flex items-center justify-between gap-2">
<span className="text-[11px] text-gray-500 dark:text-gray-400">
Delete this board and its widgets?
</span>
<span className="flex gap-1.5 shrink-0">
<button
type="button"
onClick={() => setConfirmingDelete(false)}
className="rounded-lg px-2 py-1 text-[11px] text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
Keep
</button>
<button
type="button"
onClick={onDelete}
className="rounded-lg bg-red-600 px-2 py-1 text-[11px] font-medium text-white hover:bg-red-700"
>
Delete
</button>
</span>
</div>
) : (
<button
type="button"
onClick={() => setConfirmingDelete(true)}
className="text-[11px] font-medium text-red-600 dark:text-red-400 hover:underline"
>
Delete this dashboard
</button>
)}
</div>
)}
</div>
<div className="flex justify-end gap-2 px-4 py-3 border-t border-gray-100 dark:border-gray-700">
<button
type="button"
onClick={onCancel}
className="rounded-lg px-3 py-1.5 text-xs font-medium text-gray-600 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700"
>
Cancel
</button>
<button
type="button"
disabled={saving || !trimmed}
onClick={() => onSave(values())}
className="rounded-lg bg-gray-900 dark:bg-white px-3 py-1.5 text-xs font-medium text-white dark:text-gray-900 hover:opacity-80 disabled:opacity-50 transition-opacity"
>
{saving ? "Saving…" : "Save"}
</button>
</div>
</div>
</div>
);
};
export default BoardSettings;
|