-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQueryPagesPanel.tsx
More file actions
100 lines (96 loc) · 2.85 KB
/
QueryPagesPanel.tsx
File metadata and controls
100 lines (96 loc) · 2.85 KB
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
import { Button, InputGroup } from "@blueprintjs/core";
import posthog from "posthog-js";
import React, { useState } from "react";
import type { OnloadArgs } from "roamjs-components/types";
import {
getPersonalSetting,
setPersonalSetting,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";
import {
PERSONAL_KEYS,
QUERY_KEYS,
} from "~/components/settings/utils/settingKeys";
// Legacy extensionAPI stored query-pages as string | string[] | Record<string, string>.
// Coerce to string[] for backward compatibility with old stored formats.
export const getQueryPages = (snapshot?: SettingsSnapshot): string[] => {
const value: string[] | string | Record<string, string> | undefined = snapshot
? snapshot.personalSettings[PERSONAL_KEYS.query][QUERY_KEYS.queryPages]
: getPersonalSetting<string[] | string | Record<string, string>>([
PERSONAL_KEYS.query,
QUERY_KEYS.queryPages,
]);
return typeof value === "string"
? [value]
: Array.isArray(value)
? value
: typeof value === "object" && value !== null
? Object.keys(value)
: ["queries/*"];
};
const QueryPagesPanel = ({
extensionAPI,
}: {
extensionAPI: OnloadArgs["extensionAPI"];
}) => {
const [texts, setTexts] = useState(() => getQueryPages());
const [value, setValue] = useState("");
const setQueryPages = (newTexts: string[]) => {
setPersonalSetting([PERSONAL_KEYS.query, QUERY_KEYS.queryPages], newTexts);
void extensionAPI.settings.set("query-pages", newTexts);
};
return (
<div
className="flex flex-col"
style={{
width: "100%",
minWidth: 256,
}}
>
<div className={"flex gap-2"}>
<InputGroup
style={{ minWidth: "initial" }}
value={value}
onChange={(e) => setValue(e.target.value)}
/>
<Button
icon={"plus"}
minimal
disabled={!value}
onClick={() => {
const newTexts = [...texts, value];
setTexts(newTexts);
setQueryPages(newTexts);
setValue("");
posthog.capture("Query Page: Page Format Added", {
newType: value,
});
}}
/>
</div>
{texts.map((p, index) => (
<div key={index} className="flex items-center justify-between">
<span
style={{
textOverflow: "ellipsis",
whiteSpace: "nowrap",
overflow: "hidden",
}}
>
{p}
</span>
<Button
icon={"trash"}
minimal
onClick={() => {
const newTexts = texts.filter((_, jndex) => index !== jndex);
setTexts(newTexts);
setQueryPages(newTexts);
}}
/>
</div>
))}
</div>
);
};
export default QueryPagesPanel;