Skip to content

Commit

Permalink
feat(gui): cache zustand store in localstorage (#2168)
Browse files Browse the repository at this point in the history
* fix(gui): use the store to cache week start

* feat(gui): cache zustand store in localStorage

This means that before we've loaded any data, we can still display
something up-to-date. Avoid flashing!

I'll probably want to switch this to the tauri sqlite plugin later
  • Loading branch information
ellie authored Jun 19, 2024
1 parent 5f66fb6 commit 80d28ea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
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" }),
);

0 comments on commit 80d28ea

Please sign in to comment.