cornflour
cornflour
Explore posts from servers
TTCTheo's Typesafe Cult
Created by cornflour on 3/14/2024 in #questions
Dynamic record with value type depending on key suffix
you mean if it is possible for me to change the final shape? at first i had it as nested instead of the weird suffix like this, but i think i ran into some trouble with the chart library :/ Maybe i can dig into deeper to see if it can work, but at this point i feel like its more effort than its worth since everything is working now (even if the as "id" and as const are slightly annoying) at this point i'm moreso curious about whether this can theoretically be done in TS than needing it for my actual task. I think i'll close the question for now
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 3/14/2024 in #questions
Dynamic record with value type depending on key suffix
there is no real getId() function, it's just an id from server response. In actuality, I needed 3 different type of key suffixes, the third one being _trend for drawing the trendline. My code is something like this:
type DataRecord = {
[`${string}_value`]: number
[`${string}_error`]: [number, number]
[`${string}_trend`]: number
}
type DataList = ({x: number} & DataRecord)[]

// record with key being the x axis position, and the value being all the points in this position
const chartDataRecord: Record<number, DataRecord> = {}

/*
typeof datasets = {
id: string
entries: {
x: number
value: number
error: [number, number]
}[]
}[]
*/
for (const dataset of datasets) {
for (const entry of dataset.entries) {

// put all entries with the same x position across different records into the same object
if (!chartDataRecord[entry.x]) {
chartDataRecord[entry.x] = {}
}
chartDataRecord[entry.x][`${dataset.id}_value`] = entry.value
chartDataRecord[entry.x][`${dataset.id}_error`] = entry.errors
}
}

// turn records to array
for (const x in chartDataRecord) {
myData.push({
x,
...chartDataRecord[x]
})
}

for (const dataset in datasets) {
const {point1, point2} = calculateTrendline(dataset)

const datasetID = dataset.id as "id" // hack TS type
myData.push({
x: point1.x,
[`${datasetID}_trend` as const]: point1.y
})
myData.push({
x: point2.x
[`${datasetID}_trend` as const]: point2.y
})
}
type DataRecord = {
[`${string}_value`]: number
[`${string}_error`]: [number, number]
[`${string}_trend`]: number
}
type DataList = ({x: number} & DataRecord)[]

// record with key being the x axis position, and the value being all the points in this position
const chartDataRecord: Record<number, DataRecord> = {}

/*
typeof datasets = {
id: string
entries: {
x: number
value: number
error: [number, number]
}[]
}[]
*/
for (const dataset of datasets) {
for (const entry of dataset.entries) {

// put all entries with the same x position across different records into the same object
if (!chartDataRecord[entry.x]) {
chartDataRecord[entry.x] = {}
}
chartDataRecord[entry.x][`${dataset.id}_value`] = entry.value
chartDataRecord[entry.x][`${dataset.id}_error`] = entry.errors
}
}

// turn records to array
for (const x in chartDataRecord) {
myData.push({
x,
...chartDataRecord[x]
})
}

for (const dataset in datasets) {
const {point1, point2} = calculateTrendline(dataset)

const datasetID = dataset.id as "id" // hack TS type
myData.push({
x: point1.x,
[`${datasetID}_trend` as const]: point1.y
})
myData.push({
x: point2.x
[`${datasetID}_trend` as const]: point2.y
})
}
this is why i said I never had to put 2 _value in 1 object at a time, since i used a for loop to add one entry to the record at a time. But i needed the as "id" and as const hack to add the trendline points to the list, which is annoying
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 3/14/2024 in #questions
Dynamic record with value type depending on key suffix
yeah it's rough since the id is a dynamic string, so i don't know them beforehand but your response did allow me to get a bit farther by lying to TS:
type DataRecord = {
[key: `${string}_value`]: number
[key: `${string}_error`]: [number, number],
x: number
}

type DataList = DataRecord[]

const myData: DataList = []

// mock that the id is dynamic
// in reality this is part of the server response
const getId = () => {
return `id${Math.random() * 10}`
}

// lie to typescript here to pretend that this is a string literal
const id1 = getId() as 'id'
const id2 = getId() as 'id'

// this now works, though it needs the `as const` which is slightly annoying
myData.push({
x: 0,
[`${id1}_value` as const]: 1,
[`${id1}_error` as const]: [0, 1],
})

myData.push({
x: 0,
[`${id1}_value` as const]: 1,
[`${id1}_error` as const]: [0, 1],

// this fails because typescript thinks both id1 and id2 are literal "id", making the keys the same
// if we use a different string literal to pretend id2 as, there will be no more errors
[`${id2}_value` as const]: 2,
[`${id2}_error` as const]: [1, 1],
})
type DataRecord = {
[key: `${string}_value`]: number
[key: `${string}_error`]: [number, number],
x: number
}

type DataList = DataRecord[]

const myData: DataList = []

// mock that the id is dynamic
// in reality this is part of the server response
const getId = () => {
return `id${Math.random() * 10}`
}

// lie to typescript here to pretend that this is a string literal
const id1 = getId() as 'id'
const id2 = getId() as 'id'

// this now works, though it needs the `as const` which is slightly annoying
myData.push({
x: 0,
[`${id1}_value` as const]: 1,
[`${id1}_error` as const]: [0, 1],
})

myData.push({
x: 0,
[`${id1}_value` as const]: 1,
[`${id1}_error` as const]: [0, 1],

// this fails because typescript thinks both id1 and id2 are literal "id", making the keys the same
// if we use a different string literal to pretend id2 as, there will be no more errors
[`${id2}_value` as const]: 2,
[`${id2}_error` as const]: [1, 1],
})
for the code i was working at, this is sufficient because i apparently never had to put 2 _value keys into 1 object at any time, but even then it was still a bit annoying with the as "id" and as const.
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 10/9/2023 in #questions
Button with 4 triangles at 4 corners
I ended up with the 4 div solution for anyone else checking this and wanting some references, here is my code
const CtaButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, children, ...rest }, ref) => {
return (
<button
ref={ref}
className={cn(
"group relative bg-cta px-6 py-1 text-foreground hover:bg-primary",
className,
)}
{...rest}
>
{children}

{/** The 4 triangles at 4 corners */}
<div className="absolute -right-1 -top-2 h-0 w-0 rotate-[40deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -bottom-2 -right-1 h-0 w-0 rotate-[140deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -left-1 -top-2 h-0 w-0 -rotate-[40deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -bottom-2 -left-1 h-0 w-0 -rotate-[140deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
</button>
)
},
)
CtaButton.displayName = "CtaButton"
const CtaButton = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, children, ...rest }, ref) => {
return (
<button
ref={ref}
className={cn(
"group relative bg-cta px-6 py-1 text-foreground hover:bg-primary",
className,
)}
{...rest}
>
{children}

{/** The 4 triangles at 4 corners */}
<div className="absolute -right-1 -top-2 h-0 w-0 rotate-[40deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -bottom-2 -right-1 h-0 w-0 rotate-[140deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -left-1 -top-2 h-0 w-0 -rotate-[40deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
<div className="absolute -bottom-2 -left-1 h-0 w-0 -rotate-[140deg] border-b-[13px] border-l-[3px] border-r-[3px] border-t-0 border-transparent border-b-cta group-hover:border-b-primary" />
</button>
)
},
)
CtaButton.displayName = "CtaButton"
one thing to note here is that the button color cannot have opacity with it, since the triangles have a few overlap with the main button
5 replies
TTCTheo's Typesafe Cult
Created by Smadger on 7/11/2023 in #questions
Call TRPC from an api
this might help depending on what you need https://trpc.io/docs/server/server-side-calls
4 replies
TTCTheo's Typesafe Cult
Created by Romain on 7/6/2023 in #questions
Do we need a fetcher like SWR with the t3 stack
trpc already wraps around tanstack query, so you don't need swr with it
6 replies
TTCTheo's Typesafe Cult
Created by z on 6/26/2023 in #questions
NextJS "Module not found" error with "loading.tsx"?
how about cloning your repo again and try to run it from there?
13 replies
TTCTheo's Typesafe Cult
Created by z on 6/26/2023 in #questions
NextJS "Module not found" error with "loading.tsx"?
i did have this issue too when i tried to delete a loading.tsx but it seems to have fixed it when i deleted .next, so idk about your case
13 replies
TTCTheo's Typesafe Cult
Created by cornflour on 6/21/2023 in #questions
implement discord-like account switching
i'll check it out!
8 replies
TTCTheo's Typesafe Cult
Created by cornflour on 6/21/2023 in #questions
implement discord-like account switching
so im stuck at how to set it up such that i got the list of sessions (each having a jwt + maybe some other user info) from the accounts the user has signed in and the ability to swap between these sessions i have only ever set up next auth to sign in and then sign out, so im kinda lost at how account switching would work
8 replies
TTCTheo's Typesafe Cult
Created by Maj on 6/21/2023 in #questions
shadcn ui table component with dialog.
idk how it is messing up the table for you, but basically yeah you can use the cell props of the column in the column headers definition to say that the entries will have a dialog button at that cell it would be sth similar to the row action section in the shadcn data table docs: https://ui.shadcn.com/docs/components/data-table#row-actions without knowing more about what you did and what issues you faced i cant really help in more details
9 replies
TTCTheo's Typesafe Cult
Created by Maj on 6/21/2023 in #questions
shadcn ui table component with dialog.
from my understanding, you want to make a cell in every row a button to open a dialog?
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 6/21/2023 in #questions
implement discord-like account switching
bump
8 replies
TTCTheo's Typesafe Cult
Created by Complexlity on 6/19/2023 in #questions
How to send cookies and headers in app dir
from my understanding, you are trying to take the header sent from the client to send to the remote server. If so, you can use the cookies() and headers() methods from next/headers to get cookies and headers respectively
import { cookies, headers } from 'next/headers'
const Home = async () => {
const myCookie = cookies().get('my-cookie-key')
const headers = headers()
const response = await getUser(myCookies, headers) // do whatever here
return <>...</>
}
import { cookies, headers } from 'next/headers'
const Home = async () => {
const myCookie = cookies().get('my-cookie-key')
const headers = headers()
const response = await getUser(myCookies, headers) // do whatever here
return <>...</>
}
related docs https://nextjs.org/docs/app/api-reference/functions/headers#headers https://nextjs.org/docs/app/api-reference/functions/cookies
6 replies
TTCTheo's Typesafe Cult
Created by 균어 on 6/18/2023 in #questions
How to get singular type on trpc query
try (typeof serviceOrders.data)[number], i think that should work from my understanding, your type there is an array, so to get the type of an item in the array its ArrayType[number]
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 6/13/2023 in #questions
Websocket with Next appdir/RSC?
got it, tysm!
9 replies
TTCTheo's Typesafe Cult
Created by cornflour on 6/13/2023 in #questions
Websocket with Next appdir/RSC?
so as far as the websocket part itself is concerned, its the same as before, and the change that the layout stuff can be server components?
9 replies
TTCTheo's Typesafe Cult
Created by Ayato on 4/17/2023 in #questions
tRPC Mutation
The doc page for tRPC on ct3a does have a mutation example here https://create.t3.gg/en/usage/trpc#inferring-errors but perhaps having an actual section on mutation similar to query in that page might be a bit clearer for beginners? i personally find the trpc doc for mutation pretty decently clear though; I didn't have trouble trying to figure that out: https://trpc.io/docs/reactjs/usemutation
7 replies
TTCTheo's Typesafe Cult
Created by tropic on 4/20/2023 in #questions
Headless UI Stops Working when Adding addition elements to components
this div here that you have is staying on top of your nav bar, so you cant click on the nav items a way that can fix it is to have the <header> tag have "relative z-10" to move it on top of the other one, making the button clickable again and maybe removing the bg-white because it would look weird then
11 replies
TTCTheo's Typesafe Cult
Created by tropic on 4/20/2023 in #questions
Headless UI Stops Working when Adding addition elements to components
is the repo private? I cannot access it
11 replies