Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
28.0 KB
Raw Blame
"use client";

import { useEffect, useMemo, useState, type ReactNode } from "react";
import { useQuery } from "@tanstack/react-query";
import {
  Archive,
  ArchiveRestore,
  GripVertical,
  MoreHorizontal,
  Pencil,
  Plus,
  Search,
  SlidersHorizontal,
  X,
} from "@multica/ui/icons";
import { toast } from "sonner";
import { useAuthStore } from "@multica/core/auth";
import { useWorkspaceId } from "@multica/core/hooks";
import { memberListOptions } from "@multica/core/workspace/queries";
import {
  propertyUsageListOptions,
  useCreateProperty,
  useUpdateProperty,
} from "@multica/core/properties";
import type {
  IssueProperty,
  IssuePropertyOption,
  IssuePropertyType,
} from "@multica/core/types";
import { ISSUE_PROPERTY_TYPES, MAX_ISSUE_PROPERTY_SELECT_OPTIONS } from "@multica/core/types";
import { Button } from "@multica/ui/components/ui/button";
import { IconButton } from "@multica/ui/components/common/icon-button";
import { Badge } from "@multica/ui/components/ui/badge";
import { Input } from "@multica/ui/components/ui/input";
import { Textarea } from "@multica/ui/components/ui/textarea";
import { Label as FieldLabel } from "@multica/ui/components/ui/label";
import { Switch } from "@multica/ui/components/ui/switch";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@multica/ui/components/ui/select";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@multica/ui/components/ui/dialog";
import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@multica/ui/components/ui/alert-dialog";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@multica/ui/components/ui/dropdown-menu";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@multica/ui/components/ui/popover";
import { ColorPicker, COLOR_PICKER_PRESETS } from "../../common/color-picker";
import {
  PropertyIcon,
  PropertyIconGlyph,
  PropertyIconPicker,
} from "../../common/property-icon";
import { useLocale, useT } from "../../i18n";
import { SettingsReadOnlyNotice, SettingsTab } from "./settings-layout";
import { CatalogHeader, CatalogRow, CatalogRows, CatalogTable } from "./catalog-table";
import { cn } from "@multica/ui/lib/utils";
import { paletteColor } from "@multica/core/appearance";

const MAX_ACTIVE_PROPERTIES = 20;

interface OptionDraft {
  id?: string;
  name: string;
  color: string;
}

interface PropertyDraft {
  name: string;
  type: IssuePropertyType;
  description: string;
  icon: string;
  options: OptionDraft[];
}

const EMPTY_DRAFT: PropertyDraft = {
  name: "",
  type: "select",
  description: "",
  icon: "",
  options: [{ name: "", color: COLOR_PICKER_PRESETS[6] }],
};

function typeHasOptions(type: string): boolean {
  return type === "select" || type === "multi_select";
}

export function PropertiesTab() {
  const { t } = useT("settings");
  const locale = useLocale();
  const wsId = useWorkspaceId();
  const user = useAuthStore((s) => s.user);

  const [query, setQuery] = useState("");
  const [showArchived, setShowArchived] = useState(false);
  const [editing, setEditing] = useState<IssueProperty | null>(null);
  const [createOpen, setCreateOpen] = useState(false);
  const [pendingArchive, setPendingArchive] = useState<IssueProperty | null>(null);

  const { data: members = [] } = useQuery(memberListOptions(wsId));
  const currentMember = members.find((m) => m.user_id === user?.id) ?? null;
  const canManage = currentMember?.role === "owner" || currentMember?.role === "admin";

  const { data: properties = [], isLoading } = useQuery(propertyUsageListOptions(wsId));
  const update = useUpdateProperty();

  const activeCount = useMemo(
    () => properties.filter((p) => !p.archived).length,
    [properties],
  );
  const visible = useMemo(() => {
    const normalized = query.trim().toLowerCase();
    return properties
      .filter((p) => (showArchived ? true : !p.archived))
      .filter((p) => !normalized || p.name.toLowerCase().includes(normalized));
  }, [properties, query, showArchived]);

  return (
    <SettingsTab
      title={t(($) => $.properties.title)}
      scope="workspace"
    >
      {currentMember && !canManage ? <SettingsReadOnlyNotice wsId={wsId} /> : null}
      <div className="space-y-5">
        <div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
          <div className="relative w-full sm:max-w-sm">
            <Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
            <Input
              value={query}
              onChange={(event) => setQuery(event.target.value)}
              placeholder={t(($) => $.properties.search_placeholder)}
              className="pl-9"
            />
          </div>
          <div className="flex items-center gap-4">
            <label className="flex items-center gap-2 text-caption text-muted-foreground">
              <Switch checked={showArchived} onCheckedChange={setShowArchived} />
              {t(($) => $.properties.show_archived)}
            </label>
            <span className="text-caption tabular-nums text-muted-foreground">
              {t(($) => $.properties.limit_hint, {
                count: activeCount,
                max: MAX_ACTIVE_PROPERTIES,
              })}
            </span>
            {canManage && (
              <Button
                className="gap-2"
                onClick={() => setCreateOpen(true)}
                disabled={activeCount >= MAX_ACTIVE_PROPERTIES}
              >
                <Plus className="size-4" />
                {t(($) => $.properties.new_property)}
              </Button>
            )}
          </div>
        </div>

        {!canManage && (
          <p className="text-caption text-muted-foreground">
            {t(($) => $.properties.editor.admin_hint)}
          </p>
        )}

        <CatalogTable columns="minmax(10rem,1fr) 6rem minmax(10rem,1.4fr) 6rem 7rem">
          <CatalogHeader>
            <span>{t(($) => $.properties.columns.name)}</span>
            <span>{t(($) => $.properties.columns.type)}</span>
            <span>{t(($) => $.properties.columns.options)}</span>
            <span>{t(($) => $.properties.columns.usage)}</span>
            <span>{t(($) => $.properties.columns.updated)}</span>
          </CatalogHeader>

          {isLoading ? (
            <div className="px-4 py-12 text-center text-body text-muted-foreground">
              {t(($) => $.properties.loading)}
            </div>
          ) : visible.length === 0 ? (
            <div className="px-4 py-12 text-center">
              <SlidersHorizontal className="mx-auto size-6 text-faint-foreground" />
              <p className="mt-3 text-body font-medium">
                {query
                  ? t(($) => $.properties.no_results)
                  : t(($) => $.properties.empty)}
              </p>
              {!query && (
                <p className="mx-auto mt-1 max-w-sm text-caption text-muted-foreground">
                  {t(($) => $.properties.empty_hint)}
                </p>
              )}
            </div>
          ) : (
            <CatalogRows>
              {visible.map((property) => (
                <CatalogRow
                  key={property.id}
                  name={
                    <div className="flex min-w-0 items-center gap-2">
                      <PropertyIcon property={property} />
                      <span className="truncate text-body font-medium">{property.name}</span>
                      {property.archived && (
                        <Badge variant="outline" className="shrink-0 text-micro">
                          {t(($) => $.properties.archived_badge)}
                        </Badge>
                      )}
                    </div>
                  }
                  meta={
                    <>
                      <span>
                        <PropertyTypeLabel type={property.type} />
                      </span>
                      <PropertyOptionChips property={property} />
                      <span className="tabular-nums">
                        {t(($) => $.properties.usage_count, { count: property.usage_count ?? 0 })}
                      </span>
                      <span className="text-caption">
                        {new Date(property.updated_at).toLocaleDateString(locale)}
                      </span>
                    </>
                  }
                  menu={
                    canManage ? (
                      <DropdownMenu>
                        <DropdownMenuTrigger
                          render={
                            <IconButton
                              variant="ghost"
                              size="icon-sm"
                              label={t(($) => $.properties.actions.open, { name: property.name })}
                            >
                              <MoreHorizontal className="size-4" />
                            </IconButton>
                          }
                        />
                        <DropdownMenuContent align="end">
                          <DropdownMenuItem onClick={() => setEditing(property)}>
                            <Pencil className="size-4" />
                            {t(($) => $.properties.actions.edit)}
                          </DropdownMenuItem>
                          {property.archived ? (
                            <DropdownMenuItem
                              onClick={() =>
                                update.mutate(
                                  { id: property.id, archived: false },
                                  {
                                    onError: (error) =>
                                      toast.error(
                                        error instanceof Error
                                          ? error.message
                                          : t(($) => $.properties.save_failed),
                                      ),
                                  },
                                )
                              }
                            >
                              <ArchiveRestore className="size-4" />
                              {t(($) => $.properties.actions.unarchive)}
                            </DropdownMenuItem>
                          ) : (
                            <DropdownMenuItem
                              variant="destructive"
                              onClick={() => setPendingArchive(property)}
                            >
                              <Archive className="size-4" />
                              {t(($) => $.properties.actions.archive)}
                            </DropdownMenuItem>
                          )}
                        </DropdownMenuContent>
                      </DropdownMenu>
                    ) : null
                  }
                />
              ))}
            </CatalogRows>
          )}
        </CatalogTable>
      </div>

      <PropertyEditorDialog open={createOpen} onOpenChange={setCreateOpen} />
      <PropertyEditorDialog
        open={Boolean(editing)}
        onOpenChange={(open) => !open && setEditing(null)}
        property={editing}
      />
      <ArchivePropertyDialog
        property={pendingArchive}
        onClose={() => setPendingArchive(null)}
      />
    </SettingsTab>
  );
}

const WIDE_OPTION_CHIPS = 6;
const NARROW_OPTION_CHIPS = 3;

function OptionChip({
  option,
  className,
  children,
}: {
  option: IssuePropertyOption;
  className?: string;
  children?: ReactNode;
}) {
  return (
    <span
      className={cn(
        "inline-flex items-center gap-1.5 rounded-full border border-surface-border px-2 py-0.5 text-caption text-foreground",
        className,
      )}
    >
      <span
        className="size-2 shrink-0 rounded-full"
        style={{ backgroundColor: paletteColor(option.color) }}
      />
      {children ?? option.name}
    </span>
  );
}

/**
 * The Options column. When the catalog is narrow the chips take their own
 * line under the rest of the metadata, show fewer, and truncate instead of
 * wrapping, so a select with many options still fits in three lines. The cell
 * opens a read-only list of every option, so what the row hides or cuts off
 * stays reachable by touch and keyboard for members without the edit menu.
 */
function PropertyOptionChips({ property }: { property: IssueProperty }) {
  const { t } = useT("settings");
  if (!typeHasOptions(property.type)) {
    return <span className="hidden text-caption @3xl/catalog:block">—</span>;
  }
  const options = property.config.options ?? [];
  if (options.length === 0) return <div />;
  const narrowRest = options.length - NARROW_OPTION_CHIPS;
  const wideRest = options.length - WIDE_OPTION_CHIPS;
  return (
    <Popover>
      <PopoverTrigger
        aria-label={t(($) => $.properties.options_preview, { name: property.name })}
        className="order-last flex min-w-0 basis-full cursor-pointer items-center gap-1 overflow-hidden rounded-md text-left outline-none focus-visible:ring-2 focus-visible:ring-ring @3xl/catalog:order-none @3xl/catalog:flex-wrap @3xl/catalog:overflow-visible"
      >
        {options.slice(0, WIDE_OPTION_CHIPS).map((option, index) => (
          <OptionChip
            key={option.id}
            option={option}
            className={cn(
              "min-w-0 @3xl/catalog:min-w-auto",
              index >= NARROW_OPTION_CHIPS && "hidden @3xl/catalog:inline-flex",
            )}
          >
            <span className="truncate @3xl/catalog:overflow-visible @3xl/catalog:whitespace-normal">
              {option.name}
            </span>
          </OptionChip>
        ))}
        {narrowRest > 0 && (
          <span className="shrink-0 text-caption @3xl/catalog:hidden">+{narrowRest}</span>
        )}
        {wideRest > 0 && (
          <span className="hidden text-caption @3xl/catalog:inline">+{wideRest}</span>
        )}
      </PopoverTrigger>
      <PopoverContent
        align="start"
        className="max-h-[min(24rem,var(--available-height))] w-auto max-w-72 overflow-y-auto p-2"
      >
        <div className="flex flex-wrap gap-1">
          {options.map((option) => (
            <OptionChip key={option.id} option={option} />
          ))}
        </div>
      </PopoverContent>
    </Popover>
  );
}

export function PropertyTypeLabel({ type }: { type: string }) {
  const { t } = useT("settings");
  switch (type) {
    case "text":
      return <>{t(($) => $.properties.types.text)}</>;
    case "number":
      return <>{t(($) => $.properties.types.number)}</>;
    case "select":
      return <>{t(($) => $.properties.types.select)}</>;
    case "multi_select":
      return <>{t(($) => $.properties.types.multi_select)}</>;
    case "date":
      return <>{t(($) => $.properties.types.date)}</>;
    case "checkbox":
      return <>{t(($) => $.properties.types.checkbox)}</>;
    case "url":
      return <>{t(($) => $.properties.types.url)}</>;
    case "actor":
      return <>{t(($) => $.properties.types.actor)}</>;
    case "multi_actor":
      return <>{t(($) => $.properties.types.multi_actor)}</>;
    case "multi_text":
      return <>{t(($) => $.properties.types.multi_text)}</>;
    case "multi_url":
      return <>{t(($) => $.properties.types.multi_url)}</>;
    default:
      // Forward compat: newer servers may ship types this build doesn't know.
      return <>{type}</>;
  }
}

function PropertyEditorDialog({
  open,
  onOpenChange,
  property,
}: {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  property?: IssueProperty | null;
}) {
  const { t } = useT("settings");
  const create = useCreateProperty();
  const update = useUpdateProperty();
  const [draft, setDraft] = useState<PropertyDraft>(EMPTY_DRAFT);
  const [iconPickerOpen, setIconPickerOpen] = useState(false);

  useEffect(() => {
    if (!open) return;
    setIconPickerOpen(false);
    setDraft(
      property
        ? {
            name: property.name,
            type: (property.type as IssuePropertyType) ?? "text",
            description: property.description ?? "",
            icon: property.icon ?? "",
            options: (property.config.options ?? []).map((option: IssuePropertyOption) => ({
              id: option.id,
              name: option.name,
              color: option.color,
            })),
          }
        : EMPTY_DRAFT,
    );
  }, [property, open]);

  const showOptions = typeHasOptions(draft.type);
  const validOptions = draft.options.filter((option) => option.name.trim());
  const propertyTypeItems = ISSUE_PROPERTY_TYPES.map((type) => ({
    value: type,
    label: <PropertyTypeLabel type={type} />,
  }));
  const canSubmit =
    draft.name.trim().length > 0 && (!showOptions || validOptions.length > 0);

  const submit = () => {
    if (!canSubmit) return;
    const config = showOptions
      ? {
          options: validOptions.map((option) => ({
            id: option.id ?? "",
            name: option.name.trim(),
            color: option.color,
          })),
        }
      : undefined;
    const onError = (error: unknown) =>
      toast.error(
        error instanceof Error ? error.message : t(($) => $.properties.save_failed),
      );
    if (property) {
      update.mutate(
        {
          id: property.id,
          name: draft.name.trim(),
          description: draft.description.trim(),
          icon: draft.icon,
          ...(config ? { config } : {}),
        },
        { onSuccess: () => onOpenChange(false), onError },
      );
      return;
    }
    create.mutate(
      {
        name: draft.name.trim(),
        type: draft.type,
        description: draft.description.trim(),
        icon: draft.icon,
        ...(config ? { config } : {}),
      },
      { onSuccess: () => onOpenChange(false), onError },
    );
  };

  const setOption = (index: number, patch: Partial<OptionDraft>) => {
    setDraft((current) => ({
      ...current,
      options: current.options.map((option, i) =>
        i === index ? { ...option, ...patch } : option,
      ),
    }));
  };

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      {/* Options are unbounded, so the form is the part that has to give:
          header and footer stay put and the fields scroll, keeping "Save
          property" clickable no matter how many options are on the draft. */}
      <DialogContent className="flex max-h-[85%] flex-col sm:max-w-lg">
        <DialogHeader>
          <DialogTitle>
            {property
              ? t(($) => $.properties.editor.edit_title)
              : t(($) => $.properties.editor.create_title)}
          </DialogTitle>
          <DialogDescription>
            {t(($) => $.properties.editor.admin_hint)}
          </DialogDescription>
        </DialogHeader>
        {/* -mx-1/px-1 keeps the 3px focus ring of an edge-to-edge input from
            being clipped by the scroll container. */}
        <div className="-mx-1 min-h-0 flex-1 space-y-5 overflow-y-auto px-1 py-2">
          <div className="grid grid-cols-[4.25rem_minmax(0,1fr)_10rem] gap-3">
            <div className="space-y-2">
              <FieldLabel>{t(($) => $.properties.editor.icon)}</FieldLabel>
              <Popover open={iconPickerOpen} onOpenChange={setIconPickerOpen}>
                <PopoverTrigger
                  render={
                    <Button
                      type="button"
                      variant="outline"
                      className="w-full px-0 text-title"
                      aria-label={t(($) => $.properties.editor.choose_icon)}
                    >
                      {draft.icon ? (
                        <PropertyIconGlyph icon={draft.icon} />
                      ) : (
                        <SlidersHorizontal className="size-4 text-muted-foreground" />
                      )}
                    </Button>
                  }
                />
                <PopoverContent align="start" className="w-auto p-2">
                  <PropertyIconPicker
                    value={draft.icon}
                    label={t(($) => $.properties.editor.choose_icon)}
                    removeLabel={t(($) => $.properties.editor.remove_icon)}
                    onSelect={(icon) => {
                      setDraft((current) => ({ ...current, icon }));
                      setIconPickerOpen(false);
                    }}
                    onRemove={() => {
                      setDraft((current) => ({ ...current, icon: "" }));
                      setIconPickerOpen(false);
                    }}
                  />
                </PopoverContent>
              </Popover>
            </div>
            <div className="space-y-2">
              <FieldLabel htmlFor="property-name">
                {t(($) => $.properties.editor.name)}
              </FieldLabel>
              <Input
                id="property-name"
                autoFocus
                maxLength={32}
                value={draft.name}
                onChange={(event) =>
                  setDraft((current) => ({ ...current, name: event.target.value }))
                }
                placeholder={t(($) => $.properties.editor.name_placeholder)}
              />
            </div>
            <div className="space-y-2">
              <FieldLabel>{t(($) => $.properties.editor.type)}</FieldLabel>
              {property ? (
                <div
                  className="flex h-9 items-center rounded-md border border-surface-border px-3 text-body text-muted-foreground"
                  title={t(($) => $.properties.editor.type_locked_hint)}
                >
                  <PropertyTypeLabel type={property.type} />
                </div>
              ) : (
                <Select
                  items={propertyTypeItems}
                  value={draft.type}
                  onValueChange={(value) =>
                    value &&
                    setDraft((current) => ({
                      ...current,
                      type: value as IssuePropertyType,
                      options:
                        typeHasOptions(value) && current.options.length === 0
                          ? [{ name: "", color: COLOR_PICKER_PRESETS[6] }]
                          : current.options,
                    }))
                  }
                >
                  <SelectTrigger>
                    <SelectValue />
                  </SelectTrigger>
                  <SelectContent>
                    {propertyTypeItems.map((item) => (
                      <SelectItem key={item.value} value={item.value}>
                        {item.label}
                      </SelectItem>
                    ))}
                  </SelectContent>
                </Select>
              )}
            </div>
          </div>
          <div className="space-y-2">
            <FieldLabel htmlFor="property-description">
              {t(($) => $.properties.editor.description)}
            </FieldLabel>
            <Textarea
              id="property-description"
              rows={2}
              value={draft.description}
              onChange={(event) =>
                setDraft((current) => ({ ...current, description: event.target.value }))
              }
              placeholder={t(($) => $.properties.editor.description_placeholder)}
            />
          </div>
          {showOptions && (
            <div className="space-y-2">
              <FieldLabel>{t(($) => $.properties.editor.options)}</FieldLabel>
              <div className="space-y-2">
                {draft.options.map((option, index) => (
                  <div key={option.id ?? index} className="flex items-center gap-2">
                    <GripVertical className="size-4 shrink-0 text-faint-foreground" />
                    <ColorPicker
                      value={option.color}
                      onChange={(color) => setOption(index, { color })}
                      trigger={
                        <button
                          type="button"
                          aria-label={option.color}
                          // eslint-disable-next-line no-restricted-syntax -- color swatch
                          className="size-6 shrink-0 cursor-pointer rounded-full border border-surface-border transition-transform hover:scale-110"
                          style={{ backgroundColor: option.color }}
                        />
                      }
                    />
                    <Input
                      value={option.name}
                      maxLength={32}
                      onChange={(event) => setOption(index, { name: event.target.value })}
                      placeholder={t(($) => $.properties.editor.option_name_placeholder)}
                      className="h-8"
                    />
                    <IconButton
                      variant="ghost"
                      size="icon-sm"
                      label={t(($) => $.properties.editor.remove_option)}
                      disabled={draft.options.length <= 1}
                      onClick={() =>
                        setDraft((current) => ({
                          ...current,
                          options: current.options.filter((_, i) => i !== index),
                        }))
                      }
                    >
                      <X className="size-4" />
                    </IconButton>
                  </div>
                ))}
              </div>
              <Button
                variant="ghost"
                size="sm"
                className="gap-2"
                disabled={draft.options.length >= MAX_ISSUE_PROPERTY_SELECT_OPTIONS}
                onClick={() =>
                  setDraft((current) => ({
                    ...current,
                    options: [
                      ...current.options,
                      {
                        name: "",
                        color: COLOR_PICKER_PRESETS[current.options.length % COLOR_PICKER_PRESETS.length] ?? COLOR_PICKER_PRESETS[6],
                      },
                    ],
                  }))
                }
              >
                <Plus className="size-3.5" />
                {t(($) => $.properties.editor.add_option)}
              </Button>
            </div>
          )}
        </div>
        <DialogFooter>
          <Button variant="ghost" onClick={() => onOpenChange(false)}>
            {t(($) => $.properties.editor.cancel)}
          </Button>
          <Button
            onClick={submit}
            disabled={!canSubmit || create.isPending || update.isPending}
          >
            {create.isPending || update.isPending
              ? t(($) => $.properties.editor.saving)
              : t(($) => $.properties.editor.save)}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

function ArchivePropertyDialog({
  property,
  onClose,
}: {
  property: IssueProperty | null;
  onClose: () => void;
}) {
  const { t } = useT("settings");
  const update = useUpdateProperty();
  return (
    <AlertDialog open={Boolean(property)} onOpenChange={(open) => !open && onClose()}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{t(($) => $.properties.archive_dialog.title)}</AlertDialogTitle>
          <AlertDialogDescription>
            {t(($) => $.properties.archive_dialog.description, {
              name: property?.name ?? "",
            })}
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>{t(($) => $.properties.archive_dialog.cancel)}</AlertDialogCancel>
          <AlertDialogAction
            onClick={() => {
              if (!property) return;
              update.mutate(
                { id: property.id, archived: true },
                {
                  onSuccess: onClose,
                  onError: (error) =>
                    toast.error(
                      error instanceof Error
                        ? error.message
                        : t(($) => $.properties.archive_dialog.failed),
                    ),
                },
              );
            }}
          >
            {t(($) => $.properties.archive_dialog.confirm)}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}