Skip to content

Commit

Permalink
chore: dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
lencx committed Jan 24, 2023
1 parent 4a7ee4d commit ba438b0
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 106 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/app/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub fn menu_handler(event: WindowMenuEvent<tauri::Wry>) {
utils::run_check_update(app, false, None);
}
// Preferences
"control_center" => window::control_window(&app),
"control_center" => window::control_window(app.clone()),
"restart" => tauri::api::process::restart(&app.env()),
"inject_script" => open(&app, script_path),
"go_conf" => utils::open_file(utils::chat_root()),
Expand Down Expand Up @@ -431,7 +431,7 @@ pub fn tray_handler(handle: &AppHandle, event: SystemTrayEvent) {
}
}
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"control_center" => window::control_window(&app),
"control_center" => window::control_window(app),
"restart" => tauri::api::process::restart(&handle.env()),
"show_dock_icon" => {
ChatConfJson::amend(&serde_json::json!({ "hide_dock_icon": false }), Some(app)).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/app/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use wry::application::accelerator::Accelerator;
pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>> {
info!("stepup");
let chat_conf = ChatConfJson::get_chat_conf();
let url = chat_conf.origin.to_string();
let url = chat_conf.main_origin.to_string();
let theme = ChatConfJson::theme();
let handle = app.app_handle();

Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
} else {
let app = app.handle();
tauri::async_runtime::spawn(async move {
let link = if chat_conf.dashboard {
let link = if chat_conf.main_dashboard {
"index.html"
} else {
&url
Expand All @@ -65,7 +65,7 @@ pub fn init(app: &mut App) -> std::result::Result<(), Box<dyn std::error::Error>
main_win = main_win.hidden_title(true);
}

if url == "https://chat.openai.com" && !chat_conf.dashboard {
if url == "https://chat.openai.com" && !chat_conf.main_dashboard {
main_win = main_win
.initialization_script(include_str!("../vendors/floating-ui-core.js"))
.initialization_script(include_str!("../vendors/floating-ui-dom.js"))
Expand Down
10 changes: 5 additions & 5 deletions src-tauri/src/app/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ pub fn dalle2_window(
}
}

pub fn control_window(handle: &tauri::AppHandle) {
let app = handle.clone();
#[tauri::command]
pub fn control_window(handle: tauri::AppHandle) {
tauri::async_runtime::spawn(async move {
if app.app_handle().get_window("main").is_none() {
if handle.get_window("main").is_none() {
WindowBuilder::new(
&app,
&handle,
"main",
WindowUrl::App("index.html?type=control".into()),
)
Expand All @@ -110,7 +110,7 @@ pub fn control_window(handle: &tauri::AppHandle) {
.build()
.unwrap();
} else {
let main_win = app.app_handle().get_window("main").unwrap();
let main_win = handle.get_window("main").unwrap();
main_win.show().unwrap();
main_win.set_focus().unwrap();
}
Expand Down
15 changes: 9 additions & 6 deletions src-tauri/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub const BUY_COFFEE: &str = "https://www.buymeacoffee.com/lencx";
pub const GITHUB_PROMPTS_CSV_URL: &str =
"https://raw.githubusercontent.com/f/awesome-chatgpt-prompts/main/prompts.csv";
pub const DEFAULT_CHAT_CONF: &str = r#"{
"dashboard": false,
"stay_on_top": false,
"auto_update": "Prompt",
"theme": "Light",
Expand All @@ -27,14 +26,15 @@ pub const DEFAULT_CHAT_CONF: &str = r#"{
"popup_search": false,
"global_shortcut": "",
"hide_dock_icon": false,
"origin": "https://chat.openai.com",
"main_dashboard": false,
"tray_dashboard": false,
"main_origin": "https://chat.openai.com",
"tray_origin": "https://chat.openai.com",
"default_origin": "https://chat.openai.com",
"ua_window": "",
"ua_tray": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
}"#;
pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
"dashboard": false,
"stay_on_top": false,
"auto_update": "Prompt",
"theme": "Light",
Expand All @@ -43,7 +43,9 @@ pub const DEFAULT_CHAT_CONF_MAC: &str = r#"{
"popup_search": false,
"global_shortcut": "",
"hide_dock_icon": false,
"origin": "https://chat.openai.com",
"main_dashboard": false,
"tray_dashboard": false,
"main_origin": "https://chat.openai.com",
"tray_origin": "https://chat.openai.com",
"default_origin": "https://chat.openai.com",
"ua_window": "",
Expand All @@ -60,11 +62,12 @@ pub struct ChatConfJson {
pub theme: String,
// auto update policy, Prompt/Silent/Disable
pub auto_update: String,
pub dashboard: bool,
pub tray: bool,
pub popup_search: bool,
pub stay_on_top: bool,
pub origin: String,
pub main_dashboard: bool,
pub tray_dashboard: bool,
pub main_origin: String,
pub tray_origin: String,
pub default_origin: String,
pub ua_window: String,
Expand Down
1 change: 1 addition & 0 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async fn main() {
fs_extra::metadata,
window::window_reload,
window::wa_window,
window::control_window,
])
.setup(setup::init)
.menu(menu::init());
Expand Down
102 changes: 75 additions & 27 deletions src/components/SwitchOrigin/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC } from 'react';
import { Link } from 'react-router-dom';
import { Form, Select, Tag, Tooltip } from 'antd';
import { Form, Select, Tag, Tooltip, Switch } from 'antd';
import { QuestionCircleOutlined } from '@ant-design/icons';

import useJson from '@/hooks/useJson';
Expand All @@ -11,34 +11,82 @@ interface SwitchOriginProps {

const SwitchOrigin: FC<SwitchOriginProps> = ({ name }) => {
const { json: list = [] } = useJson<any[]>(CHAT_AWESOME_JSON);
const form = Form.useFormInstance();

const labelName = `(${name === 'main' ? 'Main' : 'SystemTray'})`;
const dashboardName = `${name}_dashboard`;
const originName = `${name}_origin`;
const isEnable = Form.useWatch(dashboardName, form);

return (
<Form.Item
label={
<span>
Switch Origin ({name === 'origin' ? 'Main' : 'SystemTray'}){' '}
<Tooltip
title={
<div>
If you need to set a new URL as the application loading window, please add the URL
in the <Link to="/">Awesome</Link> menu and then select it.
</div>
}
>
<QuestionCircleOutlined style={{ color: '#1677ff' }} />
</Tooltip>
</span>
}
name={name}
>
<Select showSearch {...DISABLE_AUTO_COMPLETE} optionLabelProp="url">
{[{ title: 'ChatGPT', url: 'https://chat.openai.com' }, ...list].map((i) => (
<Select.Option key={i.url} label={i.title} value={i.url} title={i.url}>
<Tag color={i.title === 'ChatGPT' ? 'orange' : 'geekblue'}>{i.title}</Tag> {i.url}
</Select.Option>
))}
</Select>
</Form.Item>
<>
<Form.Item
label={
<span>
Dashboard {labelName}{' '}
<Tooltip
title={
<div>
<p>
<b>Set the URL dashboard as an application window.</b>
</p>
<p>
If this is enabled, the <Tag color="blue">Switch Origin {labelName}</Tag>{' '}
setting will be invalid.
</p>
<p>
If you want to add a new URL to the dashboard, add it in the{' '}
<Link to="/awesome">Awesome</Link> menu and make sure it is enabled.
</p>
</div>
}
>
<QuestionCircleOutlined style={{ color: '#1677ff' }} />
</Tooltip>
</span>
}
name={dashboardName}
valuePropName="checked"
>
<Switch />
</Form.Item>
<Form.Item
label={
<span>
Switch Origin {labelName}{' '}
<Tooltip
title={
<div>
<p>
<b>Set a single URL as an application window.</b>
</p>
<p>
If you need to set a new URL as the application loading window, please add the
URL in the <Link to="/awesome">Awesome</Link> menu and then select it.
</p>
</div>
}
>
<QuestionCircleOutlined style={{ color: '#1677ff' }} />
</Tooltip>
</span>
}
name={originName}
>
<Select disabled={isEnable} showSearch {...DISABLE_AUTO_COMPLETE} optionLabelProp="url">
{[{ title: 'ChatGPT', url: 'https://chat.openai.com' }, ...list].map((i, idx) => (
<Select.Option
key={`${idx}_${i.url}`}
label={i.title}
value={i.url}
title={`${i.title}: ${i.url}`}
>
<Tag color={i.title === 'ChatGPT' ? 'orange' : 'geekblue'}>{i.title}</Tag> {i.url}
</Select.Option>
))}
</Select>
</Form.Item>
</>
);
};

Expand Down
41 changes: 27 additions & 14 deletions src/view/awesome/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export default function Awesome() {
updateJson(data);
opInfo.resetRecord();
}
}, [opInfo.opType,

formRef]);
}, [opInfo.opType, formRef]);

const hide = () => {
setVisible(false);
Expand All @@ -54,18 +52,33 @@ export default function Awesome() {

const handleOk = () => {
formRef.current?.form?.validateFields().then(async (vals: Record<string, any>) => {
if (opInfo.opType === 'new') {
const data = opAdd(vals);
await updateJson(data);
opInit(data);
message.success('Data added successfully');
}
if (opInfo.opType === 'edit') {
const data = opReplace(opInfo?.opRecord?.[opSafeKey], vals);
await updateJson(data);
message.success('Data updated successfully');
const idx = opData.findIndex((i) => i.url === vals.url);
if (idx === -1) {
if (opInfo.opType === 'new') {
const data = opAdd(vals);
await updateJson(data);
opInit(data);
message.success('Data added successfully');
}
if (opInfo.opType === 'edit') {
const data = opReplace(opInfo?.opRecord?.[opSafeKey], vals);
await updateJson(data);
message.success('Data updated successfully');
}
hide();
} else {
const data = opData[idx];
message.error(
<div style={{ width: 360 }}>
<div>
<b>
{data.title}: {data.url}
</b>
</div>
<div>This URL already exists, please edit it and try again.</div>
</div>,
);
}
hide();
});
};

Expand Down
24 changes: 24 additions & 0 deletions src/view/dashboard/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
overflow-y: auto;
padding: 15px;

&-no-data {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
flex-direction: column;
font-weight: bold;

.icon {
color: #989898;
font-size: 24px;
}

.txt {
font-size: 12px;

a {
color: #1677ff;
cursor: pointer;
}
}
}

&.dark {
background-color: #000;
}
Expand Down
21 changes: 20 additions & 1 deletion src/view/dashboard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import clsx from 'clsx';
import { Row, Col, Card } from 'antd';
import { InboxOutlined } from '@ant-design/icons';
import { os, invoke } from '@tauri-apps/api';

import useInit from '@/hooks/useInit';
Expand All @@ -10,7 +11,7 @@ import './index.scss';

export default function Dashboard() {
const { json } = useJson<Record<string, any>[]>(CHAT_AWESOME_JSON);
const [list, setList] = useState<Array<[string, Record<string, any>[]]>>([]);
const [list, setList] = useState<Array<[string, Record<string, any>[]]>>();
const [hasClass, setClass] = useState(false);
const [theme, setTheme] = useState('');

Expand All @@ -23,6 +24,7 @@ export default function Dashboard() {
});

useEffect(() => {
if (!json) return;
const categories = new Map();

json?.forEach((i) => {
Expand All @@ -43,6 +45,23 @@ export default function Dashboard() {
});
};

if (!list) return null;
if (list?.length === 0) {
return (
<div className="dashboard-no-data">
<div className="icon">
<InboxOutlined style={{ fontSize: 80, marginBottom: 5 }} />
<br />
No data
</div>
<div className="txt">
Go to <a onClick={() => invoke('control_window')}>{'Control Center -> Awesome'}</a> to add
data
</div>
</div>
);
}

return (
<div className={clsx('dashboard', theme, { 'has-top-dom': hasClass })}>
<div>
Expand Down
Loading

0 comments on commit ba438b0

Please sign in to comment.