Skip to content

Commit 7cffbee

Browse files
committed
feat(settings): ✨ support specify logseq api config in web [skip ci]
1 parent de1619f commit 7cffbee

File tree

5 files changed

+73
-9
lines changed

5 files changed

+73
-9
lines changed

src/Agenda3/components/modals/SettingsModal/GeneralSettingsForm.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { Checkbox, Select, Tooltip } from 'antd'
1+
import { useLocalStorageValue } from '@react-hookz/web'
2+
import { Checkbox, Input, Select, Tooltip } from 'antd'
23
import { useTranslation } from 'react-i18next'
4+
import { FaRegCircleQuestion } from 'react-icons/fa6'
35

6+
import { DEFAULT_LOGSEQ_API_CONFIG, LOGSEQ_API_CONFIG_KEY } from '@/Agenda3/helpers/logseq'
47
import useSettings from '@/Agenda3/hooks/useSettings'
58
import type { Filter, Language } from '@/Agenda3/models/settings'
69

710
const GeneralSettingsForm = () => {
811
const { t, i18n } = useTranslation()
912
const { settings, setSettings } = useSettings()
13+
const { value: apiConfig, set: setApiConfig } = useLocalStorageValue(LOGSEQ_API_CONFIG_KEY, {
14+
defaultValue: DEFAULT_LOGSEQ_API_CONFIG,
15+
initializeWithValue: true,
16+
})
1017

1118
const onChange = (key: string, value: number | string | boolean | undefined | Filter[] | string[]) => {
1219
setSettings(key, value)
@@ -62,6 +69,44 @@ const GeneralSettingsForm = () => {
6269
]}
6370
/>
6471
</div>
72+
{import.meta.env.VITE_MODE === 'web' ? (
73+
<>
74+
<div className="mt-4">
75+
<div className="flex cursor-pointer items-center gap-2 text-gray-500">
76+
Logseq API Server
77+
<FaRegCircleQuestion title="This configuration requires reloading the page to take effect." />
78+
</div>
79+
<Input
80+
className="w-[300px]"
81+
placeholder={DEFAULT_LOGSEQ_API_CONFIG.apiServer}
82+
value={apiConfig?.apiServer}
83+
onChange={(e) =>
84+
setApiConfig((config) => ({
85+
...config,
86+
apiServer: e.target.value?.trim() || DEFAULT_LOGSEQ_API_CONFIG.apiServer,
87+
}))
88+
}
89+
/>
90+
</div>
91+
<div className="mt-4">
92+
<div className="flex cursor-pointer items-center gap-2 text-gray-500">
93+
Logseq API Token
94+
<FaRegCircleQuestion title="This configuration requires reloading the page to take effect." />
95+
</div>
96+
<Input.Password
97+
className="w-[300px]"
98+
placeholder={DEFAULT_LOGSEQ_API_CONFIG.apiToken}
99+
value={apiConfig?.apiToken}
100+
onChange={(e) =>
101+
setApiConfig((config) => ({
102+
...config,
103+
apiToken: e.target.value?.trim() || DEFAULT_LOGSEQ_API_CONFIG.apiToken,
104+
}))
105+
}
106+
/>
107+
</div>
108+
</>
109+
) : null}
65110
</div>
66111
</>
67112
)

src/Agenda3/helpers/logseq.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,22 @@ export const navToLogseqBlock = (task: AgendaEntity, currentGraph?: { name: stri
1111
window.open(`logseq://graph/${currentGraph.name}?block-id=${uuid}`, '_blank')
1212
}
1313
}
14+
15+
export const LOGSEQ_API_CONFIG_KEY = 'logseq-api-config'
16+
export const DEFAULT_LOGSEQ_API_CONFIG = {
17+
apiServer: import.meta.env.VITE_LOGSEQ_API_SERVER,
18+
apiToken: import.meta.env.VITE_LOGSEQ_API_TOKEN,
19+
}
20+
type LogseqApiConfig = { apiServer: string; apiToken: string }
21+
/**
22+
* 获取 logseq api 配置
23+
*/
24+
export const getLogseqApiConfig = (): LogseqApiConfig => {
25+
try {
26+
const _config = JSON.parse(localStorage.getItem(LOGSEQ_API_CONFIG_KEY) || '{}') as LogseqApiConfig
27+
return { ...DEFAULT_LOGSEQ_API_CONFIG, ..._config }
28+
} catch (error) {
29+
console.error('Failed to parse logseq api config', localStorage.getItem(LOGSEQ_API_CONFIG_KEY), error)
30+
return DEFAULT_LOGSEQ_API_CONFIG
31+
}
32+
}

src/Agenda3/locales/en/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
"month": "month",
5050
"Exit": "Exit",
5151
"Title cannot be empty": "Title cannot be empty",
52-
"Pleasse specify start or deadline": "Pleasse specify start or deadline"
52+
"Pleasse specify start or deadline": "Pleasse specify start or deadline",
53+
"This configuration requires reloading the page to take effect.": "This configuration requires reloading the page to take effect."
5354
}

src/Agenda3/locales/zh-CN/translation.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@
4949
"month": "本月",
5050
"Exit": "退出",
5151
"Title cannot be empty": "标题不能为空",
52-
"Pleasse specify start or deadline": "请指定开始或截止日期"
52+
"Pleasse specify start or deadline": "请指定开始或截止日期",
53+
"This configuration requires reloading the page to take effect.": "此配置需要重新加载页面才能生效。"
5354
}

src/main.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import initializeTodoist from '@/register/todoist'
2525
import { getInitialSettings, initializeSettings } from '@/util/baseInfo'
2626
import { listenEsc, log, managePluginTheme, setPluginTheme, toggleAppTransparent } from '@/util/util'
2727

28+
import { getLogseqApiConfig } from './Agenda3/helpers/logseq'
2829
import { track } from './Agenda3/helpers/umami'
2930
import Agenda3App from './apps/Agenda3App'
3031
import TaskListApp from './apps/TaskListApp'
@@ -45,18 +46,15 @@ echarts.use([
4546
LegendComponent,
4647
])
4748

48-
const isDevelopment = import.meta.env.DEV
4949
let root: Root | null = null
5050

5151
if (import.meta.env.VITE_MODE === 'web') {
5252
// run in browser
53-
console.log('[faiz:] === meta.env.VITE_LOGSEQ_API_SERVER', import.meta.env.VITE_LOGSEQ_API_SERVER)
53+
const { apiServer, apiToken } = getLogseqApiConfig()
54+
console.log('[faiz:] === LOGSEQ_API_SERVER', apiServer)
5455
console.log(`%c[version]: v${__APP_VERSION__}`, 'background-color: #60A5FA; color: white; padding: 4px;')
5556
proxyLogseq({
56-
config: {
57-
apiServer: import.meta.env.VITE_LOGSEQ_API_SERVER,
58-
apiToken: import.meta.env.VITE_LOGSEQ_API_TOKEN,
59-
},
57+
config: { apiServer, apiToken },
6058
settings: window.mockSettings,
6159
})
6260
// logseq.provideStyle(`.drawer[data-drawer-name="agenda"] {display: none;}`)

0 commit comments

Comments
 (0)