Skip to content

Build your personal knowledge base with Trilium Notes

7.4 KB
Raw Blame
import { useCallback } from "preact/hooks";

import type FNote from "../../../entities/fnote";
import dialog from "../../../services/dialog";
import { t } from "../../../services/i18n";
import { isMobile } from "../../../services/utils";
import { CollectionPropertiesDialog, IncludedNotesCard } from "../../note_bars/CollectionProperties";
import { Card, CardSection, OptionCardSection } from "../../react/Card";
import FormSelect from "../../react/FormSelect";
import FormToggle from "../../react/FormToggle";
import { useNoteLabelBoolean, useNoteLabelWithDefault } from "../../react/hooks";
import PromotedAttributesCard from "../../react/PromotedAttributesCard";
import TemplateSelectionCard from "../../react/TemplateSelectionCard";
import type { PromotedAttribute } from "../promoted_attributes";
import SortDropdown from "../SortDropdown";
import { parseSortKey } from "../sorting";
import BoardApi from "./api";
import { COLUMN_WIDTH_LABEL, DEFAULT_COLUMN_WIDTH, parseColumnWidth } from "./columns";
import { useBoardSort } from "./sort";

/** The board's settings, other than its columns and cards. */
export default function BoardProperties({ api, note, shown, onClose }: {
    api: BoardApi,
    /** The board note, which is the parent of any template created here. */
    note: FNote,
    shown: boolean,
    onClose: () => void
}) {
    const store = useCallback(
        (templates: string[]) => api.setCardTemplateIds(templates), [ api ]);
    const storeAttributes = useCallback(
        (attributes: PromotedAttribute[]) => api.setPromotedAttributes(attributes), [ api ]);
    const deleteGrouping = useCallback(
        (deletion: () => Promise<void>, type: "label" | "relation", name: string) =>
            api.deleteGrouping(type === "relation" ? `~${name}` : name, deletion),
        [ api ]);

    return (
        <CollectionPropertiesDialog
            className="board-properties-dialog"
            title={t("board_view.properties-title")}
            show={shown}
            onHidden={onClose}
        >
            <General api={api} note={note} />

            <IncludedNotesCard note={note} archivedLabel={t("board_view.show-archived")} />

            <PromotedAttributesCard
                heading={t("board_view.promoted-attributes")}
                instruction={t("board_view.promoted-attributes-hint")}
                note={note}
                settings={api.getStoredPromotedAttributes()}
                drawnByCollection={[ api.statusAttribute ]}
                itemNotes={api.getCardNotes()}
                onChange={storeAttributes}
                runDeletion={deleteGrouping}
            />

            <TemplateSelectionCard
                heading={t("board_view.card-templates")}
                instruction={t("board_view.card-templates-hint")}
                note={note}
                newTemplateName={t("board_view.new-template-name")}
                templates={api.getCardTemplateIds()}
                onChange={store}
            />
        </CollectionPropertiesDialog>
    );
}

/** What the board draws besides its own cards, such as the inbox column, and how. */
function General({ api, note }: { api: BoardApi, note: FNote }) {
    const [ inboxShown ] = useNoteLabelBoolean(note, "board:showInbox");
    const [ addColumnHidden ] = useNoteLabelBoolean(note, "board:hideAddColumn");
    const [ columnWidth ] =
        useNoteLabelWithDefault(note, COLUMN_WIDTH_LABEL, DEFAULT_COLUMN_WIDTH);
    const defaultSort = useBoardSort(note);
    // Every column at once, and a column's own order is not kept anywhere else: the reader is asked
    // before it goes.
    const resetColumnSorts = useCallback(async () => {
        if (await dialog.confirm(t("board_view.reset-column-sorting-confirm"))) {
            await api.resetColumnSortsToDefault();
        }
    }, [ api ]);

    return (
        <Card className="board-properties-general" heading={t("board_view.general")}>
            <OptionCardSection
                name="board-show-inbox"
                label={t("board_view.show-inbox-column")}
                description={t("book_properties_config.board-inbox-column-help")}
            >
                <FormToggle
                    currentValue={inboxShown}
                    onChange={(shown) => api.setInboxEnabled(shown)}
                />
            </OptionCardSection>

            <OptionCardSection
                name="board-show-add-column"
                label={t("board_view.show-add-column-button")}
                description={t("board_view.show-add-column-button-description")}
            >
                <FormToggle
                    currentValue={!addColumnHidden}
                    onChange={(shown) => api.setAddColumnShown(shown)}
                />
            </OptionCardSection>

            {/* `body.mobile` in index.css fixes the column width against the viewport, which is
                what the three widths here would otherwise set. */}
            {!isMobile() && (
                <OptionCardSection
                    name="board-column-width"
                    label={t("board_view.column-width")}
                >
                    <FormSelect
                        values={[
                            { value: "narrow", title: t("board_view.column-width-narrow") },
                            { value: "medium", title: t("board_view.column-width-medium") },
                            { value: "wide", title: t("board_view.column-width-wide") }
                        ]}
                        keyProperty="value"
                        titleProperty="title"
                        currentValue={parseColumnWidth(columnWidth)}
                        onChange={(width) => api.setColumnWidth(parseColumnWidth(width))}
                    />
                </OptionCardSection>
            )}

            <OptionCardSection
                name="board-sort-cards"
                label={t("board_view.default-card-order")}
                description={t("board_view.default-card-order-description")}
                subSectionsVisible
                subSections={
                    <CardSection
                        className="board-sort-reset"
                        href="#"
                        noContainedNavigation
                        onAction={(event) => {
                            // The anchor is what makes the segment read as a link; the address
                            // itself leads nowhere, and `goToLink` would navigate by it.
                            event.preventDefault();
                            event.stopPropagation();
                            void resetColumnSorts();
                        }}
                    >{t("board_view.reset-column-sorting")}</CardSection>
                }
            >
                <SortDropdown
                    className="board-sort-picker"
                    orderBy={defaultSort?.orderBy}
                    isDescending={!!defaultSort?.isDescending}
                    attributes={api.getPromotedAttributes()}
                    noneTitle={t("board_view.sort-manually")}
                    hideDefault
                    // `hideDefault` leaves the menu no DEFAULT_SORT entry, and `parseSortKey`
                    // rejects that key as well: `setDefaultSort` takes a plain sort key.
                    onSelect={(orderBy) => api.setDefaultSort(parseSortKey(orderBy))}
                    onDirectionChange={(descending) => api.setDefaultSortDirection(descending)}
                />
            </OptionCardSection>
        </Card>
    );
}