-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: self-hosting improvements and rule import/export #1139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b2689d5
feat: add self-hosting improvements and rule import/export
rsnodgrass 8a0667e
fix: useCleanerEnabled checks both env var and PostHog flag
rsnodgrass 862304f
Update apps/web/utils/actions/rule.validation.ts
rsnodgrass 49d51fa
fix: include url and delayInMinutes in rule import/export
rsnodgrass 2dbc8a4
fix: validate imported rules have at least one condition
rsnodgrass 5fafc10
refactor: move rule import/export from Rules table to Settings tab
rsnodgrass 15e10ed
fix: persist categoryFilterType on rule import and remove unused grou…
rsnodgrass bc10296
Add Settings tab to Assistant for import/export access
rsnodgrass 383df27
Merge branch 'main' of github.com:elie222/inbox-zero
rsnodgrass 135c673
feat: add rule import/export for backup and migration
rsnodgrass 81a93de
Update apps/web/utils/actions/rule.validation.ts
rsnodgrass 84316be
Update apps/web/utils/actions/rule.validation.ts
rsnodgrass 3d06c87
fix(import): resolve folder names at runtime and validate label names
rsnodgrass 483b4b1
Merge branch 'feature/rule-import-export'
rsnodgrass 91708c2
fix(move-folder): add template validation and rename method to be pro…
rsnodgrass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
apps/web/app/(app)/[emailAccountId]/assistant/settings/RuleImportExportSetting.tsx
This file contains hidden or 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,146 @@ | ||
| "use client"; | ||
|
|
||
| import { useCallback, useRef } from "react"; | ||
| import { toast } from "sonner"; | ||
| import { DownloadIcon, UploadIcon } from "lucide-react"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { SettingCard } from "@/components/SettingCard"; | ||
| import { toastError } from "@/components/Toast"; | ||
| import { useRules } from "@/hooks/useRules"; | ||
| import { useAccount } from "@/providers/EmailAccountProvider"; | ||
| import { importRulesAction } from "@/utils/actions/rule"; | ||
|
|
||
| export function RuleImportExportSetting() { | ||
| const { data, mutate } = useRules(); | ||
| const { emailAccountId } = useAccount(); | ||
| const fileInputRef = useRef<HTMLInputElement>(null); | ||
|
|
||
| const exportRules = useCallback(() => { | ||
| if (!data) return; | ||
|
|
||
| const exportData = data.map((rule) => ({ | ||
| name: rule.name, | ||
| instructions: rule.instructions, | ||
| enabled: rule.enabled, | ||
| automate: rule.automate, | ||
| runOnThreads: rule.runOnThreads, | ||
| systemType: rule.systemType, | ||
| conditionalOperator: rule.conditionalOperator, | ||
| from: rule.from, | ||
| to: rule.to, | ||
| subject: rule.subject, | ||
| body: rule.body, | ||
| categoryFilterType: rule.categoryFilterType, | ||
| actions: rule.actions.map((action) => ({ | ||
| type: action.type, | ||
| label: action.label, | ||
| to: action.to, | ||
| cc: action.cc, | ||
| bcc: action.bcc, | ||
| subject: action.subject, | ||
| content: action.content, | ||
| folderName: action.folderName, | ||
| url: action.url, | ||
| delayInMinutes: action.delayInMinutes, | ||
| })), | ||
| group: rule.group?.name || null, | ||
| })); | ||
|
|
||
| const blob = new Blob([JSON.stringify(exportData, null, 2)], { | ||
| type: "application/json", | ||
| }); | ||
| const url = URL.createObjectURL(blob); | ||
| const a = document.createElement("a"); | ||
| a.href = url; | ||
| a.download = `inbox-zero-rules-${new Date().toISOString().split("T")[0]}.json`; | ||
| document.body.appendChild(a); | ||
| a.click(); | ||
| document.body.removeChild(a); | ||
| URL.revokeObjectURL(url); | ||
|
|
||
| toast.success("Rules exported successfully"); | ||
| }, [data]); | ||
|
|
||
| const importRules = useCallback( | ||
| async (event: React.ChangeEvent<HTMLInputElement>) => { | ||
| const file = event.target.files?.[0]; | ||
| if (!file) return; | ||
|
|
||
| try { | ||
| const text = await file.text(); | ||
| const rules = JSON.parse(text); | ||
|
|
||
| const rulesArray = Array.isArray(rules) ? rules : rules.rules; | ||
|
|
||
| if (!Array.isArray(rulesArray) || rulesArray.length === 0) { | ||
| toastError({ description: "Invalid rules file format" }); | ||
| return; | ||
| } | ||
|
|
||
| const result = await importRulesAction(emailAccountId, { | ||
| rules: rulesArray, | ||
| }); | ||
|
|
||
| if (result?.serverError) { | ||
| toastError({ | ||
| title: "Import failed", | ||
| description: result.serverError, | ||
| }); | ||
| } else if (result?.data) { | ||
| const { createdCount, updatedCount, skippedCount } = result.data; | ||
| toast.success( | ||
| `Imported ${createdCount} new, updated ${updatedCount} existing${skippedCount > 0 ? `, skipped ${skippedCount}` : ""}`, | ||
| ); | ||
| mutate(); | ||
| } | ||
| } catch (error) { | ||
| toastError({ | ||
| title: "Import failed", | ||
| description: | ||
| error instanceof Error ? error.message : "Failed to parse file", | ||
| }); | ||
| } | ||
|
|
||
| if (fileInputRef.current) { | ||
| fileInputRef.current.value = ""; | ||
| } | ||
| }, | ||
| [emailAccountId, mutate], | ||
| ); | ||
|
|
||
| return ( | ||
| <SettingCard | ||
| title="Import / Export Rules" | ||
| description="Backup your rules to a JSON file or restore from a previous export." | ||
| right={ | ||
| <div className="flex gap-2"> | ||
| <input | ||
| type="file" | ||
| ref={fileInputRef} | ||
| accept=".json" | ||
| onChange={importRules} | ||
| className="hidden" | ||
| aria-label="Import rules from JSON file" | ||
| /> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={() => fileInputRef.current?.click()} | ||
| > | ||
| <UploadIcon className="mr-2 size-4" /> | ||
| Import | ||
| </Button> | ||
| <Button | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={exportRules} | ||
| disabled={!data?.length} | ||
| > | ||
| <DownloadIcon className="mr-2 size-4" /> | ||
| Export | ||
| </Button> | ||
| </div> | ||
| } | ||
| /> | ||
| ); | ||
| } | ||
This file contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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 contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.