|
| 1 | +import classNames from "classnames"; |
| 2 | +import type React from "react"; |
| 3 | +import { useMemo, useState } from "react"; |
| 4 | + |
| 5 | +import type { Label } from "@/api/domain/label"; |
| 6 | +import { t } from "@/i18n"; |
| 7 | +import type { LabelsDefaultSetting } from "@/settings"; |
| 8 | +import { ObsidianIcon } from "@/ui/components/obsidian-icon"; |
| 9 | +import { PluginContext } from "@/ui/context"; |
| 10 | + |
| 11 | +type Props = { |
| 12 | + value: LabelsDefaultSetting; |
| 13 | + onChange: (val: LabelsDefaultSetting) => Promise<void>; |
| 14 | +}; |
| 15 | + |
| 16 | +export const LabelsControl: React.FC<Props> = ({ value, onChange }) => { |
| 17 | + const [selected, setSelected] = useState(value); |
| 18 | + const plugin = PluginContext.use(); |
| 19 | + const todoist = plugin.services.todoist; |
| 20 | + const i18n = t().settings.taskCreation.defaultLabels; |
| 21 | + |
| 22 | + const labelsById: Map<string, Label> = useMemo(() => { |
| 23 | + if (!todoist.isReady()) { |
| 24 | + return new Map(); |
| 25 | + } |
| 26 | + return new Map(Array.from(todoist.data().labels.iter()).map((label) => [label.id, label])); |
| 27 | + }, [todoist]); |
| 28 | + |
| 29 | + const availableLabels = useMemo(() => { |
| 30 | + const selectedIds = selected.map((l) => l.labelId); |
| 31 | + return Array.from(labelsById.values()).filter((label) => !selectedIds.includes(label.id)); |
| 32 | + }, [labelsById, selected]); |
| 33 | + |
| 34 | + const removeLabel = async (labelId: string) => { |
| 35 | + const newValue = selected.filter((l) => l.labelId !== labelId); |
| 36 | + setSelected(newValue); |
| 37 | + await onChange(newValue); |
| 38 | + }; |
| 39 | + |
| 40 | + const handleAddLabelChange = async (ev: React.ChangeEvent<HTMLSelectElement>) => { |
| 41 | + ev.stopPropagation(); |
| 42 | + ev.preventDefault(); |
| 43 | + |
| 44 | + const labelId = ev.target.value; |
| 45 | + if (labelId === "") { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const label = labelsById.get(labelId); |
| 50 | + if (label === undefined) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const newValue = [...selected, { labelId: label.id, labelName: label.name }]; |
| 55 | + setSelected(newValue); |
| 56 | + await onChange(newValue); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className="labels-control-container"> |
| 61 | + <select |
| 62 | + className="dropdown" |
| 63 | + value="" |
| 64 | + onChange={handleAddLabelChange} |
| 65 | + disabled={availableLabels.length === 0} |
| 66 | + > |
| 67 | + <option value="" disabled> |
| 68 | + {availableLabels.length === 0 ? i18n.buttonNoAvailableLabels : i18n.buttonAddLabel} |
| 69 | + </option> |
| 70 | + {availableLabels.map((label) => ( |
| 71 | + <option key={label.id} value={label.id}> |
| 72 | + {label.name} |
| 73 | + </option> |
| 74 | + ))} |
| 75 | + </select> |
| 76 | + |
| 77 | + <div className="labels-control-list"> |
| 78 | + {selected.length === 0 && <div className="labels-control-empty-state">{i18n.noLabels}</div>} |
| 79 | + {selected.map((labelSetting) => { |
| 80 | + const label = labelsById.get(labelSetting.labelId); |
| 81 | + const isDeleted = label === undefined; |
| 82 | + |
| 83 | + return ( |
| 84 | + <div |
| 85 | + key={labelSetting.labelId} |
| 86 | + className={classNames("labels-control-item", { "is-deleted": isDeleted })} |
| 87 | + > |
| 88 | + <div className="labels-control-item-details"> |
| 89 | + <ObsidianIcon |
| 90 | + size="xs" |
| 91 | + id="lucide-alert-triangle" |
| 92 | + className="label-deleted-warning-icon" |
| 93 | + aria-label={i18n.deletedWarning} |
| 94 | + /> |
| 95 | + <ObsidianIcon |
| 96 | + size="xs" |
| 97 | + id="tag" |
| 98 | + className="label-icon" |
| 99 | + data-label-color={label?.color} |
| 100 | + /> |
| 101 | + <span className="labels-control-item-name"> |
| 102 | + {labelSetting.labelName} |
| 103 | + {isDeleted && ` (${i18n.deleted})`} |
| 104 | + </span> |
| 105 | + </div> |
| 106 | + <button |
| 107 | + type="button" |
| 108 | + className="labels-control-remove-button clickable-icon" |
| 109 | + onClick={() => removeLabel(labelSetting.labelId)} |
| 110 | + > |
| 111 | + <ObsidianIcon size="xs" id="cross" /> |
| 112 | + </button> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + })} |
| 116 | + </div> |
| 117 | + </div> |
| 118 | + ); |
| 119 | +}; |
0 commit comments