feat(user): rename to identity
feat(identity): add getIota function with caching
This commit is contained in:
parent
c386e8d5cb
commit
042141c781
40 changed files with 613 additions and 668 deletions
47
packages/identity/src/wrapper.tsx
Normal file
47
packages/identity/src/wrapper.tsx
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { useEffect, useState } from "react";
|
||||
import { type SelectedUser, type UserFields, useUserFields } from "./context";
|
||||
|
||||
import { failedUser } from "@tensamin/shared/data";
|
||||
import { useStorage } from "@tensamin/storage/context";
|
||||
|
||||
// Wrapper function to pass user data to some component
|
||||
export default function Wrapper<const Fields extends UserFields>(props: {
|
||||
userId: number | "own";
|
||||
fields: Fields;
|
||||
loading: React.ReactNode;
|
||||
component: (user: SelectedUser<Fields>) => React.ReactNode;
|
||||
}) {
|
||||
const { load } = useStorage();
|
||||
const [ownUserId, setOwnUserId] = useState<number | null>(null);
|
||||
const [ownUserError, setOwnUserError] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (props.userId !== "own") return;
|
||||
let active = true;
|
||||
void load("user_id").then(
|
||||
(value) => {
|
||||
if (active) {
|
||||
setOwnUserId(value);
|
||||
setOwnUserError(false);
|
||||
}
|
||||
},
|
||||
() => {
|
||||
if (active) setOwnUserError(true);
|
||||
},
|
||||
);
|
||||
return () => {
|
||||
active = false;
|
||||
};
|
||||
}, [load, props.userId]);
|
||||
|
||||
const userId = props.userId === "own" ? ownUserId : props.userId;
|
||||
const result = useUserFields(userId, props.fields);
|
||||
if (
|
||||
(props.userId === "own" && ownUserError) ||
|
||||
(!result.loading && !result.data)
|
||||
) {
|
||||
return <>{props.component(failedUser as SelectedUser<Fields>)}</>;
|
||||
}
|
||||
|
||||
return <>{result.data ? props.component(result.data) : props.loading}</>;
|
||||
}
|
||||
Loading…
Reference in a new issue