@@ -66,6 +66,32 @@ export default function PostCard({
6666
6767 return { radius : radius . toFixed ( 2 ) , planetType } ;
6868 } ;
69+
70+ const calculatePlanetDensity = ( stellarRadius : string , fluxDifferential : string ) => {
71+ const R_star = parseFloat ( stellarRadius ) ;
72+ const deltaF = parseFloat ( fluxDifferential ) ;
73+
74+ if ( isNaN ( R_star ) || isNaN ( deltaF ) || deltaF <= 0 ) {
75+ return { density : "" , unit : "kg/m³" } ;
76+ }
77+
78+ const K = 0.414 ; // empirical constant for gas giants
79+ const alpha = 2.06 ;
80+ const pi = Math . PI ;
81+
82+ const M_earth = 5.972e24 ; // kg
83+ const R_earth = 6.371e6 ; // m
84+ const R_sun = 6.957e8 ; // m
85+
86+ const Rp_solar = R_star * Math . sqrt ( deltaF ) ;
87+ const Rp_meters = Rp_solar * R_sun ;
88+ const mass_earth = K * Math . pow ( ( Rp_meters / R_earth ) , alpha ) ;
89+ const mass_kg = mass_earth * M_earth ;
90+ const volume = ( 4 / 3 ) * pi * Math . pow ( Rp_meters , 3 ) ;
91+ const density = mass_kg / volume ;
92+
93+ return { density : density . toFixed ( 2 ) , unit : "kg/m³" } ;
94+ } ;
6995
7096 const handleRadiusInputChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
7197 const value = e . target . value ;
@@ -249,7 +275,45 @@ export default function PostCard({
249275 fetchComments ( ) ;
250276 } catch ( error : any ) {
251277 console . error ( "Error inserting comment: " , error ) ;
252- }
278+ } ;
279+ } ;
280+
281+ const handleAddDensityComment = async ( ) => {
282+ const densityInput1 = densityInputs [ `${ classificationId } -1` ] ;
283+ const densityInput2 = densityInputs [ `${ classificationId } -2` ] ;
284+
285+ if ( ! densityInput1 ?. trim ( ) || ! densityInput2 ?. trim ( ) ) {
286+ console . error ( "Both text areas must be filled" ) ;
287+ return ;
288+ } ;
289+
290+ try {
291+ const { error } = await supabase
292+ . from ( "comments" )
293+ . insert ( [
294+ {
295+ content : `${ densityInput1 } \n\n${ densityInput2 } ` ,
296+ classification_id : classificationId ,
297+ author : session ?. user ?. id ,
298+ configuration : { density : `${ densityInput2 } ` } ,
299+ surveyor : "TRUE" ,
300+ value : densityInput2 ,
301+ category : "Density" ,
302+ } ,
303+ ] ) ;
304+
305+ if ( error ) throw error ;
306+
307+ setDensityInputs ( ( prev ) => ( {
308+ ...prev ,
309+ [ `${ classificationId } -1` ] : "" ,
310+ [ `${ classificationId } -2` ] : "" ,
311+ } ) ) ;
312+
313+ fetchComments ( ) ;
314+ } catch ( error ) {
315+ console . error ( "Error adding density comment:" , error ) ;
316+ } ;
253317 } ;
254318
255319 const handleAddTemperatureComment = async ( ) => {
@@ -283,6 +347,7 @@ export default function PostCard({
283347 [ `${ classificationId } -1` ] : "" ,
284348 [ `${ classificationId } -2` ] : "" ,
285349 } ) ) ;
350+
286351 fetchComments ( ) ;
287352 } catch ( error ) {
288353 console . error ( "Error adding temperature comment:" , error ) ;
@@ -324,45 +389,6 @@ export default function PostCard({
324389 console . error ( "Error inserting comment: " , error ) ;
325390 } ;
326391} ;
327-
328- const handleAddDensityComment = async ( ) => {
329- const densityInput1 = densityInputs [ `${ classificationId } -1` ] ;
330- const densityInput2 = densityInputs [ `${ classificationId } -2` ] ;
331-
332- if ( ! densityInput1 ?. trim ( ) || ! densityInput2 ?. trim ( ) ) {
333- console . error ( "Both text areas must be filled" ) ;
334- return ;
335- }
336-
337- try {
338- const { error } = await supabase
339- . from ( "comments" )
340- . insert ( [
341- {
342- content : `${ densityInput1 } \n\n${ densityInput2 } ` ,
343- classification_id : classificationId ,
344- author : session ?. user ?. id ,
345- configuration : { density : `${ densityInput1 } , ${ densityInput2 } ` } ,
346- surveyor : "TRUE" ,
347- value : densityInput2 ,
348- category : "Density" ,
349- } ,
350- ] ) ;
351-
352- if ( error ) throw error ;
353-
354- setDensityInputs ( ( prev ) => ( {
355- ...prev ,
356- [ `${ classificationId } -1` ] : "" ,
357- [ `${ classificationId } -2` ] : "" ,
358- } ) ) ;
359-
360- fetchComments ( ) ;
361- } catch ( error ) {
362- console . error ( "Error adding density comment:" , error ) ;
363- } ;
364- } ;
365-
366392 // For sharing
367393 const [ dropdownOpen , setDropdownOpen ] = useState < boolean > ( false ) ;
368394 const dropdownRef = useRef < HTMLDivElement | null > ( null ) ;
@@ -602,6 +628,9 @@ export default function PostCard({
602628 < SelectItem value = "period" className = "focus:bg-[#1e2834] focus:text-black" >
603629 Planet Orbital Period
604630 </ SelectItem >
631+ < SelectItem value = "density" className = "focus:bg-[#1e2834] focus:text-black" >
632+ Planet Density
633+ </ SelectItem >
605634 </ SelectContent >
606635 </ Select >
607636
@@ -730,6 +759,8 @@ export default function PostCard({
730759 ? handleAddTemperatureComment
731760 : selectedCalculator === "radius"
732761 ? handleAddRadiusComment
762+ : selectedCalculator === "density"
763+ ? handleAddDensityComment
733764 : selectedCalculator === 'period'
734765 ? handleAddPeriodComment
735766 : handleAddComment
0 commit comments