@hirall/react
Version:
React hooks and components for Hirall
263 lines (262 loc) • 7.29 kB
JavaScript
// src/index.tsx
import { createContext, useContext, useState, useEffect, useCallback } from "react";
import { HirallClient } from "@hirall/client";
var HirallContext = createContext(null);
function HirallProvider({ client, children }) {
return /* @__PURE__ */ React.createElement(HirallContext.Provider, { value: client }, children);
}
function useHirall() {
const client = useContext(HirallContext);
if (!client) {
throw new Error("useHirall must be used within HirallProvider");
}
return client;
}
function useAuth() {
const hirall = useHirall();
const [user, setUser] = useState(null);
const [session, setSession] = useState(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
hirall.auth.getSession().then(({ data }) => {
setSession(data);
setUser((data == null ? void 0 : data.user) || null);
setLoading(false);
});
const { unsubscribe } = hirall.auth.onAuthStateChange((event) => {
var _a;
setSession(event.session);
setUser(((_a = event.session) == null ? void 0 : _a.user) || null);
});
return () => {
unsubscribe();
};
}, [hirall]);
const signUp = useCallback(
async (email, password) => {
const result = await hirall.auth.signUp({ email, password });
if (result.data) {
setSession(result.data);
setUser(result.data.user);
}
return result;
},
[hirall]
);
const signIn = useCallback(
async (email, password) => {
const result = await hirall.auth.signIn({ email, password });
if (result.data) {
setSession(result.data);
setUser(result.data.user);
}
return result;
},
[hirall]
);
const signOut = useCallback(async () => {
const result = await hirall.auth.signOut();
setSession(null);
setUser(null);
return result;
}, [hirall]);
return {
user,
session,
loading,
signUp,
signIn,
signOut
};
}
function useQuery(queryFn, options) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const fetchData = useCallback(async () => {
var _a, _b, _c;
try {
setLoading(true);
const result = await queryFn();
if (result.error) {
setError(result.error);
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, result.error);
} else {
setData(result.data);
(_b = options == null ? void 0 : options.onSuccess) == null ? void 0 : _b.call(options, result.data);
}
} catch (err) {
setError(err);
(_c = options == null ? void 0 : options.onError) == null ? void 0 : _c.call(options, err);
} finally {
setLoading(false);
}
}, [queryFn, options]);
useEffect(() => {
if ((options == null ? void 0 : options.enabled) !== false) {
fetchData();
}
if (options == null ? void 0 : options.refetchInterval) {
const interval = setInterval(fetchData, options.refetchInterval);
return () => clearInterval(interval);
}
}, [fetchData, options == null ? void 0 : options.enabled, options == null ? void 0 : options.refetchInterval]);
return {
data,
loading,
error,
refetch: fetchData
};
}
function useMutation(mutationFn, options) {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const mutate = useCallback(
async (variables) => {
var _a, _b, _c;
try {
setLoading(true);
setError(null);
const result = await mutationFn(variables);
if (result.error) {
setError(result.error);
(_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call(options, result.error, variables);
} else {
setData(result.data);
(_b = options == null ? void 0 : options.onSuccess) == null ? void 0 : _b.call(options, result.data, variables);
}
return result;
} catch (err) {
setError(err);
(_c = options == null ? void 0 : options.onError) == null ? void 0 : _c.call(options, err, variables);
return { data: null, error: err, status: 0 };
} finally {
setLoading(false);
}
},
[mutationFn, options]
);
return {
mutate,
loading,
error,
data
};
}
function useRealtime(channelName, event, options) {
const hirall = useHirall();
const [connected, setConnected] = useState(false);
useEffect(() => {
const channel = hirall.realtime.channel(channelName).on(event, (payload) => {
var _a;
(_a = options == null ? void 0 : options.onMessage) == null ? void 0 : _a.call(options, payload);
}).subscribe();
hirall.realtime.connect();
setConnected(true);
return () => {
channel.unsubscribe();
setConnected(false);
};
}, [hirall, channelName, event, options]);
const send = useCallback(
(eventName, payload) => {
hirall.realtime.channel(channelName).send(eventName, payload);
},
[hirall, channelName]
);
return {
send,
connected
};
}
function useStorage(bucketName) {
const hirall = useHirall();
const [uploading, setUploading] = useState(false);
const [error, setError] = useState(null);
const upload = useCallback(
async (path, file) => {
try {
setUploading(true);
setError(null);
const result = await hirall.storage.bucket(bucketName).upload(path, file);
if (result.error) {
setError(result.error);
}
return result;
} catch (err) {
setError(err);
return { data: null, error: err, status: 0 };
} finally {
setUploading(false);
}
},
[hirall, bucketName]
);
const download = useCallback(
async (path) => {
try {
setError(null);
const result = await hirall.storage.bucket(bucketName).download(path);
if (result.error) {
setError(result.error);
}
return result;
} catch (err) {
setError(err);
return { data: null, error: err, status: 0 };
}
},
[hirall, bucketName]
);
const remove = useCallback(
async (path) => {
try {
setError(null);
const result = await hirall.storage.bucket(bucketName).delete(path);
if (result.error) {
setError(result.error);
}
return result;
} catch (err) {
setError(err);
return { data: null, error: err, status: 0 };
}
},
[hirall, bucketName]
);
const getUrl = useCallback(
async (path) => {
try {
setError(null);
const result = await hirall.storage.bucket(bucketName).getSignedUrl(path);
if (result.error) {
setError(result.error);
}
return result;
} catch (err) {
setError(err);
return { data: null, error: err, status: 0 };
}
},
[hirall, bucketName]
);
return {
upload,
download,
remove,
getUrl,
uploading,
error
};
}
export {
HirallClient,
HirallProvider,
useAuth,
useHirall,
useMutation,
useQuery,
useRealtime,
useStorage
};