Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gui): cache zustand store in localstorage #2168

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function App() {
</ul>
</li>
<li className="mt-auto">
{user && !user.isLoggedIn() && (
{user && user.username === "" && !user.username && (
<Dialog>
<DialogTrigger className="w-full">
<Button
Expand Down
10 changes: 2 additions & 8 deletions ui/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ const explicitTheme: ThemeInput = {
};

export default function Home() {
const [weekStart, setWeekStart] = useState(0);

const homeInfo = useStore((state) => state.homeInfo);
const user = useStore((state) => state.user);
const calendar = useStore((state) => state.calendar);
const weekStart = useStore((state) => state.weekStart);

const refreshHomeInfo = useStore((state) => state.refreshHomeInfo);
const refreshUser = useStore((state) => state.refreshUser);
Expand All @@ -66,10 +65,6 @@ export default function Home() {
const { toast } = useToast();

useEffect(() => {
let locale = new Intl.Locale(navigator.language);
let weekinfo = locale.getWeekInfo();
setWeekStart(weekinfo.firstDay);

refreshHomeInfo();
refreshUser();
refreshCalendar();
Expand Down Expand Up @@ -110,7 +105,6 @@ export default function Home() {
<Header name={user.username} />

<div className="pt-10">
<h2 className="text-xl font-bold">Sync</h2>
<Stats
stats={[
{
Expand All @@ -132,7 +126,7 @@ export default function Home() {
/>
</div>

<div className="pt-10">
<div className="pt-10 flex justify-around">
<ActivityCalendar
theme={explicitTheme}
data={calendar}
Expand Down
12 changes: 10 additions & 2 deletions ui/src/state/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";

import { parseISO } from "date-fns";

import { fetch } from "@tauri-apps/plugin-http";
Expand Down Expand Up @@ -26,6 +28,7 @@ interface AtuinState {
vars: Var[];
shellHistory: ShellHistory[];
calendar: any[];
weekStart: number;

refreshHomeInfo: () => void;
refreshCalendar: () => void;
Expand All @@ -36,13 +39,14 @@ interface AtuinState {
historyNextPage: (query?: string) => void;
}

export const useStore = create<AtuinState>()((set, get) => ({
let state = (set, get): AtuinState => ({
user: DefaultUser,
homeInfo: DefaultHomeInfo,
aliases: [],
vars: [],
shellHistory: [],
calendar: [],
weekStart: new Intl.Locale(navigator.language).getWeekInfo().firstDay,

refreshAliases: () => {
invoke("aliases").then((aliases: any) => {
Expand Down Expand Up @@ -135,4 +139,8 @@ export const useStore = create<AtuinState>()((set, get) => ({
});
}
},
}));
});

export const useStore = create<AtuinState>()(
persist(state, { name: "atuin-storage" }),
);
Loading