Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"@tanstack/react-router-ssr-query": "1.167.2",
"@tanstack/react-start": "1.168.60",
"@tanstack/react-start-client": "1.168.39",
"@tanstack/react-store": "^0.11.2",
"@tanstack/react-table": "9.0.0",
"@tanstack/react-virtual": "^3.14.9",
"@tanstack/workflow-core": "0.0.4",
Expand Down Expand Up @@ -178,8 +179,7 @@
"vite-bundle-analyzer": "^1.3.6",
"xstate": "^5.30.0",
"yaml": "^2.9.0",
"zod": "^4.3.6",
"zustand": "^5.0.12"
"zod": "^4.3.6"
},
"devDependencies": {
"@cloudflare/vite-plugin": "^1.62.5",
Expand Down
25 changes: 6 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/components/Doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,9 @@ export function Doc({
const localCurrentFramework = useLocalCurrentFramework()
const currentFramework = React.useMemo(() => {
const fw =
frameworkProp ||
paramsFramework ||
localCurrentFramework.currentFramework ||
'react'
frameworkProp || paramsFramework || localCurrentFramework || 'react'
return typeof fw === 'string' ? fw.toLowerCase() : fw
}, [frameworkProp, paramsFramework, localCurrentFramework.currentFramework])
}, [frameworkProp, paramsFramework, localCurrentFramework])

const isTocVisible = shouldRenderToc && headings.length > 1

Expand Down
50 changes: 25 additions & 25 deletions src/components/FrameworkSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { create } from 'zustand'
import { createStore, useSelector } from '@tanstack/react-store'
import { useNavigate, useParams } from '@tanstack/react-router'
import { useQueryClient } from '@tanstack/react-query'
import { Select } from './Select'
Expand Down Expand Up @@ -48,19 +48,22 @@ export function FrameworkSelect({ libraryId }: { libraryId: LibraryId }) {
)
}

// Let's use zustand to wrap the local storage logic. This way
// Let's use TanStack Store to wrap the local storage logic. This way
// we'll get subscriptions for free and we can use it in other
// components if we need to.
export const useLocalCurrentFramework = create<{
currentFramework?: string
setCurrentFramework: (framework: string) => void
}>((set) => ({
currentFramework: getLocalStorageItem('framework') || undefined,
setCurrentFramework: (framework: string) => {
setLocalStorageItem('framework', framework)
set({ currentFramework: framework })
},
}))
export const localCurrentFrameworkStore = createStore(
{ currentFramework: getLocalStorageItem('framework') || undefined },
({ setState }) => ({
setCurrentFramework: (framework: string) => {
setLocalStorageItem('framework', framework)
setState(() => ({ currentFramework: framework }))
},
}),
)

export function useLocalCurrentFramework() {
return useSelector(localCurrentFrameworkStore, (s) => s.currentFramework)
}

/**
* Get the stored framework preference from localStorage.
Expand All @@ -77,12 +80,11 @@ export function getStoredFrameworkPreference(): string | undefined {
export function usePersistFrameworkPreference() {
const userQuery = useCurrentUserQuery()
const queryClient = useQueryClient()
const localCurrentFramework = useLocalCurrentFramework()

return React.useCallback(
(framework: string) => {
// Always update localStorage as fallback
localCurrentFramework.setCurrentFramework(framework)
localCurrentFrameworkStore.actions.setCurrentFramework(framework)
queryClient.setQueryData(currentUserQueryOptions.queryKey, (user) =>
user ? { ...user, lastUsedFramework: framework } : user,
)
Expand All @@ -91,7 +93,7 @@ export function usePersistFrameworkPreference() {
persistFrameworkToServer(framework)
}
},
[localCurrentFramework, queryClient, userQuery.data],
[queryClient, userQuery.data],
)
}

Expand Down Expand Up @@ -130,12 +132,13 @@ export function useCurrentFramework(frameworks: Framework[]) {
})

const localCurrentFramework = useLocalCurrentFramework()
const { setCurrentFramework } = localCurrentFrameworkStore.actions

// Priority: URL params > DB (logged-in) > localStorage > 'react'
const userFramework = userQuery.data?.lastUsedFramework
let framework = (paramsFramework ||
userFramework ||
localCurrentFramework.currentFramework ||
localCurrentFramework ||
'react') as Framework

framework = frameworks.includes(framework) ? framework : 'react'
Expand All @@ -146,7 +149,7 @@ export function useCurrentFramework(frameworks: Framework[]) {
params: { framework } as any,
})
// Always update localStorage as fallback
localCurrentFramework.setCurrentFramework(framework)
setCurrentFramework(framework)
queryClient.setQueryData(currentUserQueryOptions.queryKey, (user) =>
user ? { ...user, lastUsedFramework: framework } : user,
)
Expand All @@ -156,21 +159,18 @@ export function useCurrentFramework(frameworks: Framework[]) {
persistFrameworkToServer(framework)
}
},
[localCurrentFramework, navigate, queryClient, userQuery.data],
[navigate, queryClient, userQuery.data, setCurrentFramework],
)

React.useEffect(() => {
// Set the framework in localStorage if it doesn't exist
if (!localCurrentFramework.currentFramework) {
localCurrentFramework.setCurrentFramework(framework)
if (!localCurrentFramework) {
setCurrentFramework(framework)
}

// Set the framework in localStorage if it doesn't match the URL
if (
paramsFramework &&
paramsFramework !== localCurrentFramework.currentFramework
) {
localCurrentFramework.setCurrentFramework(paramsFramework)
if (paramsFramework && paramsFramework !== localCurrentFramework) {
setCurrentFramework(paramsFramework)
}
})

Expand Down
4 changes: 2 additions & 2 deletions src/components/NavbarCartButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Link, useLocation } from '@tanstack/react-router'
import { ShoppingCartIcon } from '@phosphor-icons/react/ShoppingCart'
import { twMerge } from 'tailwind-merge'
import { useCart } from '~/hooks/useCart'
import { useCartDrawerStore } from '~/components/shop/cartDrawerStore'
import { cartDrawerStore } from '~/components/shop/cartDrawerStore'

const badgeClasses = twMerge(
'absolute -top-1 -right-1 min-w-[1.125rem] h-[1.125rem] px-1',
Expand Down Expand Up @@ -34,7 +34,7 @@ const buttonClasses = twMerge(
export function NavbarCartButton() {
const { pathname } = useLocation()
const { totalQuantity } = useCart()
const openDrawer = useCartDrawerStore((s) => s.openDrawer)
const { openDrawer } = cartDrawerStore.actions
const onShopRoute = pathname === '/shop' || pathname.startsWith('/shop/')

if (!onShopRoute && totalQuantity === 0) return null
Expand Down
43 changes: 23 additions & 20 deletions src/components/VersionSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { create } from 'zustand'
import { createStore, useSelector } from '@tanstack/react-store'
import { useNavigate, useParams } from '@tanstack/react-router'
import { TagIcon } from '@phosphor-icons/react/Tag'
import { Select, SelectOption } from './Select'
Expand All @@ -26,19 +26,18 @@ export function VersionSelect({ libraryId }: { libraryId: LibraryId }) {
)
}

// Let's use zustand to wrap the local storage logic. This way
// Let's use TanStack Store to wrap the local storage logic. This way
// we'll get subscriptions for free and we can use it in other
// components if we need to.
const useLocalCurrentVersion = create<{
currentVersion?: string
setCurrentVersion: (version: string) => void
}>((set) => ({
currentVersion: getLocalStorageItem('version') || undefined,
setCurrentVersion: (version: string) => {
setLocalStorageItem('version', version)
set({ currentVersion: version })
},
}))
const localCurrentVersionStore = createStore(
{ currentVersion: getLocalStorageItem('version') || undefined },
({ setState }) => ({
setCurrentVersion: (version: string) => {
setLocalStorageItem('version', version)
setState(() => ({ currentVersion: version }))
},
}),
)

/**
* Use framework in URL path
Expand All @@ -52,9 +51,13 @@ function useCurrentVersion(versions: string[]) {
strict: false,
})

const localCurrentVersion = useLocalCurrentVersion()
const localCurrentVersion = useSelector(
localCurrentVersionStore,
(s) => s.currentVersion,
)
const { setCurrentVersion } = localCurrentVersionStore.actions

let version = paramsVersion || localCurrentVersion.currentVersion || 'latest'
let version = paramsVersion || localCurrentVersion || 'latest'

version = versions.includes(version) ? version : 'latest'

Expand All @@ -63,20 +66,20 @@ function useCurrentVersion(versions: string[]) {
navigate({
params: { version } as never,
})
localCurrentVersion.setCurrentVersion(version)
setCurrentVersion(version)
},
[localCurrentVersion, navigate],
[navigate, setCurrentVersion],
)

React.useEffect(() => {
// Set the version in localStorage if it doesn't exist
if (!localCurrentVersion.currentVersion) {
localCurrentVersion.setCurrentVersion(version)
if (!localCurrentVersion) {
setCurrentVersion(version)
}

// Set the version in localStorage if it doesn't match the URL
if (paramsVersion && paramsVersion !== localCurrentVersion.currentVersion) {
localCurrentVersion.setCurrentVersion(paramsVersion)
if (paramsVersion && paramsVersion !== localCurrentVersion) {
setCurrentVersion(paramsVersion)
}
})

Expand Down
15 changes: 10 additions & 5 deletions src/components/application-starter/ApplicationStarterProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Uses proper theme support.
*/

import { useSelector } from '@tanstack/react-store'
import {
createContext,
useEffect,
Expand All @@ -13,7 +14,7 @@ import {
type ReactNode,
} from 'react'
import { useSearch } from '@tanstack/react-router'
import { useApplicationStarterStore } from './store'
import { applicationStarterStore } from './store'
import { normalizeFrameworkId } from '~/application-starter/frameworks'

interface ApplicationStarterContextValue {
Expand All @@ -33,8 +34,11 @@ export function ApplicationStarterProvider({
}: ApplicationStarterProviderProps) {
const [ready, setReady] = useState(false)
const search = useSearch({ strict: false }) as { framework?: string }
const loadFeatures = useApplicationStarterStore((s) => s.loadFeatures)
const featuresLoaded = useApplicationStarterStore((s) => s.featuresLoaded)
const loadFeatures = applicationStarterStore.actions.loadFeatures
const featuresLoaded = useSelector(
applicationStarterStore,
(s) => s.featuresLoaded,
)
const initializedRef = useRef(false)

// Set framework from URL before loading features (only once on mount)
Expand All @@ -44,9 +48,10 @@ export function ApplicationStarterProvider({

// Set framework directly in store without triggering loadFeatures
if (search.framework) {
useApplicationStarterStore.setState({
applicationStarterStore.setState((prev) => ({
...prev,
framework: normalizeFrameworkId(search.framework),
})
}))
}
loadFeatures()
}, [loadFeatures, search.framework])
Expand Down
Loading
Loading