-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
310 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { inboxesAsyncAtom } from "@/backend/atoms/inboxes"; | ||
import { isFollowError } from "@/lib/follow"; | ||
import { atom } from "jotai"; | ||
import { unwrap } from "jotai/utils"; | ||
|
||
export const authStateAsyncAtom = atom(async (get) => { | ||
try { | ||
await get(inboxesAsyncAtom); | ||
return true; | ||
} catch (e) { | ||
if (isFollowError(e) && e.name === "AuthError") { | ||
return false; | ||
} | ||
return true; | ||
} | ||
}); | ||
|
||
export const authStateAtom = unwrap(authStateAsyncAtom, (prev) => prev); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { inboxesAtom, settingsAtom } from "@/backend/atoms"; | ||
import { settingsAtom } from "@/backend/atoms"; | ||
import { authStateAtom } from "@/backend/atoms/follow-client"; | ||
import { createStore } from "jotai"; | ||
|
||
export const container = createStore(); | ||
container.sub(settingsAtom, () => {}); | ||
|
||
container.sub(inboxesAtom, () => { | ||
console.log("inboxesAtom changed", container.get(inboxesAtom)); | ||
}); | ||
container.sub(authStateAtom, () => {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { addProperties } from "@/lib/lang"; | ||
import { atomWithRefresh, loadable as jotaiLoadable, selectAtom } from "jotai/utils"; | ||
import type { Atom, WritableAtom } from "jotai/vanilla"; | ||
import type { Loadable } from "jotai/vanilla/utils/loadable"; | ||
|
||
type Read<Value, Args extends unknown[], Result> = WritableAtom<Value, Args, Result>["read"]; | ||
|
||
export function asyncAtom<Value>(read: Read<Value, [], void>) { | ||
const baseAtom = atomWithRefresh(read); | ||
let loadableAtom: Atom<Loadable<Value>>; | ||
let unwrappedAtom: Atom<Awaited<Value> | undefined>; | ||
return addProperties(baseAtom, { loadable, unwrap }); | ||
|
||
function loadable() { | ||
loadableAtom = loadableAtom || jotaiLoadable(baseAtom); | ||
return loadableAtom; | ||
} | ||
|
||
function unwrap() { | ||
unwrappedAtom = | ||
unwrappedAtom || | ||
selectAtom(loadable(), (s, prevSlice) => { | ||
if (s.state === "loading") return prevSlice; | ||
if (s.state === "hasData") return s.data; | ||
return undefined; | ||
}); | ||
return unwrappedAtom; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useRef } from "react"; | ||
import type { SWRResponse } from "swr"; | ||
|
||
export function useLastSWRData<T>(swr: SWRResponse<T>): T | undefined { | ||
const ref = useRef(swr.data); | ||
if (!swr.isLoading) { | ||
ref.current = swr.data; | ||
} | ||
return ref.current; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.