summaryrefslogtreecommitdiff
path: root/src/components/ModalManageConnectionColumns.tsx
blob: 6c687c4062bceed5d77fdd8927cd6fd05407140c (plain)
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
import { DragDropContext, Draggable, Droppable } from '@hello-pangea/dnd';
import React from 'react';
import { ChevronDown, ChevronUp, Menu } from '~/components/shared/FeatherIcons';
import { useTranslation } from 'react-i18next';

import BaseModal from '~/components/shared/BaseModal';
import { ConnectionColumn } from '~/modules/connections/utils';

import s from './ModalManageConnectionColumns.module.scss';
import Switch from './SwitchThemed';

const getItemStyle = (isDragging, draggableStyle) => {
  return {
    ...draggableStyle,
    ...(isDragging && {
      background: 'transparent',
    }),
  };
};

export default function ModalManageConnectionColumns({
  isOpen,
  onRequestClose,
  columns,
  hiddenColumns,
  setColumns,
  setHiddenColumns,
}) {
  const { t } = useTranslation();

  const onDragEnd = (result) => {
    if (!result.destination) {
      return;
    }

    const items = Array.from(columns);
    const [removed] = items.splice(result.source.index, 1);
    items.splice(result.destination.index, 0, removed);
    setColumns(items);
  };

  const onShowChange = (column, val) => {
    const nextHiddenColumns = !val
      ? [...hiddenColumns, column.accessor]
      : hiddenColumns.filter((accessor) => accessor !== column.accessor);

    setHiddenColumns(nextHiddenColumns);
  };

  const moveColumn = (columnAccessor: string, direction: 'up' | 'down') => {
    const items: ConnectionColumn[] = Array.from(columns);
    const currentIndex = items.findIndex((c) => c.accessor === columnAccessor);
    if (currentIndex === -1) return;

    // 计算目标位置,跳过 id 列
    let targetIndex = currentIndex;
    if (direction === 'up') {
      // 往上移动
      targetIndex = currentIndex - 1;
      // 跳过 id 列
      while (targetIndex >= 0 && items[targetIndex].accessor === 'id') {
        targetIndex--;
      }
      if (targetIndex < 0) return;
    } else {
      // 往下移动
      targetIndex = currentIndex + 1;
      // 跳过 id 列
      while (targetIndex < items.length && items[targetIndex].accessor === 'id') {
        targetIndex++;
      }
      if (targetIndex >= items.length) return;
    }

    // 交换位置
    const [removed] = items.splice(currentIndex, 1);
    items.splice(targetIndex, 0, removed);
    setColumns(items);
  };

  // 获取非 id 列的显示列表
  const visibleColumns = columns.filter((i) => i.accessor !== 'id');

  return (
    <BaseModal isOpen={isOpen} onRequestClose={onRequestClose}>
      <div>
        <DragDropContext onDragEnd={onDragEnd}>
          <Droppable droppableId="droppable-modal">
            {(provided) => (
              <div {...provided.droppableProps} ref={provided.innerRef}>
                {visibleColumns.map((column, displayIndex) => {
                  const show = !hiddenColumns.includes(column.accessor);
                  const isFirst = displayIndex === 0;
                  const isLast = displayIndex === visibleColumns.length - 1;

                  return (
                    <Draggable
                      key={column.accessor}
                      draggableId={column.accessor}
                      index={columns.findIndex((a) => a.accessor === column.accessor)}
                    >
                      {(provided, snapshot) => (
                        <div
                          ref={provided.innerRef}
                          {...provided.draggableProps}
                          {...provided.dragHandleProps}
                          className={s.columnManagerRow}
                          style={getItemStyle(snapshot.isDragging, provided.draggableProps.style)}
                        >
                          <Menu size={16} />
                          <span className={s.columnManageLabel}>{t(column.Header)}</span>
                          <div className={s.columnMoveButtons}>
                            <button
                              className={s.moveBtn}
                              onClick={(e) => {
                                e.stopPropagation();
                                moveColumn(column.accessor, 'up');
                              }}
                              disabled={isFirst}
                              title={t('Move Up')}
                            >
                              <ChevronUp size={14} />
                            </button>
                            <button
                              className={s.moveBtn}
                              onClick={(e) => {
                                e.stopPropagation();
                                moveColumn(column.accessor, 'down');
                              }}
                              disabled={isLast}
                              title={t('Move Down')}
                            >
                              <ChevronDown size={14} />
                            </button>
                          </div>
                          <div className={s.columnManageSwitch}>
                            <Switch
                              size="mini"
                              checked={show}
                              onChange={(val) => onShowChange(column, val)}
                            />
                          </div>
                        </div>
                      )}
                    </Draggable>
                  );
                })}
                {provided.placeholder}
              </div>
            )}
          </Droppable>
        </DragDropContext>
      </div>
    </BaseModal>
  );
}