fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma strängvärde byggs med konkatenering i stället för att matcha mönstret.
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
'use client'
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useMemo,
|
||||
useSyncExternalStore,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import {
|
||||
DEFAULT_PALETTE,
|
||||
PALETTE_STORAGE_KEY,
|
||||
PALETTE_VALUES,
|
||||
isPalette,
|
||||
type Palette,
|
||||
} from '@/lib/theme/palettes'
|
||||
|
||||
const PALETTE_CHANGE_EVENT = 'accounted:palette-change'
|
||||
|
||||
interface PaletteContextValue {
|
||||
palette: Palette
|
||||
setPalette: (palette: Palette) => void
|
||||
}
|
||||
|
||||
const PaletteContext = createContext<PaletteContextValue | null>(null)
|
||||
|
||||
const paletteInitScript = `
|
||||
(function () {
|
||||
var palette = ${JSON.stringify(DEFAULT_PALETTE)};
|
||||
try {
|
||||
var stored = window.localStorage.getItem(${JSON.stringify(PALETTE_STORAGE_KEY)});
|
||||
if (${JSON.stringify(PALETTE_VALUES)}.indexOf(stored) !== -1) palette = stored;
|
||||
} catch (_) {}
|
||||
document.documentElement.dataset.palette = palette;
|
||||
})();
|
||||
`
|
||||
|
||||
function applyPalette(palette: Palette) {
|
||||
document.documentElement.dataset.palette = palette
|
||||
}
|
||||
|
||||
function getPaletteSnapshot(): Palette {
|
||||
const palette = document.documentElement.dataset.palette
|
||||
return isPalette(palette) ? palette : DEFAULT_PALETTE
|
||||
}
|
||||
|
||||
function subscribeToPalette(onStoreChange: () => void) {
|
||||
function handlePaletteChange() {
|
||||
onStoreChange()
|
||||
}
|
||||
|
||||
function handleStorage(event: StorageEvent) {
|
||||
if (event.key !== PALETTE_STORAGE_KEY && event.key !== null) return
|
||||
const palette = isPalette(event.newValue) ? event.newValue : DEFAULT_PALETTE
|
||||
applyPalette(palette)
|
||||
onStoreChange()
|
||||
}
|
||||
|
||||
window.addEventListener(PALETTE_CHANGE_EVENT, handlePaletteChange)
|
||||
window.addEventListener('storage', handleStorage)
|
||||
return () => {
|
||||
window.removeEventListener(PALETTE_CHANGE_EVENT, handlePaletteChange)
|
||||
window.removeEventListener('storage', handleStorage)
|
||||
}
|
||||
}
|
||||
|
||||
export function PaletteProvider({ children }: { children: ReactNode }) {
|
||||
const palette = useSyncExternalStore(
|
||||
subscribeToPalette,
|
||||
getPaletteSnapshot,
|
||||
() => DEFAULT_PALETTE,
|
||||
)
|
||||
|
||||
const setPalette = useCallback((nextPalette: Palette) => {
|
||||
applyPalette(nextPalette)
|
||||
try {
|
||||
window.localStorage.setItem(PALETTE_STORAGE_KEY, nextPalette)
|
||||
} catch {
|
||||
// The active tab still keeps the selected palette when storage is blocked.
|
||||
}
|
||||
window.dispatchEvent(new Event(PALETTE_CHANGE_EVENT))
|
||||
}, [])
|
||||
|
||||
const value = useMemo(() => ({ palette, setPalette }), [palette, setPalette])
|
||||
|
||||
return (
|
||||
<>
|
||||
<script suppressHydrationWarning dangerouslySetInnerHTML={{ __html: paletteInitScript }} />
|
||||
<PaletteContext.Provider value={value}>{children}</PaletteContext.Provider>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export function usePalette() {
|
||||
const context = useContext(PaletteContext)
|
||||
if (!context) throw new Error('usePalette must be used within PaletteProvider')
|
||||
return context
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
'use client'
|
||||
|
||||
import { useMemo, type ReactNode } from 'react'
|
||||
import { SWRConfig } from 'swr'
|
||||
import { buildReferenceFallback, type ReferenceSeed } from '@/lib/reference-data/seed'
|
||||
|
||||
/**
|
||||
* Nested SWR config carrying the server-fetched reference data as
|
||||
* `fallback`. Mounted by app/(dashboard)/layout.tsx inside CompanyProvider;
|
||||
* SWR merges this with the root SWRProvider so the shared cache, fetcher
|
||||
* and dedupe stay global while the seed is per company.
|
||||
*/
|
||||
export function ReferenceDataSeed({
|
||||
companyId,
|
||||
fiscalPeriods,
|
||||
cashAccounts,
|
||||
settings,
|
||||
children,
|
||||
}: ReferenceSeed & { companyId: string | null; children: ReactNode }) {
|
||||
const fallback = useMemo(
|
||||
() => buildReferenceFallback(companyId, { fiscalPeriods, cashAccounts, settings }),
|
||||
[companyId, fiscalPeriods, cashAccounts, settings],
|
||||
)
|
||||
return <SWRConfig value={{ fallback }}>{children}</SWRConfig>
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
'use client'
|
||||
|
||||
import { SWRConfig } from 'swr'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
/**
|
||||
* Global SWR defaults: one shared client-side cache so repeated mounts of the
|
||||
* same data (company settings, nav badges, reference data) dedupe in-flight
|
||||
* requests and render instantly from cache on back-navigation, revalidating
|
||||
* in the background instead of re-showing skeletons on every visit.
|
||||
*
|
||||
* String keys are fetched as JSON from our own API routes; hooks that read
|
||||
* Supabase directly pass an array key with their own fetcher.
|
||||
*/
|
||||
export function SWRProvider({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<SWRConfig
|
||||
value={{
|
||||
fetcher: async (key: string) => {
|
||||
const res = await fetch(key)
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status} for ${key}`)
|
||||
return res.json()
|
||||
},
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</SWRConfig>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user