generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 67
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
1 parent
761ec43
commit df40962
Showing
10 changed files
with
340 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as React from "react"; | ||
import { motion } from "framer-motion"; | ||
|
||
interface ErrorBoxProps { | ||
message: string; | ||
description?: string; | ||
actionButton?: React.ReactNode; | ||
} | ||
|
||
export const ErrorBox: React.FC<ErrorBoxProps> = ({ | ||
message, | ||
description, | ||
actionButton, | ||
}) => { | ||
return ( | ||
<motion.div | ||
initial={{ opacity: 0, y: -10 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
className="p-4 bg-[--background-primary-alt] rounded-lg shadow-md" | ||
> | ||
<div className="space-y-3"> | ||
<div className="flex items-start justify-between"> | ||
<div> | ||
<div className="text-[--text-error] font-medium mb-1"> | ||
{message} | ||
</div> | ||
{description && ( | ||
<p className="text-sm text-[--text-muted]"> | ||
{description} | ||
</p> | ||
)} | ||
</div> | ||
</div> | ||
|
||
{actionButton && ( | ||
<div className="flex items-center gap-2 mt-4"> | ||
{actionButton} | ||
</div> | ||
)} | ||
</div> | ||
</motion.div> | ||
); | ||
}; |
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,88 @@ | ||
import * as React from "react"; | ||
import { ErrorBox } from "./error-box"; | ||
import { EmptyState } from "./empty-state"; | ||
import FileOrganizer from "../../.."; | ||
|
||
interface LicenseValidatorProps { | ||
apiKey: string; | ||
onValidationComplete: () => void; | ||
plugin: FileOrganizer; | ||
} | ||
|
||
export const LicenseValidator: React.FC<LicenseValidatorProps> = ({ | ||
apiKey, | ||
onValidationComplete, | ||
plugin, | ||
}) => { | ||
const [isValidating, setIsValidating] = React.useState(true); | ||
const [licenseError, setLicenseError] = React.useState<string | null>(null); | ||
|
||
const validateLicense = React.useCallback(async () => { | ||
try { | ||
setIsValidating(true); | ||
setLicenseError(null); | ||
|
||
// should be replaced with a hardcoded value | ||
const response = await fetch(`${plugin.getServerUrl()}/api/check-key`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
if (!response.ok) { | ||
setLicenseError(data.error || "Invalid license key"); | ||
} else if (data.message !== "Valid key") { | ||
setLicenseError("Invalid license key response"); | ||
} else { | ||
onValidationComplete(); | ||
} | ||
} catch (err) { | ||
setLicenseError("Failed to validate license key"); | ||
} finally { | ||
setIsValidating(false); | ||
} | ||
}, [apiKey, onValidationComplete]); | ||
|
||
React.useEffect(() => { | ||
validateLicense(); | ||
}, [validateLicense]); | ||
|
||
if (isValidating) { | ||
return <EmptyState message="Validating license key..." />; | ||
} | ||
|
||
if (licenseError) { | ||
return ( | ||
<ErrorBox | ||
message={`License key error: ${licenseError}`} | ||
description="Please check your license key in the plugin settings." | ||
actionButton={ | ||
<div className="flex gap-2"> | ||
<button | ||
onClick={validateLicense} | ||
className="px-3 py-1.5 text-[--text-on-accent] rounded hover:opacity-90 transition-opacity duration-200" | ||
> | ||
Retry | ||
</button> | ||
<button | ||
onClick={() => { | ||
// Open Obsidian settings and navigate to plugin settings | ||
plugin.app.setting.open(); | ||
plugin.app.setting.openTabById("fileorganizer2000"); | ||
}} | ||
className="px-3 py-1.5 bg-[--interactive-accent] text-[--text-on-accent] rounded hover:bg-[--interactive-accent-hover] transition-colors duration-200" | ||
> | ||
Open Settings | ||
</button> | ||
</div> | ||
} | ||
/> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
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 was deleted.
Oops, something went wrong.
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.