Files
InvoiceShelf/resources/scripts/composables/use-company.ts
Darko Gjorgjijoski 71388ec6a5 Rename resources/scripts-v2 to resources/scripts and drop @v2 alias
Now that the legacy v1 frontend (commit 064bdf53) is gone, the v2 directory is the only frontend and the v2 suffix is just noise. Renames resources/scripts-v2 to resources/scripts via git mv (so git records the move as renames, preserving blame and log --follow), then bulk-rewrites the 152 files that imported via @v2/... to use @/scripts/... instead. The existing @ alias (resources/) covers the new path with no extra config needed.

Drops the now-unused @v2 alias from vite.config.js and points the laravel-vite-plugin entry at resources/scripts/main.ts. Updates the only blade reference (resources/views/app.blade.php) to match. The package.json test script (eslint ./resources/scripts) automatically targets the right place after the rename without any edit.

Verified: npm run build exits clean and the Vite warning lines now reference resources/scripts/plugins/i18n.ts, confirming every import resolved through the new path. git log --follow on any moved file walks back through its scripts-v2 history.
2026-04-07 12:50:16 +02:00

133 lines
3.4 KiB
TypeScript

import { ref, computed } from 'vue'
import type { Ref, ComputedRef } from 'vue'
import * as ls from '../utils/local-storage'
import { LS_KEYS } from '@/scripts/config/constants'
export interface Company {
id: number
unique_hash: string
name: string
slug: string
[key: string]: unknown
}
export interface CompanySettings {
[key: string]: unknown
}
export interface CompanyCurrency {
id: number
code: string
name: string
symbol: string
precision: number
thousand_separator: string
decimal_separator: string
swap_currency_symbol?: boolean
exchange_rate?: number
[key: string]: unknown
}
export interface UseCompanyReturn {
selectedCompany: Ref<Company | null>
companies: Ref<Company[]>
selectedCompanySettings: Ref<CompanySettings>
selectedCompanyCurrency: Ref<CompanyCurrency | null>
isAdminMode: Ref<boolean>
hasCompany: ComputedRef<boolean>
setCompany: (company: Company | null) => void
setCompanies: (data: Company[]) => void
setCompanySettings: (settings: CompanySettings) => void
setCompanyCurrency: (currency: CompanyCurrency | null) => void
enterAdminMode: () => void
exitAdminMode: () => void
}
const selectedCompany = ref<Company | null>(null)
const companies = ref<Company[]>([])
const selectedCompanySettings = ref<CompanySettings>({})
const selectedCompanyCurrency = ref<CompanyCurrency | null>(null)
const isAdminMode = ref<boolean>(ls.getBoolean(LS_KEYS.IS_ADMIN_MODE))
/**
* Composable for managing company selection and admin mode state.
* Extracted from the Pinia company store, this provides reactive company state
* with localStorage persistence for the selected company and admin mode.
*/
export function useCompany(): UseCompanyReturn {
const hasCompany = computed<boolean>(
() => selectedCompany.value !== null
)
/**
* Set the selected company and persist to localStorage.
* Automatically disables admin mode when a company is selected.
*
* @param company - The company to select, or null to deselect
*/
function setCompany(company: Company | null): void {
if (company) {
ls.set(LS_KEYS.SELECTED_COMPANY, String(company.id))
ls.remove(LS_KEYS.IS_ADMIN_MODE)
isAdminMode.value = false
} else {
ls.remove(LS_KEYS.SELECTED_COMPANY)
}
selectedCompany.value = company
}
/**
* Set the list of available companies.
*/
function setCompanies(data: Company[]): void {
companies.value = data
}
/**
* Update the selected company's settings.
*/
function setCompanySettings(settings: CompanySettings): void {
selectedCompanySettings.value = settings
}
/**
* Update the selected company's currency.
*/
function setCompanyCurrency(currency: CompanyCurrency | null): void {
selectedCompanyCurrency.value = currency
}
/**
* Enter admin mode. Clears the selected company and persists admin mode.
*/
function enterAdminMode(): void {
isAdminMode.value = true
ls.set(LS_KEYS.IS_ADMIN_MODE, true)
ls.remove(LS_KEYS.SELECTED_COMPANY)
selectedCompany.value = null
}
/**
* Exit admin mode.
*/
function exitAdminMode(): void {
isAdminMode.value = false
ls.remove(LS_KEYS.IS_ADMIN_MODE)
}
return {
selectedCompany,
companies,
selectedCompanySettings,
selectedCompanyCurrency,
isAdminMode,
hasCompany,
setCompany,
setCompanies,
setCompanySettings,
setCompanyCurrency,
enterAdminMode,
exitAdminMode,
}
}