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

* 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
This commit is contained in:
Ellie Huxtable 2024-06-19 15:46:53 +01:00 committed by GitHub
parent 5f66fb6a03
commit 80d28ea2da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 11 deletions

View File

@ -105,7 +105,7 @@ function App() {
</ul> </ul>
</li> </li>
<li className="mt-auto"> <li className="mt-auto">
{user && !user.isLoggedIn() && ( {user && user.username === "" && !user.username && (
<Dialog> <Dialog>
<DialogTrigger className="w-full"> <DialogTrigger className="w-full">
<Button <Button

View File

@ -53,11 +53,10 @@ const explicitTheme: ThemeInput = {
}; };
export default function Home() { export default function Home() {
const [weekStart, setWeekStart] = useState(0);
const homeInfo = useStore((state) => state.homeInfo); const homeInfo = useStore((state) => state.homeInfo);
const user = useStore((state) => state.user); const user = useStore((state) => state.user);
const calendar = useStore((state) => state.calendar); const calendar = useStore((state) => state.calendar);
const weekStart = useStore((state) => state.weekStart);
const refreshHomeInfo = useStore((state) => state.refreshHomeInfo); const refreshHomeInfo = useStore((state) => state.refreshHomeInfo);
const refreshUser = useStore((state) => state.refreshUser); const refreshUser = useStore((state) => state.refreshUser);
@ -66,10 +65,6 @@ export default function Home() {
const { toast } = useToast(); const { toast } = useToast();
useEffect(() => { useEffect(() => {
let locale = new Intl.Locale(navigator.language);
let weekinfo = locale.getWeekInfo();
setWeekStart(weekinfo.firstDay);
refreshHomeInfo(); refreshHomeInfo();
refreshUser(); refreshUser();
refreshCalendar(); refreshCalendar();
@ -110,7 +105,6 @@ export default function Home() {
<Header name={user.username} /> <Header name={user.username} />
<div className="pt-10"> <div className="pt-10">
<h2 className="text-xl font-bold">Sync</h2>
<Stats <Stats
stats={[ stats={[
{ {
@ -132,7 +126,7 @@ export default function Home() {
/> />
</div> </div>
<div className="pt-10"> <div className="pt-10 flex justify-around">
<ActivityCalendar <ActivityCalendar
theme={explicitTheme} theme={explicitTheme}
data={calendar} data={calendar}

View File

@ -1,4 +1,6 @@
import { create } from "zustand"; import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import { parseISO } from "date-fns"; import { parseISO } from "date-fns";
import { fetch } from "@tauri-apps/plugin-http"; import { fetch } from "@tauri-apps/plugin-http";
@ -26,6 +28,7 @@ interface AtuinState {
vars: Var[]; vars: Var[];
shellHistory: ShellHistory[]; shellHistory: ShellHistory[];
calendar: any[]; calendar: any[];
weekStart: number;
refreshHomeInfo: () => void; refreshHomeInfo: () => void;
refreshCalendar: () => void; refreshCalendar: () => void;
@ -36,13 +39,14 @@ interface AtuinState {
historyNextPage: (query?: string) => void; historyNextPage: (query?: string) => void;
} }
export const useStore = create<AtuinState>()((set, get) => ({ let state = (set, get): AtuinState => ({
user: DefaultUser, user: DefaultUser,
homeInfo: DefaultHomeInfo, homeInfo: DefaultHomeInfo,
aliases: [], aliases: [],
vars: [], vars: [],
shellHistory: [], shellHistory: [],
calendar: [], calendar: [],
weekStart: new Intl.Locale(navigator.language).getWeekInfo().firstDay,
refreshAliases: () => { refreshAliases: () => {
invoke("aliases").then((aliases: any) => { invoke("aliases").then((aliases: any) => {
@ -135,4 +139,8 @@ export const useStore = create<AtuinState>()((set, get) => ({
}); });
} }
}, },
})); });
export const useStore = create<AtuinState>()(
persist(state, { name: "atuin-storage" }),
);