mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-11 04:12:26 +00:00
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import ResourceContext from "@app/contexts/resourceContext";
|
|
import { GetResourceAuthInfoResponse } from "@server/routers/resource";
|
|
import { GetResourceResponse } from "@server/routers/resource/getResource";
|
|
import { useState } from "react";
|
|
|
|
interface ResourceProviderProps {
|
|
children: React.ReactNode;
|
|
resource: GetResourceResponse;
|
|
authInfo: GetResourceAuthInfoResponse;
|
|
}
|
|
|
|
export function ResourceProvider({
|
|
children,
|
|
resource: serverResource,
|
|
authInfo: serverAuthInfo,
|
|
}: ResourceProviderProps) {
|
|
const [resource, setResource] =
|
|
useState<GetResourceResponse>(serverResource);
|
|
|
|
const [authInfo, setAuthInfo] =
|
|
useState<GetResourceAuthInfoResponse>(serverAuthInfo);
|
|
|
|
const updateResource = (updatedResource: Partial<GetResourceResponse>) => {
|
|
if (!resource) {
|
|
throw new Error("No resource to update");
|
|
}
|
|
|
|
setResource((prev) => {
|
|
if (!prev) {
|
|
return prev;
|
|
}
|
|
|
|
return {
|
|
...prev,
|
|
...updatedResource,
|
|
};
|
|
});
|
|
};
|
|
|
|
const updateAuthInfo = (
|
|
updatedAuthInfo: Partial<GetResourceAuthInfoResponse>
|
|
) => {
|
|
if (!authInfo) {
|
|
throw new Error("No auth info to update");
|
|
}
|
|
|
|
setAuthInfo((prev) => {
|
|
if (!prev) {
|
|
return prev;
|
|
}
|
|
|
|
return {
|
|
...prev,
|
|
...updatedAuthInfo,
|
|
};
|
|
});
|
|
};
|
|
|
|
return (
|
|
<ResourceContext.Provider
|
|
value={{ resource, updateResource, authInfo, updateAuthInfo }}
|
|
>
|
|
{children}
|
|
</ResourceContext.Provider>
|
|
);
|
|
}
|
|
|
|
export default ResourceProvider;
|