Ziya/app/stores/app.ts
rizary 6efcf43691
feat: complete ESLint configuration overhaul and theme system improvements
- Migrate from legacy .eslintrc.json to modern flat config system
- Remove conflicting ESLint configuration files
- Fix auto-generation of eslint.config.mjs by Nuxt
- Update ESLint rules to use single quotes and proper formatting
- Add comprehensive theme switching system with 24 palettes
- Implement proper daisyUI theme integration
- Add theme store with persistence and dark/light mode support
- Create ThemeSwitcher component with enhanced UI
- Fix package.json scripts to work with new ESLint flat config
- Update VS Code settings for proper ESLint integration
- Add changelogen scripts for proper changelog management

BREAKING CHANGE: ESLint configuration migrated to flat config system
2025-06-22 00:53:24 +07:00

146 lines
3.5 KiB
TypeScript

import { defineStore } from 'pinia';
import { useThemeStore } from './theme';
interface AppState {
isInitialized: boolean;
isLoading: boolean;
error: string | null;
currentUser: { name: string, email: string } | null;
appVersion: string;
toastMessage: string;
toastType: 'success' | 'error' | 'info';
showToast: boolean;
}
export const useAppStore = defineStore('app', {
state: (): AppState => ({
isInitialized: false,
isLoading: false,
error: null,
currentUser: null,
appVersion: '1.0.0',
toastMessage: '',
toastType: 'info',
showToast: false,
}),
getters: {
isAuthenticated: state => state.currentUser !== null,
userInitials: (state) => {
if (!state.currentUser) return '??';
return state.currentUser.name
.split(' ')
.map(n => n[0])
.join('')
.toUpperCase();
},
},
actions: {
async initialize() {
if (this.isInitialized) return;
this.isLoading = true;
this.error = null;
try {
// Initialize theme system
const themeStore = useThemeStore();
await themeStore.initializeTheme();
// Mark as initialized
this.isInitialized = true;
console.log('App initialized successfully');
}
catch (error) {
this.error = error instanceof Error ? error.message : 'Failed to initialize app';
console.error('App initialization failed:', error);
throw error;
}
finally {
this.isLoading = false;
}
},
setLoading(loading: boolean) {
this.isLoading = loading;
},
showToastMessage(message: string, type: 'success' | 'error' | 'info' = 'info') {
this.toastMessage = message;
this.toastType = type;
this.showToast = true;
setTimeout(() => {
this.showToast = false;
}, 3000);
},
async login(email: string, password: string) {
this.setLoading(true);
try {
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 1000));
// Mock user data
this.currentUser = {
name: 'John Trader',
email: email,
};
this.showToastMessage('Welcome back!', 'success');
return true;
}
catch (error) {
this.showToastMessage('Login failed. Please try again.', 'error');
return false;
}
finally {
this.setLoading(false);
}
},
async logout() {
this.setLoading(true);
try {
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 500));
this.currentUser = null;
this.showToastMessage('You have been logged out', 'info');
}
finally {
this.setLoading(false);
}
},
// Persist user data to localStorage
async $afterStateRestored() {
if (this.currentUser) {
localStorage.setItem('ziya-user', JSON.stringify(this.currentUser));
}
else {
localStorage.removeItem('ziya-user');
}
},
// Initialize from localStorage
async initializeFromStorage() {
if (import.meta.client) {
const storedUser = localStorage.getItem('ziya-user');
if (storedUser) {
try {
this.currentUser = JSON.parse(storedUser);
}
catch (error) {
console.error('Failed to parse stored user data:', error);
localStorage.removeItem('ziya-user');
}
}
await this.initialize();
}
},
},
});