mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-18 21:19:44 +00:00
feat: update popup initial state handler
This commit is contained in:
parent
b6967abd08
commit
bb389ad429
@ -1,19 +1,25 @@
|
|||||||
import { Button } from "@mui/joy";
|
import { Button } from "@mui/joy";
|
||||||
import { useStorage } from "@plasmohq/storage/hook";
|
import { useStorage } from "@plasmohq/storage/hook";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { toast } from "react-hot-toast";
|
import { toast } from "react-hot-toast";
|
||||||
import { Shortcut } from "@/types/proto/api/v2/shortcut_service_pb";
|
import { Shortcut } from "@/types/proto/api/v2/shortcut_service_pb";
|
||||||
import "../style.css";
|
import "../style.css";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
|
|
||||||
function PullShortcutsButton() {
|
const PullShortcutsButton = () => {
|
||||||
const [domain] = useStorage("domain");
|
const [domain] = useStorage("domain");
|
||||||
const [accessToken] = useStorage("access_token");
|
const [accessToken] = useStorage("access_token");
|
||||||
const [, setShortcuts] = useStorage("shortcuts");
|
const [, setShortcuts] = useStorage("shortcuts");
|
||||||
const [isPulling, setIsPulling] = useState(false);
|
const [isPulling, setIsPulling] = useState(false);
|
||||||
|
|
||||||
const handlePullShortcuts = async () => {
|
useEffect(() => {
|
||||||
|
if (domain && accessToken) {
|
||||||
|
handlePullShortcuts(true);
|
||||||
|
}
|
||||||
|
}, [domain, accessToken]);
|
||||||
|
|
||||||
|
const handlePullShortcuts = async (silence = false) => {
|
||||||
try {
|
try {
|
||||||
setIsPulling(true);
|
setIsPulling(true);
|
||||||
const { data } = await axios.get<Shortcut[]>(`${domain}/api/v1/shortcut`, {
|
const { data } = await axios.get<Shortcut[]>(`${domain}/api/v1/shortcut`, {
|
||||||
@ -22,7 +28,9 @@ function PullShortcutsButton() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
setShortcuts(data);
|
setShortcuts(data);
|
||||||
toast.success("Shortcuts pulled");
|
if (!silence) {
|
||||||
|
toast.success("Shortcuts pulled");
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error("Failed to pull shortcuts, error: " + error.message);
|
toast.error("Failed to pull shortcuts, error: " + error.message);
|
||||||
}
|
}
|
||||||
@ -30,10 +38,10 @@ function PullShortcutsButton() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Button loading={isPulling} color="neutral" variant="plain" size="sm" onClick={handlePullShortcuts}>
|
<Button loading={isPulling} color="neutral" variant="plain" size="sm" onClick={() => handlePullShortcuts()}>
|
||||||
<Icon.RefreshCcw className="w-4 h-auto" />
|
<Icon.RefreshCcw className="w-4 h-auto" />
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default PullShortcutsButton;
|
export default PullShortcutsButton;
|
||||||
|
@ -1,15 +1,14 @@
|
|||||||
import type { Shortcut } from "@/types/proto/api/v2/shortcut_service_pb";
|
|
||||||
import { Storage } from "@plasmohq/storage";
|
import { Storage } from "@plasmohq/storage";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
|
||||||
const storage = new Storage();
|
const storage = new Storage();
|
||||||
|
|
||||||
export const getShortcutList = () => {
|
|
||||||
const queryList = [];
|
|
||||||
return axios.get<Shortcut[]>(`/api/v1/shortcut?${queryList.join("&")}`);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getUrlFavicon = async (url: string) => {
|
export const getUrlFavicon = async (url: string) => {
|
||||||
const domain = await storage.getItem("domain");
|
const domain = await storage.getItem<string>("domain");
|
||||||
return axios.get<string>(`${domain}/api/v1/url/favicon?url=${url}`);
|
const accessToken = await storage.getItem<string>("access_token");
|
||||||
|
return axios.get<string>(`${domain}/api/v1/url/favicon?url=${url}`, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
@ -10,7 +10,7 @@ interface SettingState {
|
|||||||
accessToken: string;
|
accessToken: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function IndexOptions() {
|
const IndexOptions = () => {
|
||||||
const [domain, setDomain] = useStorage<string>("domain", (v) => (v ? v : ""));
|
const [domain, setDomain] = useStorage<string>("domain", (v) => (v ? v : ""));
|
||||||
const [accessToken, setAccessToken] = useStorage<string>("access_token", (v) => (v ? v : ""));
|
const [accessToken, setAccessToken] = useStorage<string>("access_token", (v) => (v ? v : ""));
|
||||||
const [settingState, setSettingState] = useState<SettingState>({
|
const [settingState, setSettingState] = useState<SettingState>({
|
||||||
@ -33,11 +33,6 @@ function IndexOptions() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSaveSetting = () => {
|
const handleSaveSetting = () => {
|
||||||
if (!settingState.domain || !settingState.accessToken) {
|
|
||||||
toast.error("Domain and access token are required");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setDomain(settingState.domain);
|
setDomain(settingState.domain);
|
||||||
setAccessToken(settingState.accessToken);
|
setAccessToken(settingState.accessToken);
|
||||||
toast.success("Setting saved");
|
toast.success("Setting saved");
|
||||||
@ -89,6 +84,6 @@ function IndexOptions() {
|
|||||||
<Toaster position="top-center" />
|
<Toaster position="top-center" />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default IndexOptions;
|
export default IndexOptions;
|
||||||
|
@ -7,7 +7,7 @@ import PullShortcutsButton from "./components/PullShortcutsButton";
|
|||||||
import ShortcutsContainer from "./components/ShortcutsContainer";
|
import ShortcutsContainer from "./components/ShortcutsContainer";
|
||||||
import "./style.css";
|
import "./style.css";
|
||||||
|
|
||||||
function IndexPopup() {
|
const IndexPopup = () => {
|
||||||
const [domain] = useStorage<string>("domain", "");
|
const [domain] = useStorage<string>("domain", "");
|
||||||
const [accessToken] = useStorage<string>("access_token", "");
|
const [accessToken] = useStorage<string>("access_token", "");
|
||||||
const [shortcuts] = useStorage<Shortcut[]>("shortcuts", []);
|
const [shortcuts] = useStorage<Shortcut[]>("shortcuts", []);
|
||||||
@ -17,6 +17,10 @@ function IndexPopup() {
|
|||||||
chrome.runtime.openOptionsPage();
|
chrome.runtime.openOptionsPage();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleRefreshButtonClick = () => {
|
||||||
|
chrome.runtime.reload();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="w-full min-w-[480px] p-4">
|
<div className="w-full min-w-[480px] p-4">
|
||||||
@ -41,13 +45,34 @@ function IndexPopup() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="w-full mt-4">
|
<div className="w-full mt-4">
|
||||||
<ShortcutsContainer />
|
{isInitialized ? (
|
||||||
|
shortcuts.length !== 0 ? (
|
||||||
|
<ShortcutsContainer />
|
||||||
|
) : (
|
||||||
|
<div className="w-full flex flex-col justify-center items-center">
|
||||||
|
<p>No shortcut found.</p>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
) : (
|
||||||
|
<div className="w-full flex flex-col justify-start items-center">
|
||||||
|
<p>No domain and access token found.</p>
|
||||||
|
<div className="w-full flex flex-row justify-center items-center py-4">
|
||||||
|
<Button size="sm" color="primary" onClick={handleSettingButtonClick}>
|
||||||
|
<Icon.Settings className="w-5 h-auto mr-1" /> Setting
|
||||||
|
</Button>
|
||||||
|
<span className="mx-2">Or</span>
|
||||||
|
<Button size="sm" variant="outlined" color="neutral" onClick={handleRefreshButtonClick}>
|
||||||
|
<Icon.RefreshCcw className="w-5 h-auto mr-1" /> Refresh
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Toaster position="top-right" />
|
<Toaster position="top-right" />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default IndexPopup;
|
export default IndexPopup;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user