File size: 3,681 Bytes
68465df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import {useFocusEffect} from "expo-router"
import {useCallback} from "react"
import {View, Platform, Pressable, ViewStyle, TextStyle} from "react-native"

import {Text} from "@/components/ignite"
import {useAppTheme} from "@/contexts/ThemeContext"
import {useNavigationStore} from "@/stores/navigation"
import {ThemedStyle} from "@/theme"
import {textEditorStore} from "@/utils/TextEditorStore"

type TextSettingNoSaveProps = {
  label: string
  value: string
  onChangeText: (text: string) => void
  settingKey: string
  isFirst?: boolean
  isLast?: boolean
}

const TextSettingNoSave: React.FC<TextSettingNoSaveProps> = ({

  label,

  value,

  onChangeText,

  settingKey,

  isFirst,

  isLast,

}) => {
  const {theme, themed} = useAppTheme()

  const groupedStyle: ViewStyle | undefined =
    isFirst !== undefined || isLast !== undefined
      ? {
          borderTopLeftRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1,
          borderTopRightRadius: isFirst ? theme.spacing.s4 : theme.spacing.s1,
          borderBottomLeftRadius: isLast ? theme.spacing.s4 : theme.spacing.s1,
          borderBottomRightRadius: isLast ? theme.spacing.s4 : theme.spacing.s1,
          marginBottom: isLast ? 0 : theme.spacing.s2,
        }
      : undefined
  const {push} = useNavigationStore.getState()

  // Check for pending value when component gets focus
  useFocusEffect(
    useCallback(() => {
      // Only process if there's actually a pending value (meaning we just returned from text editor)
      const pendingValue = textEditorStore.getPendingValue()
      if (pendingValue && pendingValue.key === settingKey) {
        onChangeText(pendingValue.value)
        textEditorStore.setPendingValue(pendingValue.key, "") // Only clear when we use it
      } else if (pendingValue) {
        // If there's a pending value but it doesn't match, put it back
        textEditorStore.setPendingValue(pendingValue.key, pendingValue.value)
      }
    }, [settingKey]),
  )

  const handleOpenEditor = () => {
    push("/applet/text-editor", {label, value, settingKey})
  }

  return (
    <View style={[themed($container), groupedStyle]}>

      <Text style={themed($label)}>{label}</Text>



      <Pressable

        style={({pressed}) => [themed($button), pressed && themed($buttonPressed)]}

        onPress={handleOpenEditor}

        android_ripple={{color: "rgba(0, 0, 0, 0.1)"}}>

        <Text style={themed($buttonText)} numberOfLines={2} ellipsizeMode="tail">

          {value || "Tap to edit..."}

        </Text>

      </Pressable>

    </View>
  )
}

const $container: ThemedStyle<ViewStyle> = ({colors, spacing}) => ({
  width: "100%",
  backgroundColor: colors.primary_foreground,
  borderRadius: spacing.s4,
  paddingVertical: spacing.s4,
  paddingHorizontal: spacing.s4,
})

const $label: ThemedStyle<TextStyle> = ({colors, spacing}) => ({
  fontSize: 14,
  fontWeight: "600",
  color: colors.text,
  marginBottom: spacing.s2,
})

const $button: ThemedStyle<ViewStyle> = ({colors}) => ({
  backgroundColor: colors.background,
  borderRadius: Platform.OS === "ios" ? 8 : 4,
  borderWidth: 1,
  borderColor: colors.border,
  justifyContent: "center",
  minHeight: Platform.OS === "ios" ? 44 : 48,
  padding: Platform.OS === "ios" ? 12 : 10,
})

const $buttonPressed: ThemedStyle<ViewStyle> = () => ({
  backgroundColor: Platform.OS === "ios" ? "rgba(0, 0, 0, 0.05)" : "transparent",
  opacity: Platform.OS === "ios" ? 0.8 : 1,
})

const $buttonText: ThemedStyle<TextStyle> = ({colors}) => ({
  fontSize: 16,
  color: colors.text,
})

export default TextSettingNoSave