-
-
Notifications
You must be signed in to change notification settings - Fork 831
/
TopBarOptions.tsx
160 lines (143 loc) · 4.16 KB
/
TopBarOptions.tsx
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
157
158
159
160
import {
Icon,
Key,
MonitorPlay,
Rows,
SidebarSimple,
SlidersHorizontal,
SquaresFour
} from '@phosphor-icons/react';
import clsx from 'clsx';
import { useMemo } from 'react';
import { useDocumentEventListener } from 'rooks';
import { ExplorerLayout, useSelector } from '@sd/client';
import { useKeyMatcher, useLocale } from '~/hooks';
import { KeyManager } from '../KeyManager';
import { Spacedrop, SpacedropButton } from '../Spacedrop';
import TopBarOptions, { ToolOption, TOP_BAR_ICON_STYLE } from '../TopBar/TopBarOptions';
import { useExplorerContext } from './Context';
import OptionsPanel from './OptionsPanel';
import { explorerStore } from './store';
const layoutIcons: Record<ExplorerLayout, Icon> = {
grid: SquaresFour,
list: Rows,
media: MonitorPlay
};
export const useExplorerTopBarOptions = () => {
const [showInspector, tagAssignMode] = useSelector(explorerStore, (s) => [
s.showInspector,
s.tagAssignMode
]);
const explorer = useExplorerContext();
const controlIcon = useKeyMatcher('Meta').icon;
const settings = explorer.useSettingsSnapshot();
const { t } = useLocale();
const viewOptions = useMemo(
() =>
(Object.keys(explorer.layouts) as ExplorerLayout[]).reduce(
(layouts, layout, i) => {
if (!explorer.layouts[layout]) return layouts;
const Icon = layoutIcons[layout];
const option = {
layout,
toolTipLabel: t(`${layout}_view`),
icon: <Icon className={TOP_BAR_ICON_STYLE} />,
keybinds: [controlIcon, (i + 1).toString()],
topBarActive:
!explorer.isLoadingPreferences && settings.layoutMode === layout,
onClick: () => (explorer.settingsStore.layoutMode = layout),
showAtResolution: 'sm:flex'
} satisfies ToolOption & { layout: ExplorerLayout };
return [...layouts, option];
},
[] as (ToolOption & { layout: ExplorerLayout })[]
),
[
controlIcon,
explorer.isLoadingPreferences,
explorer.layouts,
explorer.settingsStore,
settings.layoutMode,
t
]
);
const controlOptions: ToolOption[] = [
{
toolTipLabel: t('explorer_settings'),
icon: <SlidersHorizontal className={TOP_BAR_ICON_STYLE} />,
popOverComponent: <OptionsPanel />,
individual: true,
showAtResolution: 'sm:flex'
},
{
toolTipLabel: t('show_inspector'),
keybinds: [controlIcon, 'I'],
onClick: () => {
explorerStore.showInspector = !showInspector;
},
icon: (
<SidebarSimple
weight={showInspector ? 'fill' : 'regular'}
className={clsx(TOP_BAR_ICON_STYLE, '-scale-x-100')}
/>
),
individual: true,
showAtResolution: 'xl:flex',
topBarActive: showInspector
}
];
useDocumentEventListener('keydown', (e: unknown) => {
if (!(e instanceof KeyboardEvent)) return;
const meta = e.metaKey || e.ctrlKey;
if (!meta) return;
const layout = viewOptions[Number(e.key) - 1]?.layout;
if (!layout) return;
e.stopPropagation();
explorer.settingsStore.layoutMode = layout;
});
const toolOptions = [
{
toolTipLabel: 'Spacedrop',
icon: ({ triggerOpen }) => <SpacedropButton triggerOpen={triggerOpen} />,
popOverComponent: ({ triggerClose }) => <Spacedrop triggerClose={triggerClose} />,
individual: true,
showAtResolution: 'xl:flex'
},
{
toolTipLabel: t('key_manager'),
icon: <Key className={TOP_BAR_ICON_STYLE} />,
popOverComponent: <KeyManager />,
individual: true,
showAtResolution: 'xl:flex'
}
// {
// toolTipLabel: 'Tag Assign Mode',
// icon: (
// <Tag weight={tagAssignMode ? 'fill' : 'regular'} className={TOP_BAR_ICON_STYLE} />
// ),
// // TODO: Assign tag mode is not yet implemented!
// // onClick: () => (explorerStore.tagAssignMode = !explorerStore.tagAssignMode),
// onClick: () => toast.info(t('coming_soon)),
// topBarActive: tagAssignMode,
// individual: true,
// showAtResolution: 'xl:flex'
// }
] satisfies ToolOption[];
return {
viewOptions,
controlOptions,
toolOptions
};
};
export const DefaultTopBarOptions = (props: { options?: ToolOption[] }) => {
const options = useExplorerTopBarOptions();
return (
<TopBarOptions
options={[
options.viewOptions,
[...options.toolOptions, ...(props.options ?? [])],
options.controlOptions
]}
/>
);
};