Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(app): add theme assets, sharing and backup sections

Hampus516104d78e81
diff --git a/fluxer_app/src/features/theme_settings/components/AssetsSection.tsx b/fluxer_app/src/features/theme_settings/components/AssetsSection.tsx
new file mode 100644
index 000000000..3be63d811
--- /dev/null
+++ b/fluxer_app/src/features/theme_settings/components/AssetsSection.tsx
@@ -0,0 +1,265 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {SettingsSection} from '@app/features/app/components/dialogs/shared/SettingsSection';
+import {remFromPx} from '@app/features/theme/layout/RemFromPx';
+import ThemeLibrary from '@app/features/theme/state/ThemeLibrary';
+import {createThemeAssetReference, createThemeLocalFileReference} from '@app/features/theme/utils/ThemeCssUtils';
+import styles from '@app/features/theme_settings/ThemesTab.module.css';
+import {showThemeSettingsErrorModal} from '@app/features/theme_settings/utils/ThemeSettingsErrorModal';
+import {Button} from '@app/features/ui/button/Button';
+import * as ToastCommands from '@app/features/ui/commands/ToastCommands';
+import FocusRing from '@app/features/ui/focus_ring/FocusRing';
+import {getElectronAPI, isDesktop} from '@app/features/ui/utils/NativeUtils';
+import {copyText} from '@app/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils';
+import {ASSETS_SECTION_DESCRIPTOR} from '@app/features/user/components/settings_utils/section_registry/ThemesSections';
+import {msg} from '@lingui/core/macro';
+import {Trans, useLingui} from '@lingui/react/macro';
+import {CopyIcon, FilePlusIcon, TrashIcon, UploadSimpleIcon} from '@phosphor-icons/react';
+import {clsx} from 'clsx';
+import {observer} from 'mobx-react-lite';
+import type React from 'react';
+import {useRef} from 'react';
+
+const ASSETS_DESCRIPTION_DESCRIPTOR = msg({
+ message:
+ "Upload images and fonts to use in your CSS. Copy a file's reference and paste it into quick CSS or a theme.",
+ comment: 'Description of the "Assets" section in Settings > Themes. Quick CSS is custom CSS the user writes.',
+});
+const UPLOADED_ASSETS_DESCRIPTOR = msg({
+ message: 'Uploaded assets',
+ comment: 'Heading in the "Assets" section in Settings > Themes.',
+});
+const LOCAL_FILE_REFERENCES_DESCRIPTOR = msg({
+ message: 'Local file references',
+ comment: 'Heading in the "Assets" section in Settings > Themes for files on this computer that CSS can use.',
+});
+const NO_UPLOADED_ASSETS_DESCRIPTOR = msg({
+ message: 'No assets yet',
+ comment: 'Empty state in the "Assets" section in Settings > Themes.',
+});
+const NO_LOCAL_FILES_REFERENCED_DESCRIPTOR = msg({
+ message: 'No local files referenced',
+ comment: 'Empty state for local file references in Settings > Themes.',
+});
+const AVAILABLE_IN_THE_DESKTOP_APP_DESCRIPTOR = msg({
+ message: 'Available in the desktop app.',
+ comment: 'Shown for local file references in Settings > Themes on the web app.',
+});
+const UPLOADED_ASSETS_TOAST_DESCRIPTOR = msg({
+ message: 'Uploaded {length, plural, one {# theme asset} other {# theme assets}}.',
+ comment: 'Toast after uploading one or more theme asset files.',
+});
+const ADDED_LOCAL_FILES_DESCRIPTOR = msg({
+ message: 'Added {length, plural, one {# local file reference} other {# local file references}}.',
+ comment: 'Toast after adding one or more desktop local file references for theme CSS.',
+});
+const REFERENCE_COPIED_DESCRIPTOR = msg({
+ message: 'Reference copied.',
+ comment: 'Toast after copying the CSS reference of a theme asset.',
+});
+const WE_COULDN_T_COPY_THAT_REFERENCE_DESCRIPTOR = msg({
+ message: "We couldn't copy that reference.",
+ comment: 'Error message after copying the CSS reference of a theme asset failed.',
+});
+const COPY_REFERENCE_FOR_DESCRIPTOR = msg({
+ message: 'Copy reference for {fileName}',
+ comment: 'Accessible label for copying the CSS reference of a theme asset or local file.',
+});
+const DELETE_FILE_DESCRIPTOR = msg({
+ message: 'Delete {fileName}',
+ comment: 'Accessible label for deleting a theme asset or local file reference.',
+});
+
+interface AssetRowProps {
+ name: string;
+ reference: string;
+ onDelete: () => void;
+}
+
+const AssetRow: React.FC<AssetRowProps> = ({name, reference, onDelete}) => {
+ const {i18n} = useLingui();
+ const handleCopy = () => {
+ void copyText(reference)
+ .then(() => ToastCommands.success(i18n._(REFERENCE_COPIED_DESCRIPTOR)))
+ .catch(() =>
+ showThemeSettingsErrorModal(
+ i18n,
+ () => i18n._(WE_COULDN_T_COPY_THAT_REFERENCE_DESCRIPTOR),
+ 'theme-settings.assets-section.copy-reference-error-modal',
+ ),
+ );
+ };
+ return (
+ <div className={styles.row} data-flx="theme-settings.assets-section.asset-row.row">
+ <div className={styles.rowText} data-flx="theme-settings.assets-section.asset-row.row-text">
+ <span className={styles.rowLabel} data-flx="theme-settings.assets-section.asset-row.row-label">
+ {name}
+ </span>
+ <span className={styles.rowMeta} data-flx="theme-settings.assets-section.asset-row.row-meta">
+ {reference}
+ </span>
+ </div>
+ <div className={styles.rowControls} data-flx="theme-settings.assets-section.asset-row.row-controls">
+ <FocusRing offset={-2} data-flx="theme-settings.assets-section.asset-row.focus-ring">
+ <button
+ type="button"
+ className={styles.iconButton}
+ aria-label={i18n._(COPY_REFERENCE_FOR_DESCRIPTOR, {fileName: name})}
+ onClick={handleCopy}
+ data-flx="theme-settings.assets-section.asset-row.icon-button.copy"
+ >
+ <CopyIcon size={remFromPx(18)} data-flx="theme-settings.assets-section.asset-row.copy-icon" />
+ </button>
+ </FocusRing>
+ <FocusRing offset={-2} data-flx="theme-settings.assets-section.asset-row.focus-ring--2">
+ <button
+ type="button"
+ className={clsx(styles.iconButton, styles.iconButtonDanger)}
+ aria-label={i18n._(DELETE_FILE_DESCRIPTOR, {fileName: name})}
+ onClick={onDelete}
+ data-flx="theme-settings.assets-section.asset-row.icon-button.delete"
+ >
+ <TrashIcon size={remFromPx(18)} data-flx="theme-settings.assets-section.asset-row.trash-icon" />
+ </button>
+ </FocusRing>
+ </div>
+ </div>
+ );
+};
+
+export const AssetsSection: React.FC = observer(() => {
+ const {i18n} = useLingui();
+ const assetInputRef = useRef<HTMLInputElement | null>(null);
+ const hasDesktopFileAccess = isDesktop() && Boolean(getElectronAPI()?.pickThemeLocalFiles);
+ const handleUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
+ const files = [...(event.target.files ?? [])];
+ event.target.value = '';
+ if (files.length === 0) return;
+ void ThemeLibrary.uploadAssets(files).then((assets) => {
+ ToastCommands.success(i18n._(UPLOADED_ASSETS_TOAST_DESCRIPTOR, {length: assets.length}));
+ });
+ };
+ const handleAddLocalFiles = () => {
+ void ThemeLibrary.addDesktopLocalFiles().then((files) => {
+ if (files.length === 0) return;
+ ToastCommands.success(i18n._(ADDED_LOCAL_FILES_DESCRIPTOR, {length: files.length}));
+ });
+ };
+ const title = i18n._(ASSETS_SECTION_DESCRIPTOR);
+ const description = i18n._(ASSETS_DESCRIPTION_DESCRIPTOR);
+ const uploadedAssets = (
+ <div className={styles.stack} data-flx="theme-settings.assets-section.stack">
+ <div className={styles.subsectionHeader} data-flx="theme-settings.assets-section.subsection-header">
+ <h4 className={styles.subsectionTitle} data-flx="theme-settings.assets-section.subsection-title">
+ {i18n._(UPLOADED_ASSETS_DESCRIPTOR)}
+ </h4>
+ <div className={styles.subsectionActions} data-flx="theme-settings.assets-section.subsection-actions">
+ <Button
+ variant="secondary"
+ small
+ leftIcon={
+ <UploadSimpleIcon size={remFromPx(16)} data-flx="theme-settings.assets-section.upload-simple-icon" />
+ }
+ onClick={() => assetInputRef.current?.click()}
+ data-flx="theme-settings.assets-section.button.click"
+ >
+ <Trans>Upload assets</Trans>
+ </Button>
+ <input
+ ref={assetInputRef}
+ type="file"
+ multiple
+ hidden
+ onChange={handleUpload}
+ aria-hidden
+ data-flx="theme-settings.assets-section.input.upload.file"
+ />
+ </div>
+ </div>
+ {ThemeLibrary.assets.length === 0 ? (
+ <p className={styles.note} data-flx="theme-settings.assets-section.note">
+ {i18n._(NO_UPLOADED_ASSETS_DESCRIPTOR)}
+ </p>
+ ) : (
+ <div className={styles.list} data-flx="theme-settings.assets-section.list">
+ {ThemeLibrary.assets.map((asset) => (
+ <AssetRow
+ key={asset.id}
+ name={asset.name}
+ reference={createThemeAssetReference(asset.name)}
+ onDelete={() => {
+ void ThemeLibrary.deleteAsset(asset.id);
+ }}
+ data-flx="theme-settings.assets-section.asset-row"
+ />
+ ))}
+ </div>
+ )}
+ </div>
+ );
+ const localFileStackClass = styles.stack;
+ const localFileHeader = (
+ <div className={styles.subsectionHeader} data-flx="theme-settings.assets-section.subsection-header--2">
+ <h4 className={styles.subsectionTitle} data-flx="theme-settings.assets-section.subsection-title--2">
+ {i18n._(LOCAL_FILE_REFERENCES_DESCRIPTOR)}
+ </h4>
+ {hasDesktopFileAccess ? (
+ <div className={styles.subsectionActions} data-flx="theme-settings.assets-section.subsection-actions--2">
+ <Button
+ variant="secondary"
+ small
+ leftIcon={<FilePlusIcon size={remFromPx(16)} data-flx="theme-settings.assets-section.file-plus-icon" />}
+ onClick={handleAddLocalFiles}
+ data-flx="theme-settings.assets-section.button.add-local-files"
+ >
+ <Trans>Add local files</Trans>
+ </Button>
+ </div>
+ ) : null}
+ </div>
+ );
+ let localFileContent: React.ReactNode;
+ if (!hasDesktopFileAccess) {
+ localFileContent = (
+ <p className={styles.note} data-flx="theme-settings.assets-section.note--2">
+ {i18n._(AVAILABLE_IN_THE_DESKTOP_APP_DESCRIPTOR)}
+ </p>
+ );
+ } else if (ThemeLibrary.localFiles.length === 0) {
+ localFileContent = (
+ <p className={styles.note} data-flx="theme-settings.assets-section.note--3">
+ {i18n._(NO_LOCAL_FILES_REFERENCED_DESCRIPTOR)}
+ </p>
+ );
+ } else {
+ localFileContent = (
+ <div className={styles.list} data-flx="theme-settings.assets-section.list--2">
+ {ThemeLibrary.localFiles.map((file) => (
+ <AssetRow
+ key={file.id}
+ name={file.name}
+ reference={createThemeLocalFileReference(file.path)}
+ onDelete={() => {
+ void ThemeLibrary.deleteLocalFile(file.id);
+ }}
+ data-flx="theme-settings.assets-section.asset-row--2"
+ />
+ ))}
+ </div>
+ );
+ }
+ return (
+ <SettingsSection
+ id="assets"
+ title={title}
+ description={description}
+ data-flx="theme-settings.assets-section.assets"
+ >
+ {uploadedAssets}
+ <div className={localFileStackClass} data-flx="theme-settings.assets-section.stack--2">
+ {localFileHeader}
+ {localFileContent}
+ </div>
+ </SettingsSection>
+ );
+});
diff --git a/fluxer_app/src/features/theme_settings/components/ShareAndBackUpSection.tsx b/fluxer_app/src/features/theme_settings/components/ShareAndBackUpSection.tsx
new file mode 100644
index 000000000..1159af331
--- /dev/null
+++ b/fluxer_app/src/features/theme_settings/components/ShareAndBackUpSection.tsx
@@ -0,0 +1,256 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import {ConfirmModal} from '@app/features/app/components/dialogs/ConfirmModal';
+import {SettingsSection} from '@app/features/app/components/dialogs/shared/SettingsSection';
+import {CANCEL_DESCRIPTOR} from '@app/features/i18n/utils/CommonMessageDescriptors';
+import {downloadTextFile} from '@app/features/platform/utils/DownloadFile';
+import {ShareThemeModal} from '@app/features/theme/components/modals/ShareThemeModal';
+import ThemeEditTarget from '@app/features/theme/state/ThemeEditTarget';
+import ThemeLibrary from '@app/features/theme/state/ThemeLibrary';
+import ThemeProfile from '@app/features/theme/state/ThemeProfile';
+import * as ThemeSettingsCommands from '@app/features/theme_settings/commands/ThemeSettingsCommands';
+import {useCurrentAccountLabels} from '@app/features/theme_settings/hooks/useCurrentAccountLabels';
+import styles from '@app/features/theme_settings/ThemesTab.module.css';
+import {showThemeSettingsErrorModal} from '@app/features/theme_settings/utils/ThemeSettingsErrorModal';
+import {
+ EXPORT_LIBRARY_DESCRIPTOR,
+ IMPORT_LIBRARY_DESCRIPTOR,
+} from '@app/features/theme_settings/utils/ThemeSettingsMessageDescriptors';
+import {Button} from '@app/features/ui/button/Button';
+import * as ModalCommands from '@app/features/ui/commands/ModalCommands';
+import {modal} from '@app/features/ui/commands/ModalCommands';
+import * as ToastCommands from '@app/features/ui/commands/ToastCommands';
+import {readFileText} from '@app/features/user/components/modals/tabs/appearance_tab/theme/ThemeUtils';
+import {SHARE_AND_BACK_UP_SECTION_DESCRIPTOR} from '@app/features/user/components/settings_utils/section_registry/ThemesSections';
+import {msg} from '@lingui/core/macro';
+import {Trans, useLingui} from '@lingui/react/macro';
+import {observer} from 'mobx-react-lite';
+import type React from 'react';
+import {useRef} from 'react';
+
+const SHARE_YOUR_THEME_DESCRIPTOR = msg({
+ message: 'Share your theme',
+ comment: 'Row title in the "Share and back up" section in Settings > Themes.',
+});
+const SHARE_YOUR_THEME_DESCRIPTION_DESCRIPTOR = msg({
+ message: 'Create a link to your theme or copy its CSS.',
+ comment: 'Row description in the "Share and back up" section in Settings > Themes.',
+});
+const EXPORT_LIBRARY_DESCRIPTION_DESCRIPTOR = msg({
+ message: 'Save all your themes and assets to one file.',
+ comment: 'Row description for "Export library" in Settings > Themes.',
+});
+const IMPORT_LIBRARY_DESCRIPTION_DESCRIPTOR = msg({
+ message: 'Add themes and assets from a library file.',
+ comment: 'Row description for "Import library" in Settings > Themes.',
+});
+const RESET_THEME_DATA_DESCRIPTOR = msg({
+ message: 'Reset theme data',
+ comment: 'Row title in the "Share and back up" section in Settings > Themes.',
+});
+const RESET_DESKTOP_THEME_DATA_DESCRIPTION_DESCRIPTOR = msg({
+ message:
+ 'Deletes the quick CSS of the desktop app theme, and every saved theme, asset and local file reference on this device.',
+ comment:
+ 'Row description and confirmation text for "Reset theme data" in Settings > Themes while the desktop app theme is being edited. Quick CSS is custom CSS the user writes.',
+});
+const RESET_ACCOUNT_THEME_DATA_DESCRIPTION_DESCRIPTOR = msg({
+ message:
+ 'Deletes the quick CSS for {username} on {instanceName}, and every saved theme, asset and local file reference on this device.',
+ comment:
+ 'Row description and confirmation text for "Reset theme data" in Settings > Themes while an account\'s own theme is being edited. {username} is the account\'s username and {instanceName} is its instance. Quick CSS is custom CSS the user writes.',
+});
+const RESET_CUSTOM_THEME_DATA_DESCRIPTOR = msg({
+ message: 'Reset custom theme data?',
+ comment: 'Confirmation title before resetting theme data in Settings > Themes.',
+});
+const RESET_DATA_DESCRIPTOR = msg({
+ message: 'Reset data',
+ comment: 'Button that resets theme data in Settings > Themes.',
+});
+const NO_THEME_TO_SHARE_DESCRIPTOR = msg({
+ message: 'Turn on a saved theme or write some quick CSS before sharing.',
+ comment: 'Error shown when sharing a theme in Settings > Themes while nothing is turned on and quick CSS is empty.',
+});
+const IMPORTED_LIBRARY_DESCRIPTOR = msg({
+ message:
+ 'Imported {themes, plural, one {# theme} other {# themes}} and {assets, plural, one {# asset} other {# assets}}.',
+ comment: 'Toast after importing a theme library file.',
+});
+const WE_COULDN_T_IMPORT_THAT_THEME_LIBRARY_FILE_DESCRIPTOR = msg({
+ message: "We couldn't import that theme library file.",
+ comment: 'Error message after importing a theme library file failed.',
+});
+
+interface ActionRowProps {
+ title: string;
+ description: string;
+ children: React.ReactNode;
+}
+
+const ActionRow: React.FC<ActionRowProps> = ({title, description, children}) => (
+ <div className={styles.row} data-flx="theme-settings.share-and-back-up-section.action-row.row">
+ <div className={styles.rowText} data-flx="theme-settings.share-and-back-up-section.action-row.row-text">
+ <span className={styles.rowLabel} data-flx="theme-settings.share-and-back-up-section.action-row.row-label">
+ {title}
+ </span>
+ <span
+ className={styles.rowDescription}
+ data-flx="theme-settings.share-and-back-up-section.action-row.row-description"
+ >
+ {description}
+ </span>
+ </div>
+ <div className={styles.rowControls} data-flx="theme-settings.share-and-back-up-section.action-row.row-controls">
+ {children}
+ </div>
+ </div>
+);
+
+export const ShareAndBackUpSection: React.FC = observer(() => {
+ const {i18n} = useLingui();
+ const {username, instanceName} = useCurrentAccountLabels();
+ const resetDescription =
+ ThemeEditTarget.target === 'desktop'
+ ? i18n._(RESET_DESKTOP_THEME_DATA_DESCRIPTION_DESCRIPTOR)
+ : i18n._(RESET_ACCOUNT_THEME_DATA_DESCRIPTION_DESCRIPTOR, {username, instanceName});
+ const libraryImportRef = useRef<HTMLInputElement | null>(null);
+ const handleShare = () => {
+ const target = ThemeEditTarget.target;
+ const css = [ThemeLibrary.themeCssFor(ThemeLibrary.enabledIdsFor(target)), ThemeProfile.customThemeCssFor(target)]
+ .map((value) => value?.trim() ?? '')
+ .filter(Boolean)
+ .join('\n\n');
+ if (!css) {
+ showThemeSettingsErrorModal(
+ i18n,
+ () => i18n._(NO_THEME_TO_SHARE_DESCRIPTOR),
+ 'theme-settings.share-and-back-up-section.share-empty-error-modal',
+ );
+ return;
+ }
+ ModalCommands.push(
+ modal(() => (
+ <ShareThemeModal
+ themeCss={css}
+ data-flx="theme-settings.share-and-back-up-section.handle-share.share-theme-modal"
+ />
+ )),
+ );
+ };
+ const handleExportLibrary = () => {
+ void ThemeLibrary.buildExportPayload().then((payload) => {
+ downloadTextFile(JSON.stringify(payload, null, 2), 'fluxer-theme-library.json', 'application/json;charset=utf-8');
+ });
+ };
+ const handleImportLibrary = (event: React.ChangeEvent<HTMLInputElement>) => {
+ const file = event.target.files?.[0];
+ event.target.value = '';
+ if (!file) return;
+ void readFileText(file)
+ .then((text) => ThemeLibrary.importExportPayload(JSON.parse(text)))
+ .then((result) => {
+ ToastCommands.success(i18n._(IMPORTED_LIBRARY_DESCRIPTOR, {themes: result.themes, assets: result.assets}));
+ })
+ .catch(() =>
+ showThemeSettingsErrorModal(
+ i18n,
+ () => i18n._(WE_COULDN_T_IMPORT_THAT_THEME_LIBRARY_FILE_DESCRIPTOR),
+ 'theme-settings.share-and-back-up-section.import-library-error-modal',
+ ),
+ );
+ };
+ const handleReset = () => {
+ ModalCommands.push(
+ modal(() => (
+ <ConfirmModal
+ title={i18n._(RESET_CUSTOM_THEME_DATA_DESCRIPTOR)}
+ description={resetDescription}
+ primaryText={i18n._(RESET_DATA_DESCRIPTOR)}
+ primaryVariant="danger"
+ secondaryText={i18n._(CANCEL_DESCRIPTOR)}
+ onPrimary={() => {
+ ThemeSettingsCommands.setEditedCustomThemeCss(null);
+ void ThemeLibrary.resetLibrary();
+ }}
+ data-flx="theme-settings.share-and-back-up-section.handle-reset.confirm-modal"
+ />
+ )),
+ );
+ };
+ return (
+ <SettingsSection
+ id="share-and-back-up"
+ title={i18n._(SHARE_AND_BACK_UP_SECTION_DESCRIPTOR)}
+ data-flx="theme-settings.share-and-back-up-section.share-and-back-up"
+ >
+ <div className={styles.list} data-flx="theme-settings.share-and-back-up-section.list">
+ <ActionRow
+ title={i18n._(SHARE_YOUR_THEME_DESCRIPTOR)}
+ description={i18n._(SHARE_YOUR_THEME_DESCRIPTION_DESCRIPTOR)}
+ data-flx="theme-settings.share-and-back-up-section.action-row"
+ >
+ <Button
+ variant="secondary"
+ small
+ onClick={handleShare}
+ data-flx="theme-settings.share-and-back-up-section.button.share"
+ >
+ <Trans>Share theme</Trans>
+ </Button>
+ </ActionRow>
+ <ActionRow
+ title={i18n._(EXPORT_LIBRARY_DESCRIPTOR)}
+ description={i18n._(EXPORT_LIBRARY_DESCRIPTION_DESCRIPTOR)}
+ data-flx="theme-settings.share-and-back-up-section.action-row--2"
+ >
+ <Button
+ variant="secondary"
+ small
+ onClick={handleExportLibrary}
+ data-flx="theme-settings.share-and-back-up-section.button.export-library"
+ >
+ {i18n._(EXPORT_LIBRARY_DESCRIPTOR)}
+ </Button>
+ </ActionRow>
+ <ActionRow
+ title={i18n._(IMPORT_LIBRARY_DESCRIPTOR)}
+ description={i18n._(IMPORT_LIBRARY_DESCRIPTION_DESCRIPTOR)}
+ data-flx="theme-settings.share-and-back-up-section.action-row--3"
+ >
+ <Button
+ variant="secondary"
+ small
+ onClick={() => libraryImportRef.current?.click()}
+ data-flx="theme-settings.share-and-back-up-section.button.click"
+ >
+ {i18n._(IMPORT_LIBRARY_DESCRIPTOR)}
+ </Button>
+ <input
+ ref={libraryImportRef}
+ type="file"
+ accept=".json,application/json,text/plain"
+ hidden
+ onChange={handleImportLibrary}
+ aria-hidden
+ data-flx="theme-settings.share-and-back-up-section.input.import-library.file"
+ />
+ </ActionRow>
+ <ActionRow
+ title={i18n._(RESET_THEME_DATA_DESCRIPTOR)}
+ description={resetDescription}
+ data-flx="theme-settings.share-and-back-up-section.action-row--4"
+ >
+ <Button
+ variant="danger"
+ small
+ onClick={handleReset}
+ data-flx="theme-settings.share-and-back-up-section.button.reset"
+ >
+ {i18n._(RESET_DATA_DESCRIPTOR)}
+ </Button>
+ </ActionRow>
+ </div>
+ </SettingsSection>
+ );
+});
diff --git a/fluxer_app/src/features/theme_settings/utils/LinkedThemeFile.ts b/fluxer_app/src/features/theme_settings/utils/LinkedThemeFile.ts
new file mode 100644
index 000000000..a6b40a886
--- /dev/null
+++ b/fluxer_app/src/features/theme_settings/utils/LinkedThemeFile.ts
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+import ThemeLibrary, {type ThemeLibraryTheme} from '@app/features/theme/state/ThemeLibrary';
+import type {I18n} from '@lingui/core';
+import {msg} from '@lingui/core/macro';
+
+export const LINKED_FILE_DESCRIPTOR = msg({
+ message: 'Linked to a file',
+ comment: 'Shown under a theme in Settings > Themes that is linked to a CSS file on disk and updates live.',
+});
+const LINKED_FILE_LIVE_DESCRIPTOR = msg({
+ message: 'Linked to {path}. Edit the file in any editor and saved changes apply here right away.',
+ comment:
+ 'Shown for a linked theme in Settings > Themes and the quick CSS editor. {path} is the full path of the CSS file on disk. The theme cannot be edited in the app while linked.',
+});
+const LINKED_FILE_PATH_DESCRIPTOR = msg({
+ message: 'Linked to {path}.',
+ comment:
+ 'Shown for a linked theme while the app is still checking its CSS file, or when this version of the desktop app cannot follow file changes. {path} is the full path of the CSS file on disk.',
+});
+const LINKED_FILE_MISSING_DESCRIPTOR = msg({
+ message: "Can't find {path}. Using the last loaded version.",
+ comment:
+ 'Shown for a linked theme in Settings > Themes when its CSS file was moved or deleted. {path} is the full path of the CSS file.',
+});
+const LINKED_FILE_UNREADABLE_DESCRIPTOR = msg({
+ message: "Can't read {path}. Using the last loaded version.",
+ comment:
+ 'Shown for a linked theme in Settings > Themes when its CSS file exists but cannot be read, for example because it is too large or access was removed. {path} is the full path of the CSS file.',
+});
+
+type LinkedThemeFileState = 'checking' | 'live' | 'missing' | 'unreadable';
+
+function getLinkedThemeFileState(path: string): LinkedThemeFileState {
+ const status = ThemeLibrary.linkedFileStatus.get(path);
+ if (status === undefined) return 'checking';
+ if (status === 'live') return 'live';
+ return status === 'missing' ? 'missing' : 'unreadable';
+}
+
+export function isLinkedThemeReadOnly(theme: ThemeLibraryTheme): boolean {
+ return Boolean(theme.linkedPath) && ThemeLibrary.canWatchLinkedFiles;
+}
+
+export function getLinkedThemeFileProblem(i18n: I18n, theme: ThemeLibraryTheme): string | null {
+ const path = theme.linkedPath;
+ if (!path) return null;
+ switch (getLinkedThemeFileState(path)) {
+ case 'missing':
+ return i18n._(LINKED_FILE_MISSING_DESCRIPTOR, {path});
+ case 'unreadable':
+ return i18n._(LINKED_FILE_UNREADABLE_DESCRIPTOR, {path});
+ default:
+ return null;
+ }
+}
+
+export function getLinkedThemeFileLine(i18n: I18n, theme: ThemeLibraryTheme): string | null {
+ const path = theme.linkedPath;
+ if (!path) return null;
+ const problem = getLinkedThemeFileProblem(i18n, theme);
+ if (problem) return problem;
+ const live = ThemeLibrary.canWatchLinkedFiles && getLinkedThemeFileState(path) === 'live';
+ return i18n._(live ? LINKED_FILE_LIVE_DESCRIPTOR : LINKED_FILE_PATH_DESCRIPTOR, {path});
+}