mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-17 02:16:38 +00:00
Chungus
This commit is contained in:
135
src/hooks/privateUseCertificate.ts
Normal file
135
src/hooks/privateUseCertificate.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* This file is part of a proprietary work.
|
||||
*
|
||||
* Copyright (c) 2025 Fossorial, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is licensed under the Fossorial Commercial License.
|
||||
* You may not use this file except in compliance with the License.
|
||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
||||
*
|
||||
* This file is not licensed under the AGPLv3.
|
||||
*/
|
||||
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback, useEffect } from "react";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { GetCertificateResponse } from "@server/routers/private/certificates";
|
||||
import { createApiClient } from "@app/lib/api";
|
||||
import { useEnvContext } from "@app/hooks/useEnvContext";
|
||||
|
||||
type UseCertificateProps = {
|
||||
orgId: string;
|
||||
domainId: string;
|
||||
fullDomain: string;
|
||||
autoFetch?: boolean;
|
||||
polling?: boolean;
|
||||
pollingInterval?: number;
|
||||
};
|
||||
|
||||
type UseCertificateReturn = {
|
||||
cert: GetCertificateResponse | null;
|
||||
certLoading: boolean;
|
||||
certError: string | null;
|
||||
refreshing: boolean;
|
||||
fetchCert: () => Promise<void>;
|
||||
refreshCert: () => Promise<void>;
|
||||
clearCert: () => void;
|
||||
};
|
||||
|
||||
export function useCertificate({
|
||||
orgId,
|
||||
domainId,
|
||||
fullDomain,
|
||||
autoFetch = true,
|
||||
polling = false,
|
||||
pollingInterval = 5000
|
||||
}: UseCertificateProps): UseCertificateReturn {
|
||||
const api = createApiClient(useEnvContext());
|
||||
|
||||
const [cert, setCert] = useState<GetCertificateResponse | null>(null);
|
||||
const [certLoading, setCertLoading] = useState(false);
|
||||
const [certError, setCertError] = useState<string | null>(null);
|
||||
const [refreshing, setRefreshing] = useState(false);
|
||||
|
||||
const fetchCert = useCallback(async (showLoading = true) => {
|
||||
if (!orgId || !domainId || !fullDomain) return;
|
||||
|
||||
if (showLoading) {
|
||||
setCertLoading(true);
|
||||
}
|
||||
setCertError(null);
|
||||
try {
|
||||
const res = await api.get<AxiosResponse<GetCertificateResponse>>(
|
||||
`/org/${orgId}/certificate/${domainId}/${fullDomain}`
|
||||
);
|
||||
const certData = res.data.data;
|
||||
if (certData) {
|
||||
setCert(certData);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.error("Failed to fetch certificate:", error);
|
||||
setCertError("Failed to fetch certificate");
|
||||
} finally {
|
||||
if (showLoading) {
|
||||
setCertLoading(false);
|
||||
}
|
||||
}
|
||||
}, [api, orgId, domainId, fullDomain]);
|
||||
|
||||
const refreshCert = useCallback(async () => {
|
||||
if (!cert) return;
|
||||
|
||||
setRefreshing(true);
|
||||
setCertError(null);
|
||||
try {
|
||||
await api.post(
|
||||
`/org/${orgId}/certificate/${cert.certId}/restart`,
|
||||
{}
|
||||
);
|
||||
// Update status to pending
|
||||
setTimeout(() => {
|
||||
setCert({ ...cert, status: "pending" });
|
||||
}, 500);
|
||||
} catch (error: any) {
|
||||
console.error("Failed to restart certificate:", error);
|
||||
setCertError("Failed to restart certificate");
|
||||
} finally {
|
||||
setRefreshing(false);
|
||||
}
|
||||
}, [api, orgId, cert]);
|
||||
|
||||
const clearCert = useCallback(() => {
|
||||
setCert(null);
|
||||
setCertError(null);
|
||||
}, []);
|
||||
|
||||
// Auto-fetch on mount if enabled
|
||||
useEffect(() => {
|
||||
if (autoFetch && orgId && domainId && fullDomain) {
|
||||
fetchCert();
|
||||
}
|
||||
}, [autoFetch, orgId, domainId, fullDomain, fetchCert]);
|
||||
|
||||
// Polling effect
|
||||
useEffect(() => {
|
||||
if (!polling || !orgId || !domainId || !fullDomain) return;
|
||||
|
||||
const interval = setInterval(() => {
|
||||
fetchCert(false); // Don't show loading for polling
|
||||
}, pollingInterval);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [polling, orgId, domainId, fullDomain, pollingInterval, fetchCert]);
|
||||
|
||||
return {
|
||||
cert,
|
||||
certLoading,
|
||||
certError,
|
||||
refreshing,
|
||||
fetchCert,
|
||||
refreshCert,
|
||||
clearCert
|
||||
};
|
||||
}
|
||||
29
src/hooks/privateUseRemoteExitNodeContext.ts
Normal file
29
src/hooks/privateUseRemoteExitNodeContext.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* This file is part of a proprietary work.
|
||||
*
|
||||
* Copyright (c) 2025 Fossorial, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is licensed under the Fossorial Commercial License.
|
||||
* You may not use this file except in compliance with the License.
|
||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
||||
*
|
||||
* This file is not licensed under the AGPLv3.
|
||||
*/
|
||||
|
||||
"use client";
|
||||
|
||||
import RemoteExitNodeContext from "@app/contexts/privateRemoteExitNodeContext";
|
||||
import { build } from "@server/build";
|
||||
import { useContext } from "react";
|
||||
|
||||
export function useRemoteExitNodeContext() {
|
||||
if (build == "oss") {
|
||||
return null;
|
||||
}
|
||||
const context = useContext(RemoteExitNodeContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useRemoteExitNodeContext must be used within a RemoteExitNodeProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
29
src/hooks/privateUseSubscriptionStatusContext.ts
Normal file
29
src/hooks/privateUseSubscriptionStatusContext.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* This file is part of a proprietary work.
|
||||
*
|
||||
* Copyright (c) 2025 Fossorial, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This file is licensed under the Fossorial Commercial License.
|
||||
* You may not use this file except in compliance with the License.
|
||||
* Unauthorized use, copying, modification, or distribution is strictly prohibited.
|
||||
*
|
||||
* This file is not licensed under the AGPLv3.
|
||||
*/
|
||||
|
||||
import PrivateSubscriptionStatusContext from "@app/contexts/privateSubscriptionStatusContext";
|
||||
import { build } from "@server/build";
|
||||
import { useContext } from "react";
|
||||
|
||||
export function usePrivateSubscriptionStatusContext() {
|
||||
if (build == "oss") {
|
||||
return null;
|
||||
}
|
||||
const context = useContext(PrivateSubscriptionStatusContext);
|
||||
if (context === undefined) {
|
||||
throw new Error(
|
||||
"usePrivateSubscriptionStatusContext must be used within an PrivateSubscriptionStatusProvider"
|
||||
);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
Reference in New Issue
Block a user