summaryrefslogtreecommitdiff
path: root/src/components/CollapsibleItemGroup.tsx
blob: a4e4092005fc2200cc822f55cd07da343256f712 (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
import { ButtonItem, Field, PanelSectionRow } from "@decky/ui";
import { useEffect, useState, type RefObject } from "react";
import { RiArrowDownSFill, RiArrowUpSFill } from "react-icons/ri";

export interface CollapsibleItem {
  id: string;
  label: string;
  description: string;
  disabled?: boolean;
}

export const collapsibleItemGroupStyles = `
  .LSFG_GameGroupCollapseButton_Container {
    margin-top: -2px;
    margin-bottom: 4px;
  }

  .LSFG_GameGroupCollapseButton_Container > div > div > div > button,
  .LSFG_GameGroupCollapseButton_Container > div > div > div > div > button {
    height: 24px !important;
    min-height: 24px !important;
    padding: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  .LSFG_GameGroupCollapseButton_Container svg {
    display: block;
    margin: 0;
  }
`;

export function usePersistentCollapsed(key: string) {
  const [collapsed, setCollapsed] = useState(() => {
    try {
      return localStorage.getItem(key) !== "false";
    } catch {
      return true;
    }
  });

  useEffect(() => {
    try {
      localStorage.setItem(key, String(collapsed));
    } catch {}
  }, [collapsed, key]);

  return [collapsed, () => setCollapsed((value) => !value)] as const;
}

interface Props {
  title: string;
  items: CollapsibleItem[];
  collapsed: boolean;
  onToggle: () => void;
  onSelect: (id: string) => void;
  toggleRef?: RefObject<HTMLDivElement>;
}

export function CollapsibleItemGroup({
  title,
  items,
  collapsed,
  onToggle,
  onSelect,
  toggleRef,
}: Props) {
  if (items.length === 0) return null;

  return (
    <>
      <PanelSectionRow>
        <Field label={`${title} (${items.length})`} bottomSeparator="none" />
      </PanelSectionRow>
      <PanelSectionRow>
        <div
          ref={toggleRef}
          className="LSFG_GameGroupCollapseButton_Container"
        >
          <ButtonItem
            layout="below"
            bottomSeparator={collapsed ? "standard" : "none"}
            onClick={onToggle}
          >
            {collapsed ? <RiArrowDownSFill /> : <RiArrowUpSFill />}
          </ButtonItem>
        </div>
      </PanelSectionRow>
      {!collapsed && items.map((item) => (
        <PanelSectionRow key={item.id}>
          <Field
            label={item.label}
            description={item.description}
            disabled={item.disabled}
            onActivate={item.disabled ? undefined : () => onSelect(item.id)}
            highlightOnFocus
          />
        </PanelSectionRow>
      ))}
    </>
  );
}