diff --git a/app/components/Table.tsx b/app/components/Table.tsx index a97edb6bad..cb1a2d5800 100644 --- a/app/components/Table.tsx +++ b/app/components/Table.tsx @@ -14,7 +14,7 @@ import { } from "@tanstack/react-table"; import { useWindowVirtualizer } from "@tanstack/react-virtual"; import { observer } from "mobx-react"; -import { CollapsedIcon } from "outline-icons"; +import { CollapsedIcon, SettingsIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Waypoint } from "react-waypoint"; @@ -30,8 +30,19 @@ import { } from "~/components/ModelSelectionContext"; import NudeButton from "~/components/NudeButton"; import PlaceholderText from "~/components/PlaceholderText"; +import { + Menu, + MenuButton, + MenuContent, + MenuTrigger, +} from "~/components/primitives/Menu"; +import { MenuProvider } from "~/components/primitives/Menu/MenuContext"; import { SelectionCheckbox } from "~/components/SelectionCheckbox"; +import Tooltip from "~/components/Tooltip"; +import useMobile from "~/hooks/useMobile"; +import usePersistedState from "~/hooks/usePersistedState"; import usePrevious from "~/hooks/usePrevious"; +import { preventDefault } from "~/utils/events"; import { transparentize } from "polished"; const HEADER_HEIGHT = 40; @@ -60,6 +71,11 @@ export type RowData = { }; export type Props = { + /** + * Unique identifier for this table. When provided the visibility of columns + * can be toggled from a context menu on the header and is persisted locally. + */ + id?: string; data: TData[]; columns: Column[]; sort: ColumnSort; @@ -109,6 +125,7 @@ function Table(props: Props) { } function TableViewInner({ + id, data, columns, sort, @@ -123,13 +140,41 @@ function TableViewInner({ }: Props & { selectableCount?: number }) { const { t } = useTranslation(); const selection = useModelSelection(); + const isMobile = useMobile(); const virtualContainerRef = React.useRef(null); const [virtualContainerTop, setVirtualContainerTop] = React.useState(); + const [hiddenColumns, setHiddenColumns] = usePersistedState( + `hiddenTableColumns-${id}`, + [] + ); + + const handleToggleColumn = React.useCallback( + (columnId: string) => { + setHiddenColumns((hidden) => + hidden.includes(columnId) + ? hidden.filter((hiddenId) => hiddenId !== columnId) + : [...hidden, columnId] + ); + }, + [setHiddenColumns] + ); + + const visibleColumns = React.useMemo( + () => + id + ? columns.filter( + (column) => + column.type !== "data" || !hiddenColumns.includes(column.id) + ) + : columns, + [id, columns, hiddenColumns] + ); + const allColumns = React.useMemo(() => { if (!selection || !isRowSelectable) { - return columns; + return visibleColumns; } const selectColumn: Column = { @@ -153,8 +198,8 @@ function TableViewInner({ width: "28px", }; - return [selectColumn, ...columns]; - }, [columns, selection, isRowSelectable, selectableCount, t]); + return [selectColumn, ...visibleColumns]; + }, [visibleColumns, selection, isRowSelectable, selectableCount, t]); const columnHelper = React.useMemo(() => createColumnHelper(), []); const observedColumns = React.useMemo( @@ -251,61 +296,83 @@ function TableViewInner({ <> - {table.getHeaderGroups().map((headerGroup) => ( - - {headerGroup.headers.map((header) => { - const sorted = header.column.getIsSorted(); - const canSort = header.column.getCanSort(); - const toggleSorting = header.column.getToggleSortingHandler(); - const handleSortKeyDown = ( - ev: React.KeyboardEvent - ) => { - if (!ev.repeat && (ev.key === "Enter" || ev.key === " ")) { - ev.preventDefault(); - toggleSorting?.(ev); - } - }; - - return ( - { + const headerRow = ( + + {headerGroup.headers.map((header) => { + const sorted = header.column.getIsSorted(); + const canSort = header.column.getCanSort(); + const toggleSorting = header.column.getToggleSortingHandler(); + const handleSortKeyDown = ( + ev: React.KeyboardEvent + ) => { + if (!ev.repeat && (ev.key === "Enter" || ev.key === " ")) { + ev.preventDefault(); + toggleSorting?.(ev); } - > - - {flexRender( - header.column.columnDef.header, - header.getContext() - )} - {sorted === "asc" ? ( - - ) : sorted === "desc" ? ( - - ) : ( -
- )} - - - ); - })} - - ))} + + {flexRender( + header.column.columnDef.header, + header.getContext() + )} + {sorted === "asc" ? ( + + ) : sorted === "desc" ? ( + + ) : ( +
+ )} + + + ); + })} + + ); + + return id && !isMobile ? ( + + {headerRow} + + ) : ( + {headerRow} + ); + })} + {id && !isMobile && ( + + )} ({ // The observer wrapper does not preserve the generic signature of the table. const TableView = observer(TableViewInner) as typeof TableViewInner; +type ColumnVisibilityProps = { + columns: Column[]; + hiddenColumns: string[]; + onToggleColumn: (columnId: string) => void; +}; + +/** + * Menu items that toggle the visibility of individual columns. Only columns + * that display data can be hidden, and at least one must remain visible. + */ +function ColumnVisibilityItems({ + columns, + hiddenColumns, + onToggleColumn, +}: ColumnVisibilityProps) { + const dataColumns = columns.filter( + (column): column is Column & DataColumn => + column.type === "data" + ); + const visibleCount = dataColumns.filter( + (column) => !hiddenColumns.includes(column.id) + ).length; + + return ( + <> + {dataColumns.map((column) => { + const visible = !hiddenColumns.includes(column.id); + + return ( + onToggleColumn(column.id)} + /> + ); + })} + + ); +} + +/** Column visibility options, opened by right-clicking the header row. */ +function ColumnVisibilityMenu({ + children, + ...rest +}: ColumnVisibilityProps & { children: React.ReactNode }) { + const { t } = useTranslation(); + const label = t("Columns"); + + return ( + + + {children} + + + + + + ); +} + +/** Column visibility options, opened by a button at the end of the header row. */ +function ColumnVisibilityButton(props: ColumnVisibilityProps) { + const { t } = useTranslation(); + const label = t("Columns"); + + return ( + + + + + + + + + + + + + + + ); +} + const SelectableRow = observer(function SelectableRow_({ selection, id, @@ -519,6 +676,21 @@ const SortWrapper = styled(Flex)<{ $sortable: boolean }>` } `; +const ColumnOptions = styled(NudeButton)` + position: absolute; + top: ${(HEADER_HEIGHT - 24) / 2}px; + right: 0; + color: ${s("placeholder")}; + background: ${s("background")}; + + &:hover, + &:focus-visible, + &[aria-expanded="true"] { + color: ${s("textSecondary")}; + background: ${s("sidebarControlHoverBackground")}; + } +`; + const InnerTable = styled.div` width: 100%; `; @@ -529,7 +701,7 @@ const THead = styled.div<{ $topPos: number }>` height: ${HEADER_HEIGHT}px; z-index: 1; font-size: 14px; - color: ${s("textSecondary")}; + color: ${s("text")}; font-weight: 500; border-bottom: 1px solid @@ -554,6 +726,11 @@ const TR = styled.div<{ $columns: string }>` border-bottom: 0; } + /* The header has its own border, avoid doubling the divider beneath it. */ + ${THead} & { + border-bottom: 0; + } + &:hover ${NudeButton}[aria-haspopup="menu"] { opacity: 1; } diff --git a/app/components/UserLabel.tsx b/app/components/UserLabel.tsx new file mode 100644 index 0000000000..f4c60cd999 --- /dev/null +++ b/app/components/UserLabel.tsx @@ -0,0 +1,45 @@ +import { observer } from "mobx-react"; +import type * as React from "react"; +import Text from "@shared/components/Text"; +import Avatar, { AvatarSize } from "~/components/Avatar/Avatar"; +import { HStack } from "~/components/primitives/HStack"; +import { UserHoverCard } from "~/components/UserHoverCard"; +import type User from "~/models/User"; + +type Props = { + /** The user to display, nothing is rendered when undefined. */ + user: User | null | undefined; + /** Whether the user is the primary content of the row, shown with a larger avatar. */ + primary?: boolean; + /** Additional content displayed after the user's name. */ + children?: React.ReactNode; +}; + +/** + * Displays a user's avatar alongside their name, with a card summarizing their + * profile shown on hover. + */ +export const UserLabel = observer(function UserLabel_({ + user, + primary, + children, +}: Props) { + if (!user) { + return null; + } + + return ( + + + + + {user.name} + + {children} + + + ); +}); diff --git a/app/scenes/Settings/components/ApiKeysTable.tsx b/app/scenes/Settings/components/ApiKeysTable.tsx index 8b28f88cbe..8cc6d193f5 100644 --- a/app/scenes/Settings/components/ApiKeysTable.tsx +++ b/app/scenes/Settings/components/ApiKeysTable.tsx @@ -4,7 +4,6 @@ import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import styled from "styled-components"; import type ApiKey from "~/models/ApiKey"; -import { Avatar, AvatarSize } from "~/components/Avatar"; import Badge from "~/components/Badge"; import CopyToClipboard from "~/components/CopyToClipboard"; import { HEADER_HEIGHT } from "~/components/Header"; @@ -14,6 +13,7 @@ import { SortableTable, } from "~/components/SortableTable"; import { type Column as TableColumn } from "~/components/Table"; +import { UserLabel } from "~/components/UserLabel"; import Text from "~/components/Text"; import Time from "~/components/Time"; import Tooltip from "~/components/Tooltip"; @@ -115,13 +115,7 @@ export const ApiKeysTable = observer(function ApiKeysTable(props: Props) { id: "userId", header: t("Created by"), accessor: (apiKey) => apiKey.user?.name, - component: (apiKey) => - apiKey.user ? ( - - - {apiKey.user.name} - - ) : null, + component: (apiKey) => , width: "2fr", }, { @@ -168,6 +162,7 @@ export const ApiKeysTable = observer(function ApiKeysTable(props: Props) { return ( emoji.createdBy, sortable: false, - component: (emoji) => ( - - {emoji.createdBy && ( - <> - - {emoji.createdBy.name} - - )} - - ), + component: (emoji) => , width: "2fr", }, { @@ -121,6 +112,7 @@ const EmojisTable = observer(function EmojisTable({ return ( user.name, component: (user) => ( - - - {user.name} + {user.isAdmin && {t("Admin")}} - + ), width: "3fr", }, @@ -132,9 +130,11 @@ export const GroupMembersTable = observer(function GroupMembersTable({ user.id )?.permission; return permission ? ( - - {GroupPermissionHelper.displayName(permission, t)} - + + + {GroupPermissionHelper.displayName(permission, t)} + + ) : null; }, width: "1fr", @@ -155,6 +155,7 @@ export const GroupMembersTable = observer(function GroupMembersTable({ return ( share.createdBy, sortable: false, - component: (share) => ( - - {share.createdBy && ( - <> - - {share.createdBy.name} - - )} - - ), + component: (share) => , width: "2fr", }, { @@ -137,11 +126,7 @@ export function SharesTable({ data, canManage, ...rest }: Props) { ? { type: "action", id: "action", - component: (share) => ( - - - - ), + component: (share) => , width: "50px", } : undefined, @@ -151,6 +136,7 @@ export function SharesTable({ data, canManage, ...rest }: Props) { return ( template.updatedBy?.name, sortable: false, - component: (template) => ( - - {" "} - {template.updatedBy?.name}{" "} - - ), + component: (template) => , width: "2fr", }, { @@ -135,6 +130,7 @@ export function TemplatesTable(props: Props) { return ( webhook.createdBy?.name, - component: (webhook) => - webhook.createdBy ? ( - - - {webhook.createdBy.name} - - ) : null, + component: (webhook) => , width: "2fr", }, { @@ -122,6 +116,7 @@ export const WebhookSubscriptionsTable = observer( return ( control to add placeholders that can be filled out when creating new documents": "Highlight some text and use the <1> control to add placeholders that can be filled out when creating new documents", "You’re editing a template": "You’re editing a template", "Template created, go ahead and customize it": "Template created, go ahead and customize it",