Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

SolidApp.tsx

240 lines7.1 KB

1import {
2 QueryClient,
3 QueryClientProvider,
4 keepPreviousData,
5 useQuery,
6} from '@tanstack/solid-query'
7import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
8import {
9 ErrorBoundary,
10 For,
11 Show,
12 Suspense,
13 createContext,
14 createEffect,
15 on,
16 untrack,
17 useContext,
18} from 'solid-js'
19import { isServer } from 'solid-js/web'
20import { getSearchParams, properCase } from '../utils'
21import { pokemonsQueries } from '../services/pokemons/queries'
22import { Link } from './Link'
23​
24const PokemonIdContext = createContext<() => string>()
25​
26const usePokemonID = () => {
27 const id = useContext(PokemonIdContext)
28 if (!id) throw new Error('PokemonIdContext not found')
29 return id
31​
32export const SolidApp = (props: { pokemon?: string }) => {
33 const client = new QueryClient()
34​
35 const search = getSearchParams(untrack(() => props.pokemon) || '')
36​
37 return (
38 <QueryClientProvider client={client}>
39 <SolidQueryDevtools />
40 <Suspense fallback={'Loading'}>
41 <PokemonIdContext.Provider value={search}>
42 <App />
43 </PokemonIdContext.Provider>
44 </Suspense>
45 </QueryClientProvider>
46 )
48​
49const App = () => {
50 return (
51 <div class="flex flex-1 overflow-auto">
52 <SideNav />
53 <PokemonDetails />
54 </div>
55 )
57​
58const PokemonDetails = () => {
59 const id = usePokemonID()
60 return (
61 <div class="flex-1 flex">
62 <Show when={id()}>
63 <ErrorBoundary
64 fallback={(error, reset) => {
65 createEffect(on(id, reset, { defer: true }))
66 return (
67 <div class="justify-center items-center flex-1 flex flex-col gap-2 text-zinc-700">
68 <div class="text-2xl font-medium">
69 Couldn't load this pokemon
70 </div>
71 <div>{error.message}</div>
72 </div>
73 )
74 }}
75 >
76 <Suspense fallback={'Loading'}>
77 <PokemonDex id={id()} />
78 </Suspense>
79 </ErrorBoundary>
80 </Show>
81 <Show when={!id()}>
82 <div class="justify-center items-center flex-1 flex text-2xl font-medium text-zinc-700">
83 <div class="">Select a pokemon to see its stats</div>
84 </div>
85 </Show>
86 </div>
87 )
89​
90const PokemonDex = (props: { id: string }) => {
91 const pokemon = useQuery(() => pokemonsQueries.detail(props.id))
92​
93 const pokemon_stats = useQuery(() => ({
94 ...pokemonsQueries.detail(props.id),
95 select(data) {
96 const nameMap = {
97 hp: 'HP',
98 attack: 'Attack',
99 defense: 'Defense',
100 'special-attack': 'Special Attack',
101 'special-defense': 'Special Defense',
102 speed: 'Speed',
103 }
104 return data.stats.map((stat) => ({
105 name: nameMap[stat.stat.name as keyof typeof nameMap],
106 value: stat.base_stat,
107 }))
108 },
109 reconcile: 'name',
110 }))
111​
112 const is_server_rendered = useQuery(() => ({
113 queryKey: ['is_server_rendered', props.id],
114 queryFn: () => {
115 if (isServer) return true
116 return false
117 },
118 placeholderData: keepPreviousData,
119 }))
120​
121 return (
122 <div class="flex flex-col flex-1">
123 <Show when={pokemon.data}>
124 <div class="flex justify-center text-3xl font-semibold py-4">
125 {properCase(pokemon.data?.name || '')}
126 </div>
127 <div class="flex justify-center">
128 <div class="h-64 w-64 rounded-lg relative overflow-hidden border">
129 <img
130 class="h-[400%] w-[400%] max-w-[400%] object-contain blur-2xl brightness-125 contrast-150 absolute z-10 top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2"
131 src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${props.id}.png`}
132 alt={properCase(pokemon.data?.name || '')}
133 />
134 <img
135 class="h-full w-full object-contain relative z-20"
136 src={`https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${props.id}.png`}
137 alt={properCase(pokemon.data?.name || '')}
138 />
139 </div>
140 </div>
141 </Show>
142​
143 <div class="flex justify-center py-4">
144 <div>
145 This query was rendered on the{' '}
146 <Show
147 when={is_server_rendered.data}
148 fallback={
149 <>
150 <b>client</b>. Reload this page to see the page rendered on the
151 server.
152 </>
153 }
154 >
155 <b>server. </b>
156 Click on another pokemon to see queries run and render on the
157 client.
158 </Show>
159 </div>
160 </div>
161​
162 <Show when={pokemon_stats.data}>
163 <div class="flex justify-center flex-col gap-2">
164 <For each={pokemon_stats.data}>
165 {(stat) => (
166 <div class="flex gap-2 justify-center items-center">
167 <div class="w-36">{stat.name}</div>
168 <div class="w-80 h-8 relative bg-zinc-100 rounded overflow-hidden flex justify-center items-center">
169 <div
170 class="absolute z-10 h-full top-0 left-0 bg-lime-300"
171 style={{
172 width: `${(stat.value / 160) * 100}%`,
173 transition: 'width 0.5s',
174 }}
175 />
176 <div class="relative z-20 text-sm mix-blend-darken font-semibold">
177 {stat.value}
178 </div>
179 </div>
180 </div>
181 )}
182 </For>
183 </div>
184 </Show>
185 </div>
186 )
188​
189const SideNav = () => {
190 const id = usePokemonID()
191​
192 const pokemonsList = useQuery(() => ({
193 ...pokemonsQueries.list(),
194 select(data) {
195 return data.results.map((p) => {
196 const regex = /\/pokemon\/(\d+)\/$/
197 const match = p.url.match(regex)
198 const pokemonId = match ? match[1] : ''
199 return {
200 name: properCase(p.name),
201 id: pokemonId,
202 avatar: `https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/other/official-artwork/${pokemonId}.png`,
203 }
204 })
205 },
206 placeholderData: keepPreviousData,
207 reconcile: 'id',
208 }))
209​
210 const activeClass = (pokemonID: string) =>
211 id() === pokemonID ? 'bg-gray-50' : 'hover:bg-gray-50'
212​
213 return (
214 <div class="w-72 border-r flex flex-col px-2">
215 <div class="pt-3 pb-1 text-sm font-semibold">Pokemons</div>
216​
217 <div class="flex-1 overflow-auto flex flex-col gap-1">
218 <For each={pokemonsList.data}>
219 {(pokemon) => (
220 <Link
221 class={`flex gap-2 items-center border rounded p-1 ${activeClass(
222 pokemon.id,
223 )}`}
224 href={`./?id=${pokemon.id}`}
225 >
226 <span class="flex bg-zinc-100 border relative justify-center items-center overflow-hidden rounded-sm w-9 h-9 flex-shrink-0">
227 <img
228 class="w-7 h-7 relative z-20"
229 src={pokemon.avatar}
230 alt={pokemon.name}
231 />
232 </span>
233 <span>{pokemon.name}</span>
234 </Link>
235 )}
236 </For>
237 </div>
238 </div>
239 )